Hello, I am trying to figure out the proper way to use ellipsis to pass arguments to child functions. I'm trying to create a wrapper function around stats::lm and lme4::lmer such that if the formula passed to the wrapper function contains a random effect it will fit a lme4::lmer model and if not it will fit a stats::lm object. I want the function to alway want a formula and data argument and then use the ellipsis argument to pass additional arguments to either lme4::lmer or stats::lm. The problem is that when I pass additional arguments through the ellipsis argument I get the error "Error in eval(extras, data, env) : ..1 used in an incorrect context, no ... to look in". A simplified version of my attempt with a reproducible example is here: fit_model <- function(formula, data, ...) { lm(formula, data, ...) } # Create test data data <- data.frame(x1 = rnorm(100), x2 = rnorm(100)) data$y <- data$x1 + data$x2 + rnorm(100) fit_model(y ~ x1 + x2, data = data) # This works fit_model(y ~ x1 + x2, data = data, weights = rep(1, 100)) # This does not work Any hints as to why this error occurs would be appreciated. I found [1], but I don't think that's relevant, since as far as I can tell, there should be no non-standard evaluation here. [1] stat.ethz.ch/pipermail/r-help/2010-February/228881.html Andreas Matre
On Thu, 26 May 2022 20:54:50 +0200 Andreas Matre <r at elonus.net> wrote:> fit_model <- function(formula, data, ...) { > lm(formula, data, ...) > }> fit_model(y ~ x1 + x2, data = data, weights = rep(1, 100)) # This > does not workWhen I run traceback(), I see an eval() there: lm() captures its own call, replaces lm with stats::model.frame and evaluates that call in the parent frame. I don't have a good explanation why it's the ellipsis that fails here, but adding more non-standard evaluation seems to fix the problem: fit_model2 <- function(formula, data, ...) eval(substitute(lm(formula, data, ...)), parent.frame()) fit_model2(y ~ x1 + x2, data = data, weights = rep(1, 100)) I think this works because substitute() expands the ellipsis into its return value. -- Best regards, Ivan
Inline below. Bert Gunter ... "> Any hints as to why this error occurs would be appreciated. I found [1], > but I don't think that's relevant, since as far as I can tell, there > should be no non-standard evaluation here. > > [1] stat.ethz.ch/pipermail/r-help/2010-February/228881.html" You are mistaken. Evaluation of any of the arguments in `data` **is** exactly what is meant by nse. ?lm explicitly says: "All of weights, subset and offset are evaluated in the same way as variables in formula, that is first in data and then in the environment of formula." The following is one simple, but somewhat sloppy, way to handle it (insufficient argument checking): fit_model2 <- function(formula, data,...){ arglist <- c(list(formula = formula, data = data),list(...)) do.call(lm, arglist) } fit_model2(y ~ x1 + x2, data = data, weights = rep(1, 100)) ## works> > > > Andreas Matre > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.