Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mice.impute.2lonly.function

Imputation at Level 2 (in miceadds)


Description

The imputation method mice.impute.2lonly.function is a general imputation function for level 2 imputation which allow any defined imputation function at level 1 in mice.

Usage

mice.impute.2lonly.function(y, ry, x, wy=NULL, type, imputationFunction,
     cluster_var, ...)

Arguments

y

Incomplete data vector of length n

ry

Vector of missing data pattern (FALSE=missing, TRUE=observed)

x

Matrix (n x p) of complete covariates. Only numeric variables are permitted for usage of this function.

wy

Logical vector of length(y) indicating at which positions imputations should be conducted.

type

Cluster identifier can be specified by -2 for aggregation. However, we recommend to use the argument cluster_var for specifying the cluster variable at Level 2. Predictors must be specified by 1.

imputationFunction

Imputation function for mice. Any imputation method which is defined at level 1 can be used for level 2 imputation.

cluster_var

Cluster identifier for Level 2 units

...

Other named arguments.

Value

A vector of length nmis with imputations.

See Also

See also the jomo package (jomo::jomo2) for joint multilevel imputation of level 1 and level 2 variables.

Examples

## Not run: 
#############################################################################
# EXAMPLE 1: Imputation of level 2 variables
#############################################################################

#**** Simulate some data
# x,y ... level 1 variables
# v,w ... level 2 variables

set.seed(987)
G <- 250            # number of groups
n <- 20             # number of persons
beta <- .3          # regression coefficient
rho <- .30          # residual intraclass correlation
rho.miss <- .10     # correlation with missing response
missrate <- .50     # missing proportion
y1 <- rep( stats::rnorm( G, sd=sqrt(rho)), each=n ) + stats::rnorm(G*n, sd=sqrt(1-rho))
w <- rep( round( stats::rnorm(G ), 2 ), each=n )
v <- rep( round( stats::runif( G, 0, 3 ) ), each=n )
x <-  stats::rnorm( G*n )
y <- y1 + beta  * x + .2 * w + .1 * v
dfr0 <- dfr <- data.frame( "group"=rep(1:G, each=n ), "x"=x, "y"=y,
        "w"=w, "v"=v )
dfr[ rho.miss * x + stats::rnorm( G*n, sd=sqrt( 1 - rho.miss ) ) <
                stats::qnorm(missrate), "y" ] <- NA
dfr[ rep( stats::rnorm(G), each=n ) < stats::qnorm(missrate), "w" ] <- NA
dfr[ rep( stats::rnorm(G), each=n ) < stats::qnorm(missrate), "v" ] <- NA

#* initial predictor matrix and imputation methods
predM <- mice::make.predictorMatrix(data=dfr)
impM <- mice::make.method(data=dfr)

#...
# multilevel imputation
predM1 <- predM
predM1[c("w","v","y"),"group"] <- c(0,0,-2)
predM1["y","x"] <- 1        # fixed x effects imputation
impM1 <- impM
impM1[c("y","w","v")] <- c("2l.continuous", "2lonly.function", "2lonly.function" )
# define imputation functions
imputationFunction <- list( "w"="sample", "v"="pmm5" )
# define cluster variable
cluster_var <- list( "w"="group", "v"="group" )

# impute
imp1 <- mice::mice( as.matrix(dfr), m=1, predictorMatrix=predM1, method=impM1, maxit=5,
            imputationFunction=imputationFunction, cluster_var=cluster_var )

## End(Not run)

miceadds

Some Additional Multiple Imputation Functions, Especially for 'mice'

v3.11-6
GPL (>= 2)
Authors
Alexander Robitzsch [aut,cre] (<https://orcid.org/0000-0002-8226-3132>), Simon Grund [aut] (<https://orcid.org/0000-0002-1290-8986>), Thorsten Henke [ctb]
Initial release
2021-01-21 11:48:47

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.