-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomReg.cpp
More file actions
202 lines (144 loc) · 5.13 KB
/
randomReg.cpp
File metadata and controls
202 lines (144 loc) · 5.13 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
//[[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <string>
#include <stdexcept>
#include <omp.h>
using namespace Rcpp;
using namespace std;
using namespace arma;
// [[Rcpp::export]]
arma::mat ridge_solver(arma::mat x,
arma::mat y,
double lambda){
mat d = lambda * eye(x.n_cols, x.n_cols);
mat beta = inv_sympd(x.t() * x + d) * x.t() * y;
return beta;
}
//[[Rcpp::export]]
double metric_fun(vec y, vec y_hat, std::string metric){
if (metric == "rmse"){
return sqrt( mean( square((y - y_hat)) ) );
}else if (metric == "mse"){
return mean( square((y - y_hat)) );
}else if (metric == "mae"){
return mean( abs(y - y_hat) );
}else if (metric == "mape"){
y.replace(0, 1e-15);
return mean( abs( (y - y_hat) / y ) );
}else if (metric == "mspe"){
y.replace(0, 1e-15);
return sqrt( mean( square( (y - y_hat) / y ) ) );
}else if (metric == "rmsle"){
return sqrt ( mean( square(log1p(1 + y) - log1p(1 + y_hat)) ) );
}else if (metric == "xentropy"){
return mean( y % log(y_hat) + (1-y) % log(1-y_hat) );
}else if (metric == "none"){
return 1;
}else{
throw std::invalid_argument( "unsupported metric" );
}
return -1;
}
// [[Rcpp::export]]
List randomRegression_fit(arma::mat x,
arma::mat y,
double colsample,
double subsample,
uvec holdvar,
int n_reg = 500,
double lambda = 0.01,
string weight_metric = "rmse",
bool intercept = true){
//Function sample = Environment("package:base")["sample"];
int n = x.n_rows; int p = x.n_cols;
int colsample_size; int subsample_size;
if(colsample <= 0 || colsample > 1){
colsample_size = p;
cout << "invalid colsample, reset to 1"<<endl;
}else{
colsample_size = ceil(p * colsample);
}
if(subsample <= 0 || subsample > 1){
subsample_size = n;
cout << "invalid subsample, reset to 1"<<endl;
}else{
subsample_size = ceil(n * subsample);
}
List betaList(n_reg); List fittedList(n_reg); List OOB_pred(n_reg);
vec err(n_reg); vec fitted_val = zeros(n);
vec w(n_reg); vec oob_err(n_reg);
mat beta;
for (int i=0; i<n_reg; ++i){
IntegerVector id1 = Rcpp::seq(0,n-1); IntegerVector id2 = Rcpp::seq(0,p-1);
NumericVector id1_numType=as<NumericVector>(id1); NumericVector id2_numType=as<NumericVector>(id2);
NumericVector obsId_numType = sample(id1_numType, subsample_size, true);
vec oob_index = setdiff(id1_numType, obsId_numType); vec varId_vec = sample(id2_numType, colsample_size, false);
vec obsId_vec = obsId_numType;
uvec boot_index = conv_to<uvec>::from(obsId_vec);
uvec boot_var = conv_to<uvec>::from(varId_vec);
uvec uoob_index = conv_to<uvec>::from(oob_index);
if ( !any(holdvar == -1) ){
boot_var = unique(join_cols(boot_var, holdvar));
}
mat x_try = x.submat(boot_index, boot_var);
vec y_try = y.elem(boot_index);
mat x_oob = x.submat(uoob_index, boot_var);
vec y_oob = y.elem(uoob_index);
if (intercept){
x_try = join_rows(ones(x_try.n_rows), x_try);
vec beta_full = zeros(p+1);
beta = ridge_solver(x_try, y_try, lambda);
beta_full.elem(boot_var + 1) = beta.rows(1, beta.n_rows-1);
beta_full.row(0) = beta.row(0);
betaList[i] = beta_full;
x_oob = join_rows(ones(x_oob.n_rows), x_oob);
}else{
vec beta_full = zeros(p);
beta = ridge_solver(x_try, y_try, lambda);
beta_full.elem(boot_var) = beta;
betaList[i] = beta_full;
}
vec fitted = x_try * beta;
vec oob_pred = x_oob * beta;
err(i) = metric_fun(y_try, fitted, weight_metric);
if( y_oob.n_rows == 0){
oob_err(i) = datum::inf;
}else{
oob_err(i) = metric_fun(y_oob, oob_pred, weight_metric);
}
//fittedList[i] = fitted;
}
vec err_inv = 1 / oob_err;
w = err_inv / sum(err_inv);
return List::create(_["inSample_err"] = err,
_["oob_err"] = oob_err,
_["w"] = w,
_["beta"] = betaList,
_["intercept"] = intercept,
_["n_reg"] = n_reg,
_["n"] = n,
_["p"] = p);
}
// [[Rcpp::export]]
vec randomRegression_predict(List randomReg,
mat xnew){
int n_reg = randomReg["n_reg"];
vec w = randomReg["w"];
bool intercept = randomReg["intercept"];
List betaList = randomReg["beta"];
vec pred = zeros(xnew.n_rows);
if (intercept){
xnew = join_rows(ones(xnew.n_rows), xnew);
}
for (int i=0; i<n_reg; ++i){
mat beta = betaList[i];
pred += (xnew * beta) * w[i];
}
return pred;
}
// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
//
/*** R
*/