gpt4 book ai didi

R:使用新 lme4 包的 bootMer() 引导二进制混合模型逻辑回归

转载 作者:行者123 更新时间:2023-12-04 10:32:06 25 4
gpt4 key购买 nike

我想使用新的 lme4 包(当前的开发者版本)的新 bootMer() 功能。我是 R 的新手,不知道应该为其 FUN 参数编写哪个函数。它说它需要一个数值向量,但我不知道该函数将执行什么。所以我有一个混合模型公式,它被转换为 bootMer(),并且有很多重复。所以我不知道那个外部函数是做什么的?它应该是引导方法的模板吗? bootstrapping 方法不是已经在 bootMer 中实现了吗?那么为什么他们需要一个外部的“感兴趣的统计数据”呢?我应该使用哪个感兴趣的统计数据?

以下语法是否适合使用? R 不断产生错误,即 FUN 必须是数值向量。我不知道如何将估计值与“适合”分开,甚至我应该首先这样做吗?我只能说我对那个“有趣”的论点迷失了。另外我不知道我应该使用变量“Mixed5”传递混合模型 glmer() 公式还是应该传递一些指针和引用?我在示例中看到 X(bootMer() 的第一个参数是 *lmer() 对象。我想编写 *Mixed5 但它呈现错误。

非常感谢。

我的代码是:

library(lme4)
library(boot)

(mixed5 <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4))


FUN <- function(formula) {
fit <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4)
return(coef(fit))
}

result <- bootMer(mixed5, FUN, nsim = 3, seed = NULL, use.u = FALSE,
type = c("parametric"),
verbose = T, .progress = "none", PBargs = list())

result
FUN
fit

和错误:
Error in bootMer(mixed5, FUN, nsim = 3, seed = NULL, use.u = FALSE, type = c("parametric"),  : 
bootMer currently only handles functions that return numeric vectors

-------------------------------------------------- - - - 更新 - - - - - - - - - - - - - - - - - - - - - - ----------

我按照 Ben 的指示编辑了代码。代码运行得非常好,但 SE 和偏差都为零。你还知道如何从这个输出中提取 P 值(对我来说很奇怪)?我应该使用 afex 包的混合()吗?

我修改后的代码:
library(lme4)
library(boot)

(mixed5 <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4))


FUN <- function(fit) {
fit <- glmer(DV ~ (Demo1 +Demo2 +Demo3 +Demo4 +Trt)^2
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4)
return(fixef(fit))
}

result <- bootMer(mixed5, FUN, nsim = 3)

result

-------------------------------------------------- ------ 更新 2 ------------------------------------------ -----------

我也尝试了以下但代码生成警告并没有给出任何结果。
(mixed5 <- glmer(DV ~ Demo1 +Demo2 +Demo3 +Demo4 +Trt 
+ (1 | PatientID) + (0 + Trt | PatientID)
, family=binomial(logit), MixedModelData4))

FUN <- function(mixed5) {
return(fixef(mixed5))}

result <- bootMer(mixed5, FUN, nsim = 2)

警告信息:
In bootMer(mixed5, FUN, nsim = 2) : some bootstrap runs failed (2/2)
> result

Call:
bootMer(x = mixed5, FUN = FUN, nsim = 2)

Bootstrap Statistics :
WARNING: All values of t1* are NA
WARNING: All values of t2* are NA
WARNING: All values of t3* are NA
WARNING: All values of t4* are NA
WARNING: All values of t5* are NA
WARNING: All values of t6* are NA

-------------------------------------------------- ------ 更新 3 ------------------------------------------ -----------

此代码也生成警告:
FUN <- function(fit) {
return(fixef(fit))}

result <- bootMer(mixed5, FUN, nsim = 2)

警告和结果:
Warning message:
In bootMer(mixed5, FUN, nsim = 2) : some bootstrap runs failed (2/2)
> result

Call:
bootMer(x = mixed5, FUN = FUN, nsim = 2)

Bootstrap Statistics :
WARNING: All values of t1* are NA
WARNING: All values of t2* are NA
WARNING: All values of t3* are NA
WARNING: All values of t4* are NA
WARNING: All values of t5* are NA
WARNING: All values of t6* are NA

最佳答案

这里基本上有两个(简单的)混淆。

  • 第一个位于 coef()(返回矩阵列表)和 fixef()(返回固定效应的向量)之间
    系数):我假设 fixef() 是你想要的,尽管你可能想要类似 c(fixef(mixed),unlist(VarCorr(mixed))) 的东西。
  • 第二个是 FUN 应该将拟合的模型对象作为输入...

  • 例如:
    library(lme4)
    library(boot)

    mixed <- glmer(incidence/size ~ period + (1|herd),
    weights=size, data=cbpp, family=binomial)

    FUN <- function(fit) {
    return(fixef(fit))
    }

    result <- bootMer(mixed, FUN, nsim = 3)

    result

    ## Call:
    ## bootMer(x = mixed, FUN = FUN, nsim = 3)
    ## Bootstrap Statistics :
    ## original bias std. error
    ## t1* -1.398343 -0.20084060 0.09157886
    ## t2* -0.991925 0.02597136 0.18432336
    ## t3* -1.128216 -0.03456143 0.05967291
    ## t4* -1.579745 -0.08249495 0.38272580
    ##

    关于R:使用新 lme4 包的 bootMer() 引导二进制混合模型逻辑回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18443127/

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