#load libraries 
library(pacman)
p_load("tidyverse","ggplot2", "here","gridExtra","rgeos","rgdal","rmapshaper", "reactable", "kableExtra","gtsummary","Hmisc","arsenal","lme4","missRanger","mice","broom.mixed","DiagrammeR","zoo","base","psych","readxl","haven","vctrs","dplyr","ggsci","anytime","foreign","mgcv","survival","survminer","stringr","matrixStats") 

sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 14393)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
## [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
## [5] LC_TIME=English_Australia.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] matrixStats_0.57.0 survminer_0.4.9    ggpubr_0.4.0       mgcv_1.8-36       
##  [5] nlme_3.1-149       foreign_0.8-80     anytime_0.3.9      ggsci_2.9         
##  [9] vctrs_0.3.8        haven_2.4.1        readxl_1.3.1       psych_2.0.12      
## [13] zoo_1.8-8          DiagrammeR_1.0.6.1 broom.mixed_0.2.7  mice_3.13.0       
## [17] missRanger_2.1.3   lme4_1.1-26        Matrix_1.2-18      arsenal_3.6.3     
## [21] Hmisc_4.5-0        Formula_1.2-4      survival_3.2-7     lattice_0.20-41   
## [25] gtsummary_1.4.2    kableExtra_1.3.4   reactable_0.2.3    rmapshaper_0.4.5  
## [29] rgdal_1.5-18       rgeos_0.5-5        sp_1.4-4           gridExtra_2.3     
## [33] here_1.0.1         forcats_0.5.1      stringr_1.4.0      dplyr_1.0.7       
## [37] purrr_0.3.4        readr_1.4.0        tidyr_1.1.3        tibble_3.1.2      
## [41] ggplot2_3.3.5      tidyverse_1.3.1    pacman_0.5.1      
## 
## loaded via a namespace (and not attached):
##  [1] minqa_1.2.4         colorspace_2.0-0    ggsignif_0.6.0     
##  [4] rio_0.5.16          ellipsis_0.3.2      rprojroot_2.0.2    
##  [7] htmlTable_2.1.0     base64enc_0.1-3     fs_1.5.0           
## [10] httpcode_0.3.0      rstudioapi_0.13     fansi_0.4.1        
## [13] lubridate_1.7.10    ranger_0.13.1       xml2_1.3.2         
## [16] splines_4.0.3       mnormt_2.0.2        knitr_1.30         
## [19] geojsonlint_0.4.0   jsonlite_1.7.2      nloptr_1.2.2.2     
## [22] gt_0.3.0            km.ci_0.5-2         broom_0.7.8        
## [25] cluster_2.1.0       dbplyr_2.1.1        png_0.1-7          
## [28] compiler_4.0.3      httr_1.4.2          backports_1.2.0    
## [31] assertthat_0.2.1    cli_3.0.1           visNetwork_2.1.0   
## [34] htmltools_0.5.0     tools_4.0.3         gtable_0.3.0       
## [37] glue_1.4.2          V8_3.4.2            Rcpp_1.0.7         
## [40] carData_3.0-4       cellranger_1.1.0    crul_1.1.0         
## [43] svglite_2.0.0       broom.helpers_1.3.0 xfun_0.19          
## [46] openxlsx_4.2.3      rvest_1.0.0         lifecycle_1.0.0    
## [49] rstatix_0.6.0       statmod_1.4.35      MASS_7.3-53        
## [52] scales_1.1.1        hms_1.1.0           parallel_4.0.3     
## [55] RColorBrewer_1.1-2  yaml_2.2.1          curl_4.3           
## [58] KMsurv_0.1-5        rpart_4.1-15        latticeExtra_0.6-29
## [61] stringi_1.5.3       jsonvalidate_1.3.1  checkmate_2.0.0    
## [64] zip_2.1.1           boot_1.3-25         rlang_0.4.11       
## [67] pkgconfig_2.0.3     systemfonts_1.0.2   evaluate_0.14      
## [70] htmlwidgets_1.5.3   tidyselect_1.1.0    magrittr_2.0.1     
## [73] R6_2.5.0            generics_0.1.0      DBI_1.1.0          
## [76] pillar_1.6.1        withr_2.3.0         abind_1.4-5        
## [79] nnet_7.3-14         car_3.0-10          modelr_0.1.8       
## [82] crayon_1.4.1        survMisc_0.5.5      utf8_1.1.4         
## [85] tmvnsim_1.0-2       rmarkdown_2.6       jpeg_0.1-8.1       
## [88] grid_4.0.3          data.table_1.13.4   FNN_1.1.3          
## [91] reprex_2.0.0        digest_0.6.27       webshot_0.5.2      
## [94] xtable_1.8-4        munsell_0.5.0       viridisLite_0.3.0

