---
title: "BRCA - the Cox regression merging"
author: "Agnieszka Sitko"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{BRCA - the Cox regression factor merging}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE,
                      fig.height = 5, fig.width = 10)
```

```{r}
library(factorMerger)
library(survival)
```
```{r}
if (!require("forcats")) {
    install.packages("forcats")
}
library(forcats)

```


```{r}
data("BRCA")
```


# Response - survival

```{r}
brcaSurv <- Surv(time = BRCA$time, event = BRCA$vitalStatus)
```


# Factor
## Drug name
```{r}
drugName <- BRCA$drugName # drug name
drugName <- fct_lump(as.factor(drugName), prop = 0.05) 

```

```{r}
drugNameFM <- mergeFactors(response = brcaSurv[!is.na(drugName)], 
                           factor = drugName[!is.na(drugName)], 
                           family = "survival")

plot(drugNameFM, nodesSpacing = "effects", gicPanelColor = "grey2")
```

```{r}
anova(coxph(brcaSurv[!is.na(drugName)] ~ drugName[!is.na(drugName)]))
anova(coxph(brcaSurv[!is.na(drugName)] ~ cutTree(drugNameFM)))
```


## Cancer subtype

```{r}
subtype <- BRCA$histologicalType
subtype <- fct_lump(as.factor(subtype), prop = 0.05) 

subtypeFM <- mergeFactors(response = brcaSurv[!is.na(subtype)], 
                          factor = subtype[!is.na(subtype)],
                           family = "survival")

plot(subtypeFM) 
```

## Pathologic category

```{r}
patCat <- BRCA$pathologicCategory %>% substr(1, 2)

patCatFM <- mergeFactors(response = brcaSurv[!is.na(patCat)],
                         factor = patCat[!is.na(patCat)],
                         family = "survival")

plot(patCatFM, responsePanel = "frequency", gicPanelColor = "red")
```

```{r}
anova(coxph(brcaSurv[!is.na(patCat)] ~ patCat[!is.na(patCat)]))
anova(coxph(brcaSurv[!is.na(subtype)] ~ cutTree(subtypeFM)))
```

