---
title: "Regression analyses"
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)
```

## Formatting
```{r echo=FALSE}
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$pase_6<-as.numeric(dta$pase_6)
dta$bmi<-as.numeric(dta$bmi)
dta$mdi_6<-as.numeric(dta$mdi_6)
dta$pase_0_bin<-factor(dta$pase_0_bin,levels=c("lower","higher"))
```


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


# Linear regression analysis

```{r}
library(broom)
library(daDoctoR)
library(lubridate)
```

## Tests of variables to adjust for
```{r}
# Possible variables to include
adjs<-c("age","sex","smoke_ever","civil","bmi","diabetes", "hypertension", "afli","pad","nihss_0","rep_any")
```

```{r}
# Variables with p<10% i bivariable linear regression analysis
print(adjs_10<-rep_lm(meas = "mdi_6",string=c("pase_0_bin","rtreat",adjs),data=dta,cut.p = .1)[[2]])
```

## True mean estimations (adjusted)

```{r}
strt<-append(print_pred_stratum(meas = "mdi_6",adj=unique(c("pase_0_bin",adjs_10)),strat="rtreat",data=dta,include.stratum = T),print_pred_stratum(meas = "mdi_6",adj=c(adjs_10[adjs_10!="pase_0_bin"],"rtreat"),strat="pase_0_bin",data=dta,include.stratum = T))

for (i in 1:length(strt)){
  write.csv(strt[[i]][[1]],paste0("tbl_md6_",substr(names(strt)[i],1,3),".csv"))
}

c<-c()
for (i in 1:length(strt)){
  c<-c(c,paste("Estimated true mean,",names(strt)[i]),strt[[i]][[5]])
}
mat_true<-matrix(c(c,c("Variables adjusted for:",paste(c("rtreat",adjs_10), collapse=', '))), ncol=2, byrow = T)
```

# MDI outcome 2x2 

```{r}
sts<-c("pase_0_bin","rtreat")
# sts<-c("rtreat","pase_0_bin")
adjs_10m<-adjs_10[adjs_10!="pase_0_bin"]
```

## One month

### Enriched

```{r echo=FALSE}
outs<-c("mdi_1_enr")

source("/Volumes/Data/depression/script_regression_frame.R")

show(reg_frm)
write.csv(reg_frm,paste0(outs,"_matrix_",today(),".csv"))
```


### Raw
```{r echo=FALSE}
outs<-c("mdi_1")

source("/Volumes/Data/depression/script_regression_frame.R")

show(reg_frm)
write.csv(reg_frm,paste0(outs,"_matrix_",today(),".csv"))
```


## Six months

### New Observations - enriched
```{r echo=FALSE}
outs<-c("mdi_6_newobs_enr")  # Outcome is based on new observations for 2nd (6 months) visit

# mean(dta$inc_time[!is.na(dta[,outs])])
# quantile(dta$inc_time[!is.na(dta[,outs])])

source("/Volumes/Data/depression/script_regression_frame.R")

show(reg_frm)
write.csv(reg_frm,paste0(outs,"_matrix_",today(),".csv"))
```

### New Observations
```{r echo=FALSE}
outs<-c("mdi_6_newobs")  # Outcome is based on new observations for 2nd (6 months) visit

# mean(dta$inc_time[!is.na(dta[,outs])])
# quantile(dta$inc_time[!is.na(dta[,outs])])

source("/Volumes/Data/depression/script_regression_frame.R")

show(reg_frm)
write.csv(reg_frm,paste0(outs,"_matrix_",today(),".csv"))
```

### New observations - adjusted for 6 months PASE
```{r echo=FALSE}
outs<-c("mdi_6_newobs")
adjs_10m<-c(adjs_10[adjs_10!="pase_0_bin"],"pase_6")

# mean(dta$inc_time[!is.na(dta[,outs])])
# quantile(dta$inc_time[!is.na(dta[,outs])])

source("/Volumes/Data/depression/script_regression_frame.R")

show(reg_frm)
write.csv(reg_frm,paste0(outs,"_matrix_",today(),".csv"))
```


## Dichotomized sensitivity analysis

```{r}
dta$composite_out<-case_when(dta$open_treat=="yes"|(dta$mdi_6_newobs-dta$mdi_1)>5~"yes",
          is.na(dta$mdi_6_newobs)~"NA",
          is.na(dta$mdi_1)~"NA",
          TRUE~"no")
dta$composite_out[dta$composite_out=="NA"]<-NA
summary(dta$composite_out<-factor(dta$composite_out))
```

## Enriching and cleaning variables

```{r}
# Enriching
dta$pad[is.na(dta$pad)]<-"no"
dta$hypertension[is.na(dta$hypertension)]<-"no"

# Cleaning
dta$civil<-factor(ifelse(dta$civil=="unknown",NA,dta$civil))
```


```{r}
table(dta$rtreat,dta$pase_0_bin)

outs<-"composite_out"
sts<-c("pase_0_bin","rtreat")
# sts<-c("rtreat","pase_0_bin")
adjs_10m<-adjs_10[adjs_10!="pase_0_bin"]
dta_frm<-dta[!is.na(dta$composite_out),c(outs,sts,adjs_10m)]

summary(dta_frm)

# colnames(dta_frm)[1]<-"outs"

# print_log(meas="composite_out",var=sts[2],adj=c(sts[1],adjs_10m),data=dta_frm)

# print_pred(meas="composite_out",adj=c(sts[2],adjs_10m),data=dta_frm[dta_frm$pase_0_bin=="lower",],n.by.adj = T)

composite_out_lst<-list(print_pred_stratum(meas="composite_out",strat = sts[1],adj=c(sts[2],adjs_10m),
                                           data=dta_frm,n.by.adj = T),
                        print_pred_stratum(meas="composite_out",strat = sts[2],adj=c(sts[1],adjs_10m),
                                           data=dta_frm,n.by.adj = T))

# show(composite_out_lst)
capture.output(show(composite_out_lst), 
               file = paste0("composite_out_lst",today(),".txt"))

```



