plm 或 lme4 用于面板数据的随机和固定效应模型

2024-01-07

我可以使用以下命令在面板数据上指定随机效应模型和固定效应模型吗lme4 /questions/tagged/lme4?

我正在重做来自 Wooldridge (2013, p. 494-5) 的示例 14.4r /questions/tagged/r。谢谢这个网站 http://www.urfie.net/downloads14.html and 这篇博文 https://econometricswithr.wordpress.com/wooldridge-2013/chapter-14/我已经成功做到了plm /questions/tagged/plm包,但我很好奇我是否可以在lme4 /questions/tagged/lme4包裹?

这是我在plm /questions/tagged/plm包裹。如果有任何关于我如何使用同样的方法的指示,我将不胜感激lme4 /questions/tagged/lme4。首先,需要的包和数据的加载,

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

其次,我使用示例 14.4 (Wooldridge 2013) 中估计的三个模型来估计plm /questions/tagged/plm包裹,

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")

第三,我使用输出结果观星者 /questions/tagged/stargazer模拟 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

有没有同样简单的方法来做到这一点lme4 /questions/tagged/lme4?我是否应该坚持plm /questions/tagged/plm?为什么/为什么不?


除了估计方法的不同之外,似乎确实主要是一个问题 词汇和语法的

# 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 sense).
这就是 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 sense).
这就是 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) 使用回归和多级/分层模型进行数据分析。剑桥大学出版社 (一本非常非常好的书!)

创建于 2018-03-08 由代表包 http://reprex.tidyverse.org(v0.2.0)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

plm 或 lme4 用于面板数据的随机和固定效应模型 的相关文章

随机推荐