Import data

Adult survey data and ED data

## NULL
load("S:/R-MNHS-SPHPM-SRH/Hazelwood/Study Streams/Hazelinks/Hospital/Analysis/Emergency/Data/tmp_adult.Rdata")
load("S:/R-MNHS-SPHPM-SRH/Hazelwood/Study Streams/Hazelinks/Hospital/Analysis/Emergency/Data/tmp_ED_survival.Rdata")

Descriptive tables

Morwell vs Sale characteristics

Consenters vs Non-consenters

Boxplots

Exposure

Pre and Post counts per person

## TableGrob (1 x 2) "arrange": 2 grobs
##   z     cells    name           grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]

Multiple Imputation(MI)

Imputate missing survey data

# library(mice)
# 
# # Risk factors requiring imputation are: Gender(2), Education(15), employment(14), smoking group (18)
# 
# input<-adult[,c("HAZELWOOD_ID","firePM_TotalDiary","STUDY_GROUP_CODE","gender","Age","Married","Education","Employment","IRSAD_Score","Smoking","Work_Exposed_3grp","weight","pre_ED_no","post_ED_no")]
# 
# #summary(input)
# 
# #Only Gender,Education, Smoking,  Employment, had NAs
# input$gender<-as.factor(as.character(input$gender))
# 
# #do the imputation
# input_data<-mice(input,m=5,maxit = 50,method = "pmm",seed=123, printFlag = FALSE)
# complete_data<-complete(input_data,"all")
# 
# cleaning<-function(data){
#   data<-data %>%
#       mutate(firePM_TotalDiary_per10=firePM_TotalDiary/10)%>%
#       mutate(age_group=ifelse(Age<=64,"20-64 years","65 years or above")) %>%
#       mutate(edu_group=ifelse(is.na(Education),NA,ifelse(Education%in%c("Secondary up to year 10","Secondary year 11-12"),"Up to       secondary","Higher than secondary")))%>%
#       mutate(employ_group=ifelse(is.na(Employment),NA,ifelse(Employment%in%c("Paid employment (FT, PT, self-employed)"),"In paid employment","Not in paid employment")))%>%
#       mutate(smoke_group=ifelse(is.na(Smoking),NA,ifelse(Smoking=="Never","Non smoker","Previous/Current smoker")))%>%
#       mutate(occu_exp_group=ifelse(is.na(Work_Exposed_3grp),NA,ifelse(Work_Exposed_3grp=="Not exposed","Not exposed","Work Exposed")))%>%
#       mutate(age_group=as.factor(age_group),
#              gender=as.factor(gender),
#              edu_group=as.factor(edu_group),
#              occu_exp_group=as.factor(occu_exp_group),
#              employ_group=as.factor(employ_group),
#              smoke_group=as.factor(smoke_group))
#   return(data)
# }
# 
# complete_data<-lapply(complete_data, cleaning)
# 
# save(complete_data,file = "S:/R-MNHS-SPHPM-SRH/Hazelwood/Study Streams/Hazelinks/Hospital/Analysis/Emergency/Data/complete_data.Rdata")


load("../Data/complete_data.Rdata")

Model Functions

Overall MI estimates

