## Overview
<!--START_EXPLANATION-->
This document contains exploratory plots for count PD data as well as the R code that generates these graphs. The plots presented here are based on simulated data ([see: PKPD Datasets](PKPD_Datasets.html)). Data specifications can be accessed on [Datasets](Datasets.html) and Rmarkdown template to generate this page can be found on [Rmarkdown-Template](Rmarkdown/Multiple_Ascending_Dose_PD_count.Rmd). You may also download the Multiple Ascending Dose PK/PD dataset for your reference ([download dataset](Data/Multiple_Ascending_Dose_Dataset2.csv)).
<!--END_EXPLANATION-->

## Setup

```{r, error = TRUE, echo = TRUE, warning = FALSE, message = FALSE}
library(ggplot2)
library(dplyr)
library(tidyr)
library(xgxr)

#flag for labeling figures as draft
status = "DRAFT"

## ggplot settings
xgx_theme_set()

#directories for saving individual graphs
dirs = list(
  parent_dir = "Parent_Directory", 
  rscript_dir  = "./", 
  rscript_name = "Example.R", 
  results_dir  = "./", 
  filename_prefix   = "", 
  filename     = "Example.png")
```

## Load Dataset

```{r, error = TRUE, warning = FALSE, message = FALSE}
#load dataset
pkpd_data <- read.csv("../Data/Multiple_Ascending_Dose_Dataset2.csv")

DOSE_CMT = 1
PD_CMT = 4
SS_PROFDAY = 6 # steady state prof day
PD_PROFDAYS = c(0, 2, 4, 6)

#ensure dataset has all the necessary columns
pkpd_data = pkpd_data %>%
    mutate(ID      = ID,     #ID   column
           TIME    = TIME,   #TIME column name 
           NOMTIME = NOMTIME, #NOMINAL TIME column name
           PROFDAY = case_when(
             NOMTIME < (SS_PROFDAY - 1)*24 ~ 1 + floor(NOMTIME / 24), 
             NOMTIME >= (SS_PROFDAY - 1)*24 ~ SS_PROFDAY
           ), #PROFILE DAY day associated with profile, e.g. day of dose administration
           LIDV    = LIDV,   #DEPENDENT VARIABLE column name
           CENS    = CENS,   #CENSORING column name
           CMT     = CMT,    #COMPARTMENT column
           DOSE    = DOSE,   #DOSE column here (numeric value)
           TRTACT  = TRTACT, #DOSE REGIMEN column here (character, with units), 
           LIDV_UNIT = EVENTU, 
           DAY_label = ifelse(PROFDAY > 0, paste("Day", PROFDAY), "Baseline")
    )

#create a factor for the treatment variable for plotting
pkpd_data = pkpd_data %>%
  arrange(DOSE) %>%
  mutate(TRTACT_low2high = factor(TRTACT, levels = unique(TRTACT)), 
         TRTACT_high2low = factor(TRTACT, levels = rev(unique(TRTACT)))) %>%
  select(-TRTACT)

#create pd dataset
pd_data <- pkpd_data %>%
  filter(CMT == PD_CMT) %>%
  mutate(count_low2high = factor(LIDV, levels = sort(unique(LIDV))), 
         count_high2low = factor(LIDV, levels = rev(sort(unique(LIDV)))))

#units and labels
  time_units_dataset = "hours"
  time_units_plot    = "days"
  trtact_label       = "Dose"
  dose_units         = unique((pkpd_data %>% filter(CMT == DOSE_CMT))$LIDV_UNIT) %>% as.character()
  dose_label         = paste0("Dose (", dose_units, ")")
  pd_units           = unique(pd_data$LIDV_UNIT) %>% as.character()
  pd_label           = paste0("PD Marker (", pd_units, ")")  

```


## Provide an overview of the data
<!--START_EXPLANATION-->
Summarize the data in a way that is easy to visualize the general trend of PD over time and between doses. Using summary statistics can be helpful, e.g. Mean +/- SE, or median, 5th & 95th percentiles.
<!--END_EXPLANATION-->

### Count over time, colored by Dose, median, 5th & 95th percentiles by nominal time

