PLM或LME4适用于面板数据的随机效应和固定效应模型。

14
我能否使用在面板数据上指定随机效应和固定效应模型?
我正在使用重新执行Wooldridge(2013年,第494-5页)的示例14.4。感谢this sitethis blog post,我已经成功在包中完成了它,但我想知道是否可以在包中完成相同的操作?

这是我在包中完成的工作。非常感谢任何关于如何使用完成相同工作的指导。首先,需要加载包和数据,

# install.packages(c("wooldridge", "plm", "stargazer"), dependencies = TRUE)
library(wooldridge) 
data(wagepan)

第二步,我使用 包估计了在例子14.4(Wooldridge 2013)中估计的三个模型。
library(plm) 
Pooled.ols <- plm(lwage ~ educ + black + hisp + exper+I(exper^2)+ married + union +
                  factor(year), data = wagepan, index=c("nr","year") , model="pooling")

random.effects <- plm(lwage ~ educ + black + hisp + exper + I(exper^2) + married + union +
                      factor(year), data = wagepan, index = c("nr","year") , model = "random") 

fixed.effects <- plm(lwage ~ I(exper^2) + married + union + factor(year), 
                     data = wagepan, index = c("nr","year"), model="within")

第三步,我使用输出结果,以模拟Wooldridge(2013)中的表14.2。

stargazer::stargazer(Pooled.ols,random.effects,fixed.effects, type="text",
           column.labels=c("OLS (pooled)","Random Effects","Fixed Effects"), 
          dep.var.labels = c("log(wage)"), keep.stat=c("n"),
          keep=c("edu","bla","his","exp","marr","union"), align = TRUE, digits = 4)
#> ======================================================
#>                         Dependent variable:           
#>              -----------------------------------------
#>                              log(wage)                
#>              OLS (pooled) Random Effects Fixed Effects
#>                  (1)           (2)            (3)     
#> ------------------------------------------------------
#> educ          0.0913***     0.0919***                 
#>                (0.0052)      (0.0107)                 
#>                                                       
#> black         -0.1392***    -0.1394***                
#>                (0.0236)      (0.0477)                 
#>                                                       
#> hisp            0.0160        0.0217                  
#>                (0.0208)      (0.0426)                 
#>                                                       
#> exper         0.0672***     0.1058***                 
#>                (0.0137)      (0.0154)                 
#>                                                       
#> I(exper2)     -0.0024***    -0.0047***    -0.0052***  
#>                (0.0008)      (0.0007)      (0.0007)   
#>                                                       
#> married       0.1083***     0.0640***      0.0467**   
#>                (0.0157)      (0.0168)      (0.0183)   
#>                                                       
#> union         0.1825***     0.1061***      0.0800***  
#>                (0.0172)      (0.0179)      (0.0193)   
#>                                                       
#> ------------------------------------------------------
#> Observations    4,360         4,360          4,360    
#> ======================================================
#> Note:                      *p<0.1; **p<0.05; ***p<0.01

中是否有同样简单的方法?我应该坚持使用吗?为什么/为什么不?


1
这个问题是否更适合[stats.se]? - Jaap
@Jaap,感谢您的评论。我认为这是一个主要针对程序员的问题,而不是统计/交叉验证问题。但如果您认为它属于CV,我很乐意将其移动。 - Eric Fail
1
请注意,lme4 是最大似然框架,因此不会是“相同的”:plm 的vignette第7章与pkg nlme 进行了一些比较,这类似于 lme4,您应该能够从中获取信息。 - Helix123
@Helix123,谢谢您的评论。我会查看它。 - Eric Fail
1个回答

25
除了估算方法的差异外,这似乎主要是词汇和语法问题。
# install.packages(c("wooldridge", "plm", "stargazer", "lme4"), dependencies = TRUE)
library(wooldridge) 
library(plm) 
#> Le chargement a nécessité le package : Formula
library(lme4)
#> Le chargement a nécessité le package : Matrix
data(wagepan)

你的第一个例子是一个简单的线性模型,忽略了组别nr
使用lme4无法实现这一点,因为没有“随机效应”(在lme4意义上)。
这就是Gelman和Hill所称的完全汇集方法。

Pooled.ols <- plm(lwage ~ educ + black + hisp + exper+I(exper^2)+ married + 
                      union + factor(year), data = wagepan, 
                  index=c("nr","year"), model="pooling")

Pooled.ols.lm <- lm(lwage ~ educ + black + hisp + exper+I(exper^2)+ married + union +
                      factor(year), data = wagepan)

你的第二个例子似乎相当于一个随机截距混合模型,其中nr作为随机效应(但所有预测变量的斜率都是固定的)。
这就是Gelman和Hill所说的部分汇集方法。

random.effects <- plm(lwage ~ educ + black + hisp + exper + I(exper^2) + married + 
                          union + factor(year), data = wagepan, 
                      index = c("nr","year") , model = "random") 

