<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p>Hi,</p>
<p>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.</p>
<p>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.</p>
<p>#include <Rcpp.h><br />using namespace Rcpp;<br />// [[Rcpp::export]]<br />NumericVector misclass(NumericMatrix obs_mat) {<br />  // Obtain environment containing function<br />  Rcpp::Environment base("package:stats");<br /><br />  // Picking up glm() and summary() function from base stats<br />  Rcpp::Function glm_r = base["glm"];<br />  Rcpp::Function sum_r = base["summary.glm"];<br /><br />  Rcpp::NumericVector d = obs_mat(_, 1);<br />  Rcpp::NumericVector e = no_init(n);<br />  Rcpp::NumericVector mod_coef = no_init(n);<br />  // e is calculated in other section of the code<br />  e = as<IntegerVector>(e);<br />  Rcpp::List mod_pois = glm_r(_["formula"] = "d ~ e",<br />                    _["family"] = "poisson");<br />  Rcpp::List mod_sum = sum_r(mod_pois);<br />  Rcpp::NumericMatrix M_coef = mod_sum[12];<br />  mod_coef = M_coef(2, 1);<br /><br />  return mod_coef;<br />}</p>
<p><br /></p>
<p>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.</p>
<p>Thanks,</p>
<p>Denis</p>

</body></html>