if (FALSE) {
#############################################################################
# EXAMPLE 1: PLS imputation method for internet data
#############################################################################
data(data.internet)
dat <- data.internet
# specify predictor matrix
predictorMatrix <- matrix( 1, ncol(dat), ncol(dat) )
rownames(predictorMatrix) <- colnames(predictorMatrix) <- colnames(dat)
diag( predictorMatrix) <- 0
# use PLS imputation method for all variables
impMethod <- rep( "pls", ncol(dat) )
names(impMethod) <- colnames(dat)
# define predictors for interactions (entries with type 4 in predictorMatrix)
predictorMatrix[c("IN1","IN15","IN16"),c("IN1","IN3","IN10","IN13")] <- 4
# define predictors which should appear as linear and quadratic terms (type 5)
predictorMatrix[c("IN1","IN8","IN9","IN10","IN11"),c("IN1","IN2","IN7","IN5")] <- 5
# use 9 PLS factors for all variables
pls.facs <- as.list( rep( 9, length(impMethod) ) )
names(pls.facs) <- names(impMethod)
pls.facs$IN1 <- 15 # use 15 PLS factors for variable IN1
# choose norm or pmm imputation method
pls.impMethod <- as.list( rep("norm", length(impMethod) ) )
names(pls.impMethod) <- names(impMethod)
pls.impMethod[ c("IN1","IN6")] <- "pmm"
# some arguments for imputation method
pls.impMethodArgs <- list( "IN1"=list( "donors"=10 ),
"IN2"=list( "ridge2"=1E-4 ) )
# Model 1: Three parallel chains
imp1 <- mice::mice(data=dat, method=impMethod,
m=3, maxit=5, predictorMatrix=predictorMatrix,
pls.facs=pls.facs, # number of PLS factors
pls.impMethod=pls.impMethod, # Imputation Method in PLS imputation
pls.impMethodArgs=pls.impMethodArgs, # arguments for imputation method
pls.print.progress=TRUE, ls.meth="ridge" )
summary(imp1)
# Model 2: One long chain
imp2 <- miceadds::mice.1chain(data=dat, method=impMethod,
burnin=10, iter=21, Nimp=3, predictorMatrix=predictorMatrix,
pls.facs=pls.facs, pls.impMethod=pls.impMethod,
pls.impMethodArgs=pls.impMethodArgs, ls.meth="ridge" )
summary(imp2)
# Model 3: inclusion of additional derived variables
# define derived variables for IN1
derived_vars <- list( "IN1"=~I( ifelse( IN2>IN3, IN2, IN3 ) ) + I( sin(IN2) ) )
imp3 <- miceadds::mice.1chain(data=dat, method=impMethod, derived_vars=derived_vars,
burnin=10, iter=21, Nimp=3, predictorMatrix=predictorMatrix,
pls.facs=pls.facs, pls.impMethod=pls.impMethod,
pls.impMethodArgs=pls.impMethodArgs, ls.meth="ridge" )
summary(imp3)
#*** example for using imputation function at the level of a variable
# extract first imputed dataset
imp1 <- mice::complete(imp1, action=1)
data_imp1[ is.na(dat$IN1), "IN1" ] <- NA
# define variables
y <- data_imp1$IN1
x <- data_imp1[, -1 ]
ry <- ! is.na(y)
cn <- colnames(dat)
p <- ncol(dat)
type <- rep(1,p)
names(type) <- cn
type["IN1"] <- 0
# imputation of variable 'IN1'
imp0 <- miceadds::mice.impute.pls(y=y, x=x, ry=ry, type=type, pls.facs=10, pls.impMethod="norm",
ls.meth="ridge", extract_data=FALSE )
}
Run the code above in your browser using DataLab