Textime
Textime

Reputation: 99

Iterate over list and append in order to do a regression in R

I know that somewhere there will exist this kind of question, but I couldn't find it. I have the variables a, b, c, d and I want to write a loop, such that I regress and append the variables and regress again with the additional variable

lm(Y ~ a, data = data), then lm(Y ~ a + b, data = data), then

lm(Y ~ a + b + c, data = data) etc.

How would you do this?

Upvotes: 3

Views: 636

Answers (3)

jay.sf
jay.sf

Reputation: 72813

You could do this with a lapply / reformulate approach.

formulae <- lapply(ivars, function(x) reformulate(x, response="Y"))
lapply(formulae, function(x) summary(do.call("lm", list(x, quote(dat)))))

Data

set.seed(42)
dat <- data.frame(matrix(rnorm(80), 20, 4, dimnames=list(NULL, c("Y", letters[1:3]))))
ivars <- sapply(1:3, function(x) letters[1:x])  # create an example vector ov indep. variables

Upvotes: 3

zx8754
zx8754

Reputation: 56149

Using paste and as.formula, example using mtcars dataset:

myFits <- lapply(2:ncol(mtcars), function(i){
  x <- as.formula(paste("mpg", 
                        paste(colnames(mtcars)[2:i], collapse = "+"), 
                        sep = "~"))
  lm(formula = x, data = mtcars)
})

Note: looks like a duplicate post, I have seen a better solution for this type of questions, cannot find at the moment.

Upvotes: 3

Gregor Thomas
Gregor Thomas

Reputation: 145775

vars = c('a', 'b', 'c', 'd')
# might want to use a subset of names(data) instead of
# manually typing the names

reg_list = list()
for (i in seq_along(vars)) {
  my_formula = as.formula(sprintf('Y ~ %s', paste(vars[1:i], collapse = " + ")))
  reg_list[[i]] = lm(my_formula, data = data)
}

You can then inspect an individual result with, e.g., summary(reg_list[[2]]) (for the 2nd one).

Upvotes: 2

Related Questions