扫帚包 - lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) 错误:0(非 NA)案例

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

我有一个学生属性和考试成绩的数据框,我正在尝试为每个年级(1 到 12)拟合一个线性模型。我正在使用 broom 包为每个年级有效地创建模型。下面是一个简化的示例数据集和我正在使用的代码。

#start df creation 

grade <- rep(1:12, each = 40)
attendance_rate <- round(runif(480, min=25, max=100), 1)
test_growth <- round(runif(480, min = -12, max = 38))
binary_flag <- round(runif(480, min = 0, max = 1))
score <- round(runif(480, min = 92, max = 370))
survey_response <- round(runif(480, min = 1, max = 4))

df <- data.frame(grade, attendance_rate, test_growth, binary_flag, score, survey_response) 

df$survey_response[df$grade == 1] <- NA

# end df creation

#create train test split for each grade level
set.seed(123)

df_train <- lapply(split(seq(1:nrow(df)), df$grade), function(x) sample(x, floor(.6*length(x))))
df_test <- mapply(function(x,y) setdiff(x,y), x = split(seq(1:nrow(df)), df$grade), y = df_train)

df_train <- df[unlist(df_train),]

df_test <- df[unlist(df_test),]



#create models
models_nested <- df_train %>%
  group_by(grade) %>% nest() %>% 
  mutate(
    fit = map(data, ~ lm(score ~ attendance_rate + test_growth + binary_flag + survey_response, data = .x)),
    tidied = map(fit, tidy),
    augmented = map(fit, augment),
    glanced = map(fit, glance)
  )

不幸的是,当我尝试 运行 以 models_nested 开头的代码块时,我收到以下错误:

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

我知道这是因为一年级的所有学生在 survey_response 列中都有一个 NA 值。我不知道如何在没有 运行 一年级单独回归的情况下解决这个问题,该回归完全降低了调查响应 column/variable。如果特定成绩子集仅包含空值,是否有办法告诉 lm 函数简单地忽略变量?我显然想在其他年级模型的回归中保留该变量。

我尽力把这个问题说清楚了,但如果有必要,我很乐意在评论中澄清。

编辑 6/9/2020:我不想 return 一年级模型不适用,我只想要一年级的线性模型到 运行 没有 survey_response 列。我希望 survey_response 列包含在所有其他年级模型中。

希望有人能帮忙!

我们可以检查 survey_response 中的 NA 值并相应地使用模型。

library(broom)
library(dplyr)
library(tidyr)
library(purrr)

df_train %>%
   group_by(grade) %>% 
   nest() %>% 
    mutate(fit = map(data, ~ if(all(is.na(.x$survey_response)))
              lm(score ~ attendance_rate + test_growth + binary_flag, data = .x) 
              else lm(score ~ attendance_rate + test_growth + binary_flag + survey_response, data = .x)),
        tidied = map(fit, tidy),
        augmented = map(fit, augment),
        glanced = map(fit, glance))


#   grade data              fit    tidied           augmented          glanced          
#   <int> <list>            <list> <list>           <list>             <list>           
# 1     1 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 2     2 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 3     3 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 4     4 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 5     5 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 6     6 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 7     7 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 8     8 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
# 9     9 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
#10    10 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
#11    11 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>
#12    12 <tibble [24 × 5]> <lm>   <tibble [4 × 5]> <tibble [24 × 11]> <tibble [1 × 11]>

我们可以使用 possibly 来自 purrr

library(broom)
library(dplyr)
library(tidyr)
library(purrr)

poslm <- possibly(lm, otherwise = NA)
df_train %>%
   group_by(grade) %>% 
   nest() %>% 
   mutate(fit = map(data, ~ poslm(score ~ attendance_rate + test_growth + 
              binary_flag + survey_response, data = .x)), 
         tidied = map(fit, possibly(tidy, otherwise = NA)),
            augmented = map(fit, possibly(augment, otherwise = NA)),
          glanced = map(fit, possibly(glance, otherwise = NA)))
# A tibble: 12 x 6
# Groups:   grade [12]
#   grade data              fit       tidied           augmented          glanced          
#   <int> <list>            <list>    <list>           <list>             <list>           
# 1     1 <tibble [24 × 5]> <lgl [1]> <lgl [1]>        <lgl [1]>          <lgl [1]>        
# 2     2 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 3     3 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 4     4 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 5     5 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 6     6 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 7     7 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 8     8 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
# 9     9 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
#10    10 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
#11    11 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>
#12    12 <tibble [24 × 5]> <lm>      <tibble [5 × 5]> <tibble [24 × 12]> <tibble [1 × 11]>