library(tidyverse)
library(tableone)
library(gtsummary)
library(survival)
library(ggplot2)
library(readxl)
library(ggpubr)
library(splines)
library(xlsx)
library(optmatch)
library(MatchIt)
library(survival)
library(survminer)
library(xlsx)
library(cmprsk)

setwd("C:/Users/to909/Desktop/Andrew final/Revision")
all<- read_excel("All_edited_new_12142022.xlsx")


Baylor0913<- read_excel("Baylor0913.xlsx") %>% select(study_id,creatinine_last_available)
Indiana0913 <- read_excel("Indiana0913.xlsx") %>% select(study_id,creatinine_last_available)
Jacksonville0913<- read_excel("Jacksonville0913.xlsx") %>% select(study_id,creatinine_last_available)
Kentukey0913<- read_excel("Kentukey0913.xlsx")  %>% select(study_id,creatinine_last_available)
MCW0913<- read_excel("MCW0913.xlsx")%>% select(study_id,creatinine_last_available)
MGH0913<- read_excel("MGH0913.xlsx")%>% select(study_id,creatinine_last_available)
Michigan0913<- read_excel("Michigan0913.xlsx")%>% select(study_id,creatinine_last_available)
Oschner0913<- read_excel("Oschner0913.xlsx")%>% select(study_id,creatinine_last_available)
Rochester0913<- read_excel("Rochester0913.xlsx")%>% select(study_id,creatinine_last_available)
USC0913<- read_excel("USC0913.xlsx")%>% select(study_id) %>% mutate(creatinine_last_available=NA)
Yale0913<- read_excel("Yale0913.xlsx")%>% select(study_id,creatinine_last_available)
creatinine_last_available <- rbind(Baylor0913,Indiana0913,Jacksonville0913,Kentukey0913,MCW0913,MGH0913,Michigan0913,Oschner0913,Rochester0913,USC0913,Yale0913) 


master <- merge(all,creatinine_last_available, by="study_id",all.x=TRUE)



# CKD-EPI Creatinine Equation (2021)

# sex 1: male 2: female 

ckd_epi_gfr <- function(creat, sex, age) {
  k <- ifelse(sex == 1, 0.9, 0.7)
  a <- ifelse(sex == 1, -0.302, -0.241)
  m <- ifelse(sex == 1, 1, 1.012)
  gfr <- 142 * min(c(creat/k,1))^a * max(c(creat/k,1))^-1.2 * 0.9938^age * m
  return(gfr)
}

#  creat new variables  


master$eGFR <- mapply(ckd_epi_gfr , master$creatinine_last_available,master$sex,master$age_admission)

master <- master %>% mutate(type_of_aki=case_when(
                     final_type_of_aki == 1 ~ "Prerenal",
                     final_type_of_aki == 2 ~ "HRS-AKI",
                     final_type_of_aki == 3 ~"ATN",
                     final_type_of_aki == 4 ~"Other",
                     final_type_of_aki == 5 ~"Unable to diagnosis")) %>%
                    mutate(aki_stage_3=ifelse(aki_stage_4==4,3,aki_stage_4)) %>% 
                    mutate(LT_SLKT=case_when(
    master$kidney_transplant==0 & master$liver_transplant==1 ~ "Liver trasnplant only",
    master$kidney_transplant==1 & master$liver_transplant==1 ~  "SLKT"  )) %>% 
  # mutate(de_novo_ckd=case_when(
  #   ckd==1 ~ "Pre-exisiting CKD",
  #   ckd!=1 & eGFR<60 ~ "De-novo CKD",
  #   ckd!=1 & eGFR>=60 ~ "no CKD at 90 days",
  #   is.na(creatinine_last_available) | days_liver<90 ~ "no GFR at 90 days available"
  # ))   %>% 
  mutate(liver_transplant=ifelse(liver_transplant==1,"liver_transplant_yes","liver_transplant_no"))
test <- master %>% filter(death_in_90days_status!=1,ckd!=1,)

2. AKI Phenotype, n (%) | Stage 1 Stage 2 Stage 3

table(master$type_of_aki,master$aki_stage_3,useNA = "always")
##                      
##                         1   2   3 <NA>
##   ATN                  99 111 416    2
##   HRS-AKI              48  49 150    2
##   Other                57  37  29    0
##   Prerenal            449 252 201   12
##   Unable to diagnosis  47  22  79    1
##   <NA>                  0   0   0    0

5. Authors should include the outcomes of AKI (renal function) in patients who received a liver transplant and which was the AKI etiology in patients with received LT vs SLKT.

AKI_responders

table(master$liver_transplant,master$AKI_responders,useNA = "always")
##                       
##                        complete response no response partial response <NA>
##   liver_transplant_no                164         231               59    6
##   liver_transplant_yes                60          89               18    0
##   <NA>                               639         563              223   11

rrt_hemodialysis

table(master$liver_transplant,master$rrt_hemodialysis,useNA = "always")
##                       
##                           0    1 <NA>
##   liver_transplant_no   231   31  198
##   liver_transplant_yes   34   39   94
##   <NA>                  100  100 1236
table(master$liver_transplant,master$AKI_responders,useNA = "always")
##                       
##                        complete response no response partial response <NA>
##   liver_transplant_no                164         231               59    6
##   liver_transplant_yes                60          89               18    0
##   <NA>                               639         563              223   11

