#================================================# # # # The R code used in Section 4 (example) # # # #================================================# # load mi library("mi") # load the data used in the example data("CHAIN") # display of missing data patterns missing.pattern.plot(CHAIN, clustered = FALSE) mp.plot(CHAIN, clustered = FALSE) missing.pattern.plot(CHAIN, y.order = TRUE, x.order = TRUE, clustered = FALSE) # identify structural problems in the data and preprocessing info <- mi.info(CHAIN) info CHAIN.new <- mi.preprocess(CHAIN) attr(CHAIN.new, "mi.info") # iterative imputation based on the conditional model IMP <- mi(CHAIN.new) IMP <- mi(IMP) # check the fit of conditional models and imputed values plot(IMP) # check the convergence of the procedure plot(as.bugs.array(IMP@mcmc)) # run longer after mi has converged IMP <- mi(IMP, run.past.convergence = TRUE, n.iter=10) # obtain completed datasets IMP.dat.all <- mi.completed(IMP) # obtain on specific chain of imputed dataset IMP.dat <- mi.data.frame(IMP, m = 1) # write the data into the hard driver write.mi(IMP) # pool the complete case analysis on multiply imputed datasets fit <- lm.mi(h39b.W1 ~ age.W1 + c28.W1 + pcs.W1 + mcs37.W1 + b05.W1 + haartadhere.W1, IMP) display(fit) # or do the pooling in a more felxible way m <- m(IMP) coefs <- sds <- vector("list", m) for(i in 1:m){ dat <- mi.data.frame(IMP, i) fit <- lm(h39b.W1~ c28.W1, data = dat, subset = mcs37.W1==1) coefs[[i]] <- coef(fit) sds[[i]] <- se.coef(fit) } mi.pooled(coefs, sds)