gpt4 book ai didi

r - 如何用 R 中的 lme4 估计子组的残差

转载 作者:行者123 更新时间:2023-12-05 07:38:24 26 4
gpt4 key购买 nike

我想用 R 中的 lme4 包重现霍夫曼和罗文的作品(实验心理学家的多层次模型:基础和说明性示例)中报告的结果。

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

在 Hoffman 的模型 2B 中,他们分别估计了老年人和年轻人的一级残差,并为年轻人和老年人设置了两个虚拟变量。

霍夫曼方程是 Level1 equation

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

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

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

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

最佳答案

您可以使用模块化拟合函数处理 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 创建于 2021-02-10 (v1.0.0)

关于r - 如何用 R 中的 lme4 估计子组的残差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47944264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com