-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathspPGOccPredict.cpp
More file actions
229 lines (186 loc) · 7.3 KB
/
Copy pathspPGOccPredict.cpp
File metadata and controls
229 lines (186 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#define USE_FC_LEN_T
#include <string>
#include "util.h"
#ifdef _OPENMP
#include <omp.h>
#endif
#define R_NO_REMAP
#include <R.h>
#include <Rmath.h>
#include <Rinternals.h>
#include <R_ext/Linpack.h>
#include <R_ext/Lapack.h>
#include <R_ext/BLAS.h>
#ifndef FCONE
# define FCONE
#endif
extern "C" {
SEXP spPGOccPredict(SEXP J_r, SEXP pOcc_r, SEXP X0_r, SEXP q_r,
SEXP obsD_r, SEXP obsPredD_r, SEXP betaSamples_r,
SEXP thetaSamples_r, SEXP wSamples_r,
SEXP betaStarSiteSamples_r,
SEXP nSamples_r, SEXP covModel_r, SEXP nThreads_r,
SEXP verbose_r, SEXP nReport_r, SEXP sitesLink_r,
SEXP sites0Sampled_r){
/*****************************************
Common variables
*****************************************/
int j, s, info, nProtect= 0;
const char *lower = "L";
const char *ntran = "N";
const char *ytran = "T";
const char *lside = "L";
const double one = 1.0;
const double zero = 0.0;
const int inc = 1;
/*****************************************
Set-up
*****************************************/
int J = INTEGER(J_r)[0];
int pOcc = INTEGER(pOcc_r)[0];
double *X0 = REAL(X0_r);
int q = INTEGER(q_r)[0];
double *obsD = REAL(obsD_r);
double *obsPredD = REAL(obsPredD_r);
double *betaSamples = REAL(betaSamples_r);
double *thetaSamples = REAL(thetaSamples_r);
double *wSamples = REAL(wSamples_r);
double *betaStarSite = REAL(betaStarSiteSamples_r);
int nSamples = INTEGER(nSamples_r)[0];
int covModel = INTEGER(covModel_r)[0];
std::string corName = getCorName(covModel);
int nThreads = INTEGER(nThreads_r)[0];
int verbose = INTEGER(verbose_r)[0];
int nReport = INTEGER(nReport_r)[0];
int *sitesLink = INTEGER(sitesLink_r);
int *sites0Sampled = INTEGER(sites0Sampled_r);
/*****************************************
Display
*****************************************/
#ifdef _OPENMP
omp_set_num_threads(nThreads);
#else
if(nThreads > 1){
Rf_warning("n.omp.threads > 1, but source not compiled with OpenMP support.");
nThreads = 1;
}
#endif
if(verbose){
Rprintf("----------------------------------------\n");
Rprintf("\tPrediction description\n");
Rprintf("----------------------------------------\n");
Rprintf("Spatial Occupancy model with Polya-Gamma latent\nvariable fit with %i observations.\n\n", J);
Rprintf("Number of covariates %i (including intercept if specified).\n\n", pOcc);
Rprintf("Using the %s spatial correlation model.\n\n", corName.c_str());
Rprintf("Number of MCMC samples %i.\n\n", nSamples);
Rprintf("Predicting at %i non-sampled locations.\n\n", q);
#ifdef _OPENMP
Rprintf("\nSource compiled with OpenMP support and model fit using %i threads.\n", nThreads);
#else
Rprintf("\n\nSource not compiled with OpenMP support.\n");
#endif
}
/*****************************************
Set-up sample matrices etc.
*****************************************/
// parameters
int nTheta, sigmaSqIndx, phiIndx, nuIndx;
if (corName != "matern") {
nTheta = 2; //sigma^2, phi
sigmaSqIndx = 0; phiIndx = 1;
} else{
nTheta = 3; //sigma^2, phi, nu
sigmaSqIndx = 0; phiIndx = 1; nuIndx = 2;
}
double *theta = (double *) R_alloc(nTheta, sizeof(double));
double JJ = J * J;
double qJ = q * J;
SEXP w0_r, psi0_r, z0_r;
PROTECT(w0_r = Rf_allocMatrix(REALSXP, q, nSamples)); nProtect++;
double *w0 = REAL(w0_r);
PROTECT(psi0_r = Rf_allocMatrix(REALSXP, q, nSamples)); nProtect++;
double *psi0 = REAL(psi0_r);
PROTECT(z0_r = Rf_allocMatrix(REALSXP, q, nSamples)); nProtect++;
double *z0 = REAL(z0_r);
double *S_obs = (double *) R_alloc(JJ, sizeof(double));
double *S_obsPred = (double *) R_alloc(qJ, sizeof(double));
double *beta = (double *) R_alloc(pOcc, sizeof(double));
double phi, nu, sigmaSq;
double *tmp_J = (double *) R_alloc(J, sizeof(double));
double *tmp_q = (double *) R_alloc(q, sizeof(double));
double *tmp_one = (double *) R_alloc(inc, sizeof(double));
double *tmp_one2 = (double *) R_alloc(inc, sizeof(double));
int status = 0;
GetRNGstate();
for(s = 0; s < nSamples; s++){
F77_NAME(dcopy)(&pOcc, &betaSamples[s*pOcc], &inc, beta, &inc);
phi = thetaSamples[s * nTheta + phiIndx];
if (corName == "matern") {
nu = thetaSamples[s * nTheta + nuIndx];
theta[nuIndx] = nu;
}
sigmaSq = thetaSamples[s * nTheta + sigmaSqIndx];
theta[sigmaSqIndx] = sigmaSq;
theta[phiIndx] = phi;
// Get covariance matrices
spCov(obsD, JJ, theta, corName, S_obs);
spCov(obsPredD, qJ, theta, corName, S_obsPred);
F77_NAME(dpotrf)(lower, &J, S_obs, &J, &info FCONE);
if(info != 0){Rf_error("c++ error: dpotrf failed\n");}
F77_NAME(dpotri)(lower, &J, S_obs, &J, &info FCONE);
if(info != 0){Rf_error("c++ error: dpotri failed\n");}
F77_NAME(dgemv)(ntran, &q, &pOcc, &one, X0, &q, beta, &inc, &zero, tmp_q, &inc FCONE);
// Predicting each element one at a time, instead of doing joint prediction.
for(j = 0; j < q; j++){
//get Mu
F77_NAME(dsymm)(lside, lower, &J, &inc, &one, S_obs, &J, &S_obsPred[j*J], &J, &zero, tmp_J, &J FCONE FCONE);
F77_NAME(dgemv)(ytran, &J, &inc, &one, tmp_J, &J, &wSamples[s*J], &inc, &zero, tmp_one, &inc FCONE);
//get Sigma
F77_NAME(dgemm)(ytran, ntran, &inc, &inc, &J, &one, tmp_J, &J, &S_obsPred[j*J], &J, &zero, tmp_one2, &inc FCONE FCONE);
tmp_one2[0] = sigmaSq - tmp_one2[0];
if (sites0Sampled[j] == 1) {
w0[s * q + j] = wSamples[s * J + sitesLink[j]];
} else {
w0[s * q + j] = rnorm(tmp_one[0], sqrt(tmp_one2[0]));
}
psi0[s * q + j] = logitInv(tmp_q[j] + w0[s * q + j] + betaStarSite[s * q + j], zero, one);
z0[s * q + j] = rbinom(one, psi0[s * q + j]);
}
//report
if(verbose){
if(status == nReport){
Rprintf("Samples: %i of %i, %3.2f%%\n", s, nSamples, 100.0*s/nSamples);
#ifdef Win32
R_FlushConsole();
#endif
status = 0;
}
}
status++;
R_CheckUserInterrupt();
} //end sample loop
if(verbose){
Rprintf("Samples: %i of %i, %3.2f%%\n", s, nSamples, 100.0*s/nSamples);
#ifdef Win32
R_FlushConsole();
#endif
}
PutRNGstate();
//make return object
SEXP result, resultNames;
int nResultListObjs = 0;
nResultListObjs = 3;
PROTECT(result = Rf_allocVector(VECSXP, nResultListObjs)); nProtect++;
PROTECT(resultNames = Rf_allocVector(VECSXP, nResultListObjs)); nProtect++;
SET_VECTOR_ELT(result, 0, w0_r);
SET_VECTOR_ELT(resultNames, 0, Rf_mkChar("w.0.samples"));
SET_VECTOR_ELT(result, 1, psi0_r);
SET_VECTOR_ELT(resultNames, 1, Rf_mkChar("psi.0.samples"));
SET_VECTOR_ELT(result, 2, z0_r);
SET_VECTOR_ELT(resultNames, 2, Rf_mkChar("z.0.samples"));
Rf_namesgets(result, resultNames);
//unprotect
UNPROTECT(nProtect);
return(result);
}
}