R: extracting regression results using two or more cycles

Based on this post , I created the following matrix and for loops to cycle through all the regression combinations in my df:

 all_lm <-data.frame(matrix(nrow=180, ncol=9)) names(all_lm)=c("col1", "col2", "Estimate", " Std. Error", " z value", " pValue", "2.5%", "97.5%", "r^2") 

and to save the results:

 for (i in c("A","B","C")) for (j in c(1:10)) for (k in c("D","E")) for (l in c("F", "G", "H")){ form <- formula(paste0(i,"_PC_AB_",k, " ~ ", l)) result<-lm(form, data = schools, subset=Decile==j) all_lm[i,1]<-i all_lm[i,2]<-j all_lm[i,3]<-round(coef(summary(result))[2,1],3) all_lm[i,4]<-round(coef(summary(result))[2,2],3) all_lm[i,5]<-round(coef(summary(result))[2,3],3) all_lm[i,6]<-round(coef(summary(result))[2,4],3) all_lm[i,7]<-round(confint(result)[2,1],2) all_lm[i,8]<-round(confint(result)[2,2],2) all_lm[i,9]<-round(summary(result)$r.squared, 3) } 

This loop configuration works when I use it to export graphs to Cairo , but I understand that all_lm[i,n] is the wrong approach. I do not know enough about R to solve this problem. I tried various combinations, such as all_lm[i,j,k,n] . I also tried {after each for , but that didn't work. How can I go through 180 regressions and save the results in my matrix?

0
for-loop r
source share
1 answer

In most cases in R, if you are interested in using a for loop (not to mention nested loops), you are likely to be mistaken.

A general approach to solving your problem is to use the expand.grid function to create all input combinations, then use mapply to repeat each input combination multiple times and return the result list, then use do.call combine the result list into a data frame.

Your code should look something like this:

 i <- c('A','B','C') j <- 1:10 k <- c('D','E') l <- c('F','G','H') params <- expand.grid(i, j, k, l, stringsAsFactors = FALSE) 

You now have a data frame for all input combinations.

 > head(params) Var1 Var2 Var3 Var4 1 A 1 DF 2 B 1 DF 3 C 1 DF 4 A 2 DF 5 B 2 DF 6 C 2 DF > tail(params) Var1 Var2 Var3 Var4 175 A 9 EH 176 B 9 EH 177 C 9 EH 178 A 10 EH 179 B 10 EH 180 C 10 EH 

Now configure the function that mapply will use for each row of the params data frame.

 # one_lm <- function(i, j, k, l) { form <- formula(paste0(i,"_PC_AB_",k, " ~ ", l)) result <- lm(form, data = schools, subset=Decile==j) list( col1 = i, col2 = j, estimate = round(coef(summary(result))[2,1],3), std_err = round(coef(summary(result))[2,2],3), z_value = round(coef(summary(result))[2,3],3), p_value = round(coef(summary(result))[2,4],3), pct_2.5 = round(confint(result)[2,1],2), pct_97.5 = round(confint(result)[2,2],2), r_square = round(summary(result)$r.squared, 3) ) } 

Now use mapply to process each combination one at a time and return a list of ratings, std_err, etc. for each row.

 result_list <- mapply(one_lm, params[,1], params[,2], params[,3], params[,4], SIMPLIFY = FALSE) 

You can then combine all of these lists into a data frame using the do.call and rbind .

 results <- do.call(rbind, result_list) 
+2
source share

All Articles