---
title: "Additional data analysis"
author: "Andreas Gammelgaard Damsbo"
date: "Knitted: `r format(Sys.time(), '%d %B, %Y')`"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```

# Import
```{r}
dta_all<-read.csv("/Volumes/Data/depression/dep_dataset.csv")
```

# Defining patients to include for analysis
Only including cases with complete pase_0 and MDI at 1 & 6 months
```{r}
dta<-dta_all[!is.na(dta_all$pase_0),]
# &!is.na(dta$mdi_1)&!is.na(dta$mdi_6)
quantile(dta$pase_0, probs = seq(0, 1, 0.25), names = TRUE)
```

## Formatting
```{r echo=FALSE}
dta$rtreat<-factor(dta$rtreat)
dta$pase_6<-as.numeric(dta$pase_6)
```

# Summaries

Fraction with inc_time of at least 166 days
```{r}
dt<-as.numeric(dta[dta$excluded_6%in%c("dt_6","en_6"),c("inc_time")])>=166
summary(dt)
length(dt[dt==TRUE])/length(dt)*100 # Percent after 166 days
```

5% percentiler
```{r}
quantile(dt, probs = seq(0, 1, 0.05), names = TRUE)
```


Base version
```{r}
aggregate(pase_6 ~ rtreat, data = dta, summary)
```

Fancy version
```{r}
psych::describeBy(dta$pase_6, dta$rtreat,mat=T)
```

# Mann-Whitney U test
See: https://stat-methods.com/home/mann-whitney-u-r/

```{r}
#Perform the Mann-Whitney U test
m1<-wilcox.test(pase_6 ~ rtreat, data=dta, na.rm=TRUE, 
                paired=FALSE, exact=FALSE, conf.int=TRUE)
print(m1)
```



# Boxplot

## Base function - simple
```{r}
boxplot(dta$pase_6 ~ dta$rtreat)
```

## ggplot2 - fancy version
```{r}
library(ggplot2)
 ggplot(dta, aes(x = rtreat, y = pase_6, fill = rtreat)) +
  stat_boxplot(geom ="errorbar", width = 0.5) +
  geom_boxplot(fill = "light blue") + 
  stat_summary(fun.y=mean, geom="point", shape=10, size=3.5, color="black") +   
   # Point symbol is mean value
  ggtitle("Boxplot of Treatments C and D") + 
  theme_bw() + theme(legend.position="none")
```

# Bonus: QQ plots
```{r}
library(qqplotr)
ggplot(data = dta, mapping = aes(sample = pase_6, color = rtreat, fill = rtreat)) +
  stat_qq_band(alpha=0.5, conf=0.95, qtype=1, bandType = "boot") +
  stat_qq_line(identity=TRUE) +
  stat_qq_point(col="black") +
  facet_wrap(~ rtreat, scales = "free") +
  labs(x = "Theoretical Quantiles", y = "Sample Quantiles") + theme_bw()
```

