Hi,

Sorry for a beginner's question. I'm trying to call an R function (glm()) inside my cpp code. The code compiles with no problem, but when I'm running it, it cannot find the second element of the formula, i.e. the x in y~x. The error message is: Error in eval (predvars; data, env) : object 'e' not found.

However, if I return 'e', it is correctly calculated. I guess the formula is not correctly evaluated, but I haven't found any examples that could point me in one direction or another.

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector misclass(NumericMatrix obs_mat) {
  // Obtain environment containing function
  Rcpp::Environment base("package:stats");

  // Picking up glm() and summary() function from base stats
  Rcpp::Function glm_r = base["glm"];
  Rcpp::Function sum_r = base["summary.glm"];

  Rcpp::NumericVector d = obs_mat(_, 1);
  Rcpp::NumericVector e = no_init(n);
  Rcpp::NumericVector mod_coef = no_init(n);
  // e is calculated in other section of the code
  e = as<IntegerVector>(e);
  Rcpp::List mod_pois = glm_r(_["formula"] = "d ~ e",
                    _["family"] = "poisson");
  Rcpp::List mod_sum = sum_r(mod_pois);
  Rcpp::NumericMatrix M_coef = mod_sum[12];
  mod_coef = M_coef(2, 1);

  return mod_coef;
}

I also tried providing the formula in the call, i.e. NumericVector misclass(NumericMatrix obs_mat, Formula f) and using it in glm_r, i.e. glm_r(_["formula"] = f, etc. but with the same outcome.

Thanks,

Denis
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to