---
title: "Pooling C-index of Logistic and Cox Regression Models"
author: "Martijn W Heymans"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pooling C-index of Logistic and Cox Regression Models}
  %\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)
library(survival)
```

## Introduction

The `miceafter` package includes the function `pool_cindex`, to pool c-index values from logistic and Cox regression models. This vignette shows you how to use this function. 

## Examples

## Pooling the C-index After the `mice` function and Logistic Regression

The lbp_orig is a dataset as 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 the `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 `cindex` function across the multiply imputed datasets. Finally, we pool the results by using the `pool_cindex` function. We do that in one pipe.

```{r}

  lbp_orig %>%
      mice(m=5, seed=3025, printFlag = FALSE) %>%
        mids2milist() %>% 
          with(expr = cindex(glm(Chronic ~ Gender + Radiation, family=binomial))) %>% 
            pool_cindex()

```  

## Pooling the C-index after Multiply Imputed datasets are stored in a dataframe and with Logistic Regression

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 using the `df2milist` function. Than we use the `with` function to apply repeated analyses with the `cindex` function across the multiply imputed datasets. Finally, we pool the results by using the `pool_cindex` function.

```{r}

  imp_data <- df2milist(lbpmilr, impvar = "Impnr") 

  ra <- with(data=imp_data,
   expr = cindex(glm(Chronic ~ Gender + Radiation, family=binomial)))

  res <- pool_cindex(ra)
  res

```  

## Pooling the C-index after Multiply Imputed datasets are stored in a dataframe and with Cox Regression

The dataset `lbpmicox` 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 using the `df2milist` function. Than we use the `with`  function to apply repeated analyses with the `cindex` function across the list of multiply imputed datasets. Finally, we pool the results by using the `pool_cindex` function.

```{r}

  library(survival)
  lbpmicox %>% 
    df2milist(impvar = "Impnr") %>%
      with(expr = cindex(coxph(Surv(Time, Status) ~ Radiation + Age))) %>%
        pool_cindex()

```  
 

