if (FALSE) {
library(foreign)
study5 <- read.spss("reanalysis-study-5-mt-fall-08.sav", to.data.frame=TRUE)
ASData5 <- subset(study5, select=c("ppnum", paste("as", 1:33, sep="")))
prepareMplusData(ASData5, "study5.dat")
# basic example
test01 <- prepareMplusData(mtcars, "test01.dat")
# see that syntax was stored
test01
# example when there is a factor and logical
tmpd <- mtcars
tmpd$cyl <- factor(tmpd$cyl)
tmpd$am <- as.logical(tmpd$am)
prepareMplusData(tmpd, "test_type.dat")
rm(tmpd)
# by default, if re-run, data is re-written, with a note
test01b <- prepareMplusData(mtcars, "test01.dat")
# if we turn on hashing in the filename the first time,
# we can avoid overwriting notes the second time
test01c <- prepareMplusData(mtcars, "test01c.dat", hashfilename=TRUE)
# now that the filename was hashed in test01c, future calls do not re-write data
# as long as the hash matches
test01d <- prepareMplusData(mtcars, "test01c.dat",
writeData = "ifmissing", hashfilename=TRUE)
# now that the filename was hashed in test01c, future calls do not re-write data
# as long as the hash matches
test01db <- prepareMplusData(mtcars, "test01d.dat",
writeData = "ifmissing", hashfilename=TRUE)
# however, if the data change, then the file is re-written
test01e <- prepareMplusData(iris, "test01c.dat",
writeData = "ifmissing", hashfilename=TRUE)
# tests for keeping and dropping variables
prepareMplusData(mtcars, "test02.dat", keepCols = c("mpg", "hp"))
prepareMplusData(mtcars, "test03.dat", keepCols = c(1, 2))
prepareMplusData(mtcars, "test04.dat",
keepCols = c(TRUE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
prepareMplusData(mtcars, "test05.dat", dropCols = c("mpg", "hp"))
prepareMplusData(mtcars, "test06.dat", dropCols = c(1, 2))
prepareMplusData(mtcars, "test07.dat",
dropCols = c(TRUE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))
# interactive (test08.dat)
prepareMplusData(mtcars, interactive=TRUE)
# write syntax to input file, not stdout
prepareMplusData(mtcars, "test09.dat", inpfile=TRUE)
# write syntax to alternate input file, not stdout
prepareMplusData(mtcars, "test10.dat", inpfile="test10alt.inp")
# should be error, no file
prepareMplusData(mtcars, interactive=FALSE)
# new warnings if it is going to overwrite files
# (the default to be consistent with prior behavior)
prepareMplusData(mtcars, "test10.dat")
# new warnings if it is going to overwrite files
# (the default to be consistent with prior behavior)
prepareMplusData(mtcars, "test11.dat", inpfile="test10alt.inp")
# new errors if files exist and overwrite=FALSE
prepareMplusData(mtcars, "test10.dat",
inpfile="test10alt.inp", overwrite=FALSE)
# can write multiply imputed data too
# here are three "imputed" datasets
idat <- list(
data.frame(mpg = mtcars$mpg, hp = c(100, mtcars$hp[-1])),
data.frame(mpg = mtcars$mpg, hp = c(110, mtcars$hp[-1])),
data.frame(mpg = mtcars$mpg, hp = c(120, mtcars$hp[-1])))
# if we turn on hashing in the filename the first time,
# we can avoid overwriting notes the second time
testimp1 <- prepareMplusData(idat, "testi1.dat",
writeData = "ifmissing", hashfilename=TRUE,
imputed = TRUE)
# now that the filename was hashed, future calls do not re-write data
# as long as all the hashes match
testimp2 <- prepareMplusData(idat, "testi2.dat",
writeData = "ifmissing", hashfilename=TRUE,
imputed = TRUE)
# in fact, the number of imputations can decrease
# and they still will not be re-written
testimp3 <- prepareMplusData(idat[-3], "testi3.dat",
writeData = "ifmissing", hashfilename=TRUE,
imputed = TRUE)
# however, if the data changes, then all are re-written
# note that it warns for the two files that already exist
# as these two are overwritten
idat2 <- list(
data.frame(mpg = mtcars$mpg, hp = c(100, mtcars$hp[-1])),
data.frame(mpg = mtcars$mpg, hp = c(109, mtcars$hp[-1])),
data.frame(mpg = mtcars$mpg, hp = c(120, mtcars$hp[-1])))
testimp4 <- prepareMplusData(idat2, "testi4.dat",
writeData = "ifmissing", hashfilename=TRUE,
imputed = TRUE)
}
Run the code above in your browser using DataLab