# simulate fake data
set.seed(100)
n <- 100
u1 <- rbinom(n, 1, .5)
v1 <- log(rnorm(n, 5, 1))
x1 <- u1*exp(v1)
u2 <- rbinom(n, 1, .5)
v2 <- log(rnorm(n, 5, 1))
x2 <- u2*exp(v2)
x3 <- rbinom(n, 1, prob=0.45)
x4 <- ordered(rep(seq(1, 5),100)[sample(1:n, n)])
x5 <- rep(letters[1:10],10)[sample(1:n, n)]
x6 <- trunc(runif(n, 1, 10))
x7 <- rnorm(n)
x8 <- factor(rep(seq(1,10),10)[sample(1:n, n)])
x9 <- runif(n, 0.1, .99)
x10 <- rpois(n, 4)
y <- x1 + x2 + x7 + x9 + rnorm(n)
fakedata <- cbind.data.frame(y, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
# randomly create missing values
dat <- mi:::.create.missing(fakedata, pct.mis=30)
# get information matrix of the data
inf <- mi.info(dat)
# update the variable type of a specific variable to mi.info
inf <- update(inf, "type", list(x10="count"))
# run the imputation without data transformation
#IMP <- mi(dat, info=inf, check.coef.convergence=TRUE,
# add.noise=noise.control(post.run.iter=10))
# run the imputation with data transformation
dat.transformed <- mi.preprocess(dat, inf)
#IMP <- mi(dat.transformed, n.iter=6, check.coef.convergence=TRUE,
# add.noise=noise.control(post.run.iter=6))
IMP <- mi(dat.transformed, n.iter=6, add.noise=FALSE)
# no noise
# IMP <- mi(dat, info=inf, n.iter=6, add.noise=FALSE) ## NOT RUN
# pick up where you left off
# IMP <- mi(IMP, n.iter = 6)
## this is the suggested (defautl) way of running mi
# IMP <- mi(dat, info=inf) ## NOT RUN
# convergence checking
converged(IMP, check = "data") ## You should get FALSE here because only n.iter is small
#converged(IMP, check = "coefs")
IMP.bugs1 <- bugs.mi(IMP, check = "data") ## BUGS object to look at the R hat statistics
#IMP.bugs2 <- bugs.mi(IMP, check = "coefs") ## BUGS object to look at the R hat statistics
plot(IMP.bugs1) ## visually check R.hat
# visually check the imputation
plot(IMP)
Run the code above in your browser using DataLab