---
title: "Alternative Regression Options"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Alternative Regression Options}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup}
library(Colossus)
library(data.table)
```
## General Options

Both Cox proportional hazards and Poisson model regressions have additional functions that can account for specific situations. There general situations are as follows:

| Option | Description | Cox PH | Poisson |
| :---------: | :----------: | :----------: | :-------: |
| Stratification | In Cox PH, the stratification is applied in the risks groups to compare only like rows. In Poisson model regression the stratification is an additional term in the model to account for the effects of stratified covariates | x | x |
| Simplified Model | For a multiplicative Log-Linear model, simplifications can be made to the code. These give a faster version. | x |  |
| Non-Derivative Calculation | If a single iteration is needed without derivatives, there is time and memory that can be saved | x | x |
| Competing Risks | If there is a competing event, rows are weighted by an estimate of the censoring rate to approximate a dataset without the effect of a competing event | x |  |
| Joint Analysis | A Poisson method which allows a model to solve for multiple outcomes, by creating multiple copies of rows with multiple events and using a factor variable to add covariates for specific outcomes |  | x |
| Distributed Initial Parameter Sets | The user provides distribution for each starting parameter, Colossus runs low iteration regressions at random points, and then a full run at the best guess | x | x |

The following sections review the math behind the basic functions and how each option changes that.


## Stratification

### Cox Proportional Hazards

In Cox Proportional Hazards, the Log-Likelihood is calculated by taking the ratio of the hazard ratio at each event to the sum of the hazard ratios of every other row at risk. This defines the risk group for every event time to be the intervals containing the event time. Intervals are assumed to be open on the left and closed on the right, and events are assumed to take place at the right endpoint. This gives the following common equation for the Log-Likelihood:

<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    Ll = \prod_{i}^{n} \left( \frac{r_{i}}{\sum_{j: t_j \in R_i} r_j} \right)^{\delta_i}
\end{aligned}
$$
</p>

In which r denotes hazard ratios, the denominator is the sum of hazard ratios of intervals containing the event time, and each term is raised to the power of 1 if the interval has an event and 0 otherwise. Different tie methods modify the denominator based on how the order of events is assumed, but the general form still stands. The goal is to compare each event interval to intervals within a similar time span. Stratification adds a condition: if the user stratifies over a covariate "F" then each risk group is split into subgroups with the same value of "F". So the goal becomes to compare event intervals to intervals with similar strata AND time. This is done to remove the influence of the stratification variables from the calculations.

In code, this is done by adding an additional parameter for the stratification column and using a different function to call the regression. Multiple strata columns can be provided by including a vector of columns.

```{r, eval=FALSE}
Strat_Col <- "s0"
e <- CoxRun_Strata(Cox(time1, time2, event, s0) ~ loglinear(dose), df,
  a_n = a_n, control = control
)
Strat_Cols <- c("s0", "s1", "s2")
e <- CoxRun_Strata(Cox(time1, time2, event, c(s0, s1, s2)) ~ loglinear(dose), df,
  a_n = a_n, control = control
)
```

### Poisson Regression

Poisson model regression does not have risk groups to account for, but the theory is the same. To remove the influence of stratification covariate a new term is added to account for their effects. In Colossus, this is a Log-Linear term. So the following may be the model used without stratification:

<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    R = \sum_i (x_i \cdot \beta_i)
\end{aligned}
$$
</p>

Then the stratified model may look like this:

<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    R = (\sum_i (x_i \cdot \beta_i)) \times (\exp{(\sum_{strata} (x_{strata} \cdot \beta_{strata}))})
\end{aligned}
$$
</p>

Only results associated with the non-stratified parameters are returned by default. An average event rate is calculated for every strata level, which weights the calculated risks. The weighting is performed so that the predicted rate when every model parameter is set to 0 equals the average strata rates. Note that this can lead to errors if the model specified predicts a rate of 0 when the model parameters are all 0. Purely linear models cannot be used with stratification currently for this reason.

In code, this is done by adding in a list of stratification columns and using a different function to call the regression. Colossus combines the list of stratification columns into a single interaction, so it does not matter if the user provides a list or combines them themselves.

```{r, eval=FALSE}
Strat_Col <- c("e")
e <- PoisRun(Poisson_Strata(pyr, event, e) ~ loglinear(dose), df,
  a_n = a_n, control = control
)
```

## Non-Derivative Calculation

Colossus uses Newton's method to perform the regression, which can become computationally intensive as the model becomes more complicated. So, Colossus contains functions to calculate only the scores for a parameter set and skip the derivative calculations. These results could be used to perform a bisection method of regression or plot the dependence between the score and parameter values. This ability is left for the user's convenience.

The code is similar to previous examples:
```{r, eval=FALSE}
e <- CoxRun(Cox(time1, time2, event) ~ loglinear(dose), df,
  a_n = a_n, control = control, single = TRUE
)
```

## Competing Risks

### Fine-Gray

In Cox PH there is an assumption that every individual is recorded until they have an event or they are naturally censored. Censoring is assumed to be statistically independent with regard to the event being studied. These assumptions are violated when there is a competing event occurring. In sensitivity analysis, two extremes are generally tested. Either every person with a competing event is treated as having the event of interest instead, or they are assumed to never experience the event of interest. However, there are methods to find a more realistic alternative. Colossus applies the Fine-Gray model for competing risks, which instead weights the contribution of competing event intervals in future intervals by the probability they would not have been censored.

As previously established, the risk groups are formed to measure the probability that an individual survived up to the time and experienced the event of interest, given that they did not experience an event up to that time:


<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    \lambda(t) = \lim_{\Delta t \to 0} \frac{(P(t \leq T \leq t + \Delta t)\text{ and }(k=1), T \geq t)}{\Delta t}
\end{aligned}
$$
</p>

The competing risks model adjusts this to be the probability that an individual survived up to the time and experienced the event of interest, given that they did not experience an event up to that time OR they survived up to a previous time and experienced a different event:

<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    \lambda(t) = \lim_{\Delta t \to 0} \frac{(P(t \leq T \leq t + \Delta t)\text{ and }(k=1), (T \geq t)\text{ or }((T < t)\text{ and }(k \neq 1)))}{\Delta t}
\end{aligned}
$$
</p>

This means that risk groups would contain both intervals actually at risk and intervals with competing events treated as if they were at risk. If we assume that no time-dependent covariates are being used, then all that remains is to weigh the contribution of these competing events. In general, a weighting column is provided before regression, the values can be solved from a survival curve or by using the "finegray" function of the survival library.

In code, the call is similar to the standard function. The process for finding a weighting is included below. In this example, we assume the event column, lung, to contain a 0 for no events, 1 for the primary event, and 2 for the competing event.

```{r, eval=FALSE}
pdata <- finegray(Surv(time2, event) ~ ., data = df)

