如何解决具有正负显着项数的横截面回归表输出
我有一个时间序列面板数据集,其结构如下:
df <- data.frame(
year = c(2012L,2013L,2014L,2012L,2015L),id = c(1L,1L,2L,2L),col1 = c(11L,13L,16L,15L,16L),col2 = c(10L,14L,12L,11L,17L),col3 = c(17L,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)
在我最终想要的输出中,我希望获得每个系数的平均值,并且我还希望有一列用于显示正值和显着性观察值的数量以及每个回归系数的负值和显着性观察值的数量。类似于下图中的表格: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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。