[Rcpp-devel] Using formula
Dirk Eddelbuettel
edd at debian.org
Wed Sep 4 22:24:48 CEST 2024
On 4 September 2024 at 15:37, Denis Haine wrote:
| 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"];
You are running glm() from base here. At the speed of glm() from base.
That you call it from C++ does not make it faster, sadly, it just creates
more work for you. Sorry to be a bearer of bad news.
| 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");
You need to 'work out' (ie expand) the formula on the R side. I have done
that for the lm() case of model matrices and whatnot in the fastLm()
example(s) inside eg RcppArmadillo. Once you have done the R-level unpacking
you can call the C++ function to fit.
Big takeaway from that exercise there: the time you spent dealing with the
(convenient) formula dominates the gain from using a different, simpler,
slightly faster 'fitter' by many orders of magnitude.
[ I think there are some packages dealing with glm() fits using Rcpp and
friends among the by now over 2800 CRAN packages using Rcpp -- but I can't
right now recall their names. You may find some help in those if you find
them, and if my recollection was correct in the first place ... ]
Dirk
| 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 at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
More information about the Rcpp-devel
mailing list