stargazer를 사용하여 plm FE 회귀에서 전체 R2를 얻는 방법은 무엇입니까? (How to get between and overall R2 from plm FE regression with stargazer?)


문제 설명

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)

(by BeSeLuFripaqmo)

참조 문서

  1. How to get between and overall R2 from plm FE regression with stargazer? (CC BY‑SA 2.5/3.0/4.0)

#panel #R #regression #stargazer #plm






관련 질문

R을 사용하여 패널 데이터의 마지막 이벤트 이후 실행 계산 (Using R to count a run since last event in panel data)

버튼을 클릭한 후 한 패널을 표시하고 동일한 프레임에서 두 번째 버튼을 클릭하면 다른 패널을 표시합니다. (show one panel after button is clicked and other panel when second button is clicked in the same frame)

외부를 클릭하면 패널을 닫아야 함(초점 상실) (Need To Close A Panel When Clicked Outside (Lost Focus))

asp.net gridview - updatepanel을 사용하여 행당 여러 줄에 바인딩된 데이터 분산 (asp.net gridview - spread bound data across multi lines per row with updatepanel)

문자를 포함하는 고유 식별자와 함께 xtreg 사용 (Using xtreg with unique identifier that includes letters)

matlab SVM은 NaN을 반환합니다. (matlab SVM returns NaN)

특정 형식의 JAVA GUI 제작에 대한 질문 (Question about making a JAVA GUI of a certain format)

슬라이딩 패널 구분선을 사용하여 HTML 페이지를 다른 섹션으로 분할하시겠습니까? 어떻게 이루어지나요? (Splitting HTML page into different sections using sliding panel dividers? How is it done?)

Swift Mac Os 응용 프로그램 - NSSavePanel이 '백그라운드 전용' 응용 프로그램에서 올바르게 작동하지 않습니다. (Swift Mac Os Application - NSSavePanel does not behave correctly with a 'background only' application)

Python 저주 update_panels() 및 Panel.move() 문제 (Python Curses update_panels() and Panel.move() issues)

stargazer를 사용하여 plm FE 회귀에서 전체 R2를 얻는 방법은 무엇입니까? (How to get between and overall R2 from plm FE regression with stargazer?)

Java, 패널용 그래픽 개체 생성 및 편집? (Java, create and edit a Graphics Object for Panel?)







코멘트