```{r, error = TRUE, cache = TRUE, fig.width = 8, fig.height = 5, warning = FALSE, message = FALSE} 

gg <- ggplot(data = pd_data, 
             aes(x = NOMTIME, y = LIDV, color = TRTACT_high2low, fill = TRTACT_high2low))
gg <- gg + xgx_geom_pi(percent_level = 0.95, geom = c("line", "point", "errorbar"), alpha = 0.5, position = position_dodge(-9.6))
gg <- gg + guides(color = guide_legend(""), fill = guide_legend(""))
gg <- gg + xgx_scale_x_time_units(units_dataset = "h", units_plot = "d")
gg <- gg + ylab(pd_label)
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

### Count over time, faceted by Dose, median, 5th & 95th percentiles by nominal time

```{r, error = TRUE, cache = TRUE, fig.width = 10, fig.height = 4, warning = FALSE, message = FALSE}
 
gg <- ggplot(data = pd_data, 
             aes(x = NOMTIME, y = LIDV))
gg <- gg + xgx_geom_pi(percent_level = 0.95, geom = c("line", "point", "errorbar"))
gg <- gg + guides(color = guide_legend(""), fill = guide_legend(""))
gg <- gg + xgx_scale_x_time_units(units_dataset = "h", units_plot = "d")
gg <- gg + facet_grid(~TRTACT_low2high)
gg <- gg + ylab(pd_label)
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

### Percent of subjects by Count over time, faceted by dose

```{r, error = TRUE, cache = TRUE, fig.width = 10, fig.height = 4, warning = FALSE, message = FALSE}

gg <- ggplot(data = pd_data, aes(x = factor(PROFDAY), fill = count_high2low))
gg <- gg + geom_bar(position = "fill") + scale_fill_grey(start = 0.8, end = 0.2, breaks = rev(seq(0, 18, 6)))
gg <- gg + scale_y_continuous(labels = scales::percent)
gg <- gg + ylab("Percent of Subjects") + xlab("Time (days)") + guides(fill = guide_legend(pd_label))
gg <- gg + facet_grid(.~TRTACT_low2high) 
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

## Explore variability
<!--START_EXPLANATION-->
Use spaghetti plots to visualize the extent of variability between individuals. The wider the spread of the profiles, the higher the between subject variability. Distinguish different doses by color, or separate into different panels. If coloring by dose, do the individuals in the different dose groups overlap across doses? Does there seem to be more variability at higher or lower concentrations?
<!--END_EXPLANATION-->

### Spaghetti plots of Count over time, faceted by dose

```{r, error = TRUE, cache = TRUE, fig.width = 10, fig.height = 3, warning = FALSE, message = FALSE}

gg <- ggplot(data = pd_data, 
             aes(x = NOMTIME, y = LIDV))
gg <- gg + geom_point(size = 2, alpha = 0.5) 
gg <- gg + geom_line(aes(group = ID), alpha = 0.5) 
gg <- gg + guides(color = guide_legend(""), fill = guide_legend(""))
gg <- gg + xgx_scale_x_time_units(units_dataset = "h", units_plot = "d")
gg <- gg + ylab(pd_label)
gg <- gg + facet_grid(~TRTACT_low2high)
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

## Explore irregularities in profiles
<!--START_EXPLANATION-->
Plot individual profiles in order to inspect them for any irregularities. Inspect the profiles for outlying data points that may skew results or bias conclusions.
<!--END_EXPLANATION-->

### Count over time, faceted by individual, individual line plots

