---
title: 'ILSE: a simple NHANES example'
author: "Wei Liu"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ILSE: a simple NHANES example}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

### Load real data
First, we load the 'ILSE' package and the real data which can be loaded by following command.
```{r eval = FALSE}
library("ILSE")
data("nhanes")
```

### Fit linear regression model
We fit the linear regression model using 'ILSE' function, and then compare with *CC* method and *FIML* method.
```{r eval = FALSE}
ncomp <- sum(complete.cases(nhanes))
message("Number of complete cases is ", ncomp, '\n')
ilse2 <- ilse(age~., data=nhanes, verbose=T)
print(ilse2)
```

Next, *Bootstrap* is applied to evaluate the standard error and p-values of each coefficients estimated by ILSE. We observe four significant coefficients. 
```{r eval = FALSE}
set.seed(1)
s2 <- summary(ilse2, Nbt=20)
s2
```

### Compare with CC and FIML

First, we conduct CC analysis.
```{r eval = FALSE}
lm1 <- lm(age~., data=nhanes)
s_cc <- summary.lm(lm1)
s_cc
```

We fit linear regression model using FIML method.
```{r eval = FALSE}
fimllm <- fimlreg(age~., data=nhanes)
print(fimllm)
```
We also use *bootstrap* to evaluate the standard error and p-values of each coefficients estimated by ILSE. We observe only one significant coefficients. 
```{r eval = FALSE}
s_fiml <- summary(fimllm, Nbt=20)
s_fiml
```

### Visualization 
We visualize the p-vaules of each methods, where red line denotes 0.05 in y-axis and blue line 0.1 in y-axis.
```{r eval = FALSE}
library(ggplot2)
library(ggthemes)
pMat <- cbind(CC=s_cc$coefficients[,4], ILSE=s2[,4], FIML=s_fiml[,4])
df1 <- data.frame(Pval= as.vector(pMat[-1,]),
                    Method =factor(rep(c('CC', "ILSE", "FIML"),each=3)),
                    covariate= factor(rep(row.names(pMat[-1,]), times=3)))
ggplot(data=df1, aes(x=covariate, y=Pval, fill=Method)) + geom_bar(position = "dodge", stat="identity",width = 0.5) + geom_hline(yintercept = 0.05, color='red') + geom_hline(yintercept = 0.1, color='blue') +
  scale_fill_economist()
```

## Session information
```{r}
sessionInfo()
```