【问题标题】:R-squared within for a regression with multiple fixed effects [closed]R平方内用于具有多个固定效应的回归[关闭]
【发布时间】:2020-07-21 02:34:27
【问题描述】:

我想获得具有多个固定效应(比如国家、年份、三个月)的固定效应回归的 R 平方。 最小二乘虚拟变量 (LSDV) 模型(R 中的 lm/Stata 中的 reg)仅提供整体 R 平方。 如果使用areg (Stata) 也是如此。 欢迎对 R 或 Stata 提出代码/包建议。

【问题讨论】:

  • 您的问题可能更适合CrossValidated。请考虑发布一些数据以及您尝试解决问题的方法。
  • 你可能需要 R 中的 lfe 和 Stata 中的 reghdfe
  • 你只需要在时间上贬低你的 LSDV 模型。在R 中,plm 包应该会有所帮助。

标签: r regression linear-regression stata goodness-of-fit


【解决方案1】:

考虑这个虚拟数据,但这次我们确切地知道我们想要估计的系数。

library(plm)
library(xtable)
library(texreg)
library(data.table)
set.seed(100)

我们先生成一些有时间和个体固定效应的数据

dt <- data.table(epsilon=rnorm(100),ind=rep(1:5,5),time=rep(1:5,each=5),x=rnorm(100,0,2))

dt[,mu:=6*mean(x)*rnorm(20),ind]

dt[,`:=`(delta=10*mean(x)+rnorm(20)),time]

dt[,y:=5*x+mu+delta+epsilon]


> ## head(dt)
##         epsilon ind time         x         mu    delta           y x..bari. x..bar.t
## 1: -0.247885286   1    1 3.1530482 -34.563268 37.74058 18.69467015 3.686294 3.854510
## 2:  1.234916664   2    1 4.2520514 -30.682143 39.75175 31.56477572 3.508577 3.854510
## 3:  0.117692498   3    1 2.2582500  44.240719 37.24573 92.89539109 3.936578 3.854510
## 4: -0.002265777   4    1 1.9168626 -48.510759 38.83342 -0.09529645 3.455571 3.854510
## 5: -1.424864120   5    1 1.7842555 -11.278647 38.77298 34.99074471 4.282104 3.854510
## 6: -1.441965687   1    2 0.5658582  -2.075256 38.41338 37.72545392 3.686294 3.737549 

估计模型池化 ols

pooled <- lm(y~x,data=dt)

估计模型的个体效应

individual..effect <- lm(y~x+as.factor(ind),data=dt)

估计模型时间和个体效应

individual..time..effect <- lm(y~x+as.factor(ind)+as.factor(time),data=dt)

创建随时间变化的均值和个体均值

dt[,x..bari.:=mean(x),ind]
dt[,x..bar.t:=mean(x),time]

估计内估计量

within..estimator  <-  lm(y~I(x-x..bari.-x..bar.t),data=dt)

把所有东西放在一起

screenreg(list(pooled,individual..effect,individual..time..effect
               ,within..estimator))

## ==========================================================================
##                             Model 1     Model 2     Model 3     Model 4   
## --------------------------------------------------------------------------
## (Intercept)                   0.50        1.29        4.22 ***    0.73    
##                              (0.33)      (0.77)      (0.80)      (0.44)   
## x                             5.14 ***    5.18 ***    4.99 ***            
##                              (0.21)      (0.22)      (0.18)               
## as.factor(ind)2                          -1.23       -1.08                
##                                          (1.09)      (0.86)               
## as.factor(ind)3                          -0.97       -0.88                
##                                          (1.08)      (0.85)               
## as.factor(ind)4                          -0.95       -0.82                
##                                          (1.08)      (0.86)               
## as.factor(ind)5                          -0.80       -0.59                
##                                          (1.10)      (0.87)               
## as.factor(time)2                                     -3.88 ***            
##                                                      (0.85)               
## as.factor(time)3                                     -3.99 ***            
##                                                      (0.85)               
## as.factor(time)4                                     -1.39                
##                                                      (0.85)               
## as.factor(time)5                                     -5.94 ***            
##                                                      (0.85)               
## I(x - x..bari. - x..bar.t)                                        4.99 ***
##                                                                  (0.29)   
## --------------------------------------------------------------------------
## R^2                           0.86        0.86        0.92        0.75    
## Adj. R^2                      0.86        0.85        0.91        0.75    
## Num. obs.                   100         100         100         100       
## RMSE                          3.34        3.38        2.68        4.44    
## ==========================================================================
## *** p < 0.001, ** p < 0.01, * p < 0.05  

如果你愿意,我会让你用 plm 包探索。

【讨论】:

  • 谢谢,这很有启发性。您能否也发布如何使用“plm”包执行此操作?
  • This doc 会比我的任何解释都好。
猜你喜欢
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2015-01-17
  • 2021-12-25
  • 2021-09-30
  • 2015-04-22
相关资源
最近更新 更多