---
title: "Separate 2 groups in Cox regression"
author: "André Veríssimo"
date: "`r Sys.Date()`"
output: 
  BiocStyle::html_document:
    number_sections: yes
    toc: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Separate 2 groups in Cox regression}
  %\VignetteEncoding{UTF-8}
--- 

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Instalation

```{r, eval=FALSE}
if (!require("BiocManager")) {
    install.packages("BiocManager")
}
BiocManager::install("glmSparseNet")
```

# Required Packages

```{r packages, message=FALSE, warning=FALSE, results='hide'}
library(futile.logger)
library(ggplot2)
library(glmSparseNet)
library(survival)

# Some general options for futile.logger the debugging package
flog.layout(layout.format("[~l] ~m"))
options("glmSparseNet.show_message" = FALSE)
# Setting ggplot2 default theme as minimal
theme_set(ggplot2::theme_minimal())
```

## Prepare data

```{r}
data("cancer", package = "survival")
xdata <- survival::ovarian[, c("age", "resid.ds")]
ydata <- data.frame(
    time = survival::ovarian$futime,
    status = survival::ovarian$fustat
)
```

## Separate using age as co-variate 
_(group cutoff is median calculated relative risk)_

```{r}
resAge <- separate2GroupsCox(c(age = 1, 0), xdata, ydata)
```

### Kaplan-Meier survival results

```{r, echo=FALSE}
resAge$km
```

### Plot

A individual is attributed to low-risk group if its calculated 
relative risk _(using Cox Proportional model)_ is below or equal
the median risk.

The opposite for the high-risk groups, populated with individuals 
above the median relative-risk.

```{r, echo=FALSE}
resAge$plot
```

## Separate using age as co-variate _(group cutoff is 40% - 60%)_

```{r}
resAge4060 <-
    separate2GroupsCox(c(age = 1, 0),
        xdata,
        ydata,
        probs = c(.4, .6)
    )
```

### Kaplan-Meier survival results

```{r, echo=FALSE}
resAge4060$km
```

### Plot

A individual is attributed to low-risk group if its calculated
relative risk _(using Cox Proportional model)_ is below the median risk.

The opposite for the high-risk groups, populated with individuals above 
the median relative-risk.

```{r, echo=FALSE}
resAge4060$plot
```

## Separate using age as co-variate _(group cutoff is 60% - 40%)_

This is a special case where you want to use a cutoff that includes
some sample on both high and low risks groups.

```{r}
resAge6040 <- separate2GroupsCox(
    chosenBetas = c(age = 1, 0),
    xdata,
    ydata,
    probs = c(.6, .4),
    stopWhenOverlap = FALSE
)
```

### Kaplan-Meier survival results

```{r, echo=FALSE}
cat("Kaplan-Meier results", "\n")
resAge6040$km
```

### Plot

A individual is attributed to low-risk group if its calculated 
relative risk _(using Cox Proportional model)_ is below the median risk.

The opposite for the high-risk groups, populated with individuals above 
the median relative-risk.

```{r, echo=FALSE}
resAge6040$plot
```


# Session Info

```{r sessionInfo}
sessionInfo()
```