<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>Quick follow-up and additional question.</p>
<p>I got it working with fastglm:</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 />  // Obtaining namespace of fastglm package<br />  Environment pkg = Environment::namespace_env("fastglm");<br />  Function f = pkg["fastglm"];<br /> ...<br /> mod_log = f(Named("x", ematrix), Named("y", d), Named("family", "binomial"));</p>
<div id="signature"></div>
<p><br /></p>
<p>which works fine, but I cannot figure out how to get another link than the default 'logit' (here for binomial()). I tried:</p>
<p>Named("family", "binomial(link = \"log\")")</p>
<p>Named("family", "binomial(link = log)")</p>
<p>Both gave me this error: "object of node 'function' not found.</p>
<p>Named("link", "log")</p>
<p>Named("make.link", "log")</p>
<p>These last two do not cause an error, but they give the default logit link (as if the Named was not evaluated?).</p>
<p>Thanks,</p>
<p>Denis</p>
<p><br /></p>
<p id="reply-intro">Le 2024-09-04 16:24, Dirk Eddelbuettel a écrit :</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">
<div class="pre" style="margin: 0; padding: 0; font-family: monospace"><br />On 4 September 2024 at 15:37, Denis Haine wrote:<br />| Hi,<br />| <br />| Sorry for a beginner's question. I'm trying to call an R function (glm())<br />| inside my cpp code. The code compiles with no problem, but when I'm running it,<br />| it cannot find the second element of the formula, i.e. the x in y~x. The error<br />| message is: Error in eval (predvars; data, env) : object 'e' not found.<br />| <br />| However, if I return 'e', it is correctly calculated. I guess the formula is<br />| not correctly evaluated, but I haven't found any examples that could point me<br />| in one direction or another.<br />| <br />| #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 />You are running glm() from base here. At the speed of glm() from base.<br /><br />That you call it from C++ does not make it faster, sadly, it just creates<br />more work for you. Sorry to be a bearer of bad news.<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 /><br />You need to 'work out' (ie expand) the formula on the R side. I have done<br />that for the lm() case of model matrices and whatnot in the fastLm()<br />example(s) inside eg RcppArmadillo.  Once you have done the R-level unpacking<br />you can call the C++ function to fit.<br /><br />Big takeaway from that exercise there: the time you spent dealing with the<br />(convenient) formula dominates the gain from using a different, simpler,<br />slightly faster 'fitter' by many orders of magnitude.<br /><br />[ I think there are some packages dealing with glm() fits using Rcpp and<br />friends among the by now over 2800 CRAN packages using Rcpp -- but I can't<br />right now recall their names. You may find some help in those if you find<br />them, and if my recollection was correct in the first place ... ]<br /><br />Dirk<br /><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 />| }<br />| <br />| <br />| I also tried providing the formula in the call, i.e. NumericVector misclass<br />| (NumericMatrix obs_mat, Formula f) and using it in glm_r, i.e. glm_r(_<br />| ["formula"] = f, etc. but with the same outcome.<br />| <br />| Thanks,<br />| <br />| Denis<br />| <br />| _______________________________________________<br />| Rcpp-devel mailing list<br />| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br />| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank" rel="noopener noreferrer">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></div>
</blockquote>
</body></html>