I am developing a cross validation loop in Rccp and Rcpparmadillo. The same loop works perfectly in R. THe problem is that in Rcpp the old values of the loop are not discarded. Below is a very simplified code. The frmwC is a big Rcpparmadillo function which gives the same results with R same function when the number of iterations are 1 (nR) but when the nR are more the previous values are added to the new one, but i want to keep onle the last one as happens in R. The frmwC export the results as a list
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export()]]
mat mult(const mat& X,const mat& Y, const mat& Xt, IntegerVector idx, int A, int nR)
{
uvec idx1 = as<uvec>(idx);
List model;
NumericMatrix Yc1;
for (int i = 0; i <nR; i++)
{
mat X0 = X.rows(idx1);
mat Y0 = Y.rows(idx1);
mat X1 = subset_mat(X, idx);
mat Y1 = subset_mat(Y, idx);
// Rcout << X0;
model = frmwC(X1,Y1,X0, A = A);
}
mat Yc = as<mat>(model("predYt"));
return Yc;
}
Below are the results for nR = 1
X = matrix(runif(100, 1, 100),10,10)
Y = matrix(round(runif(10,1,100)))
Xt = matrix(runif(50,1,100),5,10)
Yt = matrix(round(runif(5, 1,100)))
idx = sample(size = 5, 0:9)
A = 10; nR = 1; nG = 2; Ix = matrix(0, A, ncol(X) + 1)
mult(X = X, Y = Y, Xt, idx = idx, A = A, nR = nR)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 61.28675 80.15003 76.19408 73.11867 72.55760 0 0 0 0 0
[2,] 48.45832 43.98474 36.04582 33.73255 32.79945 0 0 0 0 0
[3,] 35.35432 32.30002 37.16849 39.35326 39.58430 0 0 0 0 0
[4,] 54.93001 48.29227 44.68769 53.29742 53.36127 0 0 0 0 0
[5,] 52.17147 64.95472 65.61675 76.79194 75.09447 0 0 0 0 0
the Results for nR = 10
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 714.3052 733.1685 729.2125 726.1371 725.5760 0 0 0 0 0
[2,] 343.6533 339.1798 331.2408 328.9276 327.9945 0 0 0 0 0
[3,] 391.6130 388.5587 393.4271 395.6119 395.8430 0 0 0 0 0
[4,] 535.1815 528.5437 524.9391 533.5489 533.6127 0 0 0 0 0
[5,] 728.0217 740.8049 741.4669 752.6421 750.9447 0 0 0 0 0
And using the R version of the code
X0 = X[(idx+1),]
Y0 = Y[(idx+1),,drop = F]
X1 = X[-(idx+1),]
Y1 = Y[-(idx+1),,drop = F]
for (i in 1:10) {
testR = frmwR(X = X1,Y = Y1, Xt = X0, Ix = Ix,A = A,Iss = Iss)
}
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 61.28675 80.15003 76.19408 73.11867 72.55760 0 0 0 0 0
[2,] 48.45832 43.98474 36.04582 33.73255 32.79945 0 0 0 0 0
[3,] 35.35432 32.30002 37.16849 39.35326 39.58430 0 0 0 0 0
[4,] 54.93001 48.29227 44.68769 53.29742 53.36127 0 0 0 0 0
[5,] 52.17147 64.95472 65.61675 76.79194 75.09447 0 0 0 0 0
As long as the idx is the same all the values no matter the iterations should be the same. How can i make Rcpp to discard the previous values from the loop and keep the last only? My cross validation is more complicated and i can not just divide the values with the number of iterations. THanky you