## TALOS Depression regression analyses
## Created: 07.apr.2022
## New regression analyses incl anova in new tables based on gtsummary


## =============================================================================
## Data import and formatting
## =============================================================================

dta_all<-read.csv("/Volumes/Data/depression/dep_dataset.csv",na.strings = c("NA","unknown"))

dta<-dta_all[!is.na(dta_all$pase_0),]

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,labels=c("no","yes"))
dta$pad<-factor(dta$pad)
dta$nihss_0<-as.numeric(dta$nihss_0)
dta$age<-as.numeric(dta$age)
dta$rtreat<-factor(dta$rtreat, levels=c("Placebo","Active"))
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"))

dta_b<-dta


library(broom)
library(daDoctoR)
library(lubridate)
library(gtsummary)
library(dplyr)
library(gt)

## =============================================================================
## Regression setup
## =============================================================================

# Possible variables to adjust for
adjs<-c("age","sex","smoke_ever","civil","diabetes", "hypertension", "afli","pad","nihss_0","rep_any")

preds<-c("pase_0_bin","rtreat")

# Selection of variables
print(adjs_10<-rep_lm(meas = "mdi_6",string=c(preds,adjs),data=dta,cut.p = .1)[[2]])

## =============================================================================
## One month regression
## =============================================================================

out="mdi_1_enr"

ds<-dta%>%select(all_of(c(out,preds,adjs_10)))

# AOV
# aov<-aov(as.formula(paste(out,".",sep = "~")), 
#          data = ds) %>% 
#   tidy() %>%
#   gt()

# Interaction
int<-lm(as.formula(paste(out,paste0(paste(preds,collapse = "*"),"+."),sep = "~")), 
        data = ds) %>% 
  tbl_regression()%>%
  # add_n%>%
  bold_labels() %>%
  italicize_levels()

# Bivariate
biv<-tbl_uvregression(data=ds,
                      y=out,
                      method=lm
)  %>% 
  bold_labels() %>%
  add_n%>%
  italicize_levels()

# Multivariate
mul<-lm(as.formula(paste(out,".",sep = "~")), 
        data = ds) %>% 
  tbl_regression()%>% 
  bold_labels() %>%
  add_n()%>%
  italicize_levels()

# Table merge
tbl_comb<-tbl_merge(
  tbls = list(biv, mul,int), 
  tab_spanner = c("**Bivariate**", 
                  "**Multivariate**",
                  "**Multivariate with interaction**") 
)

tbl_comb

tbl_reg_1_rtf <- file("tbl_reg_1.RTF", "w")
writeLines(tbl_comb%>%as_gt()%>%as_rtf(), tbl_reg_1_rtf)
close(tbl_reg_1_rtf)

## =============================================================================
## Six month regression
## =============================================================================

out="mdi_6_newobs_enr"

ds<-dta%>%select(all_of(c(out,preds,adjs_10)))

# AOV
# aov<-aov(as.formula(paste(out,".",sep = "~")), 
#          data = ds) %>% 
#   tidy() %>%
#   gt()

# Interaction
int<-lm(as.formula(paste(out,paste0(paste(preds,collapse = "*"),"+."),sep = "~")), 
        data = ds) %>% 
  tbl_regression()%>%
  # add_n%>%
  bold_labels() %>%
  italicize_levels()

# Bivariate
biv<-tbl_uvregression(data=ds,
                      y=out,
                      method=lm
)  %>% 
  bold_labels() %>%
  add_n%>%
  italicize_levels()

# Multivariate
mul<-lm(as.formula(paste(out,".",sep = "~")), 
        data = ds) %>% 
  tbl_regression()%>% 
  bold_labels() %>%
  add_n()%>%
  italicize_levels()

# Table merge
tbl_comb<-tbl_merge(
  tbls = list(biv, mul,int), 
  tab_spanner = c("**Bivariate**", 
                  "**Multivariate**",
                  "**Multivariate with interaction**") 
)

tbl_comb

tbl_reg_6_rtf <- file("tbl_reg_6.RTF", "w")
writeLines(tbl_comb%>%as_gt()%>%as_rtf(), tbl_reg_1_rtf)
close(tbl_reg_6_rtf)