Est_Overall<-function(out,data=ED_survival,recurrent=TRUE,weight=TRUE,trunc=5){
 
  
 if(recurrent==TRUE){ ## Recurrent survival 
   
      reg_data<- data %>%
       #browser()  
      filter(outcome==out) %>% 
      filter(event<=trunc)
      
  reg_data2<-lapply(complete_data, function(x) reg_data %>% left_join(x) )
    
  if(weight==TRUE){
     
  model<-lapply(reg_data2,
             function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10 +          gender+Age+Married+edu_group+employ_group + smoke_group  + occu_exp_group+ prefire + STUDY_GROUP_CODE+cluster(HAZELWOOD_ID)+strata(event), method="breslow",data=x ,weights = weight )))[,c("coef","robust se")])
  
    #print(model)
    
    } else{
     
  model<-lapply(reg_data2,
             function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10 +          gender+Age+Married+edu_group+employ_group + smoke_group  + occu_exp_group+ prefire + STUDY_GROUP_CODE+cluster(HAZELWOOD_ID)+strata(event), method="breslow",data=x )))[,c("coef","robust se")])

    }
  
  } else { ## First event survival
   
      reg_data<- data %>%
      filter(outcome==out) %>% 
      filter(event<=1)
      
  reg_data2<-lapply(complete_data, function(x) reg_data %>% left_join(x) )

     if(weight==TRUE){
     
  model<-lapply(reg_data2,
             function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10 +          gender+Age+Married+edu_group+employ_group + smoke_group  + occu_exp_group + prefire + STUDY_GROUP_CODE, method="breslow",data=x ,weights = weight )))[,c("coef","robust se")])
  
    } else{
     
  model<-lapply(reg_data2,
             function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10 +gender+Age+Married+edu_group+employ_group + smoke_group  + occu_exp_group+ prefire +STUDY_GROUP_CODE, method="breslow",data=x )))[,c("coef","se(coef)")])
  
    }
}   
  
  names(model)<-c("mi1","mi2","mi3","mi4","mi5")
  result <-cbind(model,as_tibble(model))[,c(1:10)]
  
  result$mi1.se<-result[,2]
  result$mi2.se<-result[,4]
  result$mi3.se<-result[,6]
  result$mi4.se<-result[,8]
  result$mi5.se<-result[,10]
  
  result<-result[,c("mi1.coef","mi2.coef","mi3.coef","mi4.coef","mi5.coef","mi1.se","mi2.se","mi3.se","mi4.se","mi5.se")]

  result$variable<-row.names(result)


  # Combine Beta and Se - all risk factors
  # 
  result<-result%>%
  mutate(Beta=(mi1.coef + mi2.coef + mi3.coef + mi4.coef + mi5.coef)/5)  %>%
  mutate(var_Beta=rowSds(as.matrix(result[,c("mi1.coef","mi2.coef","mi3.coef","mi4.coef","mi5.coef")]))^2) %>%
  mutate(Se=((((mi1.se)^2 + (mi2.se)^2 + (mi3.se)^2 + (mi4.se)^2 +(mi5.se)^2)/5)+1.2*var_Beta)^0.5) %>%
  mutate(HR=exp(Beta),HR_low=exp(Beta-1.96*Se),HR_up=exp(Beta+1.96*Se)) %>%
  mutate(P_value=(1-pnorm(abs(Beta/Se)))*2) 
  
  result$outcome<-out

  result<-result%>%
     filter(variable=="firePM_TotalDiary_per10")   
  
  result$ED.no_truc<-nrow(filter(reg_data,status==1))
   
  result$ED.no_full<-data%>%                  
       filter(outcome==out&status==1)%>%
       filter(HAZELWOOD_ID%in%adult$HAZELWOOD_ID)%>%nrow()


  return(result)
}

Time stratified MI estimates

