Is there a way to run a model (for simplicity, a linear model) using specified columns of a data.frame?
For example, I would like to be able to do something like this:
set.seed(1)
ET = runif(10, 1,20)
x1 = runif(10, 1,20)
x2 = runif(10, 1,30)
x3 = runif(10, 1,40)
Xdf = data.frame(ET = ET, X1 = x1, X2 =x2, X3 = x3)
lm(ET~Xdf[,c(2,3)], data = Xdf)
lm(ET~X1 +X2, data = Xdf)
Here's a (rather ugly) way to make it work.
I use as.formula
and the paste
function to make the formula before calling lm
.
I'm sure there are better ways to do this, but this is what came to mind.
# ET ~ X1 + X2
f1 <- as.formula(paste("ET~", paste(names(Xdf)[c(2,3)],
collapse="+")))
lm(f1, data=Xdf)
You can also specify the individual columns, though it might be more work
lm(ET ~ Xdf[,2] + Xdf[,3], data=Xdf)