【发布时间】:2012-11-15 22:28:01
【问题描述】:
我正在尝试对数据集中的每个邮政编码运行回归并将系数保存到数据框中,但我遇到了麻烦。
每当我运行下面的代码时,我都会得到一个名为“coefficients”的数据框,其中包含每个邮政编码,但每个邮政编码的截距和系数等于简单回归 lm(Sealed$hhincome ~ Sealed$square_footage) 的结果。
当我运行下面链接中 Ranmath 示例中所示的代码时,一切都按预期运行。在使用 STATA 多年后,我是 R 新手,因此将不胜感激任何帮助:)
R extract regression coefficients from multiply regression via lapply command
library(plyr)
Sealed <- read.csv("~/Desktop/SEALED.csv")
x <- function(df) {
lm(Sealed$hhincome ~ Sealed$square_footage)
}
regressions <- dlply(Sealed, .(Sealed$zipcode), x)
coefficients <- ldply(regressions, coef)
【问题讨论】: