lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) 中的错误:使用 lm 时出现 0 种(非 NA)情况

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases while using lm

提问人:lxrnax 提问时间:8/30/2023 最后编辑:lxrnax 更新时间:9/9/2023 访问量:62

问:

使用此代码后:

 output.lm = count_all %>% 
  group_by(year.y) %>% 
  do(tidy(lm(ENS~total.p, data = .)))%>% 
  filter(term == "total.p")

output.lm

我收到以下错误:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases

为什么?

我的数据很大,但这是 dput:

dput(count_all$total.p)
c(7.1, NA, NA, 4.84, 3.87, NA, NA, NA, NA, NA, NA, NA, NA, 4.19, 
NA, NA, 2.29, 4.52, NA, NA, 4.84, NA, 2.94, NA, NA, 4.84, NA, 
3.55, 11.61, 6.13, 2.52, 2.74, 2.39, 4.19, 3.23, 4.19, 3.23, 
NA, NA, 3.55, 4.84, 5.48, 3.55, 2.35, 4.52, 4.84, 5.81, 4.19, 
3.06, 5.16, 2.1, 5.81, 2.19, NA, 3.87, 3.16, 6.13, 2.52, 2.52, 
NA, 2.19, 3.23, 4.19, 3.55, 2.9, 1.65, 1.87, 1.68, 3.06, 3.87, 
2.06, 2.42, 2.29, 0.68, 1.42, 3.23, 2.35, 3.87, 4.19, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 3.032, 2.548, 3.226, 2.968, 
2.194, 2.032, 2.613, 2.129, 2.419, 2.258, 3.871, 2.903, 2.742, 
3.226, 3.548, 3.129, 3.548, 3.871, 3.548, NA, 3.065, 2.839, 3.097, 
NA, NA, NA, 3.548, 3.161, 2.645, 2.516, 2.419, 4.516, 2.258, 
1.968, 6.129, 2.903, 2.29, 2.677, 2.645, 1.806, 3, 2.194, 3.226, 
2.774, 3.032, 3.065, 3.129, 3.161, 2.935, 2.71, 2.935, 2.613, 
3.871, 2.419, 1.71, 2.258, 1.613, 2.484, 3.129, 1.839, 2.387, 
3.032, 2.258, 2.258, 3.226, 2.129, 2.548, 2.613, 3.097, 2.645, 
3.548, 4.516, 5.806, 3.548, 3.065, 4.516, 3.871, 3.548, 3.548, 
2.226, 3.032, 2.419, 1.613, 1.419, 2.097, 1.613, 1.871, 2.935, 
3.097, 3.548, 2.935, 3.548, 6.129, 5.484, 3.226, 3.548, 2, 2.968, 
4.194, 2.839, 2, 2.29, 1.839, 1.935, 1.968, 1.29, 0.645, 1.968, 
3.097, 2.935, 3.065, 2.806, 3.226, 4.194, 3.065, 2.968, NA, NA, 
5.806, 6.129, 4.839, 4.516, 4.194, 4.194, 2.29, 3.548, 2.806, 
2.806, 3.871, 2.871, 2.71, 3.097, 3.226, 3.548, 3.548, 3.871, 
4.839, 3.065, 5.806, 3.548, NA, NA, 3.871, 3.871, 3.871, 3.548, 
3.129, 3.548, 2.806, 3.871, 2.323, 2.097, 4.839, 1.839, 3.032, 
1.774, 2.355, 3.548, 3.871, 4.516, 3.871, 3.548, 3.548, 3.548, 
3.871, 4.516, 4.516, 3.548, 3.226, 3.548, 3.871, 3.871, 3.032, 
2.806, 2.903, 2.194, 2.645, 2.968, 2.645, 3.226, 4.194, 3.226, 
3.548, 3.548, 3.871, 3.226, 2.935, 3.548, 2.71, 2.613, 3.129, 
2.129, 1.871, 2.645, 2.29, 2.387, 2.323, 2.194, 1.968, 1.484, 
1.581, 1.968, 2.581, 3.226, 3.548, 4.194, 3.548, 3.871, 4.839, 
5.161, 4.194, 2.419, 2.645, 2.968, 4.516, 4.839, 2.71, 2.774, 
3.548, 3.548, 2.581, 1.839, 1.323, 3.226, 2.452, 3.161, 2.581, 
2.71, 3.226, 3.129, 2.742, 1.742, 1.968, 2.548, 2.548, 2.613, 
2.097, 2.419, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4.52, 2.39, 3.55, 
4.52, 3.16, 2.39, 3, 4.52, 5.81, 5.16, 4.19, 3.06, 4.84, 2.03, 
4.19, 2.1, 1.29, 1.32, 1.77, 1.77, 1.45, 2.23, 2.16, 1.84, 1.61, 
1.81, 1.29, 2.29, 2.35, 2.52, 2.61, 2.71, 1.61, 1.58, 2.1, 1.9, 
1.9, 2.52, 2.81, 2.61, 4.52, 2.81, 2.87, 4.19, 3.16, 4.19, 4.84, 
5.48, 3.19, 4.52, 4.52, 4.84, 5.49, 5.49, 5.81, 4.52, 5.17, 3.87, 
2.84, 4.52, 4.2, 2.71, 3.87, 3.55, 2.32, 1.78, 1.81, 1.03, 1.13, 
1.26, 2.07, 1.58, 2.1, 1, 2.2, 2.1, 2.03, 3.55, 2.52, 2.1, 2.97, 
2.58, 3, 2.36, 2.65, 2.74, 1.94, 2.52, 2.65, 2.97, 2.42, NA, 
3.16, 2.94, 2.13, 3.55, 3.87, 4.2, 5.49, 3.16, 3.55, 5.49, 6.33, 
3.13, 1.8, 2.9, 4, 2.9, 2.53, 3.67, 6.33, 3.27, 3.67, 4, 2, 2.9, 
1.33, NA, 1.6, 1.43, 1.57, NA, 1.67, 1.5, 1.2, 1.5, 2.83, 2.63, 
3.27, 2.63, 2.77, 2.87, 2.63, 1.87, 1.87, 2.1, 1.8, 1.87, 2.73, 
2.93, 2.37, 2.6, 2.37, 2.5, 4.33, 2.17, 2.4, 2.9, 2.17, 5.33, 
4.67, 6, 6.45, 5.48, 7.74, 3.55, 3.03, 3.55, 5.16, 3.87, 3.87, 
2.42, 2.84, 1.84, 1.77, 1.81, 2.35, 3.55, 2.48, 2.35, 3.55, 2.23, 
2.55, 2.26, 2.74, 2.29, 1.77, 2.52, 2.19, 1.61, 2, 1.87, 1.71, 
2.45, 2.97, 3.55, 2.68, 3.55, 3.23, 2.35, 3.1, 2.84, 3.23, 3.23, 
2.77, 2.48, 2.16, 4.52, 1.94, 1.71, 3.55, 2.97, 2.61, 3.1, 2.94, 
2.61, 2.77, 2.06, 2.32, 2.71, 1.58, NA, 2.06, NA, 1.94, 1.58, 
1.94, 2, 1.65, 3.55, 2.29, 1.68, 1.39, 1.94, 0.97, 1.32, 1.35, 
1.52, 2.06, 1.71, 2.32, 3.16, 2.13, 2.77, 2.48, 2.71, 3.55, 2.68, 
3.23, 2.48, 1.74, 2.1, 1.61, 2.29, 1.74, 2.35, 3.87, 3.13, 2.52, 
2.13, 2.94, 3.03, 4.19, 2.39, 4.52, 2.03, 2.55, 2.23, 2.94, 4.52, 
2.55, 2.9, 3.55, 3, 3.06, 2.29, 5.81, 2.03, 2.52, 1.68, 1.68, 
2.26, 1.16, 1.71, 1.39, 2.16, 2.81, 2.74, 2.1, 2.77, 2.61, 3.23, 
2.61, 2.9, 3, 3.23, 3.23, 2.94, 2.48, 2.16, 2.55, 2.42, 2.71, 
2.68, 2.26, 2.45, 2.06, 3.55, 4.52, 4.19, 3.23, 2.52, 3.87, 2.13, 
3.06, 3.03, 3.23, 3.87, NA, NA, NA, NA, 3, 3.55, 3.23, 3, 3.23, 
3.55, 2.48, 2.61, 4.19, 1.35, 1.71, 1.03, 1.23, 2.35, 0.94, 2.42, 
2.9, 2.58, 2.84, 2.23, 2.26, 2.45, 3.55, 2.32, 2.58, 2.1, 3.06, 
3.23, 4.19, 2.9, 2.26, 3.23, 2.9, 2.52, 2.13, 2.94, 2.26, 3.55, 
4.52, 2.84, 3.87, 2.94, 3.19, 2.81, 2.55, 2.42, 1.81, 2.48, 2.74, 
6.45, 5.16, 3, 3.87, 3, 3.87, 3.19, 3, 2.42, 3.19, 2.71, 2.03, 
2.39, 1.9, 1.84, 1.42, 2.03, 1.35, 1.77, 1.94, 1.61, 1.65, 2.68, 
1.9, 1.87, 1.61, 2.39, 3.55, 2.81, 2.81, 2.77, 3.23, 3.23, 2.9, 
2.68, 3.87, 3.23, 2.9, 4.19, 3.16, 4.19, 4.19, 3.55, 4.19, 2.48, 
3.55, 2.71, 3.03, 2.48, 3.23, 3.87, 3.55, 2.61, 3.55, 2.71, 4.19, 
2.16, 2.61, 2.61, 1.39, 1.61, 1.52, 1.58, 1.58, 1.77, 1.74, 1.23, 
1.52, 3.19, 1.65, 1.9, 1.52, 1.9, 2.55, 3.1, 2.42, 2.61, 2, 2.74, 
3.06, 3.87, 2.23, 2.48, 2.71, 2.65, 2.42, 2.87, 4.84, 4.19, 3.55, 
2.23, 2.39, 2.68, 2.26, 3.87, 3.23, 3.23, 1.97, 2.29, 4.19, 3.16, 
3.23, 4.52, 4.19, 4.84, 3.87, 3.06, 3.55, 2.65, 2.9, 4.84, 2.9, 
2.29, 2.55, 2.23, 2.03, 1.68, 1.81, 1.13, 2.9, 1.61, 3.19, 2.06, 
1.94, 2.1, 2.16, 2.29, 2.32, 2.42, 3.55, 3.87, 3.87, 2.71, 3.23, 
3.87, 3.23, 2.81, 1.97, 2.06, 2.42, 3.23, 3.55, 2.52, 3.55, 3.55, 
3.55, 3.87, 4.52, 2.74, 2.06, 2.35, 2.65, 2.26, 2.97, 1.68, 2.13, 
2.03, 2.84, 1.87, 2.42, 4.52, 5.81, 3.23, 2.68, 3.23, 2.58, 2.52, 
2.81, 1.52, 2.77, 1.26, 3.23, 1.94, 1.58, 1.55, 2.1, 1.84, 1.45, 
1.42, NA, 2.29, 2.13, 3.16, 2.39, 2.74, 4.52, 2.9, 2.77, 1.9, 
1.52, 3.55, 2.61, 4.52, 2.13, 2.06, 3.23, 2.23, 3.23, 3, 2.94, 
1.58, 2.23, 6.77, 2.71, 4.19, 4.19, 5.16, 5.81, 5.81, 2.68, 4.19, 
4.19, 3.55, 5.48, 2.77, 3.55, 2.03, 1.58, 2.77, 1.94, 2.48, 2.45, 
3.1, 2.29, 1.84, 1.77, 2, 1.45, 2.03, 1.9, 2.48, 2.06, 2.26, 
3.87, 2.13, 1.74, 2.48, 2.48, 2.23, 2.06, 2.1, 2.81, 2.87, 2.55, 
2.03, 2.19, 1.71, 1.61, 1.81, 1.9, 2.26, 1.65, 1.77, 2.16, 1.97, 
1.55, 2.1, 2.16, 2, 2.35, 2.45, 3.1, 3.55, 1.19, 2.97, 2.87, 
1.94, 1.23, 1.29, 1.35, 1.06, 1.71, 1.29, 2.35, 1.23, 1.16, 1.42, 
1.74, 2.48, 1.94, 1.42, 2, 1.58, 1.35, 3, 1.84, 1.87, 2.87, 2.1, 
1.94, 2.29, 2.65, 1.84, 2.23, 1.97, 2.84, 2.48, 2.68, 2.84, 1.42, 
2.45, 1.42, 1.48, 1.71, 1.32, 1.84, 2.26, 2.52, 1.9, 1.61, 2.23, 
1.55, 2.19, 1.52, 1.65, 2.16, 1.97, 1.48, 0.74, 1.55, 1.52, 1.32, 
0.74, 1.1, 1.39, 1.29, 1.77, 2.1, 1.45, 1.26, 1.55, 1.68, 0.94, 
0.74, 0.77, 1.58, 0.77, 2.23, 2.13, 1.9, 1.13, 1.26, 1.65, 1.84, 
1.94, 1.39, 1.35, 1.55, 1.81, 1.87, 1.45, 1.39, 1.74, 1.81, 1.55, 
2.13, 1.48, 1.87, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 3.871, NA, NA, 3.097, 2.968, 3.226, 6.774, 2.742, 2.935, 
3.097, 4.839, 4.516, 3.871, 2.71, 2.194, 1.806, 2.097, 2.677, 
2.839, 2.323, 2.968, 1.548, 2.581, 2.581, 3.097, 2.742, 2.613, 
1.548, 1.387, 1.387, 2.387, 2.613, 1.516, 2.161, 5.161, 2.871, 
2.226, 2.613, 2.194, 1.484, 2.613, 3.161, 3.032, 3.871, 3.548, 
1.548, 1.742, 3.194, 2.29, 1.613, 5.484, 2.71, NA, 3.871, 1.645, 
2.032, 1.645, 1.806, 2.194, 3.226, 3.548, 4.194, 2.935, 4.839, 
2.194, 2.774, 2.839, 2.452, 1.548, 1.903)

