---
title: "Pooling Levene's test statistic"
author: "Martijn W Heymans"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pooling Levene's test statistic}
  %\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)
```

### Levene's test

Levene's test is used to test if the variance between groups is comparable. The test can be used to compare the variances between two groups, but also between more than two groups.

## Examples

## Pooling Levene's test after the `mice` function 

The lbp_orig as part of the miceafter package is a dataset with missing values. So we first impute them with the `mice` function. Than we use the `mids2milist` function to turn a `mids` object with multiply imputed datasets, as a result of using `mice`, into a `milist` object. Than we use the `with` function to apply repeated analyses with the `levene_test` function across the list of multiply imputed datasets. Finally, we pool the results by using the `pool_levenetest` function.

```{r}

  imp_data <- mice(lbp_orig, m=5, seed=3025, printFlag = FALSE) 

  imp_list <- mids2milist(imp_data)
  
  ra <- with(data=imp_list,
   expr = levene_test(Pain ~ factor(Satisfaction)))

  res <- pool_levenetest(ra, method = "D1")
  res

```  

## Pooling Levene's test after the `mice` function in one Pipe

The lbp_orig as part of the miceafter package is a dataset with missing values. So we first impute them with the `mice` function. Than we use the `mids2milist` function to turn a `mids` object with multiply imputed datasets, as a result of using `mice`, into a `milist` object. Than we use the `with` function to apply repeated analyses with the `levene_test` function across the list of multiply imputed datasets. Finally, we pool the results by using the `pool_levenetest` function.

```{r}

  lbp_orig %>% 
    mice(m=5, seed=3025, printFlag = FALSE) %>%
      mids2milist() %>%
        with(expr = levene_test(Pain ~ factor(Satisfaction))) %>%
          pool_levenetest(method = "D1")

```  

## Pooling Levene's test after Multiply Imputed datasets are stored in a separate dataframe 

The dataset `lbpmilr` as part of the miceafter package is a long dataset that contains 10 multiply imputed datasets. The datasets are distinguished by the `Impnr` variable. First we convert the dataset into a `milist` object by the `df2milist` function. Than we use the `with` function to apply repeated analyses with the `levene_test` function across the multiply imputed datasets. Finally, we pool the results by using the `pool_levenetest` function. As pooling method we use `D1` (`D2` is also possible).

```{r}

  lbpmilr %>%
    df2milist(impvar = "Impnr") %>%
      with(expr = levene_test(Pain ~ factor(Satisfaction))) %>%
        pool_levenetest(method = "D1")

```  

