You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

glmer()运行异常:无z值/p值且anova模型对比失败求助

问题描述

我的因变量是二元响应变量(1=正确,0=错误),固定效应为语言组(English或Turkish),随机效应为参与者。运行以下glmer代码后,模型未输出任何效应的z-score与p值,同时出现警告:

model <- glmer(Correct ~ Participant.Starting.Group + (1|Participant.Private.ID), 
               data = mixedSingle, 
               family = binomial(link="logit"))

数据集mixedSingle结构:

Participant.Private.ID Participant.Starting.Group Correct
<int>                  <chr>                      <int>
7911618                English                      0       
7911618                English                      0       
7911618                English                      1   
... 
7993996                Turkish                      0       
7993996                Turkish                      1       
7993996                Turkish                      0   

模型输出及警告:

Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: binomial  ( logit )
Formula: Correct ~ Participant.Starting.Group + (1 | Participant.Private.ID)
   Data: mixedSingle
      AIC       BIC    logLik  deviance  df.resid 
 985.0169  998.8206 -489.5085  979.0169       733 
Random effects:
 Groups                 Name        Std.Dev.
 Participant.Private.ID (Intercept) 0.5815  
Number of obs: 736, groups:  Participant.Private.ID, 23
Fixed Effects:
                      (Intercept)  Participant.Starting.GroupTurkish  
                           0.1031                             0.4892  
Warning messages:
1: In class(object) <- "environment" :
  Setting class(x) to "environment" sets attribute to NULL; result will no longer be an S4 object
2: In class(object) <- "environment" :
  Setting class(x) to "environment" sets attribute to NULL; result will no longer be an S4 object

另外,使用anova(model1, model2)对比同数据的模型时,出现错误:

Error in anova.merMod(singleGLMER_English, singleGLMER) : 
models were not all fitted to the same size of dataset
解决方案

一、模型无z-score、p值及警告问题处理

1. 获取z值与p值

lme4包默认不输出混合效应模型固定效应的p值(因自由度计算存在争议),但可以通过以下方式获取:

  • 用summary()输出完整结果,会包含z值,再结合辅助工具计算p值:
    # 输出带z值的模型摘要
    summary(model)
    # 用broom.mixed提取并计算p值
    library(broom.mixed)
    tidy(model, conf.int = TRUE)
    
  • 用car包的Anova()函数做卡方检验,直接得到固定效应的显著性:
    library(car)
    Anova(model, type = "II")
    

2. 消除class(object)警告

该警告多由lme4版本兼容问题或包冲突导致,解决方法:

  • 更新lme4到最新版本:
    install.packages("lme4")
    
  • 检查并卸载冲突包(比如plyr与lme4可能冲突),再重新拟合模型:
    detach("package:plyr", unload = TRUE)
    model <- glmer(Correct ~ Participant.Starting.Group + (1|Participant.Private.ID), 
                   data = mixedSingle, 
                   family = binomial(link="logit"))
    

二、anova模型对比数据集不一致错误处理

错误核心是两个对比模型的拟合数据集行数不同,处理步骤:

  1. 先确认两个模型的观测数:
    nobs(singleGLMER_English)
    nobs(singleGLMER)
    
  2. 统一拟合数据集:
    • 若因缺失值导致差异,先清理数据集再重新拟合:
      cleaned_data <- na.omit(mixedSingle)
      # 用同一清理后的数据集拟合两个模型
      singleGLMER <- glmer(Correct ~ Participant.Starting.Group + (1|Participant.Private.ID), 
                           data = cleaned_data, 
                           family = binomial(link="logit"))
      singleGLMER_English <- glmer(...) # 此处公式可调整,但必须用cleaned_data
      
    • 若其中一个模型是子集数据拟合的,需确保对比模型基于同一数据集的不同公式(比如一个含固定效应,一个不含),而非不同子集。

内容的提问来源于stack exchange,提问作者Aaron Holmberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:45:37