This document demonstrates how to perform passive multiple imputation in a fictive simulated dataset with five questionnaries each containing 10 items. In this document the following packages are used: mice, mitools and dmo.

library(mice)
library(mitools)
library(dmo)

Generate simulated data

Data are generated for 100 subjects. Each subjects has observations for 5 questionnaires and 3 covariates.

#generate questionnaire data
k<- c(10,10,10,10,10)
nq <- 5
x <- gen_qdata(n=100, nq=nq, k= k)

#generate covariate data
cov <- MASS::mvrnorm(n=100, mu=c(5,5,5), Sigma=matrix(c(10,1,1,1,10,1,1,1,10),3,3))
colnames(cov) <- c("cov1", "cov2", "cov3")

x <- data.frame(x,cov)

Generate missing values

Missing values are generated with the MAR mechanism for 25% of the subjects with a random pattern. Missings are only generated in the item scores (not in the covariates).

alpha <- 0.25
pattern <- matrix(c(sample(c(0,1), size=2*sum(k),replace = TRUE)),nrow=2)
pattern <- cbind(pattern, matrix(c(1,1,1,1,1,1),nrow=2))
f <- c(0.5,0.5)
g <- c(4,4)
x <- MAR(x,alpha,pattern,f,g)
colnames(x) <- c(paste0("I",1:sum(k)),c("cov1", "cov2", "cov3") )

Calculate total scores for questionnaires

ts <- dmo::calculate_ts(x, nq=5,k= c(10,10,10,10,10))
data <- data.frame(x,ts)

Passive multiple imputation

set up imputation model

Methods

First the methods are adapted for the questionnaire total scores.

