Frank E Harrell Jr
2009-Aug-20 23:01 UTC
[R] Problem passing a statistical model as an argument
Dear Colleagues: I would like to write a front end for a modeling function, but run into trouble when passing a weights argument. y <- 1:5 x <- c(1,3,2,5,4) w <- function(formula, ...) lm(formula, ...) lm(y ~ x, weights=c(1,1,1,2,2)) w(y ~ x) w(y ~ x, weights=c(1,1,1,2,2)) Error in eval(expr, envir, enclos) : ..1 used in an incorrect context, no ... to look in w <- function(formula, weights, ...) lm(formula, weights=weights, ...) w(y ~ x, weights=c(1,1,1,2,2)) Error in model.frame.default(formula = formula, weights = weights, drop.unused.levels = TRUE) : invalid type (closure) for variable '(weights)' Is there a general approach that solves this? Thanks -Frank > sessionInfo() R version 2.9.0 (2009-04-17) x86_64-pc-linux-gnu locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] splines stats graphics grDevices utils datasets methods [8] base other attached packages: [1] survival_2.35-4 -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
Gabor Grothendieck
2009-Aug-21 00:06 UTC
[R] Problem passing a statistical model as an argument
Try: w <- function(formula, ...) do.call("lm", list(formula, ...)) On Thu, Aug 20, 2009 at 7:01 PM, Frank E Harrell Jr<f.harrell at vanderbilt.edu> wrote:> Dear Colleagues: > > I would like to write a front end for a modeling function, but run into > trouble when passing a weights argument. > > y <- 1:5 > x <- c(1,3,2,5,4) > > w <- function(formula, ...) lm(formula, ...) > lm(y ~ x, weights=c(1,1,1,2,2)) > w(y ~ x) > w(y ~ x, weights=c(1,1,1,2,2)) > > Error in eval(expr, envir, enclos) : > ?..1 used in an incorrect context, no ... to look in > > w <- function(formula, weights, ...) lm(formula, weights=weights, ...) > w(y ~ x, weights=c(1,1,1,2,2)) > > Error in model.frame.default(formula = formula, weights = weights, > drop.unused.levels = TRUE) : > ?invalid type (closure) for variable '(weights)' > > Is there a general approach that solves this? ?Thanks -Frank > >> sessionInfo() > R version 2.9.0 (2009-04-17) > x86_64-pc-linux-gnu > > locale: > LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C > > attached base packages: > [1] splines ? stats ? ? graphics ?grDevices utils ? ? datasets ?methods > [8] base > > other attached packages: > [1] survival_2.35-4 > > -- > Frank E Harrell Jr ? Professor and Chair ? ? ? ? ? School of Medicine > ? ? ? ? ? ? ? ? ? ? Department of Biostatistics ? Vanderbilt University > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Charles C. Berry
2009-Aug-21 01:16 UTC
[R] Problem passing a statistical model as an argument
On Thu, 20 Aug 2009, Frank E Harrell Jr wrote:> Dear Colleagues: > > I would like to write a front end for a modeling function, but run into > trouble when passing a weights argument.You might want to look at the code at the beginning of lm for some ideas. Something like: w <- function(formula, ...) { mc <- match.call() mc[[1L]] <- as.name("lm") eval(mc,parent.frame()) } might get you going. HTH, Chuck> > y <- 1:5 > x <- c(1,3,2,5,4) > > w <- function(formula, ...) lm(formula, ...) > lm(y ~ x, weights=c(1,1,1,2,2)) > w(y ~ x) > w(y ~ x, weights=c(1,1,1,2,2)) > > Error in eval(expr, envir, enclos) : > ..1 used in an incorrect context, no ... to look in > > w <- function(formula, weights, ...) lm(formula, weights=weights, ...) > w(y ~ x, weights=c(1,1,1,2,2)) > > Error in model.frame.default(formula = formula, weights = weights, > drop.unused.levels = TRUE) : > invalid type (closure) for variable '(weights)' > > Is there a general approach that solves this? Thanks -Frank > >> sessionInfo() > R version 2.9.0 (2009-04-17) > x86_64-pc-linux-gnu > > locale: > LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C > > attached base packages: > [1] splines stats graphics grDevices utils datasets methods > [8] base > > other attached packages: > [1] survival_2.35-4 > > -- > Frank E Harrell Jr Professor and Chair School of Medicine > Department of Biostatistics Vanderbilt University > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901