Est_Tstrat<-function(out,data=ED_survival,recurrent=TRUE,weight=TRUE,trunc=5,cutpoint=913){
  
 if(recurrent==TRUE){ ## Recurrent survival 
   
    reg_data<- data %>%
    filter(outcome==out) %>% 
    filter(event<=trunc)
    
    #merge imputed complete survey data 
    reg_data2<-lapply(complete_data, function(x) reg_data %>% left_join(x) )
  
   # time split data into first 2.5 years and last 2.5 years.    
   reg_data3 <- lapply(reg_data2, function(x) survSplit(Surv(t_start,t_stop,status) ~ ., data= x, cut=c(cutpoint),episode= "tgroup"))

    
  if(weight==TRUE){
     
  model<-lapply(reg_data3,function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10:strata(tgroup) +gender+Age+Married+edu_group+employ_group+smoke_group +occu_exp_group+pre_fire_ED+STUDY_GROUP_CODE+strata(event), method = "breslow", data =x ,weights = weight,id=HAZELWOOD_ID )))[,c("coef","robust se")])
  
   #print(model)
   
     } else{
     
  model<-lapply(reg_data3,
             function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10:strata(tgroup) +gender+Age+Married+edu_group+employ_group+smoke_group +occu_exp_group+pre_fire_ED+STUDY_GROUP_CODE+strata(event), method = "breslow", data =x,id=HAZELWOOD_ID )))[,c("coef","robust se")])

    } 
   
  } else { ## First event survival
   
      reg_data<- data %>%
      filter(outcome==out) %>% 
      filter(event<=1)
      
    #merge imputed complete survey data 
    reg_data2<-lapply(complete_data, function(x) reg_data %>% left_join(x) )
    
       reg_data3 <- lapply(reg_data2, function(x) survSplit(Surv(t_start,t_stop,status) ~ ., data= x, cut=c(cutpoint),episode= "tgroup"))

  
    # time split data into first 2 years and last 3 years.    
    # reg_data3 <- survSplit(Surv(t_start,t_stop,status) ~ ., data= reg_data2, cut=c(cutpoint),episode= "tgroup")

     if(weight==TRUE){
     
  model<-lapply(reg_data3,function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~firePM_TotalDiary_per10:strata(tgroup) +gender+Age+Married+edu_group+employ_group+smoke_group +occu_exp_group+pre_fire_ED+STUDY_GROUP_CODE, method = "breslow", data =x ,weights = weight,id=HAZELWOOD_ID )))[,c("coef","robust se")])
  
    } else{
     
 model<-lapply(reg_data3,function(x) coef(summary(coxph(Surv(t_start,t_stop,status) ~ firePM_TotalDiary_per10:strata(tgroup)+gender+Age+Married+edu_group+employ_group+smoke_group +occu_exp_group+pre_fire_ED+STUDY_GROUP_CODE, method = "breslow", data =x,id=HAZELWOOD_ID )))[,c("coef","se(coef)")])

    }
}   
  
  names(model)<-c("mi1","mi2","mi3","mi4","mi5")
  result <-cbind(model,as_tibble(model))[,c(1:10)]
  
  result$mi1.se<-result[,2]
  result$mi2.se<-result[,4]
  result$mi3.se<-result[,6]
  result$mi4.se<-result[,8]
  result$mi5.se<-result[,10]
  
  result<-result[,c("mi1.coef","mi2.coef","mi3.coef","mi4.coef","mi5.coef","mi1.se","mi2.se","mi3.se","mi4.se","mi5.se")]

  
  result$variable<-row.names(result)

  # Combine Beta and Se - all risk factors
  # 
  result<-result%>%
  mutate(Beta=(mi1.coef + mi2.coef + mi3.coef + mi4.coef + mi5.coef)/5)  %>%
  mutate(var_Beta=rowSds(as.matrix(result[,c("mi1.coef","mi2.coef","mi3.coef","mi4.coef","mi5.coef")]))^2) %>%
  mutate(Se=((((mi1.se)^2 + (mi2.se)^2 + (mi3.se)^2 + (mi4.se)^2 +(mi5.se)^2)/5)+1.2*var_Beta)^0.5) %>%
  mutate(HR=exp(Beta),HR_low=exp(Beta-1.96*Se),HR_up=exp(Beta+1.96*Se)) %>%
  mutate(P_value=(1-pnorm(abs(Beta/Se)))*2) 
  
  result$outcome<-out
  
  # count events by tgroup for each outcome 

  temp<-reg_data3[[1]] %>% 
  filter(status==1 & outcome==out) %>%
    group_by(tgroup) %>%
    dplyr::summarize(Tot_events=sum(status))

  
  # select exposure variables
  result<-subset(result, variable=="firePM_TotalDiary_per10:strata(tgroup)tgroup=1" | variable=="firePM_TotalDiary_per10:strata(tgroup)tgroup=2")  
  
  result$ED.no_truc<-temp$Tot_events



  return(result)
}

Plot overall estimates

