Reputation: 15
I am imputing my missing data using mice. The problem here is that it takes an hour or two to get the imputation done. So as it finishes imputing, I would like to export it for future use so that I could avoid repeating the time-consuming imputation process in case I have to revisit the analysis.
I have searched on Google and I found a function miceadds::write.mice.imputation
. I have looked at the manual. It offered an example of exporting, but I am not sure how to import it back. It seems to have generated some .dat file.
say I have the following code:
# Model 1: Imputation using mice
imp1 <- mice::mice( nhanes, m=3, maxit=5 )
# write results
write.mice.imputation(mi.res=imp1, name="mice_imp1" )
Upvotes: 1
Views: 2134
Reputation: 814
est_long <- parlmice(nhanes, cluster.seed = 123, print = FALSE, n.core = 4, n.imp.core = 100) %>%
mice::complete("long")
gives
head(est_long)
.imp .id age bmi hyp chl
1 1 1 1 22.0 1 131
2 1 2 2 22.7 1 187
3 1 3 1 27.2 1 187
4 1 4 3 27.2 2 284
5 1 5 1 20.4 1 113
6 1 6 3 20.4 1 184
You can follow up by breaking the long data set into a list of 400 data sets.
est_long <- parlmice(nhanes, cluster.seed = 123, print = FALSE, n.core = 4, n.imp.core = 100) %>%
mice::complete("long")%>%
group_by(.imp) %>%
nest()
head(est_long)
# A tibble: 6 x 2
# Groups: .imp [6]
.imp data
<int> <list>
1 1 <tibble[,5] [25 × 5]>
2 2 <tibble[,5] [25 × 5]>
3 3 <tibble[,5] [25 × 5]>
4 4 <tibble[,5] [25 × 5]>
5 5 <tibble[,5] [25 × 5]>
6 6 <tibble[,5] [25 × 5]>
Upvotes: 0
Reputation: 4873
If you noticed when you use write.mice.imputation
, its default values are to save your imputed data in various types of files (csv, spss, dat, Rdata).
We can create a sample data: set.seed(1) df <- data.frame(group = sample(c(1:5, NA), replace = TRUE, size = 10), val = sample(c(10:15, NA), replace = TRUE, size = 10))
Load and impute our data:
require(mice)
require(miceadds)
imp1 <- mice::mice(df, m=3, maxit=5 )
Write our results:
write.mice.imputation(mi.res=imp1, name="mice_imp1",
include.varnames=TRUE,
long=TRUE, mids2spss=TRUE,
spss.dec=",", dattype=NULL)
Now, we can load what ever file type is comfortable for you. For example, dat
file:
oldData <- read.table("mice_imp1/mice_imp1__LONG.dat")
Upvotes: 0