我正在使用 dplyr 和 broom 组合(如下所示)并使用 dplyr 拟合几个回归模型以按组提取回归的回归系数。但是 - 我也对每个单独回归的 R2 值感兴趣(不仅对于整个模型)。我尝试使用增强和扫视功能 - 但无法提取 R2 值。是否有捷径可寻?
提前谢谢了!
library(dplyr)
library(broom)
df.h = data.frame(
hour = factor(rep(1:24, each = 21)),
price = runif(504, min = -10, max = 125),
wind = runif(504, min = 0, max = 2500),
temp = runif(504, min = - 10, max = 25)
)
dfHour = df.h %>% group_by(hour) %>%
do(fitHour = lm(price ~ wind + temp, data = .))
# get the coefficients by group in a tidy data_frame
dfHourCoef = tidy(dfHour, fitHour)
dfHourCoef
hour term estimate std.error statistic p.value
1 1 (Intercept) 92.173945687 21.82132710 4.2240302 5.102470e-04
2 1 wind -0.020840948 0.01335945 -1.5600151 1.361653e-01
3 1 temp -0.162495052 0.90573269 -0.1794073 8.596220e-01
4 2 (Intercept) 53.569821889 20.90439474 2.5626105 1.957638e-02
5 2 wind 0.006492773 0.01273038 0.5100220 6.162329e-01
6 2 temp -0.493028932 0.78353239 -0.6292387 5.370978e-01
7 3 (Intercept) 93.949047453 14.55042590 6.4567902 4.483106e-06
8 3 wind -0.010084298 0.01179878 -0.8546902 4.039553e-01
9 3 temp -0.096177966 0.68416185 -0.1405778 8.897647e-01
10 4 (Intercept) 68.429142611 20.37382251 3.3586796 3.497149e-03