---
title: "Patient flowchart and chi^2 tests"
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)
```

# 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)
```

# Backup
```{r}
dta_b<-dta
```


# Sammentællinger
```{r}
summary(cbind(is.na(dta_all[,c("pase_0","mdi_1","mdi_1_enr","mdi_6_newobs","mdi_6_newobs_enr")]),
      both_missing=is.na(dta$mdi_1)&is.na(dta$mdi_6_newobs),
      either_missing=is.na(dta$mdi_1)|is.na(dta$mdi_6_newobs)))
```


```{r}
suppressWarnings(summary(cbind(all_particip=dta_all$mors_180=="yes",
              all_pase0=dta$mors_180=="yes",
              all_mdi_1=!is.na(dta$mdi_1)&dta$mors_180=="yes"))) # Antal der dør

table(dta$pase_0_bin,factor(dta$mors_180)) # Antal der dør, stratificeret efter PASE gruppe
```

```{r}
# summary(factor(dta$mors_v1))
# summary(factor(dta$mors_v16)) # OBS medregnet er 2 dødsfald, der ikke har MDI 1.
```

# Flow

## 1 month

Shows counts of all patients withs missing MDI 1 scores.
```{r message=FALSE}
source("/Volumes/Data/depression/function_flow.R") # Home made flow function
show(flow_prog(df=dta[dta$excluded_1%in%c("ex_1","mi_1","ca_1"),],
          sngl=c("mors_v1","drop1"),
          sngl_keep=c("no","yes"),
          mltp=c("open_treat","wants_out","side_effect","side_effect2")))
# v1<-dta$rnumb[dta$excluded_1%in%c("ex_1","mi_1")]
```

Same overview, but vectorised
```{r}
summary(factor(dta$excluded_1))

# dt_1 are organic data, en_1 are enriched, ex_1 are excluded, mi_1 were missing, 
# ca_1 were missing at 1 month, but held data at 6 months, and thus carried along.
```



## 6 months

Shows counts of all patients withs missing MDI 6 scores.
```{r}
show(flow_prog(df=dta[is.na(dta$mdi_6_newobs_enr)&dta$excluded_6%in%c("ex_6"),], # 
          sngl=c("mors_v16","drop16"),
          sngl_keep=c("no","yes"),
          mltp=c("open_treat","wants_out","side_effect","side_effect2")))
# v2<-dta$rnumb[is.na(dta$mdi_6_newobs_enr)&dta$excluded_1%in%c("ca_1","dt_1")]
```

```{r}
summary(factor(dta$excluded_6))

# dt_6 are organic data, en_6 are enriched, ex_6 are excluded, mi_6 were excluded at 1 month.
# At 6 month 118 are excluded due to any cause
# Due to later inclusion of ca_1 patients, the sum of patients excluded at 6 months is 71+62-16=117
```

This flow counts all patients dying or dropping out early after 1 month. Some have a recorded MDI at dropout. This is just to give a perspective on data.
```{r}
show(flow_prog(df=dta, # 
          sngl=c("mors_v16","drop16"),
          sngl_keep=c("no","yes"),
          mltp=c("open_treat","wants_out","side_effect","side_effect2")))
# v2<-dta$rnumb[is.na(dta$mdi_6_newobs_enr)&dta$excluded_1%in%c("ca_1","dt_1")]
```

```{r}
summary(as.numeric(dta$inc_time[dta$drop16=="yes"]))
```


# Chi^2 tests

```{r}
source("/Volumes/Data/depression/function_chi_test_sum.R")
ex_lst<-list()
ex_var<-c("mdi_1_enr","rtreat","pase_0_bin")
for (i in 2:3){
 ex_lst <- append(ex_lst,chi_test_sum(a=is.na(dta[,ex_var[1]]),
                                      b=dta[,ex_var[i]],
                                      aname=ex_var[1],
                                      bname=ex_var[i]))
}
```

```{r}
# ex_var<-c("open_treat","rtreat","pase_0_bin")
# for (i in 2:3){
#  ex_lst <- append(ex_lst,
#                   chi_test_sum(a=dta[,ex_var[1]],
#                                b=dta[,ex_var[i]],
#                                aname=ex_var[1],
#                                bname=ex_var[i]))
# }

## Taget ud grundet for lave tal
```

```{r}
ex_var<-c("mdi_1_enr","rtreat","pase_0_bin")
for (i in 2:3){
 ex_lst <- append(ex_lst,
                  chi_test_sum(a=is.na(dta$mdi_6_newobs_enr)&!is.na(dta$mdi_1_enr),
                                      b=dta[,ex_var[i]],
                                      aname="Excluded at 6 months",
                                      bname=ex_var[i]))
}
for (i in 2:3){
 ex_lst <- append(ex_lst,
                  chi_test_sum(a=is.na(dta$mdi_6_newobs_enr),
                                      b=dta[,ex_var[i]],
                                      aname="Total unavailable at 6 months",
                                      bname=ex_var[i]))
}
```


```{r}
# ex_var<-c("open_treat","rtreat","pase_0_bin")
# for (i in 2:3){
#  ex_lst <- append(ex_lst,
#                   chi_test_sum(a=dta[,ex_var[1]],
#                                b=dta[,ex_var[i]],
#                                aname=ex_var[1],
#                                bname=ex_var[i]))
# }
show(ex_lst)
```
