---
title: "mice and miceafter for Regression modelling"
author: "Martijn W Heymans"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{mice and miceafter for Regression modelling}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(miceafter)
library(mice)
library(magrittr)
library(dplyr)
```

# Installing the miceafter and mice packages

You can install the development version from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("mwheymans/miceafter")
```
You can install mice with:

``` r
install.packages("mice")

``` 
# Examples 

+ [mice and miceafter for pooling logistic regression models]
+ [mice and miceafter for pooling linear regression models]
+ [mice and miceafter for selecting logistic regression models]
+ [mice and miceafter for selecting linear regression models]

## mice and miceafter for pooling logistic regression models

lbp_orig is a dataset that is part of the miceafter package with missing values. So we first impute them with the `mice` function. Than we use the `mids2milist` function to turn a `mids` object, as a result of using `mice`, into a `milist` object with multiply imputed datasets. Than we use the `with` function to apply repeated logistic regression analyses. With the `pool_glm` function we obtain the results for the pooled model.

```{r}

  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Chronic ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport, 
                      family = binomial))
  
  poolm <- pool_glm(ra, method="D1")
  poolm$pmodel
  poolm$pmultiparm
 
```

Back to [Examples]

## mice and miceafter for pooling linear regression models

The lbp_orig is a dataset that is part of the miceafter package with missing values. So we first impute them with the `mice` function. Than we use the `mids2milist` function to turn a `mids` object, as a result of using `mice`, into a `milist` object with multiply imputed datasets. Than we use the `with` function to apply repeated linear regression analyses. With the `pool_glm` function we obtain the results for the pooled model.

```{r}

  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Pain ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport))
  
  poolm <- pool_glm(ra, method="D1")
  poolm$pmodel
  poolm$pmultiparm
 
```

Back to [Examples]

## mice and miceafter for selecting logistic regression models

We follow the same procedure as the first example but also apply model selection here.

```{r}

  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Chronic ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport, 
                      family = binomial))
  
  poolm <- pool_glm(ra, method="D1", p.crit = 0.15, direction = "BW")
  poolm$pmodel
  poolm$pmultiparm
 
```

Back to [Examples]

## mice and miceafter for selecting linear regression models

We follow the same procedure as the second example but also apply model selection here.

```{r}

  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Pain ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport))
  
  poolm <- pool_glm(ra, method="D1", p.crit = 0.15, direction = "BW")
  poolm$pmodel
  poolm$pmultiparm
  
```

Back to [Examples]