Plot_Overall<-function(filename,figname){
#Plot_Overall<-function(filename){

forest<-filename %>%
  select(c("outcome","ED.no_full","ED.no_truc","Beta","Se","HR", "HR_low","HR_up","P_value")) 


forest<-forest%>%
  dplyr::mutate(Index=seq.int(1:length(forest$outcome)))%>%
  mutate('HR (95% CI)'=paste0(format(round(HR, digits=2), nsmall=2), " (" ,
                                  format(round( HR_low, digits = 2),nsmall=2)," - " ,
                                  format(round(HR_up, digits = 2),nsmall=2),")")) %>% 
      mutate( 'p-value' =format(round(P_value, digits=2 ), nsmall=2)) %>% 
      mutate('p-value'= ifelse(`p-value`=="0.00","<0.001",`p-value`)) %>%
  mutate(Outcome=c("All cause","All cardiovascular disease","Ischemic heart disease", "Cardiac arrhythmia","Cerebrovascular disease","Stroke","Atherothrombotic disease","All respiratory","Chronic obstructive pulmonary disease","Pneumonia & acute bronchitis","All mental","Anxiety","Injury"))%>%
mutate(ICDcodes=c("All except Z00-Z99","I00-99;G45-46", "I20-25,49.0", "I47-49","I61-69;G45-46","I60-61,63-64;G45","I20-25,46,61-69,71-74,49.0;G45-46","J00-99","J41-44","J12-18,20-21","F00-99;X60-84;R40-46","F40-44","S00-99;T00-14"))


#main plot in the middle 
plot1<- ggplot(forest, aes(x = HR, y = Index,colour = "black") )+
        geom_point() +
        geom_errorbarh(aes(xmin = HR_low, xmax = HR_up),  height = 0.3) +
        ylab(NULL) +
        ggtitle(paste("\n  \n ")) +
        xlab(expression(paste("HR (95% CI) per 10 ", group("",mu*g/{m^3}, "")," increase in mean  ", PM[2.5]))) +
        scale_colour_manual(values = c("black"))  +
        scale_y_continuous(name = "", breaks=1:13,labels = forest$Outcome, trans="reverse") +
       xlim(0.5, 2.0)+ 
        theme(plot.title = element_text(size = 10,face = "bold"),
              axis.text.y = element_blank(),
              axis.title.x= element_text(size = 11,face = "bold"),
              axis.ticks.y = element_blank(),
              axis.text.x = element_text(size=11,face = "bold"),
              panel.border = element_blank(),
              axis.line.x = element_line(colour = "black"),
              panel.background = element_blank(),
              panel.grid = element_blank(),
              plot.margin = margin(0, 0, 0.0, 0, "cm"),
              legend.position = "none")  + 
        geom_vline(aes(xintercept = 1), linetype = "dashed") 



#base plot for adding text 
tab_base <- ggplot(forest, aes(y = Index)) +
        ylab(NULL) + xlab("") + 
        scale_y_continuous(name = "", breaks=1:13,labels = forest$Outcome, trans="reverse") +
        theme(plot.title = element_text(hjust = 0.5, size = 11,face="bold"), ## centering title on text
              axis.text.x = element_text(color = "white"), ## need text to be printed so it stays aligned with figure but white so it's invisible
              axis.line = element_blank(),
              axis.text.y = element_blank(),
              axis.ticks = element_blank(),
              axis.title.y = element_blank(),
              plot.margin = margin(0, 0, 0.4, 0, "cm"),
              legend.position = "none",
              panel.background = element_blank(),
              panel.grid = element_blank())

# Predictors
tab1 <- tab_base + 
        geom_text(aes(x = 0, label = Outcome, y = Index), hjust = 1,nudge_x = 0.05,size=4.5) +
        scale_x_continuous(limits = c(-1.5,0.1)) +
        ggtitle("\n    ED diagnoses \n")
    
    
 # Group 
 tab2 <- tab_base + 
        geom_text(aes(x = 0, label = ICDcodes,colour = "black"),hjust = 1,size=4.5) + 
        scale_x_continuous(limits = c(-0.5,0.2)) +
        scale_colour_manual(values = c("black") ) +
        ggtitle("\n   ICD codes \n") # Group 

 tab3 <- tab_base +
        geom_text(aes(x = 0, label = ED.no_truc,colour = "black"),hjust = 1,size=4.5) +
        scale_x_continuous(limits = c(-0.5,0.2)) +
        scale_colour_manual(values = c("black") ) +
        ggtitle("\n Number \n of events")


 # OR
 tab4 <- tab_base +
        geom_text(aes(x = 0, label = `HR (95% CI)`, colour = "black"),size=4.5 ) + 
        scale_colour_manual(values =c("black") ) +
        ggtitle("\n HR (95% CI) \n")

 # individual p-value
tab5 <- tab_base + 
        geom_text(aes(x = 0, label = `p-value`, colour = "black"),size=4.5) +
        scale_x_continuous(limits = c(-0.1,0.1)) +
        scale_colour_manual(values = c("black" )) +
        ggtitle("\n p-value \n")


plot <- grid.arrange(tab1, tab2, tab3, plot1, tab4,tab5,
             layout_matrix = matrix(c(rep(1,10), 
                                      rep(2,13),
                                      rep(3,3),
                                      rep(4,13),
                                      rep(5,5),
                                      rep(6,2)), nrow = 1))

}