random.effects.lme4 <- lmer(lwage ~ educ + black + hisp + exper + I(exper^2) + married + 
                                union + factor(year) + (1|nr), data = wagepan) 

你的第三个例子似乎对应于nr为固定效应的情况,并且你为每个组计算了不同的nr截距。
同样:你不能使用lme4来实现这一点,因为在lme4中没有“随机效应”。
这就是Gelman和Hill所说的“无汇集”方法。

fixed.effects <- plm(lwage ~ I(exper^2) + married + union + factor(year), 
                     data = wagepan, index = c("nr","year"), model="within")

wagepan$nr <- factor(wagepan$nr)
fixed.effects.lm <- lm(lwage ~  I(exper^2) + married + union + factor(year) + nr, 
                     data = wagepan)

比较结果:

stargazer::stargazer(Pooled.ols, Pooled.ols.lm, 
                     random.effects, random.effects.lme4 , 
                     fixed.effects, fixed.effects.lm,
                     type="text",
                     column.labels=c("OLS (pooled)", "lm no pool.",
                                     "Random Effects", "lme4 partial pool.", 
                                     "Fixed Effects", "lm compl. pool."), 
                     dep.var.labels = c("log(wage)"), 
                     keep.stat=c("n"),
                     keep=c("edu","bla","his","exp","marr","union"), 
                     align = TRUE, digits = 4)
#> 
#> =====================================================================================================
#>                                                Dependent variable:                                   
#>              ----------------------------------------------------------------------------------------
#>                                                     log(wage)                                        
#>                 panel         OLS         panel            linear           panel           OLS      
#>                 linear                    linear       mixed-effects       linear                    
#>              OLS (pooled) lm no pool. Random Effects lme4 partial pool. Fixed Effects lm compl. pool.
#>                  (1)          (2)          (3)              (4)              (5)            (6)      
#> -----------------------------------------------------------------------------------------------------
#> educ          0.0913***    0.0913***    0.0919***        0.0919***                                   
#>                (0.0052)    (0.0052)      (0.0107)         (0.0108)                                   
#>                                                                                                      
#> black         -0.1392***  -0.1392***    -0.1394***       -0.1394***                                  
#>                (0.0236)    (0.0236)      (0.0477)         (0.0485)                                   
#>                                                                                                      
#> hisp            0.0160      0.0160        0.0217           0.0218                                    
#>                (0.0208)    (0.0208)      (0.0426)         (0.0433)                                   
#>                                                                                                      
#> exper         0.0672***    0.0672***    0.1058***        0.1060***                                   
#>                (0.0137)    (0.0137)      (0.0154)         (0.0155)                                   
#>                                                                                                      
#> I(exper2)     -0.0024***  -0.0024***    -0.0047***       -0.0047***      -0.0052***     -0.0052***   
#>                (0.0008)    (0.0008)      (0.0007)         (0.0007)        (0.0007)       (0.0007)    
#>                                                                                                      
#> married       0.1083***    0.1083***    0.0640***        0.0635***        0.0467**       0.0467**    
#>                (0.0157)    (0.0157)      (0.0168)         (0.0168)        (0.0183)       (0.0183)    
#>                                                                                                      
#> union         0.1825***    0.1825***    0.1061***        0.1053***        0.0800***      0.0800***   
#>                (0.0172)    (0.0172)      (0.0179)         (0.0179)        (0.0193)       (0.0193)    
#>                                                                                                      
#> -----------------------------------------------------------------------------------------------------
#> Observations    4,360        4,360        4,360            4,360            4,360          4,360     
#> =====================================================================================================
#> Note:                                                                     *p<0.1; **p<0.05; ***p<0.01

Gelman A, Hill J (2007) 《回归与多层次/分层模型数据分析》。剑桥大学出版社(一本非常好的书!)

使用reprex package(v0.2.0)于2018-03-08创建。


非常出色的回答。非常感谢。您是否知道Gelman和Hill(2007)是否涵盖了估计方法的差异?再次感谢! - Eric Fail
1
Gelman & Hill涵盖了(受限)最大似然和MCMC / Bayesian方法。但我认为他们没有涵盖“plm”包中讨论的方法。 - Gilles San Martin
很棒的回答。我有一个快速问题,Pooled OLS、RE或FE模型中的任何一个是否严格算作“纵向”分析,还是需要像educ*year一样将IV与时间交互?提前感谢。 - Marco Pastor Mayo
1
应该定义“严格纵向”(对于不同的人来说,其含义可能会有所不同)。在混合模型(部分池化)中,通常会区分随机截距模型和随机斜率模型。在随机斜率模型中,例如 lmer(lwage ~ year + (1+year|nr), data = wagepan),您需要为每个 nr 计算 lwage~year 的不同斜率,然后计算一种加权平均的全局(元参数)斜率。 - Gilles San Martin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接