0

I am trying to compile the following code. Please see below what I have tried so far. Is there anything that I am missing. Any help would be appreciated.

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;

// [[Rcpp::export]]
List beta(const arma::rowvec beta,
          const int n, 
          const int L1,
          const int p,
          const arma::mat YWeight1,
          const arma::mat z){

    double S0=0;

    for(int i = 0;i < n; ++i){
        arma::rowvec zr = z.rows(i,i);
        S0 +=  exp(arma::as_scalar(beta*zr.t()));
    }

    List res;
    res["1"] = S0;
    return(res);
}

I can't copy the error but this is what I am getting.

no match for call to '(Rcpp::traits::input_parameter<const arma::Row<double> 

and so on...

coatless
  • 20,011
  • 13
  • 69
  • 84
Hello
  • 61
  • 1
  • 9
  • Thank you for the quick response. I still get the same error after the change. I also changed beta to beta.t() just to make it compatible in line with S0 +=. – Hello Jul 24 '19 at 16:08

1 Answers1

2

There is a rowvec converter. The issue here is:

filece5923f317b2.cpp:39:34: error: type 'Rcpp::traits::input_parameter::type' (aka 'ConstInputParameter >') does not provide a call operator rcpp_result_gen = Rcpp::wrap(beta(beta, n, L1, p, YWeight1, z));

Few thoughts: 1. There is already a function called beta() and 2. there is a variable named beta that might be causing havoc with Rcpp attributes.

Solution:

  • Remove the using namespace Rcpp;
  • Rename the function away from beta() to beta_estimator().
  • Specify the length of Rcpp::List
  • Access by numeric index instead of string.

Corrected code:

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
Rcpp::List beta_estimator( // renamed function
          const arma::rowvec beta,
          const int n, 
          const int L1,
          const int p,
          const arma::mat YWeight1,
          const arma::mat z){

    double S0 = 0;

    for(int i = 0;i < n; ++i){
        arma::rowvec zr = z.rows(i,i);
        S0 += exp(arma::as_scalar(beta*zr.t()));
    }

    // Specify list length
    Rcpp::List res(1);
    res[0] = S0;
    return(res);
}
coatless
  • 20,011
  • 13
  • 69
  • 84
  • That worked just fine. Thanks for your help. I am very new to C++. I am trying to write my R code in C++ to gain efficiency. – Hello Jul 24 '19 at 17:07
  • One more question. In the above code, I am simply adding arma::mat S1(p,1) right after the for loop. Then I have Rcpp::List res(2); res[0]=S0; res[1]=S1;return(res);. I am getting an error that S1 was not declared in this scope. – Hello Jul 24 '19 at 17:16
  • `S1` is not defined in the function given above. Only `S0` is defined. Add a line defining it similar to `double S0 = 0;` outside of the loop and you should be fine. – coatless Jul 24 '19 at 20:13
  • Got it. Thanks for you help. – Hello Jul 26 '19 at 13:15