横截面回归 table 输出具有正负显着项的数量
Cross-sectional regression table output with number of positive and negative significant terms
我有一个时间序列面板数据集,其结构如下:
df <- data.frame(
year = c(2012L, 2013L, 2014L, 2012L, 2013L, 2014L, 2015L),
id = c(1L, 1L, 1L, 2L, 2L, 2L, 2L),
col1 = c(11L, 13L, 13L, 16L, 15L, 15L, 16L),
col2 = c(10L, 14L, 12L, 13L, 11L, 16L, 17L),
col3 = c(17L, 12L, 12L, 14L, 19L, 21L, 12L),
)
> df
year id col1 col2 col3
1 2012 1 11 10 17
2 2013 1 13 14 12
3 2014 1 13 12 12
4 2012 2 16 13 14
5 2013 2 15 11 19
6 2014 2 15 16 21
7 2015 2 16 17 12
>
我想按年份对面板数据进行分组,运行 一列对另一列的横截面回归。这是我目前的代码:
reg =
df %>%
group_by(year) %>%
do({model = lm(col1 ~ col2 + col3, data=.) # create your model
data.frame(tidy(model), # get coefficient info
glance(model))})
reg_results =
reg %>%
select(year, term, estimate, adj.r.squared) %>%
spread(term, estimate)
stargazer(as.data.frame(reg_results), title = "Full regression",
type = "text", nobs = TRUE, mean.sd = TRUE, median = TRUE,
iqr = TRUE)
在我最终期望的输出中,我希望得到每个系数的平均值,并且我还希望有一个列用于每个回归系数的正面和显着观察值的数量以及负面和显着观察值的数量。类似于下图中的table:https://imgur.com/a/GD3gYlp
按 year
分组后,您可以 summarise
lm
的结果作为列表列,然后 unnest
,按 term
分组,并定义您感兴趣的汇总统计数据:
library(tidyverse)
library(broom)
df %>%
group_by(year) %>%
summarise(model = list(lm(col1 ~ col2 + col3, data = .) %>% tidy)) %>%
unnest(model) %>%
filter(term != "(Intercept)") %>%
group_by(term) %>%
summarise(avg_coef = mean(estimate),
n_pos_sig = sum(estimate > 0 & p.value < .05),
n_neg_sig = sum(estimate < 0 & p.value < .05))
输出
# A tibble: 2 x 4
term avg_coef n_pos_sig n_neg_sig
<chr> <dbl> <int> <int>
1 col2 0.464 0 0
2 col3 0.0682 0 0
我有一个时间序列面板数据集,其结构如下:
df <- data.frame(
year = c(2012L, 2013L, 2014L, 2012L, 2013L, 2014L, 2015L),
id = c(1L, 1L, 1L, 2L, 2L, 2L, 2L),
col1 = c(11L, 13L, 13L, 16L, 15L, 15L, 16L),
col2 = c(10L, 14L, 12L, 13L, 11L, 16L, 17L),
col3 = c(17L, 12L, 12L, 14L, 19L, 21L, 12L),
)
> df
year id col1 col2 col3
1 2012 1 11 10 17
2 2013 1 13 14 12
3 2014 1 13 12 12
4 2012 2 16 13 14
5 2013 2 15 11 19
6 2014 2 15 16 21
7 2015 2 16 17 12
>
我想按年份对面板数据进行分组,运行 一列对另一列的横截面回归。这是我目前的代码:
reg =
df %>%
group_by(year) %>%
do({model = lm(col1 ~ col2 + col3, data=.) # create your model
data.frame(tidy(model), # get coefficient info
glance(model))})
reg_results =
reg %>%
select(year, term, estimate, adj.r.squared) %>%
spread(term, estimate)
stargazer(as.data.frame(reg_results), title = "Full regression",
type = "text", nobs = TRUE, mean.sd = TRUE, median = TRUE,
iqr = TRUE)
在我最终期望的输出中,我希望得到每个系数的平均值,并且我还希望有一个列用于每个回归系数的正面和显着观察值的数量以及负面和显着观察值的数量。类似于下图中的table:https://imgur.com/a/GD3gYlp
按 year
分组后,您可以 summarise
lm
的结果作为列表列,然后 unnest
,按 term
分组,并定义您感兴趣的汇总统计数据:
library(tidyverse)
library(broom)
df %>%
group_by(year) %>%
summarise(model = list(lm(col1 ~ col2 + col3, data = .) %>% tidy)) %>%
unnest(model) %>%
filter(term != "(Intercept)") %>%
group_by(term) %>%
summarise(avg_coef = mean(estimate),
n_pos_sig = sum(estimate > 0 & p.value < .05),
n_neg_sig = sum(estimate < 0 & p.value < .05))
输出
# A tibble: 2 x 4
term avg_coef n_pos_sig n_neg_sig
<chr> <dbl> <int> <int>
1 col2 0.464 0 0
2 col3 0.0682 0 0