Error: Problem with `mutate()` column (...) must be size 15 or 1, not 17192

Error: Problem with `mutate()` column (...) must be size 15 or 1, not 17192

我目前正在尝试在 this tutorial 之后绘制 lme4::lmer 函数的结果。我已经尝试了教程中的代码,它按预期工作。 在本教程中,一列 pred_dist 在绘图之前添加到拟合函数的数据集中。当我用自己的数据尝试此操作时,出现以下错误:

Error: Problem with `mutate()` column `pred_dist`.
ℹ `pred_dist = fitted(model0)`.
ℹ `pred_dist` must be size 15 or 1, not 17192.
ℹ The error occurred in group 1: sgroup = 578, group = 1.

Sample data

我的代码:

# Loaded libraries
library(dplyr)
library(ggplot2)
library(lme4)
library(lmerTest)
library(lattice)

# My lmer model. myData is fairly 
model0 <- lmer(outcome ~ (1|group), data=myData, REML = FALSE)
summary(model0)

myData %>% 
    # save predicted values
    mutate(pred_dist = fitted(model0)) 

我想了解的是:导致此错误消息的原因是什么,我该如何解决?


更新:

根据 bouncyball 的评论我添加了 ungroup()mutate() 之前。这适用于我的初始模型,但不适用于后续模型。 我尝试了以下方法:

> model1 <- lmer(outcome ~ predictor + (1|group), myData, REML=FALSE) 

> plotVar$pred_dist = fitted(model1). 

Error: Assigned data 'value' must be compatible with existing data. 
✖ Existing data has 17192 rows. 
✖ Assigned data has 16794 rows. 
ℹ Only vectors of size 1 are recycled.

> plotVar %>% ungroup(.) 
          %>% mutate(pred_dist = fitted(model1)) 

Error: Problem with 'mutate()' column 'pred_dist'. 
ℹ 'pred_dist = fitted(model1)'. 
ℹ 'pred_dist' must be size 17192 or 1, not 16794.

我非常强烈的猜测是这里发生了两种不同的事情:(1) 分组 (2) NA 值。

让我们组成一个示例,将 分组并包含 NA 个值:

library(dplyr)
library(lme4)
ss <- sleepstudy |> group_by(Subject)
ss$Days[1:5] <- NA

m0 <- lmer(Reaction ~ Days + (Days | Subject), ss)

试试原码:

ss |> mutate(pred = fitted(m0))

pred must be size 10 or 1, not 175.
ℹ The error occurred in group 1: Subject = 308.

试试 ungroup():

ss |> ungroup() |> mutate(pred = fitted(m0))

pred = fitted(m0).
pred must be size 180 or 1, not 175.

我们仍然收到错误,但请注意尺寸不同。

现在使用 na.action = na.exclude 更新模型(这可以在第一个模型中完成,或者可以通过设置 options(na.action = "na.exclude"):

m1 <- update(m0, na.action = na.exclude)
ss |> ungroup() |> mutate(pred = fitted(m1))