data(studentratings)
fml <- ReadDis + SES ~ ReadAchiev + (1|ID)
imp <- panImpute(studentratings, formula = fml, n.burn = 1000, n.iter = 100, m = 5)
implist <- mitmlComplete(imp)
# * Example 1: data transformation
# calculate and save cluster means
new1.implist <- within(implist, Means.ReadAchiev <- clusterMeans(ReadAchiev, ID))
# center variables, calculate interaction terms, ignore byproducts
new2.implist <- within(implist, {
M.SES <- mean(SES)
M.CognAbility <- mean(CognAbility)
C.SES <- SES - M.SES
C.CognAbility <- CognAbility - M.CognAbility
SES.CognAbility <- C.SES * C.CognAbility
}, ignore = c("M.SES", "M.CognAbility"))
# * Example 2: fitting statistical models
# fit regression model
fit.lm <- with(implist, lm(ReadAchiev ~ ReadDis))
# fit multilevel model with lme4
require(lme4)
fit.lmer <- with(implist, lmer(ReadAchiev ~ ReadDis + (1|ID)))
if (FALSE) {
# fit structural equation model with lavaan (with include.data = TRUE)
require(lavaan)
mod <- "ReadAchiev ~ ReadDis"
fit.sem <- with(implist,
sem(model = mod, cluster = "ID", estimator = "MLR"),
include.data = TRUE)
}
Run the code above in your browser using DataLab