I would like to get the R-squared within for a fixed effect regression with multiple fixed effects (let's say Country, Year, Trimester).
The least squared dummy variable (LSDV) model (lm
in R/ reg
in Stata) would only provide the overall R-squared.
The same is true if one uses areg
(Stata).
Code/packages suggestions for R or Stata are both welcome.
问问题
613 次
1 回答
2
考虑这个虚拟数据,但这次我们确切地知道我们想要估计的系数。
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
如果你愿意,我会让你用包来探索。
于 2020-04-08T17:07:18.743 回答