## Overview

This document contains exploratory plots for ordinal response 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_ordinal.Rmd). You may also download the Multiple Ascending Dose PK/PD dataset for your reference ([download dataset](Data/Multiple_Ascending_Dose_Dataset2.csv)).

Ordinal data can be thought of as categorical data that has a natural order. For example, mild, moderate or severe. Another example could be Grade 1, Grade 2, Grade 3. Ordinal data can also come out of stratifying continuous data, for example binning a continuous variable into quartiles, or defining (arbitrary or meaningful) cutoffs for a continuous variable. Since ordinal data has a natural order, it is important to preserve that order when creating plots. 

## Setup

```{r, echo = TRUE, warning=FALSE, message=FALSE}
# remove reference to home directory in libPaths
.libPaths(grep("home", .libPaths(), value=TRUE, invert=TRUE))
.libPaths(grep("usr", .libPaths(), value=TRUE, invert=TRUE))

# add localLib to libPaths for locally installed packages
.libPaths(c("localLib", .libPaths()))

# will load from first filepath first, then look in .libPaths for more packages not in first path
# version matches package in first filepath, in the case of multiple instances of a package

# library(rmarkdown)
library(gridExtra)
library(grid)
library(ggplot2)
library(dplyr)
library(RxODE)
library(caTools)

#flag for labeling figures as draft
  draft.flag      = TRUE

## ggplot settings
theme_set(theme_bw(base_size=12))

```


## Define Useful Graphics Functions

```{r}

# annotation of plots with status of code
AnnotateStatus <- function(draft.flag, log.y=FALSE, log.x=FALSE, fontsize=7, color="grey") {
  
  x.pos <- -Inf
  if (log.x)
    x.pos <- 0
  
  y.pos <- -Inf
  if (log.y)
    y.pos <- 0
  
  if(draft.flag) {
    annotateStatus <- annotate("text",
                               label="DRAFT",
                               x=x.pos, y=y.pos,
                               hjust=-0.1, vjust=-1.0,
                               cex=fontsize,
                               col=color, alpha=0.7, fontface="bold")
  } else {
    annotateStatus <- NULL
  }
  
  return(annotateStatus)
}

```

## Load Dataset

```{r, warning=FALSE, message=FALSE}
my.data <- read.csv("../Data/Multiple_Ascending_Dose_Dataset2.csv")

my.data$TRTACT <- factor(my.data$TRTACT,levels = c("Placebo",paste(unique(my.data$DOSE[my.data$DOSE!=0]),"mg")))

my.data$Severity <- my.data$LIDV
my.data[my.data$CMT!=5,]$Severity <- NA
my.data$Severity_label <- factor(plyr::mapvalues(my.data$Severity,c(1,2,3), c("Mild", "Moderate","Severe")), levels = c("Mild","Moderate","Severe"))

```

## Provide an overview of the data

### Percent of subjects by response category over time, faceted by dose

```{r, cache = TRUE, fig.width = 10, fig.height = 3, warning = FALSE, message= FALSE}
data_to_plot <- my.data[my.data$CMT==5,]

data_to_plot$Response <- factor(data_to_plot$Severity_label, levels = rev(levels(data_to_plot$Severity_label)))
# data_to_plot$Response <- data_to_plot$Severity_label

gg <- ggplot(data = data_to_plot, aes(x = factor(PROFDAY), fill = Response)) + theme_bw()
gg <- gg + geom_bar(position = "fill") + scale_y_continuous(labels = scales::percent)
gg <- gg + ylab("Responder Rate (%)") + xlab("Time (days)") + guides(fill=guide_legend(""))
gg <- gg + scale_fill_brewer(palette=6)
gg + facet_grid(.~TRTACT)
```


## Explore variability

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?

### Spaghetti plots of ordinal response over time, faceted by dose

```{r, cache = TRUE, fig.width = 10, fig.height = 3, warning = FALSE, message= FALSE}
 
data_to_plot <- my.data[my.data$CMT==5,]
set.seed(12345)
data_to_plot$Severity2 <- jitter(data_to_plot$Severity, amount=0.1)
data_to_plot$DAY2 <- jitter(data_to_plot$PROFDAY, amount=0.1)

gg <- ggplot(data = data_to_plot, 
               aes(x=DAY2,y=Severity2))+theme_bw()
  gg <- gg + geom_point( size=2, alpha = 0.3) +  geom_line( aes(group = ID), alpha = 0.3) 
  gg <- gg + guides(color=guide_legend(""),fill=guide_legend(""))
  gg <- gg  + xlab("Time (days)")
  gg <- gg + facet_grid(~TRTACT)
    gg <- gg + ylab("Severity") + scale_y_continuous(breaks=c(1,2,3), labels=c("Mild","Moderate","Severe"))
gg

```


## Explore irregularities in profiles

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.

### Ordinal response over time, faceted by individual, individual line plots

```{r, cache = TRUE, fig.width = 10, fig.height = 8, warning = FALSE, message= FALSE}
 
data_to_plot <- my.data[my.data$CMT==5,]

gg <- ggplot(data = data_to_plot, 
               aes(x=PROFDAY,y=Severity_label))+theme_bw()
  gg <- gg + geom_point( size=2) +  geom_line( aes(group = ID)) 
  gg <- gg + guides(color=guide_legend(""),fill=guide_legend(""))
  gg <- gg  + xlab("Time (days)")+ scale_x_continuous(breaks = seq(0,max(data_to_plot$PROFDAY)+1,7))
  gg <- gg + facet_wrap(~ID+TRTACT,ncol = length(unique(data_to_plot$ID))/length(unique(data_to_plot$DOSE)) )
    gg <- gg + ylab("Severity") #+ scale_y_continuous(labels=scales::percent)
gg

```