#     

Plot time stratified estimates

Plot_Tstrat<-function(filename,figname,noutcomes=13){

nb<-noutcomes*2

forest<-filename %>%
  select(c("outcome","ED.no_truc","Beta","Se","HR", "HR_low","HR_up","P_value")) %>% 
  mutate(time=rep(c("First 2.5 yrs","2.5 to 5 yrs"),times=noutcomes)) %>% 
  mutate(Index=seq.int(1:nb)) %>%
  mutate(Index=ifelse(Index%%2==1,Index+0.1,Index-0.1)) %>%
  mutate('HR (95% CI)'=paste0(format(round(HR, digits=2), nsmall=2), " (" ,
                                  format(round( HR_low, digits = 2),nsmall=2)," - " ,
                                  format(round(HR_up, digits = 2),nsmall=2),")")) %>% 
  mutate( 'p-value' =format(round(P_value, digits=2 ), nsmall=2)) %>% 
  mutate('p-value'= ifelse(`p-value`=="0.00","<0.001",`p-value`)) %>%
  group_by(outcome) %>% 
  mutate(Index_all=mean(Index)) %>% 
  ungroup() %>% 
  mutate(outcome = factor(ifelse(time== "2.5 to 5 yrs",NA,as.character(outcome)))) 



forest$y_loc=forest$Index

#colours <- c("#065F9B","#66933E") # colours from somatic plot

plot1<- ggplot(forest, aes(x = HR, y = Index,colour = time) )+
        geom_point() +
        geom_errorbarh(aes(xmin = HR_low, xmax = HR_up),  height = 0.3) +
        ylab(NULL) +
        ggtitle(paste("\n  \n ")) +
        xlab(expression(paste("HR (95% CI) per 10 ", group("",mu*g/{m^3}, "")," increase in mean  ", PM[2.5]))) +
        scale_colour_manual(values = c("#065F9B","#66933E"))  +
        scale_y_continuous(name = "", breaks=1:nb,labels = forest$outcome, trans="reverse") +
       xlim(0.5, 2.0)+ 
        theme(plot.title = element_text(size = 10,face = "bold"),
              axis.text.y = element_blank(),
              axis.title.x= element_text(size = 11,face = "bold"),
              axis.ticks.y = element_blank(),
              axis.text.x = element_text(size=11,face = "bold"),
              panel.border = element_blank(),
              axis.line.x = element_line(colour = "black"),
              panel.background = element_blank(),
              panel.grid = element_blank(),
              plot.margin = margin(0, 0, 0.0, 0, "cm"),
              legend.position = "none")  + 
        geom_vline(aes(xintercept = 1), linetype = "dashed") 



#base plot for adding text 
tab_base <- ggplot(forest, aes(y = Index)) +
        ylab(NULL) + xlab("") + 
        scale_y_continuous(name = "", limits = c(nb,1), breaks=1:nb,labels = forest$outcome, trans="reverse") +
        theme(plot.title = element_text(hjust = 0.5, size = 11,face="bold"), ## centering title on text
              axis.text.x = element_text(color = "white"), ## need text to be printed so it stays aligned with figure but white so it's invisible
              axis.line = element_blank(),
              axis.text.y = element_blank(),
              axis.ticks = element_blank(),
              axis.title.y = element_blank(),
              plot.margin = margin(0, 0, 0.4, 0, "cm"),
              legend.position = "none",
              panel.background = element_blank(),
              panel.grid = element_blank())

# Predictors
tab1 <- tab_base + 
        geom_text(aes(x = 0, label = outcome, y = Index_all), hjust = 1,nudge_x = 0.05,size=4.5) +
        scale_x_continuous(limits = c(-1.5,0.1)) +
        ggtitle("\n    ED diagnoses \n")
    
tab2 <- tab_base +
        geom_text(aes(x = 0, label = time,colour = time),hjust = 1,size=4.5) +
        scale_x_continuous(limits = c(-0.5,0.2)) +
        scale_colour_manual(values = c("#065F9B","#66933E") ) +
        ggtitle("\n Follow-up \n post mine fire")
   
tab3 <- tab_base +
        geom_text(aes(x = 0, label = ED.no_truc,colour = "black"),hjust = 1,size=4.5) +
        scale_x_continuous(limits = c(-0.5,0.2)) +
        scale_colour_manual(values = c("black") ) +
        ggtitle("\n Number \n of events")


tab4 <- tab_base +
        geom_text(aes(x = 0, label = `HR (95% CI)`, colour = time),size=4.5 ) + 
        scale_colour_manual(values =c("#065F9B","#66933E") ) +
        ggtitle("\n HR (95% CI) \n")

 # individual p-value
tab5 <- tab_base + 
        geom_text(aes(x = 0, label = `p-value`, colour = time),size=4.5) +
        scale_x_continuous(limits = c(-0.1,0.1)) +
        scale_colour_manual(values = c("#065F9B","#66933E")) +
        ggtitle("\n p-value \n")


# plot <- grid.arrange(tab1,tab2,tab3, plot1, tab4,tab5,
#              layout_matrix = matrix(c(rep(1,9), 
#                                       rep(2,6),
#                                       rep(3,4),
#                                       rep(4,16),
#                                       rep(5,7),
#                                       rep(6,3)), nrow = 1))



plot <- grid.arrange(tab1, tab2, tab3, plot1, tab4,tab5,
             layout_matrix = matrix(c(rep(1,8), 
                                      rep(2,13),
                                      rep(3,3),
                                      rep(4,13),
                                      rep(5,5),
                                      rep(6,2)), nrow = 1))




}

