受SO 这个答案的启发,我正在使用 teh do
dplyr一次执行多个回归,但是我想使用texreg显示我的输出并do()
生成一个rowwise_df
对象,但是如果我提取回归列表,一些信息似乎已经丢失。有没有简单的方法来解决这个问题?下面的最小示例。
首先是一些必需的包
# install.packages(c("tidyverse", "broom", "texreg"), dependencies = TRUE)
library(tidyverse)
二、一些虚拟数据
df.h = data.frame(
hour = factor(rep(1:6, each = 21)),
price = runif(504, min = -10, max = 125),
wind = runif(504, min = 0, max = 2500),
temp = runif(504, min = - 10, max = 25)
)
第三个do()
dfHour = df.h %>% group_by(hour) %>%
do(fitHour = lm(price ~ wind + temp, data = .))
第四,整齐地按组获取系数data_frame
library(broom)
dfHourCoef = tidy(dfHour, fitHour)
dfHourCoef
#> # A tibble: 72 x 6
#> # Groups: hour [6]
#> hour term estimate std.error statistic p.value
#> <fct> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 1 (Intercept) 78.2 17.6 4.44 0.000316
#> 2 1 wind 0.000145 0.0107 0.0135 0.989
#> 3 1 temp - 1.27 0.834 -1.52 0.145
#> 4 2 (Intercept) 69.7 18.9 3.68 0.00171
#> 5 2 wind - 0.0150 0.0121 -1.24 0.232
#> 6 2 temp - 0.00355 0.989 -0.00359 0.997
#> 7 3 (Intercept) 61.0 14.1 4.32 0.000413
#> 8 3 wind - 0.00599 0.00987 -0.607 0.552
#> 9 3 temp 0.603 0.704 0.858 0.402
#> 10 4 (Intercept) 57.9 19.1 3.02 0.00729
#> # ... with 8 more rows
但是,我想使用texreg,我尝试过类似的方法,但是输出以某种方式被打乱了。任何帮助,将不胜感激。
library(texreg)
class(dfHour[[2]])
#> [1] "list"
screenreg(dfHour[[2]]) # Not working
手动操作看起来像这样,
fit1 <- lm(price ~ wind + temp, data = subset(df.h, hour == 1))
fit2 <- lm(price ~ wind + temp, data = subset(df.h, hour == 2))
fit3 <- lm(price ~ wind + temp, data = subset(df.h, hour == 3))
fit4 <- lm(price ~ wind + temp, data = subset(df.h, hour == 4))
fit5 <- lm(price ~ wind + temp, data = subset(df.h, hour == 5))
fit6 <- lm(price ~ wind + temp, data = subset(df.h, hour == 6))
fits <- list(fit1, fit2, fit3, fit4, fit5, fit6)
texreg::screenreg(fits)
#> =================================================================================
#> Model 1 Model 2 Model 3 Model 4 Model 5 Model 6
#> ---------------------------------------------------------------------------------
#> (Intercept) 78.23 *** 69.73 ** 60.96 *** 57.87 ** 89.18 *** 64.29 ***
#> (17.62) (18.94) (14.11) (19.14) (19.08) (15.62)
#> wind 0.00 -0.01 -0.01 -0.01 -0.01 0.00
#> (0.01) (0.01) (0.01) (0.01) (0.01) (0.01)
#> temp -1.27 -0.00 0.60 1.39 -0.48 -2.17 *
#> (0.83) (0.99) (0.70) (0.94) (0.98) (0.93)
#> ---------------------------------------------------------------------------------
#> R^2 0.11 0.08 0.05 0.11 0.06 0.23
#> Adj. R^2 0.02 -0.02 -0.05 0.01 -0.05 0.15
#> Num. obs. 21 21 21 21 21 21
#> RMSE 35.24 41.60 32.59 41.44 39.87 38.39
#> =================================================================================
#> *** p < 0.001, ** p < 0.01, * p < 0.05#>