MICE pool()函数的输出可以用dplyr进行分组吗?

问题描述 投票:1回答:1

在使用MICE包对一些数据集进行多重归入后,我想分别计算两个因变量(score_1,score_2)的线性回归模型。两个模型的自变量(手臂、性别、年龄、基线分数)是相同的。不幸的是,我没有管理(1)将mice::pool()函数集成到dplyr管道中,(2)我不知道如何通过因变量(df_lm$score)对mice::pool()函数进行分组。

library(tidyverse)
library(mice)


# SIMULATE DATA


df <- data.frame(id = 1:120,
                 arm = sample(c('intervention', 'control'), 120, replace = TRUE),
                 sex = sample(c('m', 'f'), 120, replace = TRUE),
                 age = round(rnorm(120, 55, 10)),
                 score_1 = round(rnorm(120, 50, 5)),
                 score_2 = round(rnorm(120, 50, 7)))

df <- df %>% bind_rows(df) %>%
  mutate(time = c(rep('baseline', 120), rep('follow_up', 120))) %>%
  select(id, arm, time, everything()) %>%
  gather(score, measure , -(id:age)) %>%
    spread(key = time, value = measure)

# INSERT SOME MISSING VALUES

df$follow_up[seq(1, 240, 5)] <- NA

# IMPUTATION MODEL 

init <- mice(df, maxit = 0)
predM <- init$predictorMatrix
# remove as predictor
predM[ , c('arm')] <- 0


mids_from_df <- mice(df,
  method = 'pmm',
  predictorMatrix = predM,
  m = 5,
  seed = 123,
  print = FALSE
)


# COMPUTE MODELS


fmla <- "follow_up ~ baseline + arm + sex + age"

df_lm <- mids_from_df %>%
  mice::complete("long", include = FALSE) %>%
  group_by(.imp, score) %>%
  nest() %>%
  mutate(lm_model = map(data, ~lm(fmla, data = .))) 

我想分别得到每个因变量的集合结果。然而,我不知道如何将mice::pool()与dplyr和purr一起使用。下面的代码抛出了一个错误。

df_lm <- mids_from_df %>%
  mice::complete("long", include = FALSE) %>%
  group_by(.imp, score) %>%
  nest() %>%
  mutate(lm_model = map(data, ~lm(fmla, data = .)))  %>%
  group_by(.imp, score) %>%
  pool(., lm_model) # does not work

错误信息是: "Error: 没有针对integer类对象的glance方法"

先谢谢您的帮助!

r dplyr purrr imputation r-mice
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.