문제 설명
stargazer를 사용하여 plm FE 회귀에서 전체 R2를 얻는 방법은 무엇입니까? (How to get between and overall R2 from plm FE regression with stargazer?)
면책 조항: 이 질문은 내가 이틀 전에 질문한 이 질문과 매우 관련이 있지만 지금은 구현과 관련이 있습니다. 이전과 같이 summary()
에 없는 stargazer()
출력의 전체 R2 사이.
plm()
이 R2와 전체 R2 사이를 계산하고 stargazer()
출력에 포함시키는 방법이 있습니까?
R2 사이, 전체 및 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()
출력에 전체 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
참조 솔루션
방법 1:
To do this in stargazer
, you can use the add.lines()
argument. However, this adds the lines to the beginning of the summary stats section and there is no way to alter this without messing with the source code, which is beastly. I much prefer huxtable
, which provides a grammar of table building and is much more extensible and customizable.
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
Created on 2020‑04‑08 by the reprex package (v0.3.0)