因此,我试图构建一个可重复的例子。首先是编辑后的 R 代码和软件包:

library(tidyverse)
library(reshape2)
library(vegan)
library(broom)
library(metafor)
library(lubridate)
library(cowplot)
library(psych)
library(lme4)

count_all |> na.omit() |>
  dplyr::group_map(~lm(ENS~total.p, data = .x))


lme4::lmList(ENS~total.p | year.y, count_all) |> 
  broom.mixed::tidy()

此时: 警告消息: In lme4::lmList(ENS ~ total.p | year.y, count_all) : in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...): NA (6 times)

主要部分:

output.lm = count_all %>% 
  group_by(year.y) %>% 
  do(tidy(lm(ENS~total.p, data = .)))%>% 
  filter(term == "total.p")

output.lm

total.p 的 dput 已经在上面。

str(count_all$ENS)
str(count_all$year.y)
str(count_all)
R 数据帧 错误处理 LM

评论

2赞 Ben Bolker 8/30/2023
这还不足以让我们提供帮助。据推测,在排除任一变量的 NA 值后,有一年没有剩余的案例。结果是什么?with(count_all, table(year, !is.na(ENS), !is.na(total.p)))
0赞 lxrnax 8/31/2023
其结果是: year.y TRUE 1999 0 2000 0 2001 0 2002 0 2003 0 2004 0 2005 50 2006 51 2007 50 2008 52 2009 82 2010 86 2011 84 2012 84 2013 85 2014 87 2015 86 2016 85 2017 93 2018 88
0赞 lxrnax 8/31/2023
任何想法,我现在能做什么?我必须排除这些年份吗?
0赞 Ben Bolker 8/31/2023
是的,你这样做(或看到我的答案)

