From what I understand, you want to write a loop that allows you to use lm
with different formulas. The nice thing about lm
is that it can take objects of the class formula
as its first argument. Lets see how that works.
# Create a data set
df <- data.frame(col1=(1:10+rnorm(10)), col2 = 1:10, col3 = rnorm(10), col4 = rnorm(10))
If we want to run lm
on col1
as the dependent and col2
as the independent variable, then we can do this:
model_a <- lm(col1 ~ col2, data = df)
form_b <- as.formula("col1 ~ col2")
model_b <- lm(form_b, data = df)
all.equal(model_a,model_b)
# [1] "Component “call”: target, current do not match when deparsed"
So the only thing that differed between the two models is that the function call was different (in model_b
we used form_b
, not col1 ~ col2
). Other than that, the models are identical.
So now you know how to use the formula
class to run lm
. You can easily construct formulas with paste
, by setting collapse
to +
ind_vars <- paste(names(df)[-1],collapse = " + ")
form_lm <- paste(names(df)[1], "~", ind_vars)
form_lm
# [1] "col1 ~ col2 + col3 + col4"
If we want three different models, we can do a couple of things, for example:
lis <- list()
for (i in 2:length(names(df))) {
ind_vars <- paste(names(df)[2:i], collapse="+")
form_lm <- paste(names(df)[1], "~", ind_vars)
lis[[i-1]] <- lm(form_lm,data=df)
}