[R] lm with an arbitrary number of terms

Richard Nixon richard.nixon at mrc-bsu.cam.ac.uk
Thu Apr 3 12:04:36 CEST 2003


Thanks to James Holtman, Matt Wiener, Douglas Bates, Spencer Graves, John Fox,
Roger Peng, Bill Venable, Darryl Greig, Vadim Ogranovich and Peter Dalgaard

There are several ways to tackel this problem, and one easy one which I missed
:-) 


----------------------------------------------------------------------
Question
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)?

----------------------------------------------------------------------
y <- c(67,76,56,48,10,43,12,33,88,63,75,14,58,19,14)
x1 <- as.factor(c(2,2,2,2,2,1,1,1,1,1,1,1,1,1,1))
x2 <- c(25,40,35,34,51,24,37,31,26,21,41,45,24,26,29)

data<-data.frame(x1,x2)
----------------------------------------------------------------------
1) James Holtman

fun <- function(y, data.frame){
    .vars <- paste(names(data.frame), collapse="+")
    eval(parse(text=paste('lm(y~',.vars,',data.frame)')))
}

fun(y,data)

Call:
lm(formula = y ~ x1 + x2, data = data.frame)

Coefficients:
(Intercept)          x12           x2  
     84.565       18.763       -1.403  
----------------------------------------------------------------------
2) Matt Wiener

fun <- function(y, data.frame){
    lm(y ~ ., data = data.frame)
}

fun(y,data)

Call:
lm(formula = y ~ ., data = data.frame)

Coefficients:
(Intercept)          x12           x2  
     84.565       18.763       -1.403  

----------------------------------------------------------------------
3) Matt Wiener

fun <- function(y, data.frame){
    .vars <- paste(names(data.frame), collapse="+")
     lm(as.formula(paste('y~',.vars)),data.frame)
}

fun(y,data)

Call:
lm(formula = as.formula(paste("y~", .vars)), data = data.frame)

Coefficients:
(Intercept)          x12           x2  
     84.565       18.763       -1.403  


----------------------------------------------------------------------
Douglas Bates: same as (2)
Spencer Graves: similar to (3)
John Fox: mixture of (2) and (3) 
Roger Peng: similar to (3)
Bill Venable: same as (2)
Darryl Greig: simmilar to (3)
Vadim Ogranovich: same as (2)

Peter Dalgaard
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.)
Neither did I - 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



More information about the R-help mailing list