pomp
Inference for partially observed Markov processes
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
probe_nlar.c
Go to the documentation of this file.
1// -*- mode: C++; -*-
2
3#include "internal.h"
4#include "pomp_mat.h"
5#include <stdio.h>
6
7static void poly_nlar_fit(double *beta, double *y, int n, int nterms, int *lag, int *power, double *X);
8
9SEXP probe_nlar (SEXP x, SEXP lags, SEXP powers) {
10 SEXP y, beta, beta_names;
11 int n, nterms;
12 int k;
13 double *mm;
14 char tmp[BUFSIZ];
15
16 n = LENGTH(x); // n = # of observations
17 nterms = LENGTH(lags);
18
19 PROTECT(y = duplicate(AS_NUMERIC(x)));
20 PROTECT(beta = NEW_NUMERIC(nterms));
21
22 mm = (double *) R_alloc(n*nterms,sizeof(double)); // storage for the model matrix
23 poly_nlar_fit(REAL(beta),REAL(y),n,nterms,INTEGER(lags),INTEGER(powers),mm);
24
25 PROTECT(beta_names = NEW_STRING(nterms));
26 for (k = 0; k < nterms; k++) {
27 snprintf(tmp,BUFSIZ,"nlar.%d^%d",INTEGER(lags)[k],INTEGER(powers)[k]);
28 SET_STRING_ELT(beta_names,k,mkChar(tmp));
29 }
30 SET_NAMES(beta,beta_names);
31
32 UNPROTECT(3);
33 return beta;
34}
35
36// Code for polynomial auto-regression
37// The original version of the following code is due to Simon N. Wood.
38// Modifications by AAK.
39static void poly_nlar_fit (double *beta, double *y, int n,
40 int nterms, int *lag, int *power, double *X) {
41 // 'x' is an n vector of data.
42 // 'nterms' gives the number of terms on the rhs of the autoregression.
43 // 'lag[i]' gives the lag of the ith term on the rhs.
44 // 'power[i]' gives the power to which the ith rhs term should be raised.
45 // 'beta' contains the ar coefficients
46
47 int maxlag, nx, ny, ok, ct;
48 int i, j, k;
49 double xx, *yp;
50 double obs1;
51
52 // find maxlag
53 for (maxlag = 0, i = 0; i < nterms; i++)
54 maxlag = (lag[i] > maxlag) ? lag[i] : maxlag;
55 ny = n - maxlag; // maximum response vector length
56
57 // compute the series mean
58 for (j = 0, ct = 0, xx = 0.0; j < n; j++)
59 if (R_FINITE(y[j])) {
60 xx += y[j];
61 ct++;
62 }
63 xx /= ct; // series mean
64
65 // center the whole series
66 // also check to see if there is any variability in the predictors
67 ok = 0;
68 obs1 = R_NaReal;
69 for (j = 0; j < n; j++) {
70 if (R_FINITE(y[j])) {
71 if (!ok && (j < ny)) { // j < ny means x[j] is a predictor
72 if (!R_FINITE(obs1)) obs1 = y[j];
73 else if (y[j] != obs1) ok = 1;
74 }
75 y[j] -= xx; // subtracting series mean
76 }
77 }
78
79 if (!ok) { // data had no variability
80
81 for (i = 0; i < nterms; i++) beta[i] = 0.0;
82
83 } else { // data not all the same
84
85 double *Xp;
86 int finite[ny];
87
88 // test for NA rows in model matrix and response vector
89 for (nx = 0, yp = y+maxlag, j = 0; j < ny; j++) {
90 finite[j] = (R_FINITE(yp[j])) ? 1 : 0; // finite response?
91 for (i = 0; i < nterms; i++)
92 finite[j] = (R_FINITE(yp[j-lag[i]])) ? finite[j] : 0; // finite in model matrix row j, column i
93 if (finite[j]) nx++;
94 }
95 // nx is now the number of non-NA rows in the model matrix
96
97 // build the model matrix, omitting NA rows
98 for (Xp = X, i = 0; i < nterms; i++) { // work through the terms
99 for (j = 0; j < ny; j++) {
100 if (finite[j]) {
101 xx = yp[j-lag[i]]; // current predictor
102 *Xp = xx;
103 for (k = 1; k < power[i]; k++) *Xp *= xx; // raise to the appropriate power
104 Xp++;
105 }
106 }
107 }
108 // X is now the nx by nterms model matrix
109
110 // drop the NA rows from the response data
111 for (i = 0, j = 0; j < ny; j++)
112 if (finite[j]) yp[i++] = yp[j]; // keep this row
113 // response vector is now length nx
114
115 {
116 double tau[nterms];
117 int pivot[nterms];
118
119 // first QR decompose the model matrix
120 pomp_qr(X,nx,nterms,pivot,tau);
121 // then solve R b = Q'y for b
122 pomp_qrqy(yp,X,nx,tau,nx,1,nterms,"left","transpose"); // y <- Q'y
123 pomp_backsolve(X,nx,nterms,yp,1,"Upper","No transpose","Non-unit"); // y <- R^{-1} y
124
125 // unpivot and store coefficients in beta
126 for (i = 0; i < nterms; i++) beta[pivot[i]] = yp[i];
127
128 }
129
130 }
131
132}
#define X
Definition gompertz.c:14
#define tau
Definition gompertz.c:94
static R_INLINE void pomp_qr(double *a, int m, int n, int *pivot, double *tau)
Definition pomp_mat.h:28
static R_INLINE void pomp_qrqy(double *c, double *a, int lda, double *tau, int m, int n, int k, char *side, char *transpose)
Definition pomp_mat.h:47
static R_INLINE void pomp_backsolve(double *a, int lda, int n, double *x, int incx, char *uplo, char *transpose, char *unit)
Definition pomp_mat.h:12
SEXP probe_nlar(SEXP x, SEXP lags, SEXP powers)
Definition probe_nlar.c:9
static void poly_nlar_fit(double *beta, double *y, int n, int nterms, int *lag, int *power, double *X)
Definition probe_nlar.c:39