LT vs.SLKT kidney_transplant

table(master$liver_transplant,master$type_of_aki,useNA = "always")
##                       
##                        ATN HRS-AKI Other Prerenal Unable to diagnosis <NA>
##   liver_transplant_no  149      86    20      171                  34    0
##   liver_transplant_yes  39      47     2       67                  12    0
##   <NA>                 440     116   101      676                 103    0
table(master$LT_SLKT,master$kidney_transplant,useNA = "always")
##                        
##                            0    1 <NA>
##   Liver trasnplant only  141    0    0
##   SLKT                     0   26    0
##   <NA>                  1894    1    1
table(master$LT_SLKT,master$type_of_aki,useNA = "always")
##                        
##                         ATN HRS-AKI Other Prerenal Unable to diagnosis <NA>
##   Liver trasnplant only  33      40     1       55                  12    0
##   SLKT                    6       7     1       12                   0    0
##   <NA>                  589     202   121      847                 137    0

6. In patients who were alive and not transplanted >90 days from AKI with available laboratory data,

we have included the raw incidence of de-novo CKD by AKI phenotype.

# table(master$type_of_aki,master$de_novo_ckd,useNA = "always")
# 
# test <- master %>% mutate(de_novo_test_group=case_when(de_novo_ckd=="De-novo CKD" ~ "De-novo CKD",
#                                                        de_novo_ckd=="no GFR at 90 days available" ~ "No CKD"))
# table(test$type_of_aki,test$de_novo_test_group,useNA = "always")
# 
# fisher.test(table(test$type_of_aki,test$de_novo_test_group))    

q6_cohort <- master %>% filter(death_in_90days_status==0,liver_transplant =="liver_transplant_no",ckd==0) %>% mutate(denovo_ckd=ifelse(eGFR<60,"De-novo CKD","No CKD"))

table(q6_cohort$type_of_aki,q6_cohort$denovo_ckd,useNA = "always")
##                      
##                       De-novo CKD No CKD <NA>
##   ATN                          10     16    5
##   HRS-AKI                      10      9    4
##   Other                         6      6    0
##   Prerenal                     29     64   12
##   Unable to diagnosis           3      4    4
##   <NA>                          0      0    0
fisher.test(table(q6_cohort$type_of_aki,q6_cohort$denovo_ckd)) 
## 
##  Fisher's Exact Test for Count Data
## 
## data:  table(q6_cohort$type_of_aki, q6_cohort$denovo_ckd)
## p-value = 0.3408
## alternative hypothesis: two.sided

#7. CLIF-C ACLF

table(master$type_of_aki,master$aclf_grade,useNA = "always")
##                      
##                         1   2   3 <NA>
##   ATN                  42 130 456    0
##   HRS-AKI              24  70 155    0
##   Other                39  41  43    0
##   Prerenal            170 335 409    0
##   Unable to diagnosis  31  41  77    0
##   <NA>                  0   0   0    0

Reviewer 2

5 responders stratified AKI phenotype

fit_1 <- survfit(Surv(death_in_90days_time,death_in_90days_status) ~ AKI_responders, data = master )
fit_1
## Call: survfit(formula = Surv(death_in_90days_time, death_in_90days_status) ~ 
##     AKI_responders, data = master)
## 
##    17 observations deleted due to missingness 
##                                    n events median 0.95LCL 0.95UCL
## AKI_responders=complete response 863    149     NA      NA      NA
## AKI_responders=no response       883    491     37      29      45
## AKI_responders=partial response  300    103     NA      NA      NA
ggsurvplot_facet(fit_1, master, facet.by = "type_of_aki",
                palette = c("#00ABFD","#7CAE00","#F8766D"), pval = TRUE)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## i Please use `as_tibble()` instead.
## i The signature and semantics have changed, see `?as_tibble`.
## i The deprecated feature was likely used in the survminer package.
##   Please report the issue at <https://github.com/kassambara/survminer/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: `select_()` was deprecated in dplyr 0.7.0.
## i Please use `select()` instead.
## i The deprecated feature was likely used in the dplyr package.
##   Please report the issue at <https://github.com/tidyverse/dplyr/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

6 AKI phenotypes stratified by ascites and no ascites

table(master$ascites_admission,master$type_of_aki,useNA = "always")
##       
##        ATN HRS-AKI Other Prerenal Unable to diagnosis <NA>
##   0    116       7    34      270                  28    0
##   1    512     242    89      643                 121    0
##   <NA>   0       0     0        1                   0    0
fit_2 <- survfit(Surv(death_in_90days_time,death_in_90days_status) ~ type_of_aki, data = master )
fit_2
## Call: survfit(formula = Surv(death_in_90days_time, death_in_90days_status) ~ 
##     type_of_aki, data = master)
## 
##                                   n events median 0.95LCL 0.95UCL
## type_of_aki=ATN                 628    331     58      40      77
## type_of_aki=HRS-AKI             249    122     60      42      NA
## type_of_aki=Other               123     29     NA      NA      NA
## type_of_aki=Prerenal            914    203     NA      NA      NA
## type_of_aki=Unable to diagnosis 149     67     NA      60      NA
ggsurvplot_facet(fit_2, master, facet.by = "ascites_admission",
                palette = "jco", pval = TRUE)