Hope this will help

```
<-
data2 <- read.csv("Tutorial5_MIQuestion2.csv")
View(data2) # open up data viewer to check dataset looks reasonable
data2 <- Tutorial5_MIQuestion2
#generate QALYS
data2$qol_0mth <- 0
data2$QALY <- ((0.25*(data2$qol_3mth+data2$qol_0mth))+(0.25*(data2$qol_6mth+data2$qol_3mth))+(0.5*(data2$qol_12mth+data2$qol_6mth))+(1*(data2$qol_24mth+data2$qol_12mth)))/2
summary(data2$QALY)
# install.packages("mice") # Install package. Only need to do this once on your machine
library(mice) # load package (needs to be done for every new R session)
md.pattern(data2) # use the md.pattern function in the mice library to look at the missing data
#create data matrix using only the variables to be included in the imputation
# alternatively you can select the variable in the imputation function call, but the notation is messy.
data2_sub <- data2[,c("QALY","sex", "age")]
# Impute the missing values, specifying 20 imputed data sets.
impute <- mice(data2_sub , m=20, seed = 1234)
summary(impute)
plot(impute)
impute80 <- mice.mids(impute, maxit=60, seed = 1234, print=F)
plot(impute80)
#From extra imputations decide to use 80 rather than 20
impute.extra <- mice(data2_sub , m=80, seed = 1234)
# To obtain the mean across the 80 imputations allowing for within and between variability use the following commands
impdat <- complete(impute.extra,action="long",include = FALSE)
pool_mean <- with(impdat, by(impdat, .imp, function(x) c(mean(x$QALY),sd(x$QALY))))
pool_mean
Reduce("+",pool_mean)/length(pool_mean)
#Average imputed data set for bootstrapping
impute_mean <- rep(0, length(complete(impute.extra, 1)))
for(i in 1:80){impute_mean <- impute_mean + complete(impute.extra, i)}
impute_mean <- impute_mean/80
#Bootstrap CI
library(boot) # load the boot library
# define function to be used for bootstrapping (taking the mean of TxSurv and NonTxSurv)
boot_function1 <- function(dat, d) {
E <- dat[d,] # allows boot to select sample
return(c(mean(E$QALY)))
}
Bootstrap_impute <- boot(impute_mean, boot_function1, R=5000)
Bootstrap_impute
#Bootstrap CI
boot.ci(Bootstrap_impute, type=c("norm", "perc", "bca"), index=1) #
```