这里的关键点是从
complete(imp, "long")
因为它提供了所有的插补数据集。这样做之后,你必须玩一些游戏
tidyverse
和
broom
特别是功能
nest()
和
tidy()
这在这里非常有用。试试这个:
library(tidyverse)
library(broom)
library(mice)
data <- nhanes # data
imp <- mice(data, print = F) # imputation
# complete data
data.complete <- complete(imp, "long")
glimpse(data.complete) # all the 5 imputations are here
data.complete %>%
select(-.id) %>%
nest(-.imp) %>%
mutate(model = map(data, ~lm(bmi ~ chl, data = .)),
tidied = map(model, tidy)) %>%
unnest(tidied) %>%
filter(term == "chl") %>%
mutate(adjusted = p.adjust(p.value),
lci = estimate-(1.96*std.error),
uci = estimate+(1.96*std.error))
# output
.imp term estimate std.error statistic p.value adjusted lci uci
1 1 chl 0.01972747 0.01755024 1.124057 0.272584078 0.45430932 -0.0146709916 0.05412594
2 2 chl 0.02133664 0.01719462 1.240891 0.227154661 0.45430932 -0.0123648105 0.05503808
3 3 chl 0.03070542 0.01534959 2.000407 0.057397701 0.22512674 0.0006202261 0.06079062
4 4 chl 0.04109955 0.02044568 2.010183 0.056281686 0.22512674 0.0010260220 0.08117308
5 5 chl 0.05448964 0.01585764 3.436175 0.002251967 0.01125984 0.0234086522 0.08557062