mice.impute.2lonly.function: Imputation at Level 2 (in 'miceadds')

Description Usage Arguments Value Author(s) See Also Examples

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

1
mice.impute.2lonly.function(y, ry, x, type, imputationFunction, ...)	

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.

type

Group identifier must be specified by '-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.

...

Other named arguments.

Value

A vector of length nmis with imputations.

Author(s)

Alexander Robitzsch

See Also

See mice::mice.impute.2lonly.norm and the mice::mice.impute.2lonly.pmm function.

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
## Not run: 
#############################################################################
# EXAMPLE 1: Imputation of level 2 variables
#############################################################################

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

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( rnorm( G , sd = sqrt( rho ) ) , each=n ) + rnorm(G*n , sd = sqrt( 1 - rho ))
w <- rep( round( rnorm(G ) , 2 ) , each=n )
v <- rep( round( 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 + rnorm( G*n , sd = sqrt( 1 - rho.miss ) ) < qnorm( missrate ) , 
		"y" ] <- NA
dfr[ rep( rnorm(G) , each=n ) < qnorm( missrate ) , "w" ] <- NA
dfr[ rep( rnorm(G) , each=n ) < qnorm( missrate ) , "v" ] <- NA

#....
# empty mice imputation
imp0 <- mice( as.matrix(dfr)  , maxit=0 )
predM <- imp0$predictorMatrix
impM <- imp0$method

#...
# multilevel imputation 
predM1 <- predM
predM1[c("w","y","v"),"group"] <- -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" )

# do imputation
imp1 <- mice::mice( as.matrix(dfr) , m = 1 , predictorMatrix = predM1 ,
           imputationMethod = impM1 , maxit = 5, 
           imputationFunction = imputationFunction )

## End(Not run)


Search within the miceadds package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.