[R] Problem passing a statistical model as an argument

Frank E Harrell Jr f.harrell at vanderbilt.edu
Fri Aug 21 06:13:38 CEST 2009


Gabor Grothendieck wrote:
> Try:
> 
> w <- function(formula, ...) do.call("lm", list(formula, ...))

Thanks very much Gabor.  That seems to do the trick.

Thanks also Rolf and Chuck.  Chuck I had tried your approach earlier but 
ran into a problem.

Frank

> 
> 
> 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.
>>
> 


-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list