inspired by SO this answer I'm using teh do
dplyr to perform several regressions at once, I would however like to display my output using texreg and do()
produces a rowwise_df
object, but if I extract the list of regression some information seem to have been lost. Is there a simply way to solve this? The minimal example below.
First some required packages
# install.packages(c("tidyverse", "broom", "texreg"), dependencies = TRUE)
library(tidyverse)
Second, some dummy data
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)
)
Third the do()
dfHour = df.h %>% group_by(hour) %>%
do(fitHour = lm(price ~ wind + temp, data = .))
Forth, get the coefficients by group in a tidy 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
I would however like to use texreg, I've tried something like this, but the output gets scrambled up somehow. Any help would be appreciated.
library(texreg)
class(dfHour[[2]])
#> [1] "list"
screenreg(dfHour[[2]]) # Not working
doing it manually would look something like this,
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#>