gpt4 book ai didi

r - lmer : predictions on population level trigger an error

转载 作者:行者123 更新时间:2023-12-04 11:21:57 25 4
gpt4 key购买 nike

我想使用线性混合模型并在总体水平上进行预测(即仅使用固定效应并使用 0 而不是随机效应)。

示例模型:

require(lme4)

fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
summary(fm1)
# values for prediction:
newx <- seq(min(sleepstudy$Days), max(sleepstudy$Days))

我尝试了几种人口水平的预测方法,但都失败了:
pred <- predict(fm1, newdata = data.frame(Days = newx), allow.new.levels = TRUE)
# Error: couldn't evaluate grouping factor Subject within model frame: try adding grouping factor to data frame explicitly if possible

pred <- predict(fm1, newdata = data.frame(Days = newx, Subject = NA), allow.new.levels = TRUE)
# Error: Invalid grouping factor specification, Subject

pred <- predict(fm1, newdata = data.frame(Days = newx, Subject = as.factor(NA)), allow.new.levels = TRUE)
# Error: Invalid grouping factor specification, Subject

我试图找到正确预测方法的手册,但我不知道如何?我试着看 help(package = "lme4")我找到的最接近的函数是 predict.merMod (虽然模型类 fm1lmerMod 不是 merMod )。 ?predict.merMod读到:

allow.new.levels (logical) if FALSE (default), then any new levels (or NA values) detected in newdata will trigger an error; if TRUE, then the prediction will use the unconditional (population-level) values for data with previously unobserved levels (or NAs)



它特别指出“或 NAs”,但它显然不是那样工作的!!
  • 我是否正在查看正确方法的帮助页面?如果没有,正确的方法是什么?
  • 如何使预测在人口水平上发挥作用?
  • 最佳答案

    您正在寻找 re.form :

    re.form: formula for random effects to condition on. If ‘NULL’, include all random effects; if ‘NA’ or ‘~0’, include no random effects


    require(lme4)
    fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
    newx <- seq(min(sleepstudy$Days), max(sleepstudy$Days))
    predict(fm1, newdata=data.frame(Days=newx), re.form=NA)
    ## 1 2 3 4 5 6 7 8
    ## 251.4051 261.8724 272.3397 282.8070 293.2742 303.7415 314.2088 324.6761
    ## 9 10
    ## 335.1434 345.6107

    至于你的其他问题:
  • merMod是一个“父类(super class)”,包括线性( lmerMod )和广义线性( glmerMod )模型:参见 ?"merMod-class"
  • 你的第二次尝试可能应该有效;然而,allow.new.levels专为偶尔出现的情况而设计 NA值,并非全部 NA值 ... predict(fm1, newdata = data.frame(Days = newx, Subject = "a"), allow.new.levels = TRUE)确实有效。看起来代码检测到所有 - NA列并将其解释为上游出现问题 - 这可以在代码中修复,但自 re.form 以来似乎不是非常高的优先级存在。
  • 关于r - lmer : predictions on population level trigger an error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513330/

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