【问题标题】:How to estimate residuals of subgroup with lme4 in R如何用 R 中的 lme4 估计子组的残差
【发布时间】:2018-06-05 06:29:14
【问题描述】:

我想用 R 中的 lme4 包重现 Hoffman & Rovine 的工作(实验心理学家的多级模型:基础和说明性示例)中报告的结果。

在他们的第一个例子中,他们比较了老年人和年轻人的反应时间。他们的每个参与者都有许多任务试验。因此,在个人层面,参与者的反应时间受到与他们对试验的操纵相关的各种变量的影响。在第二个层面,参与者的年龄和年龄组会影响参与者的反应时间。

在霍夫曼的模型 2B 中,他们分别估计老年人和年轻人的第一级残差,年轻人和老年人有两个虚拟变量。

霍夫曼方程是 Level1 equation

我想知道如何估计 lme4 包中的两个残差。

霍夫曼的文章和示例数据可以在Hoffman's website.找到

我已经成功地复制了他们的模型 2A 的结果,其中假设年轻人和老年人的方差相同,代码如下。

lmer(lg_rt ~ c_mean + c_sal + (1|Item) + oldage + yrs65 + (1|id), Ex1, REML = F)

【问题讨论】:

  • 这很清楚,但似乎还是有点“给我代码”。您在尝试自己实现这一点方面取得了多大的进展?
  • 我试着用谷歌搜索它并从图书馆阅读了几本关于 R 中混合模型的书。也许我没有使用正确的关键字,我在谷歌结果中找不到解决方案。在书中,他们都只提到了组间差异相等的情况。
  • 我尝试添加年轻组和老组的虚拟变量,但结果与霍夫曼的不一样。摘要(lmer(lg_rt ~ c_mean + c_sal + oldage + yrs65 + (1|Item) +(1|Young/id)+(1|Oold/id), Ex1, REML = F))
  • 不幸的是,我发现目前似乎只有nlme 可以处理异方差问题。但是,在这个模型中有两个交叉随机效应,与lme4 相比,nlme 没有简单的方法来处理交叉随机效应。
  • 有一些方法可以在 lme4 中按组处理异方差性:stackoverflow.com/questions/21409340/… 或者,也许更容易,在较新的 glmmTMB 包中...

标签: r lme4


【解决方案1】:

您可以使用模块化拟合函数处理 lme4 中的异方差。这是一个包含两个组的示例,应该可以扩展到其他类型的异方差。请注意,尽管权重是估计的,但最终拟合中参数的标准误差并未考虑权重的不确定性。这个问题应该可以使用 delta 方法来解决,参见例如https://10.3102/1076998611417628 第 2.3.3 节中的第一个方程。

set.seed(1234)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(lme4)
#> Loading required package: Matrix
#> 
#> Attaching package: 'Matrix'
#> The following objects are masked from 'package:tidyr':
#> 
#>     expand, pack, unpack

n <- 100 # number of level-2 units
m <- 3 # number of repeated observations per unit
sd_b <- .3 # random intercept standard deviation
sd_eps1 <- .1 # residual standard deviation in group 1
sd_eps2 <- .3 # residual standard deviation in group 2

# Simulate data
dat <- tibble(
  # unique ID
  id = seq_len(n),
  # explanatory variable, constant over repetitions
  x = runif(n),
  # random intercept
  b = rnorm(n, sd = sd_b),
  # group membership
  grp = sample(1:2, n, replace = TRUE)
) %>% 
  uncount(3) %>% 
  mutate(
    # residual
    eps = rnorm(nrow(.), sd = c(sd_eps1, sd_eps2)[grp]),
    # response, fixed effect is beta=1
    y = x + b + eps
  )

# now optimize over residual weights, fixing the group 1 weight to 1.
# optimize() would be sufficient, but I show it with optim() because it
# then can be directly extended to a larger number of groups
opt <- optim(
  # initial value for group 2 residual relative to group 1
  par = 1,
  fn = function(weight){
    # Compute weights from group variable
    df <- dat %>% 
      mutate(weight = c(1, weight)[grp])
    ## 1.  Parse the data and formula:
    lmod <- lFormula(y ~ x + (1|id), data = df, weights = df$weight)
    ## 2.  Create the deviance function to be optimized:
    devfun <- do.call(mkLmerDevfun, lmod)
    ## 3.  Optimize the deviance function:
    opt <- optimizeLmer(devfun)
    # return the deviance 
    opt$fval
  },
  # Use a method that allows box constraints
  method = "L-BFGS-B",
  # Weight cannot be negative
  lower = 0.01
)

# The weight estimates the following ratio, and it is pretty close
sd_eps1^2/sd_eps2^2
#> [1] 0.1111111
opt$par
#> [1] 0.1035914
# We can now fit the final model at the chosen weights
df <- dat %>% 
  mutate(weight = c(1, opt$par)[grp])
mod <- lmer(y ~ x + (1|id), data = df, weights = df$weight)

# Our estimate of sd_eps1
sigma(mod)
#> [1] 0.09899687
# True value
sd_eps1
#> [1] 0.1
# Our estimate of sd_eps2
sigma(mod) * sqrt(1/opt$par)
#> [1] 0.307581
# True value
sd_eps2
#> [1] 0.3

reprex package (v1.0.0) 于 2021-02-10 创建

【讨论】:

    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多