Learn R Programming

mitml (version 0.3-4)

with.mitml.list: Evaluate an expression in a list of imputed data set

Description

The functions with and within evaluate R expressions in a list of multiply imputed data sets.

Usage

"with"(data, expr, ...) "within"(data, expr, ...)

Arguments

data
A list of imputed data sets with class mitml.list as produced by mitmlComplete.
expr
An R expression to be evaluated for each data set.
...
Not being used.

Value

with: Returns the evaluated expression from each data set as a list (class mitml.result. This is useful for fitting statistical models to multiply imputed data. The list of fitted models can be analyzed using testEstimates, testModels, testConstraints, or anova.within: Evaluates the R expression for each data set and returns the altered data sets as a list mitml.list. This is useful for manipulating the data prior to data analysis (e.g., centering, calculating cluster means, etc.).

Details

The two functions are defined as with and within methods for objects of class mitml.list. Both with and within evaluate an R expression in each of the imputed data sets. However, the two functions return different values (see below).

See Also

mitmlComplete, anova.mitml.result, testEstimates, testModels, testConstraints

Examples

Run this code
data(studentratings)

fml <- ReadDis + SES ~ ReadAchiev + (1|ID)
imp <- panImpute(studentratings, formula=fml, n.burn=1000, n.iter=100, m=5)

implist <- mitmlComplete(imp, print=1:5)

# * Example 1: data manipulation

# calculate and save cluster means
new1.implist <- within(implist, Means.ReadAchiev <- clusterMeans(ReadAchiev, ID))

# calculate interaction terms
new2.implist <- within(implist, int.SesCa <- SES*CognAbility )

# * Example 2: fitting statistical models

# fit regression model
fit.lm <- with(implist, lm(ReadAchiev ~ ReadDis))

# fit multilevel model using lme4
require(lme4)
fit.lmer <- with(implist, lmer(ReadAchiev ~ ReadDis + (1|ID)))

# * Example 3: manual extraction of variance estimates
require(lme4)
fit.lmer <- with(implist, lmer(SES ~ (1|ID)))

# extract level-1 and level-2 variances
var.l1 <- sapply(fit.lmer, function(z) attr(VarCorr(z),"sc")^2)
var.l2 <- sapply(fit.lmer, function(z) VarCorr(z)$ID[1,1])

# calculate final estimate of the intraclass correlation
ICC <- mean( var.l2 / (var.l2+var.l1) )

Run the code above in your browser using DataLab