```{r, error = TRUE, cache = TRUE, fig.width = 10, fig.height = 8, warning = FALSE, message = FALSE}

ncol = length(unique(pd_data$ID))/length(unique(pd_data$DOSE))

gg <- ggplot(data = pd_data, aes(x = NOMTIME, y = LIDV))
gg <- gg + geom_point(size = 2) 
gg <- gg + geom_line(aes(group = ID)) 
gg <- gg + guides(color = guide_legend(""), fill = guide_legend(""))
gg <- gg + xgx_scale_x_time_units(units_dataset = "h", units_plot = "d")
gg <- gg + facet_wrap(~ID + TRTACT_low2high, ncol = ncol)
gg <- gg + theme(panel.grid.minor.x = ggplot2::element_line(color = rgb(0.9, 0.9, 0.9)), 
                 panel.grid.minor.y = ggplot2::element_line(color = rgb(0.9, 0.9, 0.9)))
gg <- gg + xgx_annotate_status(status, fontsize = 4, color = rgb(0.5, 0.5, 1))
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

## Explore covariate effects on PD
<!--START_EXPLANATION-->
Stratify by covariates of interest to explore whether any key covariates impact response. For examples of plots and code startifying by covariate, see [Single Ascending Dose Covariate Section](Single_Ascending_Dose_PK.html#explore_covariate_effects_on_pk)

**Warning** Be careful of interpreting covariate effects on PD. Covariate effects on PD could be the result of covariate effects on PK transfering to PD through the PK/PD relationship.
<!--END_EXPLANATION-->

## Explore Dose-Response Relationship

### Count vs Dose, colored by time, median, 5th & 95th percentiles by nominal time

```{r, error = TRUE, cache = TRUE, fig.width = 8, fig.height = 5, warning = FALSE, message = FALSE} 

data_to_plot <- pd_data %>% subset(PROFDAY %in% PD_PROFDAYS)

gg <- ggplot(data = data_to_plot, 
             aes(x = DOSE, y = LIDV, color = DAY_label, fill = DAY_label))
gg <- gg + xgx_geom_pi(percent_level = 0.95, 
                       geom = c("point", "errorbar"), position = position_dodge(50))
gg <- gg + guides(color = guide_legend(""), fill = guide_legend(""))
gg <- gg + scale_x_continuous(breaks = unique(data_to_plot$DOSE))
gg <- gg + xlab(dose_label) + ylab(pd_label)
gg <- gg + geom_smooth(method = "glm", method.args = list(family = poisson), position = position_dodge(50))
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

### Count vs Dose, faceted by Time, boxplots by nominal time

```{r, error = TRUE, cache = TRUE, fig.width = 5, fig.height = 4, warning = FALSE, message = FALSE}
 
data_to_plot  <- pd_data %>% subset(PROFDAY %in% SS_PROFDAY)

gg <- ggplot(data = data_to_plot, 
             aes(x = DOSE, y = LIDV))
gg <- gg + geom_boxplot(aes(group = DOSE))
gg <- gg + guides(color = guide_legend(""), fill = guide_legend(""))
gg <- gg + geom_smooth(method = "glm", method.args = list(family = poisson), color = "black")
gg <- gg + xlab(dose_label) 
gg <- gg + ylab(pd_label)
gg <- gg + facet_grid(~DAY_label)
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 

```

### Count vs Dose, faceted by Time, boxplots by nominal time

```{r, error = TRUE, cache = TRUE, fig.width = 10, fig.height = 4, warning = FALSE, message = FALSE}
 
gg %+% (data = pd_data %>% subset(PROFDAY %in% PD_PROFDAYS))

```

### Percent of subjects by Count

```{r, error = TRUE, cache = TRUE, fig.width = 5, fig.height = 4, warning = FALSE, message = FALSE}
data_to_plot <- pd_data %>% subset(PROFDAY %in% SS_PROFDAY)

gg <- ggplot(data = data_to_plot, aes(x = factor(DOSE), fill = count_high2low))
gg <- gg + geom_bar(position = "fill") + scale_fill_grey(start = 0.8, end = 0.2, breaks = rev(seq(0, 18, 6)))
gg <- gg + scale_y_continuous(labels = scales::percent)
gg <- gg + ylab("Percent of Subjects") + xlab(dose_label) 
gg <- gg + guides(fill = guide_legend(""))
gg <- gg + facet_grid(.~DAY_label)
gg <- gg + xgx_annotate_status(status)
gg <- gg + xgx_annotate_filenames(dirs)

gg 
```

### Percent of subjects by Count, faceted by time

```{r, error = TRUE, cache = TRUE, fig.width = 10, fig.height = 4, warning = FALSE, message = FALSE}
gg %+% (data =  pd_data %>% subset(PROFDAY %in% PD_PROFDAYS))

```


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