# mice.impute.2l.contextual.pmm: Imputation by Predictive Mean Matching or Normal Linear... In miceadds: Some Additional Multiple Imputation Functions, Especially for 'mice'

## Description

This imputation method imputes a variable using linear regression with predictive mean matching as the imputation method. Including a contextual effects means that an aggregated variable at a cluster level is included as a further covariate.

## Usage

 ```1 2 3 4 5``` ```mice.impute.2l.contextual.pmm(y, ry, x, type, imputationWeights = NULL, interactions = NULL, quadratics = NULL, ...) mice.impute.2l.contextual.norm(y, ry, x, type, ridge = 10^(-5), imputationWeights = NULL, interactions = NULL, quadratics = NULL, ...) ```

## 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. `type` Type of predictor variables. `type=-2` refers to the cluster variable, `type=2` denotes a variable for which also a contextual effect is included and `type=1` denotes all other variables which are included as 'ordinary' predictors. `imputationWeights` Optional vector of sample weights `interactions` Vector of variable names used for creating interactions `quadratics` Vector of variable names used for creating quadratic terms `...` Further arguments to be passed `ridge` Ridge parameter in the diagonal of \bold{X}'\bold{X}

## Value

A vector of length `nmis=sum(!ry)` with imputed values.

## Author(s)

Alexander Robitzsch

For imputations at level 2 variables see `mice::mice.impute.2lonly.norm` and `mice::mice.impute.2lonly.pmm`.
 ``` 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 50 51 52 53 54 55 56 57 58 59 60 61``` ```## Not run: ############################################################################# # EXAMPLE 1: Sequential hierarchical imputation for data.ma05 dataset ############################################################################# data(data.ma05) dat <- data.ma05 # empty imputation imp0 <- mice::mice( dat , m=0 , maxit=0 ) summary(imp0) # define predictor matrix predM <- imp0\$pred # exclude student IDs predM[ , "idstud"] <- 0 # define idclass as the cluster variable (type=-2) predM[ , "idclass" ] <- -2 # initialize with norm method impMethod <- rep( "norm" , length(imp0\$method) ) names(impMethod) <- names( imp0\$method ) impMethod[ c("idstud","idclass")] <- "" #***** # STUDENT LEVEL (Level 1) # Use a random slope model for Dscore and Mscore as the imputation method. # Here, variance homogeneity of residuals is assumed (contrary to # the 2l.norm imputation method in the mice package). impMethod[ c("Dscore" , "Mscore") ] <- "2l.pan" predM[ c("Dscore","Mscore") , "misei" ] <- 2 # random slopes on 'misei' predM[ , "idclass" ] <- -2 # For imputing 'manote' and 'denote' use contextual effects (i.e. cluszer means) # of variables 'misei' and 'migrant' impMethod[ c("denote" , "manote") ] <- "2l.contextual.pmm" predM[ c("denote" , "manote") , c("misei","migrant")] <- 2 # Use no cluster variable 'idclass' for imputation of 'misei' impMethod[ "misei"] <- "norm" predM[ "misei" , "idclass"] <- 0 # use no multilevel imputation model # Variable migrant: contextual effects of Dscore and misei impMethod[ "migrant"] <- "2l.contextual.pmm" predM[ "migrant" , c("Dscore" , "misei" ) ] <- 2 predM[ "migrant" , "idclass" ] <- -2 #**** # CLASS LEVEL (Level 2) # impute 'sprengel' and 'groesse' at the level of classes impMethod[ "sprengel"] <- "2lonly.pmm" impMethod[ "groesse"] <- "2lonly.norm" predM[ c("sprengel","groesse") , "idclass" ] <- -2 # do imputation imp <- mice::mice( dat , predictorMatrix = predM , m = 3 , maxit = 4 , imputationMethod = impMethod , paniter=100) summary(imp) ## End(Not run) ```