2

I use the mice package to conduct imputation, but it takes a couple of hours to do, so I would really appreciate it, if I could store the resulting mids object and just load that in the future. I'm sorry if this is a really rookie sort of question. But I have tried simply using the base save and load functions, but it doesn't appear to be saving it correctly.

EDIT: I found out that it works if I load plainly. However, if I load into a new object, it won't be loaded as a mids-object. I have added the following minimum reproducable example.

x<-rnorm(10,14,2)
y<-rnorm(10,7,3)
z<-rnorm(10,18,5)
df<-data.frame(x,y,z)

df$x[sample(1:nrow(df), 0.2*nrow(df))] = NA
df$y[sample(1:nrow(df), 0.2*nrow(df))] = NA
df$z[sample(1:nrow(df), 0.2*nrow(df))] = NA

View(df)

imp <- mice(df, pred=quickpred(df))

save(imp, file = "impExample.rda")
rm(imp)
load("impExample.rda")
rm(imp)
impExample <- load("impExample.rda")
BBKim
  • 151
  • 1
  • 8
  • What do you mean when you say that "it doesn't appear to be saving it correctly"? Can you provide some sort of [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) which demonstrates the issue? I would expect save/load to work for most objects. – MrFlick Oct 05 '18 at 15:53
  • 1
    Suppose the result of your imputation is named `imp` then try `save(imp, file = "filename_1.rda")`. You should be able to load this using `load("filename_1.rda")` assuming the file is in your working directory. – Niek Oct 08 '18 at 06:50
  • Thanks for the comments. Please see edit. – BBKim Oct 09 '18 at 08:21

1 Answers1

3

What might work is the command saveRDS and retreive it with readRDS. It will leave the objects un-altered, I use it a lot for all kinds of R-objects. (I am not yet granted to make comments, so this was the only way to send you my very little and simple answer. Hope it does the trick for you!)

Marlein
  • 115
  • 2
  • 8