首页 > 解决方案 > lme4 中 lme4 随机效应预测的标准误差

问题描述

我对具有不同生长习性 ( growth_type)、嵌套在生长类型中的基因型 ( ge) 以及也嵌套在生长类型中的块 ( block) 进行了实验。目的是测试植物性能的生长类型和基因型的影响。这是一个示例数据和可重现的示例。

data1 <- read.csv(text = "
growth_type,ge,block,performance
dwarf,A,1,128.32
dwarf,A,2,97.01
dwarf,A,3,91.05
dwarf,B,1,108.51
dwarf,B,2,121.11
dwarf,B,3,84.15
dwarf,C,1,132.55
dwarf,C,2,129.45
dwarf,C,3,122.33
tall,D,1,79.68
tall,D,2,122.5
tall,D,3,143.42
tall,E,1,149.29
tall,E,2,162.13
tall,E,3,135.42
tall,F,1,90.45
tall,F,2,127.4
tall,F,3,78.99")

这些是我使用的库:

library(dplyr)
library(lme4)
library(lsmeans)

第一步是拟合模型:

model.fit <- 
  lmer(performance ~ growth_type + (1 | block:growth_type) + (1 | ge:growth_type),         
       data = data1)

从这个模型中,我可以使用以下方法提取增长类型的固定效应lsmeans

fixed.effect.estimates <- lsmeans::lsmeans(model.fit, "growth_type")

这是输出:

在此处输入图像描述

我需要获得的是随机效应的相同输出。我能够获得预测区间,但无法获得标准误差。这是我尝试过的:

# RANDOM EFFECT ESTIMATES
data1$pred.performance <- 
  predict(model.fit, 
          newdata = data1,
          re.form= ~(1 | ge:growth_type)) 

pred.ge <- data1 %>%
  distinct(ge, growth_type, pred.performance)

这就是我得到的。到目前为止,一切都很好。

在此处输入图像描述

然后我使用该bootMer函数通过引导建立预测区间。

mySumm <- function(.) {
  predict(., newdata=pred.ge, re.form= ~(1 | ge:growth_type))
}

####Collapse bootstrap into median, 95% PI
sumBoot <- function(merBoot) {
  return(
    data.frame(fit = apply(merBoot$t, 2, function(x) as.numeric(quantile(x, probs=.5, na.rm=TRUE))),
               lwr = apply(merBoot$t, 2, function(x) as.numeric(quantile(x, probs=.025, na.rm=TRUE))),
               upr = apply(merBoot$t, 2, function(x) as.numeric(quantile(x, probs=.975, na.rm=TRUE)))
        )
  )
}

##lme4::bootMer() method 1
PI.boot1.time <- system.time(
  boot1 <- lme4::bootMer(model.fit, mySumm, nsim=250, use.u=TRUE, type="parametric")
)

PI.boot1 <- sumBoot(boot1)

cbind(pred.ge, PI.boot1)

这是我得到的:

在此处输入图像描述

总之,我的问题是:

  1. 我怎样才能像我对固定效果组件所做的那样获得标准错误?
  2. 为什么随机效应估计与lme4::predict不同lme4::bootMer

抱歉,解释太长了。

标签: rlme4mixed-models

解决方案


推荐阅读