如何将glmer()调用转换为lme();并包括随机效果的list()

时间:2017-02-20 07:43:58

标签: r lme4 mixed-models nlme

我以前在包lme4中使用glmer()运行混合模型分析。我现在想在包nlme中使用lme()运行相同的分析。这是因为随后使用的函数需要输出或调用lme()混合模型。

随后使用的函数尝试使用segmented.lme()函数在数据中查找断点。可以在此处找到此函数的代码:https://www.researchgate.net/publication/292986444_segmented_mixed_models_in_R_code_and_data

以前,我使用过函数:

global.model <- glmer(response ~ predictor1*predictor2*predictor3*predictor4 + covariate1 + covariate2 + covariate3 + (1|block/transect), data=dat, family="gaussian", na.action="na.fail")

有关可重复的示例,请参阅下文。

请注意:随机效应是:(1 | block / transect),即考虑块内的块和横断面之间的交互作用。

现在,我不知道如何重写lme()的随机效果部分以完全匹配glmer()中使用的代码,特别是因为segmented.lme()似乎需要'list'。我尝试过以下方法:

random = list(block = pdDiag(~ 1 + predictor1))

请注意:我对预测器1的数据中的潜在断点感兴趣。

必需的包:lme4,nlme

参考工作文件可在此处获取:https://www.researchgate.net/publication/292629179_Segmented_mixed_models_with_random_changepoints_in_R

这是数据的子集:

structure(list(block = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"), class = "factor"), transect = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("B1L", 
"B1M", "B1S", "B2L", "B2M", "B2S", "B3L", "B3M", "B3S", "B4L", 
"B4M", "B4S", "B5L", "B5M", "B5S", "B6L", "B6M", "B6S", "B7L", 
"B7M", "B7S", "B8L", "B8M", "B8S"), class = "factor"), predictor1 = c(28.63734661, 
31.70995133, 27.40407982, 25.48842992, 21.81094637, 24.02032756
), predictor2 = c(5.002945364, 6.85567854, 0, 22.470422, 
0, 0), predictor3 = c(3.72, 3.55, 3.66, 3.65, 3.53, 3.66), 
predictor4 = c(504.8, 547.6, 499.7, 497.8, 473.8, 467.5), 
covariate1 = c(391L, 394L, 351L, 336L, 304L, 335L), covariate2 = c(0.96671086, 
2.81939707, 0.899512367, 1.024730094, 1.641161861, 1.419433714
), covariate3 = c(0.787505444, 0.641693911, 0.115804751, 
-0.041146951, 1.983567486, -0.451039179), response = c(0.81257636, 
0.622662116, 0.490330786, 0.709929461, -0.156398286, -1.185175095
)), .Names = c("block", "transect", "predictor1", "predictor2", "predictor3", "predictor4", "covariate1", "covariate2", "covariate3", "response"), row.names = c(NA, 6L), class = "data.frame")

非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

我不熟悉segmented.lme,但如果它的功能与nlme相同(问题的开头似乎建议),那么您可以按如下方式指定随机效果。

我使用了一些自己的数据作为示例,因为您的数据集不包含足够的信息来估计模型。您应该能够为自己的数据集推导出所需的模型。

library(lme4)
    global.model <- lmer(Schaalscore ~ Leeftijd + (1|SCHOOL/LeerlingID),data = Data_RW5, na.action = "na.exclude")
    summary(global.model)

library(nlme)
global.model2 <- lme(Schaalscore ~ Leeftijd, random= list(SCHOOL = ~1, LeerlingID = ~ 1) ,data = Data_RW5, na.action = "na.exclude")
summary(global.model2)

您的模型表示块和横断面上的随机截距,其中横断面嵌套在块中。我的数据具有相同的结构,但LeerlingID嵌套在SCHOOL中。我使用lmer而不是glmer(因为警告消息将显示:calling glmer() with family=gaussian (identity link) as a shortcut to lmer() is deprecated; please call lmer() directly)。但是lmer和glmer的想法是一样的。输出如下:

> summary(global.model)
Linear mixed model fit by REML ['lmerMod']
Formula: Schaalscore ~ Leeftijd + (1 | SCHOOL/LeerlingID)
   Data: Data_RW5

REML criterion at convergence: 58562.1

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.2088 -0.5855 -0.0420  0.5380  4.6893 

Random effects:
 Groups            Name        Variance Std.Dev.
 LeerlingID:SCHOOL (Intercept) 213.46   14.610  
 SCHOOL            (Intercept)  28.39    5.328  
 Residual                       62.35    7.896  
Number of obs: 7798, groups:  LeerlingID:SCHOOL, 1384; SCHOOL, 59

Fixed effects:
            Estimate Std. Error t value
(Intercept) -89.0261     1.2116  -73.48
Leeftijd     18.3646     0.1081  169.86

Correlation of Fixed Effects:
         (Intr)
Leeftijd -0.725




> summary(global.model2)
Linear mixed-effects model fit by REML
 Data: Data_RW5 
       AIC      BIC    logLik
  58572.08 58606.89 -29281.04

Random effects:
 Formula: ~1 | SCHOOL
        (Intercept)
StdDev:    5.327848

 Formula: ~1 | LeerlingID %in% SCHOOL
        (Intercept) Residual
StdDev:    14.61033  7.89634

Fixed effects: Schaalscore ~ Leeftijd 
                Value Std.Error   DF   t-value p-value
(Intercept) -89.02613 1.2116148 6413 -73.47726       0
Leeftijd     18.36460 0.1081172 6413 169.85827       0
 Correlation: 
         (Intr)
Leeftijd -0.725

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.2087839 -0.5855190 -0.0420062  0.5379625  4.6892515 

Number of Observations: 7798
Number of Groups: 
                SCHOOL LeerlingID %in% SCHOOL 
                    59                   1384 

您可以看到随机和固定效果估计值相同且收敛时的REML标准为&#39;等于-2 * logLik。总之,您可以将随机结构指定为random= list(block= ~1, transect= ~ 1)以获得相同的模型。

edit:pdDiag是标准pdMat类的一部分,用于指定随机效果的方差 - 协方差矩阵。您的原始模型仅指定两个级别的随机拦截,因此pdDiag不执行任何操作。如果指定随机斜率和随机截距,则pdDiag将斜率 - 截距相关设置为0.参见Bates&amp; Pinheiro(2000)了解详情。