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] https://stat.ethz.ch/pipermail/r-help/2010-February/228881.html

Andreas Matre

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to