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

时间:2019-06-27 10:15:00

标签: r dplyr purrr imputation r-mice

在使用MICE软件包对某些数据集进行多次插补之后,我想为两个因变量(得分1,得分_2)中的每一个分别计算线性回归模型。两个模型的自变量(手臂,性别,年龄,基线得分)相同。不幸的是,我无法管理(1)将mouse :: pool()函数集成到dplyr管道中,以及(2)我不知道如何通过因变量对mouse :: pool()函数进行分组( df_lm $ score)。

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 = .))) 

我想分别获取每个因变量的合并结果。但是,我不知道如何与dplyr和purr一起使用mouse :: pool()。以下代码引发错误:

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

错误消息是:“错误:类整数对象的无视方法”

在此先感谢您的帮助!

0 个答案:

没有答案