ini <- mice(data, max=0, print=FALSE)
meth <- ini$meth #for each questionnaire adapt imputation method (meth): make function to calculate TS between iterations. Example TS1: #meth["TS1"] <- "~I(I1+I2+I3+I4+I5+I6+I7+I8+I9+I10)" - loop below automates for simulation data nq <- 5 k <- c(10,10,10,10,10) for(q in seq_along(1:nq)){ meth[paste0("TS",q)] <- paste0("~I(", paste(paste0("I",1:k[q]), collapse="+"), ")") } meth ## I1 I2 ## "pmm" "pmm" ## I3 I4 ## "pmm" "pmm" ## I5 I6 ## "" "" ## I7 I8 ## "pmm" "pmm" ## I9 I10 ## "pmm" "pmm" ## I11 I12 ## "pmm" "pmm" ## I13 I14 ## "pmm" "pmm" ## I15 I16 ## "" "" ## I17 I18 ## "" "" ## I19 I20 ## "pmm" "" ## I21 I22 ## "pmm" "" ## I23 I24 ## "pmm" "pmm" ## I25 I26 ## "pmm" "pmm" ## I27 I28 ## "pmm" "pmm" ## I29 I30 ## "pmm" "pmm" ## I31 I32 ## "pmm" "pmm" ## I33 I34 ## "pmm" "" ## I35 I36 ## "pmm" "pmm" ## I37 I38 ## "pmm" "" ## I39 I40 ## "pmm" "pmm" ## I41 I42 ## "" "" ## I43 I44 ## "" "pmm" ## I45 I46 ## "" "pmm" ## I47 I48 ## "pmm" "pmm" ## I49 I50 ## "" "pmm" ## cov1 cov2 ## "" "" ## cov3 TS1 ## "" "~I(I1+I2+I3+I4+I5+I6+I7+I8+I9+I10)" ## TS2 TS3 ## "~I(I1+I2+I3+I4+I5+I6+I7+I8+I9+I10)" "~I(I1+I2+I3+I4+I5+I6+I7+I8+I9+I10)" ## TS4 TS5 ## "~I(I1+I2+I3+I4+I5+I6+I7+I8+I9+I10)" "~I(I1+I2+I3+I4+I5+I6+I7+I8+I9+I10)" Predictor matrix The the predictor matrix is adapted such that items are imputed by items own questionnaire and other TSs. In predictor matrix: target variable mi is in row and column variables are predictors pred <- ini$predictorMatrix
pred[paste0("I", 1:10),] <- 0
pred[paste0("I", 1:10),paste0("I", 1:10)] <- 1
pred[paste0("I", 1:10),c("TS2","TS3","TS4","TS5")] <- 1
pred[paste0("I", 11:20),] <- 0
pred[paste0("I", 11:20),paste0("I", 11:20)] <- 1
pred[paste0("I", 11:20),c("TS1","TS3","TS4","TS5")] <- 1
pred[paste0("I", 21:30),] <- 0
pred[paste0("I", 21:30),paste0("I", 21:30)] <- 1
pred[paste0("I", 21:30),c("TS1","TS2","TS4","TS5")] <- 1
pred[paste0("I", 31:40),] <- 0
pred[paste0("I", 31:40),paste0("I", 31:40)] <- 1
pred[paste0("I", 31:40),c("TS1","TS2","TS3","TS5")] <- 1
pred[paste0("I", 41:50),] <- 0
pred[paste0("I", 41:50),paste0("I", 41:50)] <- 1
pred[paste0("I", 41:50),c("TS1","TS2","TS3","TS4")] <- 1
pred[,colnames(cov)] <- 1 #covariates as predictors for all items
pred <- pred*ini\$predictorMatrix
pred
##      I1 I2 I3 I4 I5 I6 I7 I8 I9 I10 I11 I12 I13 I14 I15 I16 I17 I18 I19
## I1    0  1  1  1  1  1  1  1  1   0   0   0   0   0   0   0   0   0   0
## I2    1  0  1  1  1  1  1  1  1   1   0   0   0   0   0   0   0   0   0
## I3    1  1  0  1  1  1  1  1  1   1   0   0   0   0   0   0   0   0   0
## I4    1  1  1  0  1  1  1  1  1   1   0   0   0   0   0   0   0   0   0
## I5    1  1  1  1  0  1  1  1  1   1   0   0   0   0   0   0   0   0   0
## I6    1  1  1  1  1  0  1  1  1   1   0   0   0   0   0   0   0   0   0
## I7    1  1  1  1  1  1  0  1  1   1   0   0   0   0   0   0   0   0   0
## I8    1  1  1  1  1  1  1  0  1   1   0   0   0   0   0   0   0   0   0
## I9    1  1  1  1  1  1  1  1  0   1   0   0   0   0   0   0   0   0   0
## I10   1  1  1  1  1  1  1  1  1   0   0   0   0   0   0   0   0   0   0
## I11   0  0  0  0  0  0  0  0  0   0   0   1   1   1   1   1   1   1   1
## I12   0  0  0  0  0  0  0  0  0   0   1   0   1   1   1   1   1   1   1
## I13   0  0  0  0  0  0  0  0  0   0   1   1   0   1   1   1   1   1   1
## I14   0  0  0  0  0  0  0  0  0   0   1   1   1   0   1   1   1   1   1
## I15   0  0  0  0  0  0  0  0  0   0   1   1   1   1   0   1   1   1   1
## I16   0  0  0  0  0  0  0  0  0   0   1   1   1   1   1   0   1   1   1
## I17   0  0  0  0  0  0  0  0  0   0   1   1   1   1   1   1   0   1   1
## I18   0  0  0  0  0  0  0  0  0   0   1   1   1   1   1   1   1   0   1
## I19   0  0  0  0  0  0  0  0  0   0   1   1   1   1   1   1   1   1   0
## I20   0  0  0  0  0  0  0  0  0   0   1   1   1   1   1   1   1   1   1
## I21   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I22   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I23   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I24   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I25   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I26   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I27   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I28   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I29   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I30   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I31   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I32   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I33   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I34   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I35   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I36   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I37   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I38   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I39   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I40   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I41   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I42   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I43   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I44   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I45   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I46   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I47   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I48   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I49   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## I50   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## cov1  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## cov2  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## cov3  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## TS1   1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## TS2   1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## TS3   1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## TS4   1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
## TS5   1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1
##      I20 I21 I22 I23 I24 I25 I26 I27 I28 I29 I30 I31 I32 I33 I34 I35 I36
## I1     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I2     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I3     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I4     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I5     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I6     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I7     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I8     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I9     0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I10    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I11    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I12    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I13    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I14    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I15    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I16    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I17    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I18    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I19    1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I20    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I21    0   0   1   1   1   1   1   1   1   1   1   0   0   0   0   0   0
## I22    0   1   0   1   1   1   1   1   1   1   1   0   0   0   0   0   0
## I23    0   1   1   0   1   1   1   1   1   1   1   0   0   0   0   0   0
## I24    0   1   1   1   0   1   1   1   1   1   1   0   0   0   0   0   0
## I25    0   1   1   1   1   0   1   1   1   1   1   0   0   0   0   0   0
## I26    0   1   1   1   1   1   0   1   1   1   1   0   0   0   0   0   0
## I27    0   1   1   1   1   1   1   0   1   1   1   0   0   0   0   0   0
## I28    0   1   1   1   1   1   1   1   0   1   1   0   0   0   0   0   0
## I29    0   1   1   1   1   1   1   1   1   0   1   0   0   0   0   0   0
## I30    0   1   1   1   1   1   1   1   1   1   0   0   0   0   0   0   0
## I31    0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1
## I32    0   0   0   0   0   0   0   0   0   0   0   1   0   1   1   1   1
## I33    0   0   0   0   0   0   0   0   0   0   0   1   1   0   1   1   1
## I34    0   0   0   0   0   0   0   0   0   0   0   1   1   1   0   1   1
## I35    0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   0   1
## I36    0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   0
## I37    0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   1
## I38    0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   1
## I39    0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   1
## I40    0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   1
## I41    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I42    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I43    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I44    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I45    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I46    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I47    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I48    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I49    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## I50    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
## cov1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## cov2   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## cov3   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## TS1    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## TS2    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## TS3    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## TS4    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
## TS5    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
##      I37 I38 I39 I40 I41 I42 I43 I44 I45 I46 I47 I48 I49 I50 cov1 cov2
## I1     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I2     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I3     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I4     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I5     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I6     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I7     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I8     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I9     0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I10    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I11    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I12    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I13    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I14    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I15    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I16    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I17    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I18    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I19    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I20    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I21    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I22    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I23    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I24    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I25    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I26    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I27    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I28    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I29    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I30    0   0   0   0   0   0   0   0   0   0   0   0   0   0    1    1
## I31    1   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I32    1   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I33    1   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I34    1   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I35    1   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I36    1   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I37    0   1   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I38    1   0   1   1   0   0   0   0   0   0   0   0   0   0    1    1
## I39    1   1   0   1   0   0   0   0   0   0   0   0   0   0    1    1
## I40    1   1   1   0   0   0   0   0   0   0   0   0   0   0    1    1
## I41    0   0   0   0   0   1   1   1   1   1   1   1   1   1    1    1
## I42    0   0   0   0   1   0   1   1   1   1   1   1   1   1    1    1
## I43    0   0   0   0   1   1   0   1   1   1   1   1   1   1    1    1
## I44    0   0   0   0   1   1   1   0   1   1   1   1   1   1    1    1
## I45    0   0   0   0   1   1   1   1   0   1   1   1   1   1    1    1
## I46    0   0   0   0   1   1   1   1   1   0   1   1   1   1    1    1
## I47    0   0   0   0   1   1   1   1   1   1   0   1   1   1    1    1
## I48    0   0   0   0   1   1   1   1   1   1   1   0   1   1    1    1
## I49    0   0   0   0   1   1   1   1   1   1   1   1   0   1    1    1
## I50    0   0   0   0   1   1   1   1   1   1   1   1   1   0    1    1
## cov1   1   1   1   1   1   1   1   1   1   1   1   1   1   1    0    1
## cov2   1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    0
## cov3   1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    1
## TS1    1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    1
## TS2    1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    1
## TS3    1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    1
## TS4    1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    1
## TS5    1   1   1   1   1   1   1   1   1   1   1   1   1   1    1    1
##      cov3 TS1 TS2 TS3 TS4 TS5
## I1      1   0   1   1   1   1
## I2      1   0   1   1   1   1
## I3      1   0   1   1   1   1
## I4      1   0   1   1   1   1
## I5      1   0   1   1   1   1
## I6      1   0   1   1   1   1
## I7      1   0   1   1   1   1
## I8      1   0   1   1   1   1
## I9      1   0   1   1   1   1
## I10     1   0   1   1   1   1
## I11     1   1   0   1   1   1
## I12     1   1   0   1   1   1
## I13     1   1   0   1   1   1
## I14     1   1   0   1   1   1
## I15     1   1   0   1   1   1
## I16     1   1   0   1   1   1
## I17     1   1   0   1   1   1
## I18     1   1   0   1   1   1
## I19     1   1   0   1   1   1
## I20     1   1   0   1   1   1
## I21     1   1   1   0   1   1
## I22     1   1   1   0   1   1
## I23     1   1   1   0   1   1
## I24     1   1   1   0   1   1
## I25     1   1   1   0   1   1
## I26     1   1   1   0   1   1
## I27     1   1   1   0   1   1
## I28     1   1   1   0   1   1
## I29     1   1   1   0   1   1
## I30     1   1   1   0   1   1
## I31     1   1   1   1   0   1
## I32     1   1   1   1   0   1
## I33     1   1   1   1   0   1
## I34     1   1   1   1   0   1
## I35     1   1   1   1   0   1
## I36     1   1   1   1   0   1
## I37     1   1   1   1   0   1
## I38     1   1   1   1   0   1
## I39     1   1   1   1   0   1
## I40     1   1   1   1   0   1
## I41     1   1   1   1   1   0
## I42     1   1   1   1   1   0
## I43     1   1   1   1   1   0
## I44     1   1   1   1   1   0
## I45     1   1   1   1   1   0
## I46     1   1   1   1   1   0
## I47     1   1   1   1   1   0
## I48     1   1   1   1   1   0
## I49     1   1   1   1   1   0
## I50     1   1   1   1   1   0
## cov1    1   1   1   1   1   1
## cov2    1   1   1   1   1   1
## cov3    0   1   1   1   1   1
## TS1     1   0   1   1   1   1
## TS2     1   1   0   1   1   1
## TS3     1   1   1   0   1   1
## TS4     1   1   1   1   0   1
## TS5     1   1   1   1   1   0