e <- CoxRun(FineGray(fgstart, fgstop, fgstatus, fgwt) ~ loglinear(dose), pdata,
  a_n = a_n, control = control
)
```

### Poisson Joint Analysis

For a dataset with multiple outcomes, there are often two ways that Poisson models are fit. Either multiple independent models are fit or the events are combined and one model is fit to several events. These methods limit models to be completely independent or identical. The true model may be a combination of shared terms and terms specific to each event, which cannot be modeled with these methods. To fit this type of model a joint analysis method (Cologne, 2019) is available in Colossus.

Suppose one has a table of person-years, a covariate, and counts for two events. Assume we are fitting the event rate ($\lambda$) for each event and have reason to believe that the background rate ($\beta$) is the same for each event.

 | Time | a | y | z |
 | :--: | :--: | :--: | :--: |
 | $t_1$  | $a_1$ | $y_1$ | $z_1$ |
 | $t_2$  | $a_2$ | $y_2$ | $z_2$ |


<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    \lambda_y(a) = \beta*\exp{(\mu_y*a)}\\
    \lambda_z(a) = \beta*\exp{(\mu_z*a)}
\end{aligned}
$$
</p>

If one were to solve the equations separately the table would be split into two tables, each with one event column. The premise of a joint analysis is to write a model that can be applied to every event, including a factor covariate to select which event is being solved for. Then the split tables can be recombined and multiple events can be solved at once allowing for shared and event-specific parameters.

 | Time | a | $\alpha_y$ | $\alpha_z$ | events |
 | :--: | :--: | :--: | :--: | :--: |
 | $t_1$  | $a_1$ | 1 | 0 | $y_1$ |
 | $t_1$  | $a_1$ | 0 | 1 | $z_1$ |
 | $t_2$  | $a_2$ | 1 | 0 | $y_2$ |
 | $t_2$  | $a_2$ | 0 | 1 | $z_2$ |


<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    \lambda(a,\alpha_y,\alpha_z) = \beta*\exp{(\mu_y*(a*\alpha_y) + \mu_z*(a*\alpha_z))}
\end{aligned}
$$
</p>

Colossus includes several functions to apply this method, a function that produces input for a joint analysis regression and a function that also runs the regression. The most general converts a table and lists of formula into the input for a regression.

```{r, eval=TRUE}
a <- c(0, 0, 0, 1, 1, 1)
b <- c(1, 1, 1, 2, 2, 2)
c <- c(0, 1, 2, 2, 1, 0)
d <- c(1, 1, 0, 0, 1, 1)
e <- c(0, 1, 1, 1, 0, 0)
df <- data.table("t0" = a, "t1" = b, "e0" = c, "e1" = d, "fac" = e)
time1 <- "t0"
time2 <- "t1"
df$pyr <- df$t1 - df$t0
pyr <- "pyr"
events <- c("e0", "e1")
```
Colossus generally accepts a formula to describe the elements of the model. For a joint analysis, Colossus instead expects a list of formula. The formula are expect to cover the model elements specific to each event, and potentially a model for shared elements. Colossus expects name of the shared model to be "shared". The left hand side of the shared model is not used.

```{r, eval=TRUE}
model_1 <- Pois(pyr, e0) ~ loglin(fac, 0)
model_2 <- Pois(pyr, e1) ~ loglin(fac, 0)
model_s <- Pois(pyr) ~ plinear(t0, 0)
formula_list <- list(model_1, model_2, "shared" = model_s)
```
The function returns a list containing the combined table and model object.
```{r, eval=TRUE}
get_form_joint(formula_list, df)
```

These results could be used as input for any of the available regression functions. Colossus also includes a wrapper function to directly call the Poisson model regression function.
```{r, eval=TRUE}
control <- list(
  "ncores" = 1, "lr" = 0.75, "maxiter" = 10, "halfmax" = 5, "epsilon" = 1e-6,
  "deriv_epsilon" = 1e-6, "verbose" = 2, "ncores" = 2
)
e <- PoisRunJoint(formula_list, df, control = control)
print(e)
```




