```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Load packages and data
For this exercise, we will use \texttt{mice} package. It is a very well known package to implement multiple imputation. 

First, we need to library the MICE package and set the data that we will be using for the example. The nhanes 2 data set is a small data set designed for missing data examples. It contains four variables that are quantitative and binary (yes no variables). 
```{r}
#install.packages("mice")
library(mice)
data(nhanes2)
dim(nhanes2)
```

## summarize the dataset and check for missing values. 
```{r}
summary(nhanes2)
#?nhanes2
```

Note that the age and hyp variables are already defined as factor variables in this dataset. If you have a dataset with categorical variables without this pre-definition, you should make the variables factors before doing \texttt{mice}.  

## Missing data

The \texttt{NA} values represent missing data. Let's look at the pattern. 
```{r}
md.pattern(nhanes2)
colSums(is.na(nhanes2))
rowSums(is.na(nhanes2))

```

## Exploratory data analyses

Let’s look at some exploratory data analyses based on the complete cases.

First make the complete cases
```{r}
ccnhanes2 <- cc(nhanes2)
#ccnhanes2 <- nhanes2[rowSums(is.na(nhanes2))==0,]
ccnhanes2
table(ccnhanes2$hyp)
```
Note that there are only 3 cases with \texttt{hypertension = yes}. Relationships will be hard to model accurately due to small sample size…

We will use these data for illustration of the MI procedures. Really we need more data to say anything meaningful about relationships among these variables.

```{r}
plot(ccnhanes2$bmi,x= ccnhanes2$chl,xlab = "cholesterol",main = "Relationship between BMI and CHL")
abline(lm(ccnhanes2$bmi ~ ccnhanes2$chl))
```

```{r}
boxplot(ccnhanes2$bmi ~ ccnhanes2$age, xlab="age", main = "relationship between bmi and age")
```

```{r}
 boxplot(ccnhanes2$bmi ~ ccnhanes2$hyp, xlab="hyp", main = "relationship between bmi and hyp")
```

## Multiple Imputation with \texttt{mice}
```{r}
 nhanes2MI5 <- mice(nhanes2,m = 5)

```

##Let's take a look at different datasets

Are they the same? how different are they? Let's take a look at the first two datasets

```{r}
d1 <- complete(nhanes2MI5,1)
View(d1)
d2 <- complete(nhanes2MI5,2)
View(d2)
plot(density(d1$chl) )
lines(density(d2$chl),col="red")
```

```{r}
```

 

One way to check the difference is try some linear regressions. 
```{r}

summary(lm(bmi~., d1))
summary(lm(bmi~., d2))
```

## Imputation diagnostics
Plot imputed and observed continuous variables
```{r}
stripplot(nhanes2MI5, col=c("grey",mdc(2)), pch=c(1,20))
```
 



Plot by values of categorical variable, say bmi by age grouping:
```{r}
 stripplot(nhanes2MI5, bmi~ age, col=c("grey",mdc(2)), pch=c(1,20))
```


And also you can plot all the imputations in one plot to see relationships:
```{r}
 
```

## Linear regression using the imputed datasets

```{r}
 
```

 


 

Let's run linear regression with all five datasets. `with()` function can help us to do so. 
```{r}
 m <- with(data= nhanes2MI5,lm(bmi~ age + hyp + chl))
summary(m)
```

We can also pool the model results.
```{r}
 m2 <- pool(m)
summary(m2)
```