Apply final imputations

imp1 for items and total scores with passive MI; imp2 total scores only form other ts and covariates

imp1 <- mice(data, m=15, meth=meth,pred=pred, maxit=10, seed=12354, print=FALSE)
tsdata <- data.frame(ts, cov)
imp2 <- mice(tsdata, 15,maxit=10,seed=61085,print=FALSE)

Check iteration plots

plot(imp1, paste0("TS",1:5))

plot(imp2, paste0("TS",1:5))

Combine imputations

combine imputations and select (per questionnaire) the ts from imp1 for persons with <70% of items missing; and the ts from imp2 for persons with >70% of items missing (in a questionnaire).

First make indicators for each q if missing <70%:

calculate_i <- function(x,nq,k){
ind <- matrix(0,nrow=nrow(x), ncol=(nq))

for (q in 1:(nq)){
ind[,q] <- apply(x[,(((q*k[q])-k[q])+1):(q*k[q])],1,function(x) {sum(is.na(x))/length(x)})
}
colnames(ind) <- paste("TS",1:nq,sep="")
ind <- ifelse(ind <0.7,1,0)
ind
}
indicator <- calculate_i(x=data, nq=5, k=c(10,10,10,10,10))

Then select correct TS from each imputation.

implist <- lapply(1:15, function(x){
x1 <- complete (imp1, x)
x2 <- complete(imp2, x)
tsimp <- lapply(1:nq, function(x){
ifelse(indicator[,x]==1, x1[,paste0("TS",x)], x2[,paste0("TS",x)] )
})
tsimp <- data.frame(tsimp)
colnames(tsimp) <- paste0("impTS",1:nq)
data.frame(x1,tsimp)
})

Analyze and pool

Save data in an imputation list for analysis and test relation between TS1 and TS3

impdata <- imputationList (implist)
fit <- with (impdata, lm(impTS1~impTS3 ))
summary(pool (fit))
##               estimate  std.error  statistic       df      p.value
## (Intercept) -0.7488062 0.85579068 -0.8749876 95.08227 3.837855e-01
## impTS3       0.7137674 0.08104528  8.8070196 94.06097 5.817569e-14