if (FALSE) {
#############################################################################
# EXAMPLE 1: Linear interaction example from Enders et al. (2014)
#############################################################################
# load packages
library(mdmb)
library(mice)
library(mitools)
library(sandwich)
#--- attach example dataset (Enders et al., 2014) from mdmb package
data( data.mb03, package="mdmb")
dat <- data.mb03
#--- center data which speeds convergence of Bayesian estimation
#--- of the imputation model
for (vv in 1:3){
M_vv <- mean( dat[,vv], na.rm=TRUE )
dat[,vv] <- dat[,vv] - M_vv
}
#--- generate initial imputed values withj mice package
imp <- mice::mice( dat, m=, maxit=20 )
data_init <- mice::complete(imp, action=1)
#--- define number of iterations and number of imputed datasets
iter <- 50000; burnin <- 5000
Nimp <- 100
#******* imputation model M3 with quadratic effects
# model for dependent variable
dep <- list("model"="linreg", "formula"=y ~ x*z + I(x^2) + I(z^2) )
# covariate models
ind_x <- list( "model"="linreg", "formula"=x ~ z + I(z^2) )
ind_z <- list( "model"="linreg", "formula"=z ~ 1 )
ind <- list( x=ind_x, z=ind_z)
#generate imputations
imp <- mdmb::frm_fb(dat=dat, dep=dep, ind=ind, burnin=burnin, iter=iter,
data_init=data_init, Nimp=Nimp)
#--- create list of multiply imputed datasets
datlist <- mdmb::frm2datlist(imp)
#-------------------------------
#--- analyze imputed datasets with mice package
# convert into object of class mids
imp2 <- miceadds::datlist2mids(datlist)
# estimate linear model on multiply imputed datasets
mod1 <- with(imp2, stats::lm( y ~ x*z ) )
summary( mice::pool(mod1) )
#-------------------------------
#--- analyze imputed datasets using sandwich standard errors
results <- list()
variances <- list()
Nimp <- length(datlist)
for (ii in 1:Nimp){
mod_ii <- stats::lm( y ~ x*z, data=datlist[[ii]] )
variances[[ii]] <- sandwich::vcovHC(mod_ii)
results[[ii]] <- coef(mod_ii)
}
mod2 <- mitools::MIcombine(results=results,variances=variances,df.complete=69)
summary(mod2)
}
Run the code above in your browser using DataLab