Search
  • Burak AYDIN

Mplus multiple imputation via R


This is not a self standing code. Still has potential to be helpful

Assume you have 20 variables with missing values indicated with -99. Lets say first 10 are continuous and others are categorical. The mplus code for 20 multiple imputation would be this;

TITLE: based on Mplus website DATA: FILE = yourdata.csv; VARIABLE: ! the following are all the variables in the data set: NAMES = X1-X20 idnumber; ! the following variables will be used to create the ! imputed data sets: USEVARIABLES = X1-X20; idvariable = idnumber;

MISSING = ALL (-99);

DATA IMPUTATION:

IMPUTE = x1-x10 x10-x20 (c); NDATASETS = 20; SAVE = imputedDATA*.txt; ANALYSIS: TYPE = BASIC; OUTPUT: TECH8;

When you run this code Mplus will output 20 data sets without missing data and a simple list that has imputed data set names, in this case

imputedDATA1.txt imputedDATA2.txt ...

imputedDATA20.txt

Lets name this .inp file as multipleIMP20.inp. In your R syntax

system(paste("mplus.exe","multipleIMP20.inp",sep=" "), wait = TRUE, show.output.on.console = F)

mplusOUTcombiner=function(NofIMP=20){

implistT0=read.table(file="imputedDATAlist.txt")

MPoutput=list()

for(i in 1:NofIMP){

MPoutput[[i]]=as.data.frame(read.table(as.character(implistT0[i,1])))

}

return(MPoutput)}

mpout=mplusOUTcombiner(20)

#give back their name

namesback=c(paste("X",1:nvar,sep=""),"idnumber")

mpout=lapply(mpout, setNames, namesback)

#sort by idnumber

mpout=lapply(mpout, function(x) x[order(x$idnumber),])


0 views

© 2015-20 by Burak AYDIN