Run main models

Recurrent overall and time stratified

## Overall
rec_overall_wt<-lapply(Outcome, Est_Overall, recurrent=TRUE,weight=TRUE)
rec_overall_wt<-bind_rows(rec_overall_wt) %>% ungroup() %>% select(c(11:21))
#write.csv(rec_overall_wt,"../Results/rec_overall_wt.csv")


## Time stratified

plot_labels<-c("All cause","All cardiovascular disease","Ischemic heart disease", "Cardiac arrhythmia","Cerebrovascular disease","Stroke","Atherothrombotic disease","All respiratory","COPD","Pneumonia & acute bronchitis","All mental","Anxiety","Injury")

rec_tstrat_wt<-lapply(Outcome, Est_Tstrat, recurrent=TRUE,weight=TRUE,cutpoint=913)
rec_tstrat_wt<-bind_rows(rec_tstrat_wt) %>% ungroup() %>% select(c(11:20)) %>% 
   mutate(outcome=factor(outcome,levels=Outcome,labels=plot_labels)) 
#write.csv(rec_tstrat_wt,"../Results/rec_tstrat_wt.csv")

Run plots

Overall

plot_rec_overall_wt<-Plot_Overall(rec_overall_wt,figname="rec_overall_wt")

Time stratified-all outcomes

plot_rec_tstrat_wt<-Plot_Tstrat(rec_tstrat_wt,"rec_tstrat_wt")

Timestratified-CVD,IHD,ATD

ph_fail_wt<-subset(rec_tstrat_wt, outcome=="All cardiovascular disease" | outcome=="Ischemic heart disease"|
                     outcome=="Atherothrombotic disease")
plot_ph_fail_wt<-Plot_Tstrat(ph_fail_wt,"ph_fail_wt",noutcomes=3)

The End