asalasa209
asalasa209

Reputation: 47

How to loop regressions over a list of data frames?

I have a list of 192 data frames equally sized with the same variables, the only difference is that the dependent variable changes (values not name) across data frames, the rest remains the same. The list looks like this:

data = (df1, df2, df3,..., df192)

and each df has a size of 17 x 7

The objective is to run a regression for each data frame in the list so, at the end, I will have 192 lm objects. This is my first loop so I have been struggling with this.

my attempt goes as follows:

reg_list = list() # I create a list in which I will store my 192 lm objects
for (i in data) {
reg_list[[i]] = lm(x1 ~ x2 +x3)
}

I know that is missing the argument of the data but as they are 192 dfs I do not know how to indicate that to R.

I appreciate any suggestions to solve this problem.

Upvotes: 0

Views: 753

Answers (1)

Chris
Chris

Reputation: 310

The following works for me:

data = list(data.frame(x=c(1,4,6),y=c(1,2,5)),data.frame(x=c(1,4,6),y=c(0,8,7)),data.frame(x=c(4,4,2),y=c(1,2,5)))
lm(y~x,data=data[[1]]) #example on number 1: specify source of data 

for(i in 1:length(data)){
    reg_list[[i]]=lm(y~x,data=data[[i]]) #loop through from 1-legnth(data)
} 

Or you could do something like:

for(i in 1:length(data)){
    reg_list[[i]]=summary(lm(y~x,data=data[[i]])) 
} 

Upvotes: 2

Related Questions