#cumulative incidence curve for death,competing risk LT in subgroup of patient with AH, comparing ATN vs HRS vs pre renal vs other, with corresponding % death 95%CI at 30, 60 and 90d.

master_no_ah <- master %>% filter(alcoholic_hepatitis_admission==0,final_type_of_aki %in% c(1,2,3,4))
master_no_ah$final_type_of_aki <- factor(master_no_ah$final_type_of_aki)
levels(master_no_ah$final_type_of_aki) <- c("Prerenal","HRS","ATN","Other")
master_no_ah$status_90days <- factor(master_no_ah$status_90days)
master_no_ah <- master_no_ah %>% rename(Group=final_type_of_aki)

str(master_no_ah$status_90days)
##  Factor w/ 3 levels "0","1","2": 1 1 2 1 1 1 2 1 1 1 ...
tidycmprsk::cuminc(Surv(time_90days,status_90days) ~ Group , data = master_no_ah) %>%
  ggcuminc() +
  add_confidence_interval() +
  add_risktable()+
  scale_x_continuous(breaks = seq(0, 90, by = 30), limits = c(0, 90))+
  theme(panel.background = element_rect(fill = "white", color = NA),
        plot.background = element_rect(fill = "white", color = NA),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
## Plotting outcome "1".

Cumulative incidence curve for death, with LT as a competing event, rrt cohort

master_rrt <- master %>% filter(rrt==1)
master_rrt$alcoholic_hepatitis_admission <- factor(master_rrt$alcoholic_hepatitis_admission,
                                                  levels = c(0, 1),
                                                  labels = c("No", "Yes"))

tidycmprsk::cuminc(Surv(time_90days,status_90days) ~ alcoholic_hepatitis_admission , data = master_rrt ) %>%
  ggcuminc() +
  add_confidence_interval() +
  add_risktable()+
  scale_x_continuous(breaks = seq(0, 90, by = 30), limits = c(0, 90))+
  theme(panel.background = element_rect(fill = "white", color = NA),
        plot.background = element_rect(fill = "white", color = NA),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
## Plotting outcome "1".

#AH vs no AH and 90 day alive vs dead

library(gmodels) 
## Registered S3 method overwritten by 'gdata':
##   method         from     
##   reorder.factor DescTools
master$alcoholic_hepatitis_admission <- factor(master$alcoholic_hepatitis_admission,
                                                  levels = c(0, 1),
                                                  labels = c("No", "Yes"))

master$final_type_of_aki <- factor(master$final_type_of_aki,
                                                  levels = c(1,2,3,4,5),
                                                  labels = c("Prerenal", "HRS-AKI","ATN","Other","Unable to diagnosis"))


# Create a new factor variable with custom labels for death_in_90days_status

gmodels::CrossTable(master$final_type_of_aki,master$alcoholic_hepatitis_admission, prop.chisq=F)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  2062 
## 
##  
##                          | master$alcoholic_hepatitis_admission 
## master$final_type_of_aki |        No |       Yes | Row Total | 
## -------------------------|-----------|-----------|-----------|
##                 Prerenal |       803 |       111 |       914 | 
##                          |     0.879 |     0.121 |     0.443 | 
##                          |     0.457 |     0.366 |           | 
##                          |     0.389 |     0.054 |           | 
## -------------------------|-----------|-----------|-----------|
##                  HRS-AKI |       207 |        42 |       249 | 
##                          |     0.831 |     0.169 |     0.121 | 
##                          |     0.118 |     0.139 |           | 
##                          |     0.100 |     0.020 |           | 
## -------------------------|-----------|-----------|-----------|
##                      ATN |       508 |       119 |       627 | 
##                          |     0.810 |     0.190 |     0.304 | 
##                          |     0.289 |     0.393 |           | 
##                          |     0.246 |     0.058 |           | 
## -------------------------|-----------|-----------|-----------|
##                    Other |       111 |        12 |       123 | 
##                          |     0.902 |     0.098 |     0.060 | 
##                          |     0.063 |     0.040 |           | 
##                          |     0.054 |     0.006 |           | 
## -------------------------|-----------|-----------|-----------|
##      Unable to diagnosis |       130 |        19 |       149 | 
##                          |     0.872 |     0.128 |     0.072 | 
##                          |     0.074 |     0.063 |           | 
##                          |     0.063 |     0.009 |           | 
## -------------------------|-----------|-----------|-----------|
##             Column Total |      1759 |       303 |      2062 | 
##                          |     0.853 |     0.147 |           | 
## -------------------------|-----------|-----------|-----------|
## 
##