R向数据框添加回归系数 [英] R adding regression coeffcients to data frame

查看:68
本文介绍了R向数据框添加回归系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多数据子集的数据帧列表(470ish).我正在尝试对每个变量进行回归并将回归系数添加到数据框.数据框将包含每个子组上所有因变量的系数.我尝试使用for循环进行迭代,但显然这不是正确的方法.我认为解决方案与lapply有关?

I have a list of dataframes that contains many subsets of data (470ish). I am trying to run a regression on each of them and add the regression coefficients to a dataframe. The dataframe will contain the coefficients for all dependent variables on each subgroup. I tried iterating with a for loop but obviously that is not the right way. I think the solution has something to do with lapply?

for (i in ListOfTraining){


    lm(JOB_VOLUME ~  FEB+MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC        data=ListOfTraining[[i]])

}

感谢您的任何建议!

推荐答案

如果愿意,可以使用 for 循环来解决此问题.您的问题是,随着循环的进行,结果没有保存到对象中.您可以在下面看到使用内置 mtcars 数据框的示例.

You can solve this using the for loop, if you prefer. Your problem is that the results aren't being saved to an object as the loop progresses. You can see the below for an example using the built-in mtcars dataframe.

(这是根据OP的要求修改的第一个示例,该示例还提供了如何提取R平方值的示例.)

(This first example is revised based on OP's request for an example of how to also extract the R squared value.)

ListOfTraining <- list(mtcars, mtcars)
results <- list()

for (i in seq_along(ListOfTraining)) {
  lm_obj <- lm(disp ~ qsec, data = ListOfTraining[[i]])
  tmp <- c(lm_obj$coefficients, summary(lm_obj)$r.squared)
  names(tmp)[length(tmp)] <- "r.squared"
  results[[i]] <- tmp
}

results <- do.call(rbind, results)
results

您还可以使用 lapply 重写 for 循环,如下所示.

You can also rewrite the for loop using lapply as demoed below.

ListOfTraining <- list(mtcars, mtcars)
results <- list()

results <- lapply(ListOfTraining, function(x) {
  lm(disp ~ qsec, data = x)$coefficients
})

results <- do.call(rbind, results)
results

最后,您可以使用 plyr 包的 ldply 函数,该函数会自动将列表应用的输出转换为数据帧(如果可能).

Finally, you can use the plyr package's ldply function which will convert the list applied outputs into a dataframe automatically (if possible).

ListOfTraining <- list(mtcars, mtcars)
results <- plyr::ldply(ListOfTraining, function(x) {
  lm(disp ~ qsec, data = x)$coefficients
})
results

这篇关于R向数据框添加回归系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