---
title: "Pooling and Selection of Cox Regression Models"
author: "Martijn W Heymans"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Pooling and Selection of Cox Regression Models}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

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

# Introduction

With the `psfmi` package you can pool Cox regression models by using  
the following pooling methods: RR (Rubin's Rules), D1, D2, and MPR 
(Median R Rule). You can also use forward or backward selection from
the pooled model. This vignette show you examples of how to apply these 
procedures.

# Examples 

* [Cox regression]
    + [Pooling without BW and method D1] 
    + [Pooling with FW and method MPR] 
    + [Pooling with FW including interaction terms and method D1]
    + [Pooling with BW including spline coefficients and method D1]
    + [Pooling with FW including spline coefficients and method MPR]
    + [Pooling with BW for a stratified Cox model]

## Pooling without BW and method D1

If you set p.crit at 1 than no selection of variables takes place. 
Either using direction = "FW" or direction = "BW" will produce the same
result.

```{r}

  library(psfmi)
  pool_coxr <- psfmi_coxr(data=lbpmicox, nimp=5, impvar="Impnr", 
                formula = Surv(Time, Status) ~ Duration + Radiation + Onset + 
                Function + Age + Previous + Tampascale + JobControl + 
                JobDemand + Social + factor(Expect_cat), p.crit=1,
                method="D1", direction = "BW")
  
  pool_coxr$RR_model
  pool_coxr$multiparm
  
```  

Back to [Examples]

## Pooling with FW and method MPR

```{r, eval=TRUE}

  library(psfmi)
  pool_coxr <- psfmi_coxr(data=lbpmicox, nimp=5, impvar="Impnr", 
                formula = Surv(Time, Status) ~ Duration + Radiation + Onset + 
                Function + Age + Previous + Tampascale + JobControl + 
                JobDemand + Social + factor(Expect_cat), p.crit=0.05,
                method="D1", direction = "FW")
  
  pool_coxr$RR_model_final
  pool_coxr$multiparm_final
  pool_coxr$predictors_in
  
```  

Back to [Examples]

## Pooling with FW including interaction terms and method D1

Pooling Cox regression models over 5 imputed datasets with backward selection
using a p-value of 0.05 and as method D1 including interaction terms with 
a categorical predictor and forcing the predictor Tampascale in the models 
during backward selection.

```{r}

  library(psfmi)
  
  pool_coxr <- psfmi_coxr(data=lbpmicox, nimp=5, impvar="Impnr", 
                formula = Surv(Time, Status) ~ Duration + Radiation + Onset + 
                Function + Age + Previous + Tampascale + factor(Expect_cat) +
                factor(Satisfaction) + Tampascale:Radiation + 
                factor(Expect_cat):Tampascale, keep.predictors = "Tampascale",
                p.crit=0.05, method="D1", direction = "FW")
  
  pool_coxr$RR_model_final
  pool_coxr$multiparm_final
  pool_coxr$predictors_in

```  

Back to [Examples]

## Pooling with BW including spline coefficients and method D1

Pooling Cox regression models over 5 imputed datasets with backward selection
using a p-value of 0.05 and as method D1 including a restricted cubic spline 
predictor and forcing Tampascale in the models during backward selection.

```{r}

  library(psfmi)
  
  pool_coxr <- psfmi_coxr(data=lbpmicox, nimp=5, impvar="Impnr", 
                formula = Surv(Time, Status) ~ Duration + Radiation + Onset + 
                Function + Previous + rcs(Tampascale, 3) + 
                factor(Satisfaction) + rcs(Tampascale, 3):Radiation,  
                keep.predictors = "Tampascale",
                p.crit=0.05, method="D1", direction = "BW")
  
  pool_coxr$RR_model_final
  pool_coxr$multiparm_final
  pool_coxr$predictors_in

```  

Back to [Examples]

## Pooling with FW including spline coefficients and method MPR

Pooling Cox regression models over 5 imputed datasets with forward selection
using a p-value of 0.05 and as method MPR including a restricted cubic spline 
predictor and forcing Tampascale in the models during forward selection.

```{r}

  library(psfmi)
  pool_coxr <- psfmi_coxr(data=lbpmicox, nimp=5, impvar="Impnr", 
                formula = Surv(Time, Status) ~ Duration + Radiation + Onset + 
                Function + Previous + rcs(Tampascale, 3) + 
                factor(Satisfaction) + rcs(Tampascale, 3):Radiation,  
                keep.predictors = "Tampascale",
                p.crit=0.05, method="MPR", direction = "FW")
  
  pool_coxr$RR_model_final
  pool_coxr$multiparm_final
  pool_coxr$predictors_in
  
```  

Back to [Examples]

## Pooling with BW for a stratified Cox model

Pooling Cox regression models over 5 imputed datasets with backward selection
using a p-value of 0.05 and as method MPR for a stratified Cox model.

```{r}

  library(psfmi)
  pool_coxr <- psfmi_coxr(data=lbpmicox, nimp=5, impvar="Impnr", 
                formula = Surv(Time, Status) ~ Duration + Onset + 
                Function + Previous + rcs(Tampascale, 3) + 
                factor(Satisfaction) + strata(Radiation), 
                p.crit=0.05, method="MPR", direction = "BW")
  
  pool_coxr$RR_model_final
  pool_coxr$multiparm_final
  pool_coxr$formula_step
  
```  

Back to [Examples]