答:

0赞 Ben Bolker 8/31/2023 #1

出现此问题的原因是,一旦丢弃响应中的值任何预测变量(仅在本例中),其中一个组就没有剩余观测值。NAtotal.p

下面是一个可重现的示例:

library(tidyverse)
mm <- (mtcars
   |> mutate(across(mpg, ~ ifelse(cyl == 6, NA, .)),
             across(cyl, factor))
   |> group_by(cyl)
)
mm |> dplyr::group_map(~lm(mpg ~ hp, data = .x))

一些可能的解决方案:

  • 在开始之前,请自行省略这些值:NA
mm |> na.omit() |>
    dplyr::group_map(~lm(mpg ~ hp, data = .x))
  • 使用 +(注意,您应该通过以下方式从 Github 安装开发版本——我刚刚注意到并修复了一个错误)lme4::lmList()broom.mixed::tidy()broom.mixedremotes::install_github("bbolker/broom.mixed")
lme4::lmList(mpg ~ hp | cyl, mm) |> 
    broom.mixed::tidy()

评论

0赞 lxrnax 8/31/2023
就我而言是cyl year.y,对吧?如果是这样,我仍然收到与上面相同的“lme4:...”错误,即使第一个代码(省略 NA 值有效)。
0赞 Ben Bolker 9/1/2023
是的,等同于 。如果我有一个可重复的例子,我可以尝试看看发生了什么,但如果没有一个就无法诊断......stackoverflow.com/questions/5963269/......cylyear.y
0赞 lxrnax 9/7/2023
我编辑了我的帖子,也许有帮助。
0赞 Ben Bolker 9/7/2023
对不起,这仍然不是一个可重现的答案,因为我们没有.count_all
0赞 lxrnax 9/9/2023
对不起!它适用于 str() 吗?