---
title: "Case Based Reasoning: Cox-Beta-Model"
author: "Dr. Simon Müller"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float: true
    theme: readable
    highlight: tango
    css: kable.css
vignette: >
  %\VignetteIndexEntry{Case Based Reasoning: Cox-Beta-Model}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

In the first example, we use the Cox-Model and the `ovarian` data set from the 
`survival` package. In the first step we initialize the R6 data object. 

```{r, warning=FALSE, message=FALSE}
library(tidyverse)
library(survival)
library(CaseBasedReasoning)
ovarian$resid.ds <- factor(ovarian$resid.ds)
ovarian$rx <- factor(ovarian$rx)
ovarian$ecog.ps <- factor(ovarian$ecog.ps)

# initialize R6 object
coxBeta <- CoxBetaModel$new(Surv(futime, fustat) ~ age + resid.ds + rx + ecog.ps)
```
All cases with missing values in the learning and end point variables are dropped (`na.omit`) and the reduced data set without missing values is saved internally. You get a text output on how many cases were dropped. `character` variables will be transformed to `factor`.

## Similar Cases 

After the initialization, we may want to get for each case in the query data the most similar case from the learning data. 
```{r}
n <- nrow(ovarian)
trainID <- sample(1:n, floor(0.8 * n), F)
testID <- (1:n)[-trainID]

# fit model 
ovarian[trainID, ] %>% 
  coxBeta$fit()
# get similar cases
ovarian[trainID, ] %>%
  coxBeta$get_similar_cases(queryData = ovarian[testID, ], k = 3) -> matchedData
knitr::kable(head(matchedData))
```
You may extract then the similar cases and the verum data and put them together:

**Note 1:** In the initialization step, we dropped all cases with missing values in the variables of ` data` and ` endPoint`. So, you need to make sure that NA handling is done by you.

**Note 2:** The `data.table` returned from `coxBeta$get_similar_cases` has four additional columns:

1. `caseId`: By this column you may map the similar cases to cases in data, e.g. if you had chosen ` k = 3`, then the first three elements in the column `caseId` will be ` 1` (following three ` 2` and so on). This means that this three cases are the three most similar cases to case ` 0` in verum data.
2. `scDist`: The calculated distance
3. `scCaseId`: Grouping number of query with matched data
4. `group`: Grouping matched or query data


### Distance Matrix

Alternatively, you may just be interested in the distance matrix, then you go this way:

```{r}
ovarian %>%
  coxBeta$calc_distance_matrix() -> ditMatrix
```
`coxBeta$calc_distance_matrix()` calculates the full distance matrix. This matrix the dimension: cases of data versus cases of query data. If the query dataset is bot available, this functions calculates a n times n distance matrix of all pairs in data. 
The distance matrix is saved internally in the *CoxBetaModel* object: ` coxBeta$distMat`.


## Check Proportional Hazard Assumption:

```{r, warning=FALSE, message=FALSE}
pp <- coxBeta$check_ph()
pp
```

### Validation of the matching Variables

**TBD**
