简体   繁体   English

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

[英]plm or lme4 for Random and Fixed Effects model on Panel Data

Can I specify a Random and a Fixed Effects model on Panel Data using ? 我可以使用在Panel Data上指定Random和Fixed Effects模型吗?

I am redoing Example 14.4 from Wooldridge (2013, p. 494-5) in . 我从伍尔德里奇(2013,第494-5)在实施例重做14.4 Thanks to this site and this blog post I've manged to do it in the package, but I'm curious if I can do the same in the package? 感谢这个网站这个博客文章,我已经在包中做了它,但我很好奇我是否可以在包中做同样的

Here's what I've done in the package. 这是我在包中所做的。 Would be grateful for any pointers as to how I can do the same using . 非常感谢有关如何使用进行相同操作的任何指示。 First, packages needed and loading of data, 首先,需要包和加载数据,

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

Second, I estimate the three models estimated in Example 14.4 (Wooldridge 2013) using the package, 其次,我估计使用包在例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")

Third, I output the resultants using to emulate Table 14.2 in Wooldridge (2013), 第三,我使用输出结果来模拟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

is there an equally simple way to do this in ? 有一个同样简单的方法吗? Should I stick to ? 我应该坚持 Why/Why not? 为什么/为什么不呢?

Excepted for the difference in estimation method it seems indeed to be mainly a question of vocabulary and syntax 由于估算方法的差异,它似乎确实主要是词汇和语法问题

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

Your first example is a simple linear model ignoring the groups nr . 你的第一个例子是一个简单的线性模型,忽略了组nr
You can't do that with lme4 because there is no "random effect" (in the lme4 sense). 你不能用lme4做到这一点,因为没有“随机效应”(在lme4意义上)。
This is what Gelman & Hill call a complete pooling approach. 这就是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)

Your second example seems to be equivalent to a random intercept mixed model with nr as random effect (but the slopes of all predictors are fixed). 你的第二个例子似乎相当于随机截距混合模型,其中nr为随机效应(但所有预测变量的斜率都是固定的)。
This is what Gelman & Hill call a partial pooling approach. 这就是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) 

Your third example seems to correspond to a case were nr is a fixed effect and you compute a different nr intercept for each group. 你的第三个例子似乎对应于一个案例,因为nr是一个固定的效果,你为每个组计算一个不同的nr截距。
Again : you can't do that with lme4 because there is no "random effect" (in the lme4 sense). 再说一遍:你不能用lme4做到这一点,因为没有“随机效应”(在lme4意义上)。
This is what Gelman & Hill call a "no pooling" approach. 这就是格尔曼和希尔称之为“不合并”的方法。

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)

Compare the results : 比较结果:

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) Data analysis using regression and multilevel/hierarchical models. Gelman A,Hill J(2007)使用回归和多级/分层模型的数据分析。 Cambridge University Press (a very very good book !) 剑桥大学出版社(一本非常好的书!)

Created on 2018-03-08 by the reprex package (v0.2.0). reprex包 (v0.2.0)于2018-03-08创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 lme4 对具有 2 个索引变量的面板数据进行混合效应模型? - Mixed effects model on panel data with 2 indexing variables using lme4? 具有固定和随机效应的库(lme4)中的MLM设计矩阵 - Design matrix for MLM from library(lme4) with fixed and random effects 在lme4中指定随机效果 - specify random effects in lme4 nlme和lme4中的不同随机效应 - different random effects in nlme and lme4 如何使用 lme4 将没有随机效应的模型与具有随机效应的模型进行比较? - How to compare a model with no random effects to a model with a random effect using lme4? 在 R 中指定逻辑混合 model 中重复测量的随机效应:lme4::glmer - Specifying random effects for repeated measures in logistic mixed model in R: lme4::glmer 从 lmer 对象中提取随机效应的原始模型矩阵 (lme4, R) - Extract raw model matrix of random effects from lmer objects (lme4, R) 如何在lme中建模固定效果? - How to model fixed effects in lme? 使用来自混合效应模型 (lme4) 和模型平均 (MuMIn) 的二项式数据绘制逻辑回归的结果 - Plotting results of logistic regression with binomial data from mixed effects model (lme4) with model averaging (MuMIn) plm 固定效应错误 - R 中的空模型 - 不平衡数据 - plm fixed effects error - empty model in R - unbalanced data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM