---
title: "Unified Equation Representation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Unified Equation Representation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

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

# Prospective feature, presented for user suggestion

The default method to specify a survival model using a series of vectors can be overly complicated at times. This may not be straightforward for typical R users and made it complicated to translate from a model to a script input. A function is provided with Colossus to help this process by converting from a simple string expression of the model into the required vectors.

## Equation Expression

The basic idea of this function is to take a string expression, similar to the model objects in the Survival package, which contains all of the information required in a more easily read format. This is intended to serve three functions: simplify the required input, make it easier to add multiple elements to each subterm, and make it easier to add factors.

Generally, a survival model is defined by using a Surv object, initialized with the interval and event columns, and a list of columns. Because Colossus can handle much more complicated models, the definition has to be more complicated. Instead of a Surv object, the model is specified with 'cox' or 'poisson'. Based on the number of entries, the format of the time interval is assumed. The right side of the equation is listed similarly. Columns are added to subterm types (loglinear, linear, plinear). Each subterm has a list of columns and a term number. Finally, the method to combine terms is added as a separate item (multiplicative, additive). To prevent any issues with function definitions, the model equation is stored in a string.

<p class="text-center" style="background-color: aliceblue">
$$
\begin{aligned}
    \text{Surv(interval, event) ~ name + ... + factor(name)} \\
    \text{'Survival(interval, event) ~ subterm(name, factor(name), term_number) + ... + term_model()'}
\end{aligned}
$$
</p>

The factor option can be used with or without a baseline specified. By default, no baseline is used. A baseline can be added by changing 'factor(name)' to 'factor(name;baseline=level)'. If a valid baseline is used, the proper factor will not be included in the model expression or the final datatable returned. If an invalid baseline is used a warning is thrown and no baseline is used.

The following expressions are equivalent.

```{r}
term_n <- c(0, 0, 1)
tform <- c("loglin", "loglin", "lin")
names <- c("dose0", "dose1", "dose2")
modelform <- "M"
tstart <- "t0"
tend <- "t1"
event <- "lung"

Model_Eq <- "cox(t0, t1, lung) ~ loglinear(dose0, dose1, 0) + linear(dose2, 1) + multiplicative()"
Model_Eq <- "cox(t0, t1, lung) ~ loglinear(dose0, dose1) + linear(dose2, 1)"

df <- data.table("dose0" = 1:4, "dose1" = 2:5, "dose2" = 3:6)
Convert_Model_Eq(Model_Eq, df)
```

Different model formulas and subterm types are being added as needed. Please contact the developer if the methods you want to use have not yet been added. The following tables cover the subterms and term models that are currently implemented. Every option listed has multiple equivalent aliases that all refer to the same subterm or model formula.

| Subterm Type | Equivalent Aliases |
| :---------: | :----------: |
| plin | "plin", "plinear", "product-linear" |
| lin | "lin", "linear" |
| loglin | "loglin", "loglinear", "log-linear" |
| loglin_slope/loglin_top | "loglin-dose", "loglinear-dose", "log-linear-dose" |
| lin_slope/lin_top | "lin-dose", "linear-dose", "linear-piecewise" |
| quad_slope | "quadratic", "quad", "quad-dose", "quadratic-dose" |
| step_slope/step_int | "step-dose", "step-piecewise" |
| lin_quad_slope/lin_quad_int | "lin-quad-dose", "linear-quadratic-dose", "linear-quadratic-piecewise" |
| lin_exp_slope/lin_exp_int/lin_exp_exp_slope | "lin-exp-dose", "linear-exponential-dose", "linear-exponential-piecewise" |

| Term Type | Equivalent Aliases |
| :---------: | :----------: |
| M | "m", "me", "multiplicative", "multiplicative-excess" |
| A | "a", "additive" |
| PA | "pa", "product-additive" |
| PAE | "pae", "product-additive-excess" |
| GMIX | "gmix","geometric-mixture" |
| GMIX-R | "gmix-r","relative-geometric-mixture" |
| GMIX-E | "gmix-e","excess-geometric-mixture" |
