-1

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

JRR
  • 3,024
  • 2
  • 13
  • 37
NickSc79
  • 11
  • 3
  • 2
    This does not present a [Minimal, Complete, and Verifiable Example](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjkiKiG0JXgAhUH-6wKHW-ADM0QFjAAegQIAxAC&url=https%3A%2F%2Fstackoverflow.com%2Fhelp%2Fmcve&usg=AOvVaw0S639LB-n61Tf1wySi7YAL) (see also [How to create a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) -- some of that will be applicable to you). It's hard for others to help you when we don't know what your functions or data look like. – duckmayr Jan 30 '19 at 13:38
  • The frmwc is a big function which does PLS in essence and at the end generates a list of 17 elements which most of them are class mat armadillo. I am not familiar with C++ at all and i would like to know if previous elements of the for loop are allocated in the memory and are added to the new values and how we can avoid it. The data are just random numbers. – NickSc79 Jan 30 '19 at 17:33
  • error: ‘subset_mat’ was not declared in this scope ; error: ‘frmwC’ was not declared in this scope. It is hard to help you with a non reproducible example – JRR Feb 03 '19 at 12:55

1 Answers1

0

I finally resolved the issue the problem was in the frmwC function where the intialisation of matrixes was declared. Initially, i did not declare an initial value and that caused the problem. Now i declared an initial value and everything works as expected (check the code script below. However i am not sure why did this happen. If anyone is able to elaborate it would be perfect

mat T = zeros(X.n_rows, A), P = zeros(X.n_cols, A), V = P, W = P,
    B = zeros(X.n_cols, Y.n_cols), Q = zeros(Y.n_cols, A), 
    Ys = zeros(X.n_rows, A-1),
    Yts = zeros(Xt.n_rows, A), Si = zeros(X.n_cols, X.n_cols), 
    Bt = zeros(X.n_cols, A),Xi = zeros(X.n_cols, X.n_rows),
    Res = zeros(A, 6), Cor = zeros(A,1);
NickSc79
  • 11
  • 3