<!--START_EXPLANATION-->
Rmarkdown template to generate this page can be found on [Rmarkdown-Template](Rmarkdown/Multiple_Ascending_Dose_PD_time_to_event.Rmd).
<!--END_EXPLANATION-->

```{r, warning=FALSE, message=FALSE}
library(ggplot2)
library(dplyr)
library(broom)
library(knitr)
library(tidyr)
library(survival)
library(survminer)
library(xgxr)
set.seed(123456)
```

# Explore Dose-Response Relationship

## Create a Dataset

```{r warning=FALSE}
#Use the lung dataset to create a fake dose-response dataset
km_data <- lung %>%
  mutate(dose = ph.ecog) %>%
  filter(dose!=3) %>%
  mutate(dose_label = paste(dose," mg")) %>%
  select(time, dose, dose_label, status, sex)

#these columns are required for your dataset
km_data <- km_data %>%
  mutate(dose   = dose,   #dose,
         time   = time,   #time of the event (or censoring)
         event  = status) #status: there are a three options for this column (see ?Surv)
#        a) 0 = censored (alive), 1 = dead (event)
#        b) 1 = censored (alive), 2 = dead (event)
#        c) 0 = right censored, 1 = event at time, 
#           2 = left censored,  3 = interval censored.
```

## Plot Kaplan Meier with Confidence Intervals stratifying by dose
<!--START_EXPLANATION-->
Time-to-event plots can be summarized by Kaplan-Meier plots and stratified by dose to give an overview of the dose-response.  To see if there's an effect on exposure vs response, look to see if there is separation between the doses.
<!--END_EXPLANATION-->
```{r warning=FALSE, message=FALSE}
km_fit <- survfit(Surv(time, status) ~ dose_label, data = km_data, conf.int = 0.95)
gg <- ggsurvplot(km_fit, km_data, conf.int = TRUE, ggtheme = xgx_theme())
gg <- gg + xgx_scale_x_time_units(units_dataset = "day", units_plot = "year")
print(gg)
```

## Plot Kaplan Meier faceted by exposure quartile for assessing covariates
<!--START_EXPLANATION-->
This plot can help in assessing the impact of a covariate (sex) on outcome.
<!--END_EXPLANATION-->

```{r warning=FALSE, message=FALSE}
km_fit_sex <- survfit(Surv(time, status) ~ sex, 
                       data = km_data, 
                       conf.int = 0.95)
gg <- ggsurvplot_facet(km_fit_sex, 
                       km_data, 
                       facet.by = c("dose_label"),
                       conf.int = TRUE,
                       ggtheme = xgx_theme(),
                       legend.labs = paste0("SEX=",sort(unique(km_data$sex))))
gg <- gg + xgx_scale_x_time_units(units_dataset = "day", units_plot = "year")
print(gg)
```

## Performing Cox regression on the above data. 
<!--START_EXPLANATION-->
In this data only a few variables are available, so variable selection isn't needed. If there are multiple variables, applying a variable selection approach (e.g. stepwiseAIC) would be advisable. Look for small p values or large hazard ratios to identify covariates that have a large effect.
<!--END_EXPLANATION-->

```{r warning=FALSE, message=FALSE}
km_cox <- coxph(Surv(time) ~ dose + sex,
             data =km_data)
km_cox_summary <- broom::tidy(km_cox) %>%
  mutate_at(vars(-term),signif,2)
kable(km_cox_summary)  
```

## Mean Event Rate over time
<!--START_EXPLANATION-->
For repeated time to event data, the rate of occurance of the events can be plotted. The observed average rate of occurance would be related to the Hazard rate in a time-to-event model. 

Calculate the rate of occurance of an event as the number of events occuring within specific time intervals, average over the number of subjects being observed. 

Observe the mean event rate, do you see any patterns? Does the event rate increase or decrease over time, or at specific time points (e.g. dosing intervals, circadian rhythms)?
<!--END_EXPLANATION-->

```{r}
# (coming soon)
```

<img src="SiteResources/Event_Rate.png" width=100%>


## Mean Cumulative Function
<!--START_EXPLANATION-->
The mean cumulative function is another way of looking at repeated time-to-event data. The mean cumulative function is the population average of the cumulative number of events over time, and would be related to the Cumulative Hazard in a time-to-event model. 

Calculate the mean cumulative function by ordering all of the events by increasing time, calculate the cumulative number of occurances up to each time point, and take the average over the number of subjects being observed. For repeated time-to-event data, the mean cumulative function can achieve values greater than 1.
<!--END_EXPLANATION-->

```{r}
# (coming soon)
```

<img src="SiteResources/Mean_Cumulative_Function.png" width=75%>

## R Session Info
```{r}
sessionInfo()
```
