---
title: "Table One - enriched"
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<-read.csv("/Volumes/Data/depression/dep_dataset.csv")
```

## Formatting
```{r}
dta$diabetes<-factor(dta$diabetes)
dta$pad<-factor(dta$pad)
dta$civil<-factor(dta$civil)
dta$hypertension<-factor(dta$hypertension)
dta$afli<-factor(dta$afli)
dta$smoke_ever<-factor(dta$smoke_ever)
dta$ami<-factor(dta$ami)
dta$tci<-factor(dta$tci)
dta$thrombolysis<-factor(dta$thrombolysis)
dta$thrombechtomy<-factor(dta$thrombechtomy)
dta$rep_any<-factor(dta$rep_any)
dta$pad<-factor(dta$pad)
dta$nihss_0<-as.numeric(dta$nihss_0)
dta$age<-as.numeric(dta$age)
dta$rtreat<-factor(dta$rtreat)
dta$sex<-factor(dta$sex)
dta$pase_0<-as.numeric(dta$pase_0)
dta$bmi<-as.numeric(dta$bmi)
dta$mdi_6<-as.numeric(dta$mdi_6)
dta$inc_time<-as.numeric(dta$inc_time)

dta$bmi_isna<-is.na(dta$bmi)
dta$nihss_0_isna<-is.na(dta$nihss_0)
```

# Defining patients to include for analysis
```{r  message=FALSE}
library(dplyr)
```


Only including cases with complete pase_0 and MDI at 1 & 6 months
```{r}
dta<-dta[!is.na(dta$pase_0),]
# &!is.na(dta$mdi_1)&!is.na(dta$mdi_6)
```

## Defining table one stratification
```{r}
dta$strat_table_one<-factor(case_when(dta$excluded_6%in%c("mi_6","ex_6")~"zExcluded",
                         dta$pase_0_bin=="lower"~"xLower",
                         dta$pase_0_bin=="higher"~"yHigher"))

summary(dta$strat_table_one)
```

```{r}
dta$in_ex<-plyr::mapvalues(dta$strat_table_one, 
                           from=c("xLower", "yHigher"), 
                           to=c("xIncluded","xIncluded"))
# summary(dta$in_ex)
```


# Basic analyses
```{r}
show(mdn<-median(dta$pase_0))
hist(dta$pase_0,100)
hist(sqrt(dta$pase_0),100)
```

# Table One
```{r}
library(tableone)
```

```{r}
tbl_norm<-c("rtreat","age","sex","bmi","bmi_isna","smoke_ever","civil","diabetes", 
            "hypertension", "afli", "ami", "tci","pad","nihss_0","nihss_0_isna", 
            "thrombolysis", "thrombechtomy","rep_any","inc_time")

tbl_cat<-c("rtreat","sex","bmi_isna","diabetes", "hypertension", "smoke_ever","civil", 
           "ami", "tci","nihss_0_isna", "thrombolysis", 
           "thrombechtomy","rep_any")

tbl_non<-c("age","nihss_0","inc_time")
```

```{r}
tab1 <- CreateTableOne(vars = tbl_norm, data = dta, 
                       factorVars = tbl_cat,includeNA = TRUE)

tbl1_1<-print(tab1, contDigits = 1, missing=T,showAllLevels=T ,
              nonnormal = tbl_non, smd = FALSE, quote = F, noSpaces = TRUE)
```

```{r}
tab2 <- CreateTableOne(vars = tbl_norm, strata="pase_0_bin",
                       data = dta, factorVars = tbl_cat,includeNA = T)

tbl1_2<-print(tab2, contDigits = 1, missing=T,showAllLevels=T,
              nonnormal = tbl_non, smd = F,test = T, quote = F, noSpaces = TRUE)
```

```{r}
tab3 <- CreateTableOne(vars = tbl_norm, strata="strat_table_one",
                       data = dta, factorVars = tbl_cat,includeNA = T)
tbl1_3<-print(tab3, contDigits = 1, missing=T,showAllLevels=T,
              nonnormal = tbl_non, smd = F,test = T, quote = F, noSpaces = TRUE)
```

```{r}
tab4 <- CreateTableOne(vars = tbl_norm, strata="in_ex",
                       data = dta, factorVars = tbl_cat,includeNA = T)

tbl1_4<-print(tab4, contDigits = 1, missing=T,showAllLevels=T,
              nonnormal = tbl_non, smd = F,test = T, quote = F, noSpaces = TRUE)
```

```{r}
table(is.na(dta$nihss_0),dta$strat_table_one)
table(is.na(dta$bmi),dta$strat_table_one)
```


```{r}
dta<-dta[dta$strat_table_one!="zExcluded",]
dta$strat_table_one<-factor(dta$strat_table_one)
tab5 <- CreateTableOne(vars = tbl_norm, strata="strat_table_one",
                       data = dta, factorVars = tbl_cat,includeNA = T)

tbl1_5<-print(tab5, contDigits = 1, missing=T,showAllLevels=T,
              nonnormal = tbl_non, smd = F,test = T, quote = F, noSpaces = TRUE)
```

```{r}
tbl_list<-list(tbl1_1,tbl1_2,tbl1_3,tbl1_4,tbl1_5)
for (i in 1:length(tbl_list)){
  nm<-paste0("tbl1_",i)
  write.csv(tbl_list[[i]],
            paste0("/Volumes/Data/depression/",nm,"_enr_",
                   lubridate::today(),".csv"))
}
```
