本文介绍了使用`plm()`估计具有嵌套结构的重复测量随机效应模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用plm()估计具有嵌套结构的重复措施随机效应模型 title =显示标记为'plm'的问题"" rel ="tag"> plm 包?

Is it possible to estimate a repeated measures random effects model with a nested structure using plm() from the plm package?

我知道软件包.但是,lmer()依赖于似然性框架,我很好奇如何使用plm()来做到这一点.

I know it is possible with lmer() from the lme4 package. However, lmer() rely on a likelihood framework and I am curious to do it with plm().

这是我的最小工作示例,它受此问题的启发.首先是一些必需的软件包和数据,

Here's my minimal working example, inspired by this question. First some required packages and data,

# install.packages(c("plm", "lme4", "texreg", "mlmRev"), dependencies = TRUE)
data(egsingle, package = "mlmRev")

数据集egsingle是一个不平衡的面板,由五个时间点上的6021所学校的1721名学童组成.有关详细信息,请参见?mlmRev::egsingle

the data-set egsingle is a unbalanced panel consisting of 1721 school children, grouped in 60 schools, across five time points. For details see ?mlmRev::egsingle

一些灯光数据管理

dta <- egsingle
dta$Female <- with(dta, ifelse(female == 'Female', 1, 0))

也是相关数据的一小段

dta[118:127,c('schoolid','childid','math','year','size','Female')]
#>     schoolid   childid   math year size Female
#> 118     2040 289970511 -1.830 -1.5  502      1
#> 119     2040 289970511 -1.185 -0.5  502      1
#> 120     2040 289970511  0.852  0.5  502      1
#> 121     2040 289970511  0.573  1.5  502      1
#> 122     2040 289970511  1.736  2.5  502      1
#> 123     2040 292772811 -3.144 -1.5  502      0
#> 124     2040 292772811 -2.097 -0.5  502      0
#> 125     2040 292772811 -0.316  0.5  502      0
#> 126     2040 293550291 -2.097 -1.5  502      0
#> 127     2040 293550291 -1.314 -0.5  502      0

现在,在很大程度上依靠 Robert Long的答案,这就是我如何随机估计重复措施的方法使用来自lmer()的嵌套结构的效果模型 > lme4 软件包,

Now, relying heavily on Robert Long's answer, this is how I estimate a repeated measures random effects model with a nested structure using lmer() from the lme4 package,

dta$year <- as.factor(dta$year)
require(lme4)
Model.1 <- lmer(math ~ Female + size + year + (1 | schoolid /childid), dta)
# summary(Model.1)

我在 man 页面中查找了plm(),它有一个索引命令index,但是只需要一个索引和时间,即index = c("childid", "year"),而忽略schoolid模型将如下所示,

I looked in man page for plm() and it has an indexing command, index, but it only takes a single index and time, i.e., index = c("childid", "year"), ignoring the schoolid the model would look like this,

dta$year <- as.numeric(dta$year)
library(plm)
Model.2 <- plm(math~Female+size+year, dta, index = c("childid", "year"), model="random")
# summary(Model.2)

总结问题

下面是两个模型的实际估算结果,

Below is the actual estimation results form the two models,

# require(texreg)
names(Model.2$coefficients) <- names(coefficients(Model.1)$schoolid) #ugly!
texreg::screenreg(list(Model.1, Model.2), digits = 3)  # pretty!
#> ==============================================================
#>                                    Model 1        Model 2
#> --------------------------------------------------------------
#> (Intercept)                           -2.693 ***    -2.671 ***
#>                                       (0.152)       (0.085)
#> Female                                 0.008        -0.025
#>                                       (0.042)       (0.046)
#> size                                  -0.000        -0.000 ***
#>                                       (0.000)       (0.000)
#> year-1.5                               0.866 ***     0.878 ***
#>                                       (0.059)       (0.059)
#> year-0.5                               1.870 ***     1.882 ***
#>                                       (0.058)       (0.059)
#> year0.5                                2.562 ***     2.575 ***
#>                                       (0.059)       (0.059)
#> year1.5                                3.133 ***     3.149 ***
#>                                       (0.059)       (0.060)
#> year2.5                                3.939 ***     3.956 ***
#>                                       (0.060)       (0.060)
#> --------------------------------------------------------------
#> AIC                                16590.715
#> BIC                                16666.461
#> Log Likelihood                     -8284.357
#> Num. obs.                           7230          7230
#> Num. groups: childid:schoolid       1721
#> Num. groups: schoolid                 60
#> Var: childid:schoolid (Intercept)      0.672
#> Var: schoolid (Intercept)              0.180
#> Var: Residual                          0.334
#> R^2                                                  0.004
#> Adj. R^2                                             0.003
#> ==============================================================
#> *** p < 0.001, ** p < 0.01, * p < 0.05

推荐答案

基于我为具有嵌套结构的重复测量随机效应模型编写了以下模型规范,在使用Wallace和Hussain的( 1969 )方法,即random.method = "walhus",用于估计方差分量,

Based on Helix123's comment I wrote the following model specification for a repeated measures random effects model with a nested structure, in plm() from the plm package using Wallace and Hussain's (1969) method, i.e. random.method = "walhus", for estimation of the variance components,

p_dta <- pdata.frame(dta, index = c("childid", "year", "schoolid"))
Model.3 <- plm(math ~ Female + size + year, data = p_dta, model = "random",
               effect = "nested", random.method = "walhus")

在下面的Model.3中看到的结果与Model.1中的估计值几乎一样,正如我所期望的那样.只有截距略有不同(请参见下面的输出).

The results, seen in Model.3 below, is as close to identical, to the estimates in Model.1, as I could expect. Only the intercept is slightly different (see output below).

对此有任何权威的参考文献.我将继续努力.

names(Model.3$coefficients) <- names(coefficients(Model.1)$schoolid)
texreg::screenreg(list(Model.1, Model.3), digits = 3,
                  custom.model.names = c('Model 1', 'Model 3'))
#> ==============================================================
#>                                    Model 1        Model 3
#> --------------------------------------------------------------
#> (Intercept)                           -2.693 ***    -2.697 ***
#>                                       (0.152)       (0.152)
#> Female                                 0.008         0.008
#>                                       (0.042)       (0.042)
#> size                                  -0.000        -0.000
#>                                       (0.000)       (0.000)
#> year-1.5                               0.866 ***     0.866 ***
#>                                       (0.059)       (0.059)
#> year-0.5                               1.870 ***     1.870 ***
#>                                       (0.058)       (0.058)
#> year0.5                                2.562 ***     2.562 ***
#>                                       (0.059)       (0.059)
#> year1.5                                3.133 ***     3.133 ***
#>                                       (0.059)       (0.059)
#> year2.5                                3.939 ***     3.939 ***
#>                                       (0.060)       (0.060)
#> --------------------------------------------------------------
#> AIC                                16590.715
#> BIC                                16666.461
#> Log Likelihood                     -8284.357
#> Num. obs.                           7230          7230
#> Num. groups: childid:schoolid       1721
#> Num. groups: schoolid                 60
#> Var: childid:schoolid (Intercept)      0.672
#> Var: schoolid (Intercept)              0.180
#> Var: Residual                          0.334
#> R^2                                                  0.000
#> Adj. R^2                                            -0.001
#> ==============================================================
#> *** p < 0.001, ** p < 0.01, * p < 0.05#>

这篇关于使用`plm()`估计具有嵌套结构的重复测量随机效应模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:24