[R] lm with an arbitrary number of terms
Roger Peng
rpeng at stat.ucla.edu
Thu Apr 3 01:48:40 CEST 2003
You need to paste() together a formula. There's an example in ?formula.
Try,
n <- 10
rhs <- paste("x", 1:n, collapse = "+", sep = "")
lhs <- "y ~"
f <- as.formula(paste(lhs, rhs))
Then pass `f' into lm().
-roger
_______________________________
UCLA Department of Statistics
rpeng at stat.ucla.edu
http://www.stat.ucla.edu/~rpeng
On Wed, 2 Apr 2003, Richard Nixon wrote:
> Hello folks,
>
> Any ideas how to do this?
>
> data.frame is a data frame with column names "x1",...,"xn"
> y is a response variable of length dim(data.frame)[1]
>
> I want to write a function
>
> function(y, data.frame){
> lm(y~x1+...+xn)
> }
>
> This would be easy if n was always the same.
> If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)?
>
> Thanks
> Richard
>
> --
> Dr. Richard Nixon
> MRC Biostatistics Unit, Cambridge, UK
> http://www.mrc-bsu.cam.ac.uk/personal/richard
> Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
More information about the R-help
mailing list