如何使用 stargazer 从 plm FE 回归中获得 R2 和整体 R2?
How to get between and overall R2 from plm FE regression with stargazer?
免责声明:这个问题与我两天前问的 极其相关 - 但现在它涉及 stargazer()
输出中的 between R2 和整体 R2 的实现,而不是 summary()
和以前一样。
有没有办法让 plm()
为我计算 R2 和整体 R2 并将它们包含在 stargazer()
输出中?
要阐明我在 R2 之间、整体和内部的意思,请参阅 StackExchange 上的此答案。
我的理解是plm只在R2内计算。
我是 运行 模型中的双向效应。
library(plm)
library(stargazer)
# Create some random data
set.seed(1)
x=rnorm(100); fe=rep(rnorm(10),each=10); id=rep(1:10,each=10); ti=rep(1:10,10); e=rnorm(100)
y=x+fe+e
data=data.frame(y,x,id,ti)
# Get plm within R2
reg=plm(y~x,model="within",index=c("id","ti"), effect = "twoways", data=data)
stargazer(reg)
我现在还想在 stargazer()
输出中包含 between 和 overall R2。我该怎么做?
为了明确我对 between R2 和整体 R2 的意思:
# Pooled Version (overall R2)
reg1=lm(y~x)
summary(reg1)$r.squared
# Between R2
y.means=tapply(y,id,mean)[id]
x.means=tapply(x,id,mean)[id]
reg2=lm(y.means~x.means)
summary(reg2)$r.squared
要在 stargazer
中执行此操作,您可以使用 add.lines()
参数。 但是,这会将这些行添加到摘要统计部分的开头,并且在不弄乱源代码的情况下无法更改它,这太糟糕了。我更喜欢 huxtable
,它提供了 table 构建的语法,并且更具可扩展性和可定制性。
library(tidyverse)
library(plm)
library(huxtable)
# Create some random data
set.seed(1)
x=rnorm(100); fe=rep(rnorm(10),each=10); id=rep(1:10,each=10); ti=rep(1:10,10); e=rnorm(100)
y=x+fe+e
data=data.frame(y,x,id,ti)
# Get plm within R2
reg=plm(y~x,model="within",index=c("id","ti"), effect = "twoways", data=data)
stargazer(reg, type = "text",
add.lines = list(c("Overall R2", round(r.squared(reg, model = "pooled"), 3)),
c("Between R2", round(r.squared(update(reg, effect = "individual", model = "between")), 3))))
#>
#> ========================================
#> Dependent variable:
#> ---------------------------
#> y
#> ----------------------------------------
#> x 1.128***
#> (0.113)
#>
#> ----------------------------------------
#> Overall R2 0.337
#> Between R2 0.174
#> Observations 100
#> R2 0.554
#> Adjusted R2 0.448
#> F Statistic 99.483*** (df = 1; 80)
#> ========================================
#> Note: *p<0.1; **p<0.05; ***p<0.01
# I prefer huxreg, which is much more customizable!
# Create a data frame of the R2 values
r2s <- tibble(
name = c("Overall R2", "Between R2"),
value = c(r.squared(reg, model = "pooled"),
r.squared(update(reg, effect = "individual", model = "between"))))
tab <- huxreg(reg) %>%
# Add new R2 values
add_rows(hux(r2s), after = 4)
# Rename R2
tab[7, 1] <- "Within R2"
tab %>% huxtable::print_screen()
#> ─────────────────────────────────────────────────
#> (1)
#> ─────────────────────────
#> x 1.128 ***
#> (0.113)
#> ─────────────────────────
#> N 100
#> Overall R2 0.337
#> Between R2 0.174
#> Within R2 0.554
#> ─────────────────────────────────────────────────
#> *** p < 0.001; ** p < 0.01; * p < 0.05.
#>
#> Column names: names, model1
由 reprex package (v0.3.0)
于 2020-04-08 创建
免责声明:这个问题与我两天前问的 stargazer()
输出中的 between R2 和整体 R2 的实现,而不是 summary()
和以前一样。
有没有办法让 plm()
为我计算 R2 和整体 R2 并将它们包含在 stargazer()
输出中?
要阐明我在 R2 之间、整体和内部的意思,请参阅 StackExchange 上的此答案。
我的理解是plm只在R2内计算。 我是 运行 模型中的双向效应。
library(plm)
library(stargazer)
# Create some random data
set.seed(1)
x=rnorm(100); fe=rep(rnorm(10),each=10); id=rep(1:10,each=10); ti=rep(1:10,10); e=rnorm(100)
y=x+fe+e
data=data.frame(y,x,id,ti)
# Get plm within R2
reg=plm(y~x,model="within",index=c("id","ti"), effect = "twoways", data=data)
stargazer(reg)
我现在还想在 stargazer()
输出中包含 between 和 overall R2。我该怎么做?
为了明确我对 between R2 和整体 R2 的意思:
# Pooled Version (overall R2)
reg1=lm(y~x)
summary(reg1)$r.squared
# Between R2
y.means=tapply(y,id,mean)[id]
x.means=tapply(x,id,mean)[id]
reg2=lm(y.means~x.means)
summary(reg2)$r.squared
要在 stargazer
中执行此操作,您可以使用 add.lines()
参数。 但是,这会将这些行添加到摘要统计部分的开头,并且在不弄乱源代码的情况下无法更改它,这太糟糕了。我更喜欢 huxtable
,它提供了 table 构建的语法,并且更具可扩展性和可定制性。
library(tidyverse)
library(plm)
library(huxtable)
# Create some random data
set.seed(1)
x=rnorm(100); fe=rep(rnorm(10),each=10); id=rep(1:10,each=10); ti=rep(1:10,10); e=rnorm(100)
y=x+fe+e
data=data.frame(y,x,id,ti)
# Get plm within R2
reg=plm(y~x,model="within",index=c("id","ti"), effect = "twoways", data=data)
stargazer(reg, type = "text",
add.lines = list(c("Overall R2", round(r.squared(reg, model = "pooled"), 3)),
c("Between R2", round(r.squared(update(reg, effect = "individual", model = "between")), 3))))
#>
#> ========================================
#> Dependent variable:
#> ---------------------------
#> y
#> ----------------------------------------
#> x 1.128***
#> (0.113)
#>
#> ----------------------------------------
#> Overall R2 0.337
#> Between R2 0.174
#> Observations 100
#> R2 0.554
#> Adjusted R2 0.448
#> F Statistic 99.483*** (df = 1; 80)
#> ========================================
#> Note: *p<0.1; **p<0.05; ***p<0.01
# I prefer huxreg, which is much more customizable!
# Create a data frame of the R2 values
r2s <- tibble(
name = c("Overall R2", "Between R2"),
value = c(r.squared(reg, model = "pooled"),
r.squared(update(reg, effect = "individual", model = "between"))))
tab <- huxreg(reg) %>%
# Add new R2 values
add_rows(hux(r2s), after = 4)
# Rename R2
tab[7, 1] <- "Within R2"
tab %>% huxtable::print_screen()
#> ─────────────────────────────────────────────────
#> (1)
#> ─────────────────────────
#> x 1.128 ***
#> (0.113)
#> ─────────────────────────
#> N 100
#> Overall R2 0.337
#> Between R2 0.174
#> Within R2 0.554
#> ─────────────────────────────────────────────────
#> *** p < 0.001; ** p < 0.01; * p < 0.05.
#>
#> Column names: names, model1
由 reprex package (v0.3.0)
于 2020-04-08 创建