-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcppFuncs.cpp
More file actions
289 lines (204 loc) · 6.22 KB
/
cppFuncs.cpp
File metadata and controls
289 lines (204 loc) · 6.22 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <RcppArmadillo.h>
#include <math.h>
using namespace Rcpp;
using namespace arma;
//[[Rcpp::depends(RcppArmadillo)]]
LogicalVector isNA(NumericVector x) {
int n = x.size();
LogicalVector out(n);
for (int i = 0; i < n; ++i) {
out[i] = NumericVector::is_na(x[i]);
}
return out;
}
// [[Rcpp::export]]
uvec isNotNArowvec(const rowvec x) {
int n = x.size();
vec out(n);
for (int i = 0; i < n; ++i) {
out[i] = NumericVector::is_na(x[i]);
}
uvec ids = find(out==0);
// return x.elem(ids);
return(ids);
}
// [[Rcpp::export]]
uvec isNotNAcolvec(const colvec x) {
int n = x.size();
vec out(n);
for (int i = 0; i < n; ++i) {
out[i] = NumericVector::is_na(x[i]);
}
uvec ids = find(out==0);
// return x.elem(ids);
return(ids);
}
// [[Rcpp::export]]
mat fastLmResid(const mat& Y, const mat& X){
int n = X.n_rows, k = X.n_cols;
mat res;
mat coef = Y*X*inv(trans(X)*X); // fit model y ~ X
res = Y - coef*trans(X); // residuals
return res;
}
// [[Rcpp::export]]
mat fastLmResidWeighted(const mat& Y, const mat& X, const rowvec& wa){
int n = X.n_rows, k = X.n_cols;
mat res;
//rowvec ws=rowvec(wa.n_elem);
//for (int j=0; j<ws.n_elem; j++){
// ws[j]=std::sqrt(wa[j]);
//}
mat W=diagmat(wa);
// coeff=dat%*%W%*%modtmp %*% solve(t(modtmp) %*% W %*% modtmp)
mat coef = Y*W*X*inv(trans(X)*W*X); // fit model y ~ X
res = Y - coef*trans(X); // residuals
//res.each_row()%=ws;
return res;
}
// [[Rcpp::export]]
mat fastLmPredicted(const mat& Y, const mat& X){
int n = X.n_rows, k = X.n_cols;
mat res;
mat coef = Y*X*inv(trans(X)*X); // fit model y ~ X
return coef*trans(X); // residuals
}
// [[Rcpp::export]]
List fastLm(const mat& Y, const mat& X) {
int n = X.n_rows, k = X.n_cols;
// coeff=data[i,]%*%mod %*% solve(t(mod) %*% mod)
// resid[i, ] = data[i,] -(coeff %*% t(mod))\
mat coef = Y*X*inv(trans(X)*X); // fit model y ~ X
mat res = Y - coef*trans(X); // residuals
// std.errors of coefficients
double s2 = std::inner_product(res.begin(), res.end(), res.begin(), 0.0)/(n - k);
// colvec std_err = sqrt(s2 * diagvec(pinv(trans(X)*X)));
return List::create(Named("coefficients") = coef,
// Named("stderr") = std_err,
Named("df.residual") = n - k);
}
// [[Rcpp::export]]
mat fastLmResidMat(const mat& Y, const mat& X) {
uvec ids;
mat rmat=mat(Y.n_rows, Y.n_cols);
rmat.fill(datum::nan);
uvec vec_i=uvec(1);
int i,j;
for (int i=0; i<Y.n_rows; i++){
vec_i[0]=i;
ids=isNotNArowvec(Y.row(i));
if(ids.n_elem>X.n_cols){
rmat.submat(vec_i, ids)=fastLmResid(Y.submat(vec_i, ids), X.rows(ids));
}
}
return(rmat);
}
// [[Rcpp::export]]
mat fastLmPredictedMat(const mat& Y, const mat& X) {
uvec ids;
mat rmat=mat(Y.n_rows, Y.n_cols);
rmat.fill(datum::nan);
uvec vec_i=uvec(1);
int i,j;
for (int i=0; i<Y.n_rows; i++){
vec_i[0]=i;
ids=isNotNArowvec(Y.row(i));
if(ids.n_elem>X.n_cols){
rmat.submat(vec_i, ids)=fastLmPredicted(Y.submat(vec_i, ids), X.rows(ids));
}
}
return(rmat);
}
// [[Rcpp::export]]
mat fastLmResidMatWeighted(const mat& Y, const mat& X, const mat& W) {
uvec ids;
mat rmat=mat(Y.n_rows, Y.n_cols);
rmat.fill(datum::nan);
uvec vec_i=uvec(1);
int i,j;
for (int i=0; i<Y.n_rows; i++){
vec_i[0]=i;
ids=isNotNArowvec(Y.row(i));
if(ids.n_elem>X.n_cols){
rmat.submat(vec_i, ids)=fastLmResidWeighted(Y.submat(vec_i, ids), X.rows(ids), W.submat(vec_i, ids));
}
}
return(rmat);
}
// [[Rcpp::export]]
mat fastLmResidMatWeightedTrans(const mat& Y, const mat& X, const mat& W) {
uvec ids;
mat rmat=mat( Y.n_rows,Y.n_cols);
rmat.fill(datum::nan);
uvec vec_i=uvec(1);
mat Yt=trans(Y);
int i,j;
for (int i=0; i<Y.n_rows; i++){
vec_i[0]=i;
// cerr<<"Here"<<endl;
ids=isNotNAcolvec(Yt.col(i));
//cerr<<"Here2"<<endl;
//cerr<<i<<endl;
if(ids.n_elem>X.n_cols){
rmat.submat( vec_i, ids)=(fastLmResidWeighted(Y.submat( vec_i, ids), X.rows(ids), W.submat(vec_i, ids)));
}
}
return(rmat);
}
// [[Rcpp::export]]
mat fastLmResidWeightedPredict(const mat& Y, const mat& X, const rowvec& wa, const mat& newX){
int n = X.n_rows, k = X.n_cols;
mat res;
mat W=diagmat(wa);
mat coef = Y*W*X*inv(trans(X)*W*X); // fit model y ~ X
res = coef*trans(newX); // predictions
return res;
}
// [[Rcpp::export]]
mat fastLmResidMatWeightedPredict(const mat& Y, const mat& X, const mat& W, const mat& newX) {
uvec ids;
mat rmat=mat(Y.n_rows, newX.n_rows);
uvec vec_i=uvec(1);
int i,j;
int nc=rmat.n_cols-1;
int nrNew=newX.n_rows-1;
for (int i=0; i<Y.n_rows; i++){
vec_i[0]=i;
ids=isNotNArowvec(Y.row(i));
// rmat.submat(vec_i, ids)=fastLmResidWeighted(Y.submat(vec_i, ids), X.rows(ids), W.submat(vec_i, ids));
rmat.submat(span(i,i),span(0,nrNew))=fastLmResidWeightedPredict(Y.submat(vec_i,ids), X.rows(ids), W.submat(vec_i,ids), newX);
}
return(rmat);
}
/*** R
set.seed(123)
x=rnorm(10)
mod=model.matrix(~1+x)
y=rbind(rnorm(10))
w=rbind(rnorm(10)^2)
y[1,1]=NA
#make sure this is working
message("Running checks")
fastLmResidMatWeightedPredict(rbind(y), mod, rbind(w), rbind(mod, mod))
testpred=fastLmResidMatWeightedPredict(rbind(y), mod, rbind(w), rbind(mod))
#test normal lm
iinotna=which(!is.na(y))
#do normal lm no weights
lmres=lm(y[iinotna]~0+mod[iinotna,])
rvals=stats::resid(lmres)
myvals=naresidCPP(rbind(y[iinotna]), mod[iinotna,])
stopifnot(mean(abs(rvals-myvals))<1e-6)
#do normal lm with weights
lmres=lm(y[iinotna]~0+mod[iinotna,], weights = w[iinotna])
rvals=stats::resid(lmres)
myvals=naresidCPP(rbind(y[iinotna]), mod[iinotna,],weights = rbind(w[iinotna]))
#naresidCPP includes a stadartization step
stopifnot(mean(abs(rvals*sqrt(w[iinotna])-myvals))<1e-6)
#generate full predictions
lmpred= mod%*%coef(lmres)
#make sure they are the same
stopifnot(sum(abs(rbind(t(lmpred))-testpred))<1e-6)
myvalscpp=naresidCPP(rbind(y[iinotna]), mod[iinotna,],weights = rbind(w[iinotna]))
#naresidCPP also includes a stadartization step
stopifnot(mean(abs(rvals*sqrt(w[iinotna])-myvals))<1e-6)
*/