R: Evaluate an expression in a list of imputed data sets

with.mitml.list {mitml} | R Documentation

## Evaluate an expression in a list of imputed data sets

### Description

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

### Usage

    ## S3 method for class 'mitml.list'
    with(data, expr, ...)
    ## S3 method for class 'mitml.list'
    within(data, expr, ignore=NULL, ...)

### 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.
`ignore` | A character vector denoting objects not to be saved.
`...` | Not being used.

### 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: `with` returns the evaluated expression, whereas `within` returns the resuling data sets. The `ignore` argument may be used to declare objects that are not to be saved within `within`.

### 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.).

### Author(s)

Simon Grund

### See Also

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

### Examples

    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))

    # 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 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) )

[Package mitml version 0.3-5 Index]