[R] Problem passing a statistical model as an argument

Frank E Harrell Jr f.harrell at vanderbilt.edu
Fri Aug 21 01:01:10 CEST 2009

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)


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

More information about the R-help mailing list