## Explore covariate effects on PD

(coming soon)

## Explore Dose-Response Relationship

### Percent of subjects by response category against dose, faceted by time

```{r, cache = TRUE, fig.width = 5, fig.height = 4, warning = FALSE, message= FALSE}
data_to_plot <- my.data[my.data$CMT==5,]
data_to_plot$DAY_label <- paste("Day", data_to_plot$PROFDAY)
data_to_plot$DAY_label[data_to_plot$DAY_label=="Day 0"] = "Baseline"
data_to_plot <- data_to_plot[data_to_plot$DAY_label%in%c("Day 5"),]

# data_to_plot$DAY_label <- factor(data_to_plot$DAY_label, levels = rev(sort(unique(data_to_plot$DAY_label))))

data_to_plot$Response <- factor(data_to_plot$Severity_label, levels = rev(levels(data_to_plot$Severity_label)))

gg <- ggplot(data = data_to_plot, aes(x = factor(DOSE), fill = Response)) + theme_bw()
gg <- gg + geom_bar(position = "fill") + scale_y_continuous(labels = scales::percent)
gg <- gg + ylab("Responder Rate (%)") + xlab("Dose (mg)") + guides(fill=guide_legend(""))
gg <- gg + scale_fill_brewer(palette=6)
gg + facet_grid(.~DAY_label)
```


### Percent of subjects by response category against dose, faceted by day

```{r, cache = TRUE, fig.width = 8, fig.height = 3, warning = FALSE, message= FALSE}
data_to_plot <- my.data[my.data$CMT==5,]
data_to_plot$DAY_label <- paste("Day", data_to_plot$PROFDAY)
data_to_plot$DAY_label[data_to_plot$DAY_label=="Day 0"] = "Baseline"
data_to_plot <- data_to_plot[data_to_plot$DAY_label%in%c("Day 1","Day 3","Day 5"),]

# data_to_plot$DAY_label <- factor(data_to_plot$DAY_label, levels = rev(sort(unique(data_to_plot$DAY_label))))

data_to_plot$Response <- factor(data_to_plot$Severity_label, levels = rev(levels(data_to_plot$Severity_label)))

gg <- ggplot(data = data_to_plot, aes(x = factor(DOSE), fill = Response)) + theme_bw()
gg <- gg + geom_bar(position = "fill") + scale_y_continuous(labels = scales::percent)
gg <- gg + ylab("Responder Rate (%)") + xlab("Dose (mg)") + guides(fill=guide_legend(""))
gg <- gg + scale_fill_brewer(palette=6)
gg + facet_grid(.~DAY_label)
```

### Ordinal response against dose

```{r, cache = TRUE, fig.width = 5, fig.height = 4, warning = FALSE, message= FALSE}
data_to_plot <- my.data[my.data$CMT==5,]
data_to_plot$DAY_label <- paste("Day", data_to_plot$PROFDAY)
data_to_plot$DAY_label[data_to_plot$DAY_label=="Day 0"] = "Baseline"
data_to_plot <- data_to_plot[data_to_plot$DAY_label%in%c("Day 5"),]


data_to_plot$TRTACT <- factor(data_to_plot$TRTACT, levels = rev(levels(data_to_plot$TRTACT)))

gg <- ggplot(data = data_to_plot, aes(y=DOSE,x=Severity_label))+theme_bw()
gg <- gg + geom_jitter(data = data_to_plot, 
                       aes(color = TRTACT), shape=19,  width = 0.1, height = 0, alpha = 0.5)
gg <- gg + geom_boxplot(width = 0.5, fill = NA, outlier.shape=NA) 
gg <- gg + guides(color=guide_legend(""),fill=guide_legend(""))
gg <- gg + coord_flip() 
gg <- gg + xlab("Severity") + ylab("Dose (mg)")
gg + facet_grid(.~DAY_label)

```

### Ordinal response against dose, faceted by day

```{r, cache = TRUE, fig.width = 8, fig.height = 3, warning = FALSE, message= FALSE}
data_to_plot <- my.data[my.data$CMT==5,]
data_to_plot$DAY_label <- paste("Day", data_to_plot$PROFDAY)
data_to_plot$DAY_label[data_to_plot$DAY_label=="Day 0"] = "Baseline"
data_to_plot <- data_to_plot[data_to_plot$DAY_label%in%c("Day 1","Day 3","Day 5"),]


data_to_plot$TRTACT <- factor(data_to_plot$TRTACT, levels = rev(levels(data_to_plot$TRTACT)))

gg <- ggplot(data = data_to_plot, aes(y=DOSE,x=Severity_label))+theme_bw()
gg <- gg + geom_jitter(data = data_to_plot, 
                       aes(color = TRTACT), shape=19,  width = 0.1, height = 0, alpha = 0.5)
gg <- gg + geom_boxplot(width = 0.5, fill = NA, outlier.shape=NA) 
gg <- gg + guides(color=guide_legend(""),fill=guide_legend(""))
gg <- gg + coord_flip() 
gg <- gg + xlab("Severity") + ylab("Dose (mg)")
gg + facet_grid(.~DAY_label)

```


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