[R] lm with an arbitrary number of terms
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Thu Apr 3 00:21:09 CEST 2003
Richard Nixon <richard.nixon at mrc-bsu.cam.ac.uk> writes:
> 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)?
>
You could build up the formula in a for loop; something like
n <- names(my.data.frame)
f <- as.symbol(n[1])
for (i in 2:length(n))
f <- substitute(f+a,list(a=as.symbol(n[i]))
f <- substitute(y~f)
(I didn't try it. Most likely it doesn't quite work.)
However, what's wrong with
lm(y ~ ., data=my.data.frame)
??
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list