# Dr Nichanan
# Nichanan Jongthitinon <soundnichanan _at_ gmail.com>

library(epicalc, warn.conflicts = FALSE)
## Loading required package: foreign
## Loading required package: survival
## Loading required package: MASS
## Loading required package: nnet
library(readxl, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(tidyr, warn.conflicts = FALSE)
library(broom, warn.conflicts = FALSE)
library(gtsummary, warn.conflicts = FALSE)
library(flextable, warn.conflicts = FALSE)
library(survival, warn.conflicts = FALSE)
library(survminer, warn.conflicts = FALSE)
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:epicalc':
## 
##     alpha
## Loading required package: ggpubr
## 
## Attaching package: 'ggpubr'
## The following objects are masked from 'package:flextable':
## 
##     border, font, rotate
options("width"=300)

setwd("D:/Consults/Thirachit/Nichanan/HLK")

source("D:/Consults/Thirachit/leukemia/TableStack.R")

data1 <- read_excel("Final data1.xlsx") 
## Warning: Expecting numeric in BT57 / R57C72: got a date
## Warning: Expecting numeric in BT96 / R96C72: got a date
## Warning: Expecting numeric in BT120 / R120C72: got a date
## Warning: Expecting numeric in BT210 / R210C72: got a date
## Warning: Expecting numeric in BT243 / R243C72: got a date
## Warning: Expecting numeric in BT249 / R249C72: got a date
## Warning: Expecting numeric in BT251 / R251C72: got a date
## Warning: Expecting numeric in BT253 / R253C72: got a date
## Warning: Expecting numeric in BT291 / R291C72: got a date
## Warning: Expecting numeric in BT305 / R305C72: got a date
## Warning: Expecting numeric in BT363 / R363C72: got a date
## Warning: Expecting numeric in BT410 / R410C72: got a date
## Warning: Expecting numeric in BT419 / R419C72: got a date
## Warning: Expecting numeric in BT440 / R440C72: got a date
## Warning: Expecting numeric in BT441 / R441C72: got a date
## Warning: Expecting numeric in BT498 / R498C72: got a date
## Warning: Expecting numeric in BT583 / R583C72: got a date
## Warning: Expecting numeric in BT590 / R590C72: got a date
## Warning: Expecting numeric in BT658 / R658C72: got a date
## Warning: Expecting numeric in BT719 / R719C72: got a date
## Warning: Expecting numeric in BT794 / R794C72: got a date
## Warning: Expecting numeric in BT841 / R841C72: got a date
## Warning: Expecting numeric in BT970 / R970C72: got a date
## Warning: Expecting numeric in BT987 / R987C72: got a date
## Warning: Expecting numeric in BT1104 / R1104C72: got a date
## New names:
## • `if yes (protocol)` -> `if yes (protocol)...56`
## • `start date` -> `start date...57`
## • `if yes (date)` -> `if yes (date)...59`
## • `if yes (date)` -> `if yes (date)...61`
## • `if yes (date)` -> `if yes (date)...63`
## • `site` -> `site...64`
## • `if combined` -> `if combined...65`
## • `if yes (protocol)` -> `if yes (protocol)...67`
## • `start date` -> `start date...68`
## • `if yes (date)` -> `if yes (date)...70`
## • `if yes (date)` -> `if yes (date)...72`
## • `if yes (date)` -> `if yes (date)...74`
## • `site` -> `site...75`
## • `if combined` -> `if combined...76`
## • `` -> `...81`
data2 <- read_excel("Final data2.xlsx") 
## New names:
## • `Others` -> `Others...8`
## • `68. Neurological complication` -> `68. Neurological complication...63`
## • `Others` -> `Others...80`
## • `68. Neurological complication` -> `68. Neurological complication...108`
data1
## # A tibble: 1,133 × 81
##    `1.site` Year_diag No_year 2.regi…¹    HN   Sex place dob                 `A_ visit`          A_dx                  Age    BW    HT Right Right…²   Mor 13.1A…³ 13.2A…⁴  Chro Chro_…⁵ 15.1s…⁶    wbc    hb   hct    plt blast 16.1s…⁷   bun creat…⁸ calcium phosp…⁹  uric  SGOT  SGPT   ALP   LDH Media…˟
##    <chr>        <dbl>   <dbl>    <dbl> <dbl> <dbl> <dbl> <dttm>              <dttm>              <dttm>              <dbl> <dbl> <dbl> <dbl> <chr>   <dbl>   <dbl>   <dbl> <dbl> <chr>   <lgl>    <dbl> <dbl> <dbl>  <dbl> <dbl> <lgl>   <dbl>   <dbl>   <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
##  1 PSU           2521       1       NA 28952     1     1 2519-11-12 00:00:00 2521-10-16 00:00:00 2521-10-16 00:00:00    23   9    NA       4 social…     2      NA       8     1 <NA>    NA       20200  12.6  35   120000     2 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
##  2 PSU           2522       1       NA  5045     2     1 2521-03-11 00:00:00 2522-02-09 00:00:00 2522-02-09 00:00:00    11   8    NA       4 social…     2      NA       8     1 <NA>    NA       95000   6.8  18.5     NA    85 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
##  3 PSU           2522       2       NA  9559     2     7 2520-03-09 00:00:00 2522-03-09 00:00:00 2522-03-09 00:00:00    24  10    NA       4 social…     2      NA       8     1 <NA>    NA       19900   2.6   8   120000    68 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
##  4 PSU           2522       3       NA 28109     2     1 2521-06-21 00:00:00 2522-06-19 00:00:00 2522-06-20 00:00:00    12   8    82       4 social…     1       3      NA     1 <NA>    NA        1900   6.2  22   120000     2 NA         10    NA        NA      NA NA       NA    NA    NA    NA       2
##  5 PSU           2522       4       NA 11457     1     1 2520-05-16 00:00:00 2522-11-16 00:00:00 2522-11-27 00:00:00    30  11.5  83       4 social…     1       3      NA     1 <NA>    NA        2000   4.1  11.5  70000    10 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
##  6 PSU           2523       1       NA 15875     2     1 2521-03-01 00:00:00 2523-03-07 00:00:00 2523-03-07 00:00:00    24  10    NA       4 social…     1       3      NA     1 <NA>    NA        6400   8.2  17    30000    98 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
##  7 PSU           2523       2       NA 30558     2     1 2522-03-20 00:00:00 2523-06-16 00:00:00 2523-06-18 00:00:00    15   8    77.5     4 social…     1       3      NA     1 <NA>    NA       14150   7.6  24   120000    15 NA          9     6.8      NA      NA  5.25    NA    NA    NA    NA       2
##  8 PSU           2523       3       NA 33787     1     7 2522-07-15 00:00:00 2523-06-15 00:00:00 2523-06-15 00:00:00    11  10    NA       4 social…     2      NA       8     1 <NA>    NA      265000   8    20    88000    65 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
##  9 PSU           2523       4       NA 33959     2     2 2520-11-04 00:00:00 2523-06-20 00:00:00 2523-06-24 00:00:00    31   8    80       4 social…     1       3      NA     1 <NA>    NA      122000   5.6  12    75000    93 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
## 10 PSU           2525       2       NA  2350     1     2 2517-12-01 00:00:00 2525-02-01 00:00:00 2525-02-01 00:00:00    86  15   106.      4 social…     2      NA       8     1 <NA>    NA       19500   8.4  28   120000    80 NA         NA    NA        NA      NA NA       NA    NA    NA    NA       2
## # … with 1,123 more rows, 44 more variables: Terticular_mass <dbl>, Fever <dbl>, Arthitis <dbl>, Arthitis_d <chr>, Xray <dbl>, hepato <dbl>, hepato_size <dbl>, Splenomegaly <dbl>, Spleen_size <dbl>, lymphodenopathy <dbl>, Exopthalmos <dbl>, Skin_bleeding <dbl>, Gum_hypertrophy <dbl>,
## #   Bleed_gum <dbl>, Jaundice <dbl>, Underlying <dbl>, Underlying_d <chr>, `31.1st protocol chemo.` <dbl>, `if yes (protocol)...56` <chr>, `start date...57` <dttm>, `31.1 remission` <dbl>, `if yes (date)...59` <dttm>, `31.2 complete protocol` <dbl>, `if yes (date)...61` <dttm>,
## #   `31.3 relapsed` <dbl>, `if yes (date)...63` <dttm>, site...64 <dbl>, `if combined...65` <chr>, `32. 2nd protocol chemo.` <dbl>, `if yes (protocol)...67` <chr>, `start date...68` <dttm>, `32.1 remission` <dbl>, `if yes (date)...70` <dttm>, `32.2 complete protocol` <dbl>,
## #   `if yes (date)...72` <dbl>, `32.3 relapsed` <dbl>, `if yes (date)...74` <dttm>, site...75 <dbl>, `if combined...76` <chr>, `33. status` <dbl>, `if death, cause` <dbl>, `date of death` <dttm>, `34. Last FU` <dttm>, ...81 <chr>, and abbreviated variable names ¹​`2.register no`, ²​Right_other,
## #   ³​`13.1ALL`, ⁴​`13.2ANLL`, ⁵​Chro_Others, ⁶​`15.1st CBC`, ⁷​`16.1st Serum chem`, ⁸​creatinine, ⁹​phosphorus, ˟​Mediastinal_mass
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
data2
## # A tibble: 740 × 117
##    start               end                 1. Site nu…¹ HN    Year_…² No_year Dx    Other…³ Sex   A_CMT A_pro…⁴ A_date              A_Rem…⁵ A_Remiss_day        A_Comp A_LastCMT           A_Rel…⁶ A_Relapse_date      A_Sit…⁷  A_BM A_CNS A_tes…⁸ A_ Co…⁹ A_com…˟ A_typ…˟ B_CMT B_pro…˟ B_date             
##    <dttm>              <dttm>              <lgl>        <chr>   <dbl>   <dbl> <chr> <lgl>   <chr> <chr> <chr>   <dttm>              <chr>   <dttm>              <chr>  <dttm>              <chr>   <dttm>              <chr>   <dbl> <dbl>   <dbl>   <dbl> <chr>   <chr>   <chr> <chr>   <dttm>             
##  1 2021-09-27 19:04:34 2021-11-06 20:24:48 NA           2790…    2542       4 76.1… NA      1. M… 1. Y… MCCG 1… 1999-03-24 00:00:00 1. Yes  1999-05-07 00:00:00 1. Yes 2002-05-13 00:00:00 2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
##  2 2021-09-27 19:07:11 2021-09-27 19:09:17 NA           8908…    2542       5 76.1… NA      1. M… 1. Y… MCCG 1… 1999-04-01 00:00:00 1. Yes  1999-05-10 00:00:00 1. Yes 2002-05-20 00:00:00 2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
##  3 2021-09-26 00:12:29 2021-09-26 07:33:09 NA           8375…    2541      11 76.1… NA      1. M… 1. Y… MCCG 1… 1998-03-13 00:00:00 1. Yes  1998-04-21 00:00:00 1. Yes 2001-04-02 00:00:00 2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
##  4 2021-09-26 07:33:09 2021-09-26 07:36:22 NA           8368…    2541      12 76.1… NA      2. F… 1. Y… MCCG 1… 1998-04-23 00:00:00 3. Not… NA                  2. No  NA                  2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    <NA>  <NA>    NA                 
##  5 2021-09-27 19:09:17 2021-09-27 19:11:44 NA           8931…    2542       6 76.1… NA      2. F… 1. Y… MCCG 1… 1999-04-16 00:00:00 3. Not… NA                  2. No  NA                  2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
##  6 2021-09-26 07:36:23 2021-09-26 07:42:54 NA           8378…    2541      13 76.1… NA      1. M… 1. Y… MCCG 1… 1998-04-24 00:00:00 1. Yes  1998-06-05 00:00:00 2. No  NA                  2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
##  7 2021-09-26 07:42:54 2021-09-26 07:45:28 NA           8354…    2541      14 76.1… NA      1. M… 1. Y… MCCG 1… 1998-05-02 00:00:00 1. Yes  1998-06-08 00:00:00 1. Yes 2001-06-11 00:00:00 2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
##  8 2021-09-26 07:51:11 2021-09-26 07:56:15 NA           8393…    2541      16 76.1… NA      2. F… 1. Y… MCCG 1… 1998-05-09 00:00:00 1. Yes  1998-06-17 00:00:00 1. Yes 2001-06-24 00:00:00 1. Yes  2003-09-18 00:00:00 1. BM       1     0       0       0 <NA>    4. N/A  1. Y… Refrac… 2003-09-18 00:00:00
##  9 2021-09-26 07:56:15 2021-09-26 08:01:31 NA           8428…    2541      17 76.2… NA      1. M… 1. Y… BFM 83  1998-05-27 00:00:00 2. No   NA                  2. No  NA                  2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    1. Y… BFM 83  1998-06-23 00:00:00
## 10 2021-09-26 08:01:31 2021-09-26 08:04:56 NA           8457…    2541      18 76.2… NA      1. M… 1. Y… BFM 83  1998-06-17 00:00:00 1. Yes  1998-06-29 00:00:00 1. Yes 2000-12-07 00:00:00 2. No   NA                  <NA>       NA    NA      NA      NA <NA>    <NA>    2. No <NA>    NA                 
## # … with 730 more rows, 89 more variables: B_Remiss <chr>, B_remiss_day <dttm>, B_Comp <chr>, B_LastCMT <dttm>, B_relapsed <chr>, B_relapsed_date <dttm>, B_Site_relapsed <chr>, B_BM <dbl>, B_CNS <dbl>, B_testis <dbl>, `B_ Combine` <dbl>, B_type_relapsed <chr>, C_CMT <chr>, C_protocol <chr>,
## #   C_date <dttm>, C_Remiss <chr>, C_remiss_day <dttm>, C_Comp <chr>, C_LastCMT <dttm>, C_relapsed <chr>, C_relapsed_date <dttm>, C_Site_relapsed <chr>, C_BM <dbl>, C_CNS <dbl>, C_testis <dbl>, `C_ Combine` <dbl>, C_type_relapsed <chr>, Status <chr>, death_cause <chr>, death_date <dttm>,
## #   Last_date <dttm>, CNS_diagnosis <chr>, `67. Metabolic complication` <lgl>, TLS <chr>, `68. Neurological complication...63` <lgl>, Seizure <chr>, ICH <chr>, Ischemic_stroke <chr>, IICP <chr>, Imaging <chr>, Surgery <chr>, Finding <chr>, Operation <chr>, `69. Respiratory complication` <lgl>,
## #   `Respiratory _status` <chr>, `69.2 Diagnosis` <lgl>, Pneumonia <chr>, Pleural_effusion <chr>, ARDS <chr>, Pul_leuko <chr>, `Pul_ hem` <chr>, Others...80 <chr>, Others_data <chr>, DIC <chr>, `71. Renal complication` <lgl>, Renal_insuf <chr>, RRT <chr>, RRT_med <chr>, `72. Infection` <lgl>,
## #   Infect <chr>, Sepsis <chr>, `Sepsis_ site` <chr>, `site_ CNS` <dbl>, Site_respi <dbl>, Site_CVS <dbl>, Site_GI <dbl>, Site_GU <dbl>, Site_skin <dbl>, Site_others <dbl>, `site_ CNS_d` <lgl>, Site_respi_d <chr>, Site_CVS_d <lgl>, Site_GI_d <chr>, Site_GU_d <chr>, Site_skin_d <chr>,
## #   `Site_others _d` <chr>, `73. Management` <lgl>, Intubation <chr>, ICU <chr>, `68. Neurological complication...108` <lgl>, `_id` <dbl>, `_uuid` <chr>, `_submission_time` <dttm>, `_validation_status` <lgl>, `_notes` <lgl>, `_status` <chr>, `_submitted_by` <lgl>, `_tags` <lgl>, `_index` <dbl>, and
## #   abbreviated variable names ¹​`1. Site number`, ²​Year_diag, ³​Others...8, ⁴​A_protocol, ⁵​A_Remiss, ⁶​A_Relapsed, ⁷​A_Site_relapsed, ⁸​A_testis, ⁹​`A_ Combine`, ˟​A_combine_data, ˟​A_type_relapsed, ˟​B_protocol
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
names(data1)[17] <- "ALL"
names(data1)[55] <- "chemotherapy"


# Omit 1 cases with 'HN' 1302964 and 'No_year' 4 (duplicate see email of Sep 25, 2022)
data1 <- filter(data1, !(HN==1302964 & No_year==4))

# Omit variable 'Sex' from data2 (already exists in data1)
data2$Sex <- NULL

any(is.na(data1$HN))
## [1] FALSE
data1$HN <- as.character(data1$HN)

# Merge
data <- inner_join(data1, data2, by=c("HN","Year_diag","No_year"))
dim(data)
## [1] 740 194
length(unique(data$HN))
## [1] 740
group_by(data, HN) %>%
  filter(n()>1) %>%
  select(1:11)
## # A tibble: 0 × 11
## # Groups:   HN [0]
## # … with 11 variables: 1.site <chr>, Year_diag <dbl>, No_year <dbl>, 2.register no <dbl>, HN <chr>, Sex <dbl>, place <dbl>, dob <dttm>, A_ visit <dttm>, A_dx <dttm>, Age <dbl>
## # ℹ Use `colnames()` to see all variable names
group_by(data, HN) %>%
  filter(n()>1) %>%
  select(12:29)
## Adding missing grouping variables: `HN`
## # A tibble: 0 × 19
## # Groups:   HN [0]
## # … with 19 variables: HN <chr>, BW <dbl>, HT <dbl>, Right <dbl>, Right_other <chr>, Mor <dbl>, ALL <dbl>, 13.2ANLL <dbl>, Chro <dbl>, Chro_Others <chr>, 15.1st CBC <lgl>, wbc <dbl>, hb <dbl>, hct <dbl>, plt <dbl>, blast <dbl>, 16.1st Serum chem <lgl>, bun <dbl>, creatinine <dbl>
## # ℹ Use `colnames()` to see all variable names
# Keep if received chemotherapy
count(data, chemotherapy)
## # A tibble: 2 × 2
##   chemotherapy     n
##          <dbl> <int>
## 1            1   690
## 2            2    50
data <- filter(data, chemotherapy==1)


#
# Corrections:
#

# HN 839991 died on July 28, 2549. (2006) 
data[, c('Status', 'death_date', 'Last_date')][data$HN=="839991",]
## # A tibble: 0 × 3
## # … with 3 variables: Status <chr>, death_date <dttm>, Last_date <dttm>
## # ℹ Use `colnames()` to see all variable names
data$death_date[data$HN=="839991"] <- as.Date("2006-09-28")

# HN 842825 died on February 14, 2545. (2002)
data[, c('Status', 'death_date', 'Last_date')][data$HN=="842825",]
## # A tibble: 1 × 3
##   Status   death_date          Last_date
##   <chr>    <dttm>              <dttm>   
## 1 4. Death 2021-02-14 00:00:00 NA
data$death_date[data$HN=="842825"] <- as.Date("2002-02-14")

# HN 865930 lost follow-up, and we lack the information of relapse. and patient died on July 29, 2542. (1999) 
data[, c('A_Relapsed', 'Status', 'death_cause', 'death_date', 'Last_date')][data$HN=="865930",]
## # A tibble: 1 × 5
##   A_Relapsed Status   death_cause       death_date          Last_date
##   <chr>      <chr>    <chr>             <dttm>              <dttm>   
## 1 2. No      4. Death 1. Cancer related 2021-07-27 00:00:00 NA
# Assume relapse occurred at death
data$A_Relapsed[data$HN=="865930"] <- "1. Yes"
data$A_Relapse_date[data$HN=="865930"] <- as.Date("1999-09-29")


count(data, A_Relapsed)
## # A tibble: 3 × 2
##   A_Relapsed     n
##   <chr>      <int>
## 1 1. Yes       225
## 2 2. No        464
## 3 <NA>           1
filter(data, is.na(A_Relapsed)) %>% select(1:9, `31.1 remission`, `31.3 relapsed`, Status, `if death, cause`)
## # A tibble: 1 × 13
##   `1.site` Year_diag No_year `2.register no` HN        Sex place dob                 `A_ visit`          `31.1 remission` `31.3 relapsed` Status   `if death, cause`
##   <chr>        <dbl>   <dbl>           <dbl> <chr>   <dbl> <dbl> <dttm>              <dttm>                         <dbl>           <dbl> <chr>                <dbl>
## 1 PSU           2554      25   1809902669426 1724729     2     8 2554-03-17 00:00:00 2554-08-01 00:00:00                2               2 4. Death                 1
# Assume this cases did not relapse
data$A_Relapsed[data$HN=='1924929'] <- "2. No"

#
# Analysis
#

# wbc count at initial diagnosis(>/= 100000(hyperleukocytosis) and < 100000 comparison of 2 groups in term of basic patient characteristics, clinical characteristics and complication during first induction of the patients, overall survival and event free survival at 5 years and 10 years after diagnosis between hyperleukocytosis and non-hyperleukocytosis group. 

# Data management
YN <- c('Yes', 'No')

count(data, ALL)
## # A tibble: 4 × 2
##     ALL     n
##   <dbl> <int>
## 1     1    63
## 2     2   376
## 3     3    44
## 4    NA   207
count(data, Mor, ALL)
## # A tibble: 4 × 3
##     Mor   ALL     n
##   <dbl> <dbl> <int>
## 1     1     1    63
## 2     1     2   376
## 3     1     3    44
## 4     2    NA   207
# Convert categorical data to factors
data <- mutate_if(data, is.character, as.factor)


# The early complication variables have NA (TLS also has some with "3. N/A")

data <- mutate(data, 
                hlk=factor(ifelse(wbc<100000, 1, 2), labels=c('No','Yes')),
                agegp=cut(Age, breaks=c(0,24,48,92,Inf), labels=c('0-23','24-49','48-91','92+'), right=FALSE, include=TRUE),
               sex=case_when(Sex==1~'Male',
                              Sex==2~'Female',
                              TRUE~NA_character_),
                A_dx=epicalc::be2ad(as.Date(A_dx)),
                Mor=factor(Mor, labels=c('ALL', 'AML')),
                ALL=factor(ALL, labels=c('T_cell','B-cell','FAB')),
                Mediastinal_mass=factor(Mediastinal_mass, levels=c(1,2), labels=YN),
                Terticular_mass=factor(Terticular_mass, levels=c(1,2), labels=YN),
                Fever=factor(Fever, levels=c(1,2), labels=YN),
                Arthitis=factor(Arthitis, levels=c(1,2), labels=YN),
                Xray=factor(Xray, levels=c(1,2), labels=YN),
                hepato=factor(hepato, levels=c(1,2), labels=YN),
                Splenomegaly=factor(Splenomegaly, levels=c(1,2), labels=YN),
                lymphodenopathy=factor(lymphodenopathy, levels=c(1,2), labels=YN),
                Exopthalmos=factor(Exopthalmos, levels=c(1,2), labels=YN),
                Skin_bleeding=factor(Skin_bleeding, levels=c(1,2), labels=YN),
                Gum_hypertrophy=factor(Gum_hypertrophy, levels=c(1,2), labels=YN),
                Bleed_gum=factor(Bleed_gum, levels=c(1,2), labels=YN),
                Jaundice=factor(Jaundice, levels=c(1,2), labels=YN),
                Underlying=factor(Underlying, levels=c(1,2), labels=YN),
               A_type_relapsed=na_if(A_type_relapsed, "4. N/A"),
                rel_BM=factor(ifelse(grepl('BM|Bone marrow', A_Site_relapsed), 1, 2), labels=c("Yes","No")),
                rel_CNS=factor(ifelse(grepl('CNS', A_Site_relapsed), 1, 2), labels=c("Yes","No")),
                rel_Testis=factor(ifelse(grepl('Testis', A_Site_relapsed), 1, 2), labels=c("Yes","No")),
                rdate=as.Date(coalesce(A_Relapse_date, B_relapsed_date, C_relapsed_date)),
                outcome=factor(Status=="4. Death", labels=c("Alive","Dead")),
                death_date=as.Date(death_date),
                Last_date=as.Date(Last_date),
                sdate=as.Date(case_when(outcome=="Dead"~death_date, 
                                        TRUE~Last_date)),
                stime=as.numeric(difftime(sdate, A_dx, units="days")/365.25),
                event=outcome=="Dead" | !is.na(rdate),
                edate=if_else(is.na(rdate), sdate, rdate),
                etime=as.numeric(difftime(edate, A_dx, units="days")/365.25),
               TLS=factor(na_if(TLS, "3. N/A")))

count(data, hlk)
## # A tibble: 2 × 2
##   hlk       n
##   <fct> <int>
## 1 No      568
## 2 Yes     122
summary(data)
##  1.site      Year_diag       No_year      2.register no             HN           Sex            place             dob                           A_ visit                          A_dx                 Age               BW               HT            Right               Right_other   Mor     
##  PSU:690   Min.   :2541   Min.   : 1.00   Min.   :9.402e+10   1001165:  1   Min.   :1.000   Min.   : 1.000   Min.   :2526-09-19 00:00:00.0   Min.   :2541-01-08 00:00:00.0   Min.   :1998-01-09   Min.   :  2.00   Min.   :  2.10   Min.   : 44.0   Min.   :1.000   social payment:  1   ALL:483  
##            1st Qu.:2546   1st Qu.:10.00   1st Qu.:1.811e+12   1002370:  1   1st Qu.:1.000   1st Qu.: 2.000   1st Qu.:2539-06-27 12:00:00.0   1st Qu.:2546-02-13 12:00:00.0   1st Qu.:2003-02-15   1st Qu.: 34.00   1st Qu.: 12.20   1st Qu.: 91.0   1st Qu.:1.000   Social payment:  1   AML:207  
##            Median :2550   Median :19.50   Median :1.910e+12   1003096:  1   Median :1.000   Median : 6.000   Median :2544-11-09 12:00:00.0   Median :2550-05-15 00:00:00.0   Median :2007-05-16   Median : 55.50   Median : 16.00   Median :105.0   Median :1.000   NA's          :688            
##            Mean   :2550   Mean   :21.77   Mean   :1.907e+12   1007357:  1   Mean   :1.425   Mean   : 5.499   Mean   :2544-07-24 12:35:28.7   Mean   :2550-07-10 19:09:54.7   Mean   :2007-07-14   Mean   : 71.67   Mean   : 20.64   Mean   :110.1   Mean   :1.104                                 
##            3rd Qu.:2554   3rd Qu.:31.00   3rd Qu.:1.949e+12   1007449:  1   3rd Qu.:2.000   3rd Qu.: 8.000   3rd Qu.:2549-05-22 06:00:00.0   3rd Qu.:2554-01-29 18:00:00.0   3rd Qu.:2011-02-07   3rd Qu.:105.00   3rd Qu.: 24.45   3rd Qu.:127.9   3rd Qu.:1.000                                 
##            Max.   :2560   Max.   :71.00   Max.   :5.961e+12   1009398:  1   Max.   :2.000   Max.   :15.000   Max.   :2559-03-13 00:00:00.0   Max.   :2560-12-26 00:00:00.0   Max.   :2017-12-28   Max.   :180.00   Max.   :112.00   Max.   :177.0   Max.   :4.000                                 
##                                           NA's   :71          (Other):684                                                                                                                                                                                                                         
##      ALL         13.2ANLL         Chro                                                       Chro_Others  15.1st CBC          wbc                hb              hct             plt             blast        16.1st Serum chem      bun           creatinine        calcium         phosphorus    
##  T_cell: 63   Min.   :1.00   Min.   :1.000   47, xy, 21 : trisomy 21 male                          :  2   Mode:logical   Min.   :    600   Min.   : 1.400   Min.   : 4.30   Min.   :  2000   Min.   :  0.00   Mode:logical      Min.   :  2.20   Min.   :0.1300   Min.   : 4.000   Min.   : 0.310  
##  B-cell:376   1st Qu.:4.25   1st Qu.:2.000   Mosaic translocation (8;21) male karyotype            :  2   NA's:690       1st Qu.:   6165   1st Qu.: 5.600   1st Qu.:17.02   1st Qu.: 19000   1st Qu.: 13.00   NA's:690          1st Qu.:  8.70   1st Qu.:0.3600   1st Qu.: 9.000   1st Qu.: 4.100  
##  FAB   : 44   Median :5.00   Median :3.000   ?Translocation (8;14) female karyotype                :  1                  Median :  17525   Median : 7.300   Median :22.00   Median : 39500   Median : 49.00                     Median : 11.30   Median :0.5000   Median : 9.400   Median : 4.800  
##  NA's  :207   Mean   :5.33   Mean   :2.851   ?Translocation(1;7)and(21;21)male karyotype           :  1                  Mean   :  66157   Mean   : 7.423   Mean   :22.46   Mean   : 72060   Mean   : 48.15                     Mean   : 12.22   Mean   :0.5486   Mean   : 9.476   Mean   : 4.767  
##               3rd Qu.:7.00   3rd Qu.:3.000   37-46,xx,mul:multiple anomalies Female karyotype,major:  1                  3rd Qu.:  60600   3rd Qu.: 9.100   3rd Qu.:27.50   3rd Qu.: 86000   3rd Qu.: 85.00                     3rd Qu.: 14.30   3rd Qu.:0.6600   3rd Qu.: 9.900   3rd Qu.: 5.500  
##               Max.   :8.00   Max.   :4.000   (Other)                                               :136                  Max.   :1251700   Max.   :64.000   Max.   :43.00   Max.   :823000   Max.   :100.00                     Max.   :118.60   Max.   :6.0000   Max.   :18.400   Max.   :19.300  
##               NA's   :484    NA's   :1       NA's                                                  :547                                                                                      NA's   :1                                                                                             
##       uric             SGOT              SGPT             ALP              LDH          Mediastinal_mass Terticular_mass Fever     Arthitis      Arthitis_d   Xray     hepato     hepato_size     Splenomegaly  Spleen_size     lymphodenopathy Exopthalmos Skin_bleeding Gum_hypertrophy Bleed_gum
##  Min.   : 1.100   Min.   :   7.00   Min.   :  2.00   Min.   :   4.0   Min.   :   11.0   Yes: 24          Yes:  0         Yes:517   Yes: 54   Rt.elbow :  4   Yes: 50   Yes:613   Min.   : 0.000   Yes:451      Min.   : 0.000   Yes:531         Yes: 28     Yes:283       Yes: 66         Yes:101  
##  1st Qu.: 3.900   1st Qu.:  24.00   1st Qu.: 10.00   1st Qu.: 107.0   1st Qu.:  644.5   No :666          No :690         No :173   No :636   knee     :  3   No :640   No : 77   1st Qu.: 3.000   No :239      1st Qu.: 0.000   No :159         No :662     No :407       No :624         No :589  
##  Median : 5.200   Median :  32.00   Median : 15.00   Median : 141.5   Median : 1063.5                                                        Lt.knee  :  2                       Median : 4.000                Median : 3.000                                                                      
##  Mean   : 5.806   Mean   :  51.31   Mean   : 30.99   Mean   : 167.2   Mean   : 2555.3                                                        all joint:  1                       Mean   : 4.269                Mean   : 3.411                                                                      
##  3rd Qu.: 6.800   3rd Qu.:  49.00   3rd Qu.: 28.00   3rd Qu.: 193.0   3rd Qu.: 2301.2                                                        ankle    :  1                       3rd Qu.: 6.000                3rd Qu.: 5.000                                                                      
##  Max.   :43.900   Max.   :1039.00   Max.   :640.00   Max.   :1055.0   Max.   :46050.0                                                        (Other)  : 43                       Max.   :15.000                Max.   :16.000                                                                      
##                                                                       NA's   :6                                                              NA's     :636                       NA's   :9                     NA's   :55                                                                          
##  Jaundice  Underlying                      Underlying_d  chemotherapy      if yes (protocol)...56 start date...57                 31.1 remission  if yes (date)...59              31.2 complete protocol if yes (date)...61                 31.3 relapsed   if yes (date)...63                
##  Yes:  6   Yes: 39    Down's syndrome            : 24   Min.   :1     TPOG ALL 01-05  :130        Min.   :2541-01-14 00:00:00.0   Min.   :1.000   Min.   :2541-02-19 00:00:00.0   Min.   :1.000          Min.   :2543-06-22 00:00:00.0000   Min.   :1.000   Min.   :2542-06-14 00:00:00.0000  
##  No :684   No :651    SAO                        :  2   1st Qu.:1     Modified CCG 106:112        1st Qu.:2546-02-16 18:00:00.0   1st Qu.:1.000   1st Qu.:2546-05-20 00:00:00.0   1st Qu.:1.000          1st Qu.:2550-03-12 00:00:00.0000   1st Qu.:1.000   1st Qu.:2547-09-06 06:00:00.0000  
##                       alpha thalassemia trait    :  1   Median :1     ANLL-BFM 83     : 79        Median :2550-05-27 00:00:00.0   Median :1.000   Median :2550-07-24 00:00:00.0   Median :2.000          Median :2553-12-13 00:00:00.0000   Median :2.000   Median :2551-10-15 00:00:00.0000  
##                       ASD -> SIP ASD closure 2551:  1   Mean   :1     TPOG ALL 02-05  : 62        Mean   :2550-07-26 07:39:07.7   Mean   :1.348   Mean   :2550-09-30 04:13:04.5   Mean   :1.517          Mean   :2553-10-23 21:58:55.0999   Mean   :1.672   Mean   :2551-10-30 11:15:23.9000  
##                       B-thalassemia/H6E disease  :  1   3rd Qu.:1     TPOG AML 03-08  : 40        3rd Qu.:2554-02-23 18:00:00.0   3rd Qu.:1.000   3rd Qu.:2554-06-15 00:00:00.0   3rd Qu.:2.000          3rd Qu.:2557-07-20 00:00:00.0000   3rd Qu.:2.000   3rd Qu.:2555-07-28 12:00:00.0000  
##                       (Other)                    : 11   Max.   :1     TPOG ALL VHR-08 : 38        Max.   :2560-12-28 00:00:00.0   Max.   :4.000   Max.   :2561-02-05 00:00:00.0   Max.   :2.000          Max.   :2563-04-27 00:00:00.0000   Max.   :2.000   Max.   :2562-09-23 00:00:00.0000  
##                       NA's                       :650                 (Other)         :229                                                        NA's   :121                                            NA's   :357                                        NA's   :464                       
##    site...64      if combined...65 32. 2nd protocol chemo.         if yes (protocol)...67 start date...68                 32.1 remission  if yes (date)...70                 32.2 complete protocol if yes (date)...72 32.3 relapsed   if yes (date)...74                   site...75    
##  Min.   :1.000   BM+CNS   : 21     Min.   :1.000           Refractory         : 61        Min.   :2542-11-12 00:00:00.0   Min.   :1.000   Min.   :2543-05-01 00:00:00.0000   Min.   :1.000          Min.   :     2     Min.   :1.000   Min.   :2543-12-18 00:00:00.0000   Min.   :1.000  
##  1st Qu.:1.000   BM+testis:  6     1st Qu.:1.000           ANLL-BFM 83        : 14        1st Qu.:2547-12-01 06:00:00.0   1st Qu.:1.000   1st Qu.:2548-06-02 00:00:00.0000   1st Qu.:2.000          1st Qu.:237732     1st Qu.:1.000   1st Qu.:2548-08-07 18:00:00.0000   1st Qu.:1.000  
##  Median :1.000   BM+Testis:  5     Median :2.000           TPOG AML 03-08     : 14        Median :2551-11-12 12:00:00.0   Median :2.000   Median :2552-06-13 00:00:00.0000   Median :2.000          Median :238193     Median :2.000   Median :2552-07-06 00:00:00.0000   Median :1.000  
##  Mean   :1.602   CNS+BM   :  2     Mean   :1.747           Refractory Protocol:  8        Mean   :2551-11-20 11:26:53.7   Mean   :1.913   Mean   :2552-08-12 02:30:41.9000   Mean   :1.917          Mean   :223210     Mean   :1.653   Mean   :2552-01-25 22:20:41.4000   Mean   :1.403  
##  3rd Qu.:2.000   NA's     :656     3rd Qu.:2.000           Refractory protocol:  7        3rd Qu.:2555-08-03 18:00:00.0   3rd Qu.:3.000   3rd Qu.:2556-11-23 00:00:00.0000   3rd Qu.:2.000          3rd Qu.:240742     3rd Qu.:2.000   3rd Qu.:2554-10-21 00:00:00.0000   3rd Qu.:1.000  
##  Max.   :4.000                     Max.   :2.000           (Other)            : 71        Max.   :2562-10-01 00:00:00.0   Max.   :4.000   Max.   :2562-12-11 00:00:00.0000   Max.   :2.000          Max.   :241911     Max.   :2.000   Max.   :2563-01-27 00:00:00.0000   Max.   :4.000  
##  NA's   :464                       NA's   :2               NA's               :515        NA's   :516                     NA's   :517     NA's   :604                        NA's   :521            NA's   :675        NA's   :523     NA's   :632                        NA's   :633    
##           if combined...76   33. status    if death, cause date of death                       34. Last FU                               ...81         start                             end                         1. Site number         Dx      Others...8        A_CMT                 A_protocol 
##  BM+CNS           :  2     Min.   :1.000   Min.   :1.000   Min.   :2541-03-26 00:00:00.0000   Min.   :2541-03-26 00:00:00.0000   refer      :  7   Min.   :2021-09-25 23:33:25.00   Min.   :2021-09-25 23:38:13.14   Mode:logical   76.1. ALL:487   Mode:logical   1. Yes:690   MCCG 106         :176  
##  BM+CNS palliative:  1     1st Qu.:2.000   1st Qu.:1.000   1st Qu.:2547-10-28 06:00:00.0000   1st Qu.:2550-10-19 00:00:00.0000   turn CML   :  1   1st Qu.:2021-09-30 02:37:41.76   1st Qu.:2021-09-30 08:44:16.07   NA's:690       76.2. AML:203   NA's:690                    BFM 83           : 99  
##  CNS+BM           :  1     Median :4.000   Median :1.000   Median :2551-07-15 12:00:00.0000   Median :2558-11-12 00:00:00.0000   รักษาต่อ ตปท.:  1   Median :2021-10-09 14:01:16.80   Median :2021-10-09 15:21:23.00                                                              TPOG-ALL-01-05   : 76  
##  NA's             :686     Mean   :3.093   Mean   :1.208   Mean   :2551-08-10 03:29:31.4000   Mean   :2556-10-15 15:49:33.9000   NA's       :681   Mean   :2021-10-15 19:28:13.59   Mean   :2021-10-16 03:47:26.78                                                              ThaiPOG-ALL-01-05: 53  
##                            3rd Qu.:4.000   3rd Qu.:1.000   3rd Qu.:2555-02-10 06:00:00.0000   3rd Qu.:2563-06-01 00:00:00.0000                     3rd Qu.:2021-10-13 22:44:43.00   3rd Qu.:2021-10-13 23:03:14.50                                                              TPOG-ALL-02-05   : 40  
##                            Max.   :4.000   Max.   :3.000   Max.   :2563-05-30 00:00:00.0000   Max.   :2563-06-01 00:00:00.0000                     Max.   :2022-01-03 22:08:09.57   Max.   :2022-01-03 22:11:35.82                                                              TPOG-ALL-1301    : 31  
##                                            NA's   :315     NA's   :312                                                                                                                                                                                                          (Other)          :215  
##      A_date                                                     A_Remiss    A_Remiss_day                       A_Comp      A_LastCMT                       A_Relapsed  A_Relapse_date                          A_Site_relapsed      A_BM            A_CNS           A_testis        A_ Combine    
##  Min.   :1991-05-30 00:00:00.00   1. Yes                            :537   Min.   :1998-02-19 00:00:00.00   1. Yes:337   Min.   :1998-09-21 00:00:00.00   1. Yes:225   Min.   :1998-11-10 00:00:00.00   1. BM          :152    Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:2003-02-15 00:00:00.00   2. No                             : 67   1st Qu.:2003-06-27 12:00:00.00   2. No :352   1st Qu.:2007-11-05 00:00:00.00   2. No :464   1st Qu.:2004-12-21 00:00:00.00   1. BM 2. CNS   : 28    1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :2007-05-11 00:00:00.00   3. Not access                     : 55   Median :2007-08-28 00:00:00.00   NA's  :  1   Median :2011-04-04 00:00:00.00   NA's  :  1   Median :2009-03-23 12:00:00.00   2. CNS         : 20    Median :1.0000   Median :0.0000   Median :0.0000   Median :0.0000  
##  Mean   :2007-07-11 06:40:20.96   4. Death before complete induction: 29   Mean   :2007-11-08 20:57:18.80                Mean   :2011-03-13 19:00:53.41                Mean   :2009-02-07 16:55:42.85   1. BM 3. Testis: 10    Mean   :0.8694   Mean   :0.2297   Mean   :0.0901   Mean   :0.0045  
##  3rd Qu.:2011-02-11 12:00:00.00   NA's                              :  2   3rd Qu.:2011-07-28 18:00:00.00                3rd Qu.:2015-06-22 00:00:00.00                3rd Qu.:2013-01-04 12:00:00.00   3. Testis      :  7    3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:0.0000   3rd Qu.:0.0000  
##  Max.   :2017-12-28 00:00:00.00                                            Max.   :2021-11-12 00:00:00.00                Max.   :2021-04-05 00:00:00.00                Max.   :2022-04-05 00:00:00.00   (Other)        :  5    Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##  NA's   :3                                                                 NA's   :154                                   NA's   :353                                   NA's   :466                      NA's           :468    NA's   :468      NA's   :468      NA's   :468      NA's   :468     
##  A_combine_data          A_type_relapsed    B_CMT                              B_protocol      B_date                                                     B_Remiss    B_remiss_day                       B_Comp      B_LastCMT                    B_relapsed  B_relapsed_date                 
##  LN  :  1       1. T-cell        : 10    1. Yes:215   BFM 83                        : 43   Min.   :1998-04-09 00:00:00.00   1. Yes                            : 93   Min.   :1998-05-26 00:00:00.00   1. Yes: 24   Min.   :2002-09-02 00:00:00   1. Yes: 71   Min.   :1999-06-14 00:00:00.00  
##  NA's:689       2. B-cell        :100    2. No :472   Refractory protocol           : 43   1st Qu.:2003-12-18 12:00:00.00   2. No                             : 58   1st Qu.:2004-07-23 00:00:00.00   2. No :190   1st Qu.:2005-03-03 12:00:00   2. No :143   1st Qu.:2004-11-22 00:00:00.00  
##                 3. Myeloid series: 61    NA's  :  3   Refractory protocol + MCCG 106: 18   Median :2008-01-30 00:00:00.00   3. Not assess                     : 42   Median :2007-11-20 00:00:00.00   NA's  :476   Median :2008-12-15 00:00:00   NA's  :476   Median :2007-11-20 00:00:00.00  
##                 4. N/A           :  0                 TPOG-ALL-1305                 : 12   Mean   :2008-03-16 08:22:19.52   4. Death before complete induction: 22   Mean   :2008-09-02 14:27:05.80                Mean   :2010-01-28 18:00:00                Mean   :2008-07-04 16:41:44.34  
##                 NA's             :519                 TPOG-AML-03-08                :  8   3rd Qu.:2012-04-22 00:00:00.00   NA's                              :475   3rd Qu.:2013-04-29 00:00:00.00                3rd Qu.:2015-10-14 00:00:00                3rd Qu.:2011-10-12 00:00:00.00  
##                                                       (Other)                       : 91   Max.   :2021-05-18 00:00:00.00                                            Max.   :2019-11-12 00:00:00.00                Max.   :2019-04-29 00:00:00                Max.   :2020-07-24 00:00:00.00  
##                                                       NA's                          :475   NA's   :475                                                               NA's   :597                                   NA's   :666                                NA's   :621                     
##                   B_Site_relapsed      B_BM           B_CNS          B_testis        B_ Combine           B_type_relapsed    C_CMT                   C_protocol      C_date                                                     C_Remiss    C_remiss_day           C_Comp      C_LastCMT                  
##  1. Bone marrow           : 56    Min.   :0.000   Min.   :0.000   Min.   :0.0000   Min.   :0     1. T-cell        :  1    1. Yes: 49   BFM 83             :  8   Min.   :1999-07-21 00:00:00.00   1. Yes                            : 10   Min.   :2000-05-01   1. Yes:  3   Min.   :2004-03-01 00:00:00  
##  1. Bone marrow  2. CNS   :  2    1st Qu.:1.000   1st Qu.:0.000   1st Qu.:0.0000   1st Qu.:0     2. B-cell        : 28    2. No :639   Refractory protocol:  6   1st Qu.:2003-07-07 18:00:00.00   2. No                             : 17   1st Qu.:2003-03-25   2. No : 44   1st Qu.:2006-04-10 12:00:00  
##  1. Bone marrow  3. Testis:  1    Median :1.000   Median :0.000   Median :0.0000   Median :0     3. Myeloid series: 21    NA's  :  2   CCG 2891           :  3   Median :2007-07-17 00:00:00.00   3. Not access                     : 13   Median :2005-05-18   NA's  :643   Median :2008-05-20 00:00:00  
##  2. CNS                   : 10    Mean   :0.831   Mean   :0.169   Mean   :0.0423   Mean   :0     4. N/A           : 21                 TPOG-ALL-1305      :  3   Mean   :2008-02-26 06:46:57.39   4. Death before complete induction:  6   Mean   :2007-08-27                Mean   :2009-11-14 00:00:00  
##  3. Testis                :  2    3rd Qu.:1.000   3rd Qu.:0.000   3rd Qu.:0.0000   3rd Qu.:0     NA's             :619                 UKALLR1            :  3   3rd Qu.:2012-02-25 12:00:00.00   NA's                              :644   3rd Qu.:2013-02-19                3rd Qu.:2012-09-21 00:00:00  
##  NA's                     :619    Max.   :1.000   Max.   :1.000   Max.   :1.0000   Max.   :0                                           (Other)            : 26   Max.   :2019-11-07 00:00:00.00                                            Max.   :2019-12-16                Max.   :2017-01-23 00:00:00  
##                                   NA's   :619     NA's   :619     NA's   :619      NA's   :619                                         NA's               :641   NA's   :644                                                               NA's   :681                       NA's   :687                  
##   C_relapsed  C_relapsed_date                    C_Site_relapsed      C_BM         C_CNS        C_testis     C_ Combine           C_type_relapsed                                 Status               death_cause    death_date           Last_date                    CNS_diagnosis
##  1. Yes: 10   Min.   :2000-01-31 00:00:00   1. BM        :  8    Min.   :0.0   Min.   :0.0   Min.   :0     Min.   :0     2. B-cell        :  2    1. Alive on chemotherapy           :  3   1. Cancer related:255   Min.   :1998-03-03   Min.   :1999-12-08   1. CNS 1         :375  
##  2. No : 37   1st Qu.:2003-05-12 12:00:00   1. BM  2. CNS:  1    1st Qu.:1.0   1st Qu.:0.0   1st Qu.:0     1st Qu.:0     3. Myeloid series:  5    2. Alive off chemotherapy          :319   2. Complication  :112   1st Qu.:2004-11-23   1st Qu.:2021-06-07   2. CNS 2         :  2  
##  NA's  :643   Median :2008-08-13 00:00:00   2. CNS       :  1    Median :1.0   Median :0.0   Median :0     Median :0     4. N/A           :  2    3. Alive with alternative treatment:  1   NA's             :323   Median :2008-11-24   Median :2021-07-06   3. CNS 3         : 26  
##               Mean   :2007-06-27 00:00:00   NA's         :680    Mean   :0.9   Mean   :0.2   Mean   :0     Mean   :0     NA's             :681    4. Death                           :367                           Mean   :2009-02-10   Mean   :2020-04-03   4. Traumatic trap:  7  
##               3rd Qu.:2012-01-23 00:00:00                        3rd Qu.:1.0   3rd Qu.:0.0   3rd Qu.:0     3rd Qu.:0                                                                                                3rd Qu.:2012-10-03   3rd Qu.:2021-07-06   5. Not evaluated :249  
##               Max.   :2013-10-06 00:00:00                        Max.   :1.0   Max.   :1.0   Max.   :0     Max.   :0                                                                                                Max.   :2021-07-28   Max.   :2021-12-27   NA's             : 31  
##               NA's   :680                                        NA's   :680   NA's   :680   NA's   :680   NA's   :680                                                                                              NA's   :323          NA's   :339                                 
##  67. Metabolic complication     TLS      68. Neurological complication...63   Seizure        ICH      Ischemic_stroke     IICP       Imaging      Surgery                                                  Finding                       Operation   69. Respiratory complication Respiratory _status
##  Mode:logical               1. Yes: 78   Mode:logical                       1. Yes: 29   1. Yes:  8   1. Yes:  4      1. Yes: 14   1. Yes: 30   1. Yes:  1   A large area hypodense lesion at Lt frontoparietal:  1   Decompressive craniotomy:  1   Mode:logical                 1. Yes: 89         
##  NA's:690                   2. No :590   NA's:690                           2. No :633   2. No :656   2. No :660      2. No :649   2. No :634   2. No :663   Acute dural venous sinus thrombosis               :  1   NA's                    :689   NA's:690                     2. No :575         
##                             NA's  : 22                                      NA's  : 28   NA's  : 26   NA's  : 26      NA's  : 27   NA's  : 26   NA's  : 26   aSDH,MLS, uncle herniation                        :  1                                                               NA's  : 26         
##                                                                                                                                                              CNS ALL                                           :  1                                                                                  
##                                                                                                                                                              Cortex atrophy, not seen infarct                  :  1                                                                                  
##                                                                                                                                                              (Other)                                           : 24                                                                                  
##                                                                                                                                                              NA's                                              :661                                                                                  
##  69.2 Diagnosis  Pneumonia   Pleural_effusion     ARDS     Pul_leuko     Pul_ hem   Others...80           Others_data            DIC      71. Renal complication Renal_insuf      RRT             RRT_med    72. Infection     Infect       Sepsis   
##  Mode:logical   1. Yes: 76   1. Yes: 24       1. Yes: 21   2. No:662   1. Yes: 12   1. Yes:  7   pneumothorax   :  2   70.1 Yes    :114   Mode:logical           1. Yes: 41   1. Yes:  7   71.2.2 CAPD:  7   Mode:logical   1. Yes:585   1. Yes:165  
##  NA's:690       2. No :588   2. No :641       2. No :643   NA's : 28   2. No :649   2. No :657   Pneumothorax   :  4   70.2 No     :281   NA's:690               2. No :623   2. No :658   NA's       :683   NA's:690       2. No : 80   2. No :484  
##                 NA's  : 26   NA's  : 25       NA's  : 26               NA's  : 29   NA's  : 26   Pulmonary edema:  1   70.3 No data:269                          NA's  : 26   NA's  : 25                                    NA's  : 25   NA's  : 41  
##                                                                                                  NA's           :683   NA's        : 26                                                                                                              
##                                                                                                                                                                                                                                                      
##                                                                                                                                                                                                                                                      
##                                                                                                                                                                                                                                                      
##                                                                  Sepsis_ site   site_ CNS     Site_respi        Site_CVS      Site_GI          Site_GU          Site_skin       Site_others     site_ CNS_d               Site_respi_d Site_CVS_d                    Site_GI_d               Site_GU_d  
##  72.1.7 Others                                                         :336   Min.   :0     Min.   :0.0000   Min.   :0     Min.   :0.0000   Min.   :0.00000   Min.   :0.0000   Min.   :0.0000   Mode:logical   pneumonia        : 23   Mode:logical   Fungal diarrhea     : 13   UTI              : 17  
##  72.1.6 Skin and mucosa: phlebitis, abscess, candidiasis  72.1.7 Others: 59   1st Qu.:0     1st Qu.:0.0000   1st Qu.:0     1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.:1.0000   NA's:690       Pneumonia        : 13   NA's:690       Diarrhea            : 12   E. coli UTI      :  5  
##  72.1.4 GI: diarrhea, typhitis, liver abscess 72.1.7 Others            : 45   Median :0     Median :0.0000   Median :0     Median :0.0000   Median :0.00000   Median :0.0000   Median :1.0000                  IPA              : 12                  Infective diarrhea  : 10   Enterococci UTI  :  3  
##  72.1.2 Lung: pneumonia, lung abscess, IPA 72.1.7 Others               : 41   Mean   :0     Mean   :0.1402   Mean   :0     Mean   :0.1487   Mean   :0.06325   Mean   :0.1949   Mean   :0.9214                  Acute pharyngitis:  2                  Sallmonella diarrhea:  6   KP UTI           :  2  
##  72.1.6 Skin and mucosa: phlebitis, abscess, candidiasis               : 15   3rd Qu.:0     3rd Qu.:0.0000   3rd Qu.:0     3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.0000   3rd Qu.:1.0000                  AOM              :  2                  Typhitis            :  6   C. tropicalis UTI:  1  
##  (Other)                                                               : 89   Max.   :0     Max.   :1.0000   Max.   :0     Max.   :1.0000   Max.   :1.00000   Max.   :1.0000   Max.   :1.0000                  (Other)          : 30                  (Other)             : 39   (Other)          :  9  
##  NA's                                                                  :105   NA's   :105   NA's   :105      NA's   :105   NA's   :105      NA's   :105       NA's   :105      NA's   :105                     NA's             :608                  NA's                :604   NA's             :653  
##            Site_skin_d                 Site_others _d 73. Management  Intubation      ICU      68. Neurological complication...108      _id                                             _uuid     _submission_time                 _validation_status  _notes                     _status    _submitted_by 
##  Thrombophlebitis: 29   FNP                   :394    Mode:logical   1. Yes: 79   1. Yes: 96   Mode:logical                        Min.   :115986389   0001ad22-705f-49a7-8646-01b1090d4aef:  1   Min.   :2021-09-25 16:38:24.00   Mode:logical       Mode:logical   submitted_via_web:690   Mode:logical  
##  Cellulitis      : 10   E. coli septicemia    :  7    NA's:690       2. No :585   2. No :569   NA's:690                            1st Qu.:116742594   010ec787-ed83-45b1-b567-648183f2880f:  1   1st Qu.:2021-09-30 01:32:59.25   NA's:690           NA's:690                               NA's:690      
##  Oral candidiasis:  8   Pseudomonas septicemia:  7                   NA's  : 26   NA's  : 25                                       Median :118275362   02f799a4-4fa0-4d6a-be52-073751b078c0:  1   Median :2021-10-09 07:04:20.50                                                                           
##  Perianal abscess:  5   KP septicemia         :  6                                                                                 Mean   :119355355   031966b1-ddf0-4ae5-a857-6918adcffedf:  1   Mean   :2021-10-15 14:10:02.55                                                                           
##  oral candidiasis:  4   KP septicemia, FNP    :  4                                                                                 3rd Qu.:118973218   0325194d-7423-4329-b454-a6ab9a3a5b75:  1   3rd Qu.:2021-10-13 15:46:38.25                                                                           
##  (Other)         : 58   (Other)               :120                                                                                 Max.   :133448059   032b3eca-0d4b-47a3-add9-3d2f6b3c2915:  1   Max.   :2022-01-03 15:11:47.00                                                                           
##  NA's            :576   NA's                  :152                                                                                                     (Other)                             :684                                                                                                            
##   _tags             _index       hlk        agegp         sex            rel_BM    rel_CNS   rel_Testis     rdate             outcome        sdate                stime             event             edate                etime        
##  Mode:logical   Min.   :  1.0   No :568   0-23 : 89   Length:690         Yes:193   Yes: 51   Yes: 20    Min.   :1998-11-10   Alive:323   Min.   :1998-03-03   Min.   : 0.005476   Mode :logical   Min.   :1998-03-03   Min.   : 0.0000  
##  NA's:690       1st Qu.:181.2   Yes:122   24-49:204   Class :character   No :497   No :639   No :670    1st Qu.:2004-07-09   Dead :367   1st Qu.:2008-04-22   1st Qu.: 1.106092   FALSE:294       1st Qu.:2007-08-21   1st Qu.: 0.7365  
##                 Median :370.0             48-91:173   Mode  :character                                  Median :2008-08-12               Median :2017-11-10   Median : 4.360027   TRUE :396       Median :2015-11-04   Median : 3.5633  
##                 Mean   :367.5             92+  :224                                                     Mean   :2008-11-14               Mean   :2014-11-13   Mean   : 7.332885                   Mean   :2014-02-13   Mean   : 6.5849  
##                 3rd Qu.:551.8                                                                           3rd Qu.:2012-08-21               3rd Qu.:2021-07-06   3rd Qu.:12.870637                   3rd Qu.:2021-07-06   3rd Qu.:11.8898  
##                 Max.   :741.0                                                                           Max.   :2022-04-05               Max.   :2021-12-27   Max.   :23.575633                   Max.   :2022-04-05   Max.   :23.5756  
##                                                                                                         NA's   :454
select(data, HN, A_dx, Last_date, Status, outcome, sdate, stime)
## # A tibble: 690 × 7
##    HN     A_dx       Last_date  Status                    outcome sdate        stime
##    <fct>  <date>     <date>     <fct>                     <fct>   <date>       <dbl>
##  1 822768 1998-01-09 NA         4. Death                  Dead    2005-07-07  7.49  
##  2 824206 1998-01-20 2021-07-06 2. Alive off chemotherapy Alive   2021-07-06 23.5   
##  3 826638 1998-02-04 NA         4. Death                  Dead    1998-03-03  0.0739
##  4 829540 1998-02-24 NA         4. Death                  Dead    2006-06-17  8.31  
##  5 829662 1998-02-24 2021-09-22 2. Alive off chemotherapy Alive   2021-09-22 23.6   
##  6 832151 1998-03-12 NA         4. Death                  Dead    1998-06-11  0.249 
##  7 832180 1998-03-12 NA         4. Death                  Dead    1998-10-10  0.580 
##  8 834308 1998-03-30 NA         4. Death                  Dead    2000-04-12  2.04  
##  9 835337 1998-04-08 2021-07-06 2. Alive off chemotherapy Alive   2021-07-06 23.2   
## 10 837219 1998-04-20 2021-07-06 2. Alive off chemotherapy Alive   2021-07-06 23.2   
## # … with 680 more rows
## # ℹ Use `print(n = ...)` to see more rows
count(data, Terticular_mass)
## # A tibble: 1 × 2
##   Terticular_mass     n
##   <fct>           <int>
## 1 No                690
# Omit variables that have no variation
data$Terticular_mass <- NULL


# Create BMI from height and weight
summary(data$HT)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    44.0    91.0   105.0   110.1   127.9   177.0
summary(data$BW)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.10   12.20   16.00   20.64   24.45  112.00
data <- mutate(data, bmi=BW/(HT/100)^2)

summary(data$bmi)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.774  13.773  14.943  15.644  16.716  44.461
# BMI
# https://www.cdc.gov/growthcharts/html_charts/bmiagerev.htm

# Data obtained from US CDC and WHO

# CDC (2-20 year-olds)
# https://www.cdc.gov/growthcharts/html_charts/bmiagerev.htm

# Males
BMI_male <- read.csv("D:/Consults/Thirachit/Pharsai/BMI_male.csv")

head(BMI_male)
##    age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 1 24.0 14.52095 14.73732 15.09033 15.74164 16.57503 17.55719 18.16219 18.60948 19.33801 19.85986
## 2 24.5 14.50348 14.71929 15.07117 15.71963 16.54777 17.52129 18.11955 18.56111 19.27890 19.79194
## 3 25.5 14.46882 14.68361 15.03336 15.67634 16.49443 17.45135 18.03668 18.46730 19.16466 19.66102
## 4 26.5 14.43460 14.64843 14.99620 15.63403 16.44260 17.38384 17.95700 18.37736 19.05567 19.53658
## 5 27.5 14.40083 14.61379 14.95969 15.59268 16.39224 17.31871 17.88047 18.29125 18.95187 19.41849
## 6 28.5 14.36755 14.57969 14.92385 15.55226 16.34334 17.25593 17.80704 18.20892 18.85317 19.30665
tail(BMI_male)
##       age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 214 236.5 18.59716 19.01129 19.70335 21.03795 22.86909 25.23883 26.83505 28.09363 30.30971 32.04453
## 215 237.5 18.62825 19.04343 19.73733 21.07579 22.91293 25.29179 26.89507 28.15978 30.38797 32.13348
## 216 238.5 18.65861 19.07484 19.77060 21.11296 22.95626 25.34459 26.95530 28.22650 30.46758 32.22457
## 217 239.5 18.68822 19.10551 19.80312 21.14946 22.99908 25.39725 27.01575 28.29381 30.54859 32.31787
## 218 240.0 18.70274 19.12055 19.81910 21.16745 23.02029 25.42353 27.04607 28.32770 30.58964 32.36537
## 219 240.5 18.71706 19.13540 19.83489 21.18526 23.04138 25.44978 27.07645 28.36174 30.63106 32.41344
# Omit first and last records and then round age down to nearest integer
BMI_males <- slice(BMI_male, -c(1,n())) %>%
  mutate(age=trunc(age))

head(BMI_males)
##   age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 1  24 14.50348 14.71929 15.07117 15.71963 16.54777 17.52129 18.11955 18.56111 19.27890 19.79194
## 2  25 14.46882 14.68361 15.03336 15.67634 16.49443 17.45135 18.03668 18.46730 19.16466 19.66102
## 3  26 14.43460 14.64843 14.99620 15.63403 16.44260 17.38384 17.95700 18.37736 19.05567 19.53658
## 4  27 14.40083 14.61379 14.95969 15.59268 16.39224 17.31871 17.88047 18.29125 18.95187 19.41849
## 5  28 14.36755 14.57969 14.92385 15.55226 16.34334 17.25593 17.80704 18.20892 18.85317 19.30665
## 6  29 14.33478 14.54615 14.88866 15.51275 16.29584 17.19546 17.73667 18.13031 18.75949 19.20097
tail(BMI_males)
##     age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 212 235 18.56536 18.97844 19.66867 20.99946 22.82474 25.18572 26.77522 28.02801 30.23276 31.95764
## 213 236 18.59716 19.01129 19.70335 21.03795 22.86909 25.23883 26.83505 28.09363 30.30971 32.04453
## 214 237 18.62825 19.04343 19.73733 21.07579 22.91293 25.29179 26.89507 28.15978 30.38797 32.13348
## 215 238 18.65861 19.07484 19.77060 21.11296 22.95626 25.34459 26.95530 28.22650 30.46758 32.22457
## 216 239 18.68822 19.10551 19.80312 21.14946 22.99908 25.39725 27.01575 28.29381 30.54859 32.31787
## 217 240 18.70274 19.12055 19.81910 21.16745 23.02029 25.42353 27.04607 28.32770 30.58964 32.36537
# Females
BMI_female <- read.csv("D:/Consults/Thirachit/Pharsai/BMI_female.csv")

head(BMI_female)
##    age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 1 24.0 14.14735 14.39787 14.80134 15.52808 16.42340 17.42746 18.01821 18.44139 19.10624 19.56411
## 2 24.5 14.13226 14.38019 14.77965 15.49976 16.38804 17.38582 17.97371 18.39526 19.05824 19.51534
## 3 25.5 14.10241 14.34527 14.73695 15.44422 16.31897 17.30485 17.88749 18.30611 18.96595 19.42198
## 4 26.5 14.07297 14.31097 14.69516 15.39015 16.25208 17.22693 17.80489 18.22103 18.87853 19.33410
## 5 27.5 14.04396 14.27728 14.65429 15.33754 16.18735 17.15202 17.72586 18.13997 18.79591 19.25163
## 6 28.5 14.01538 14.24420 14.61434 15.28640 16.12475 17.08009 17.65035 18.06285 18.71800 19.17448
tail(BMI_female)
##       age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 214 236.5 17.43465 17.82119 18.47722 19.78248 21.67712 24.36130 26.36459 28.08801 31.53085 34.72503
## 215 237.5 17.43515 17.82256 18.48014 19.78887 21.68949 24.38426 26.39723 28.13034 31.59664 34.81810
## 216 238.5 17.43427 17.82259 18.48182 19.79420 21.70108 24.40686 26.42984 28.17291 31.66324 34.91250
## 217 239.5 17.43199 17.82127 18.48223 19.79846 21.71189 24.42910 26.46243 28.21574 31.73069 35.00831
## 218 240.0 17.43031 17.82009 18.48196 19.80018 21.71700 24.44010 26.47872 28.23727 31.76474 35.05675
## 219 240.5 17.42827 17.81856 18.48136 19.80162 21.72191 24.45101 26.49502 28.25888 31.79903 35.10556
# Omit first and last records and then round age down to nearest integer
BMI_females <- slice(BMI_female, -c(1,n())) %>%
  mutate(age=trunc(age))

head(BMI_females)
##   age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 1  24 14.13226 14.38019 14.77965 15.49976 16.38804 17.38582 17.97371 18.39526 19.05824 19.51534
## 2  25 14.10241 14.34527 14.73695 15.44422 16.31897 17.30485 17.88749 18.30611 18.96595 19.42198
## 3  26 14.07297 14.31097 14.69516 15.39015 16.25208 17.22693 17.80489 18.22103 18.87853 19.33410
## 4  27 14.04396 14.27728 14.65429 15.33754 16.18735 17.15202 17.72586 18.13997 18.79591 19.25163
## 5  28 14.01538 14.24420 14.61434 15.28640 16.12475 17.08009 17.65035 18.06285 18.71800 19.17448
## 6  29 13.98723 14.21175 14.57531 15.23671 16.06429 17.01107 17.57830 17.98962 18.64472 19.10255
tail(BMI_females)
##     age       p3       p5      p10      p25      p50      p75      p85      p90      p95      p97
## 212 235 17.43278 17.81852 18.47308 19.77505 21.66397 24.33798 26.33189 28.04591 31.46583 34.63326
## 213 236 17.43465 17.82119 18.47722 19.78248 21.67712 24.36130 26.36459 28.08801 31.53085 34.72503
## 214 237 17.43515 17.82256 18.48014 19.78887 21.68949 24.38426 26.39723 28.13034 31.59664 34.81810
## 215 238 17.43427 17.82259 18.48182 19.79420 21.70108 24.40686 26.42984 28.17291 31.66324 34.91250
## 216 239 17.43199 17.82127 18.48223 19.79846 21.71189 24.42910 26.46243 28.21574 31.73069 35.00831
## 217 240 17.43031 17.82009 18.48196 19.80018 21.71700 24.44010 26.47872 28.23727 31.76474 35.05675
# Combine males and females
BMI <- bind_rows(mutate(BMI_males, sex=factor("Male", levels=c("Male","Female"))),
                 mutate(BMI_females, sex=factor("Female", levels=c("Male","Female")))) %>%
  select(age, sex, p5, p85, p95) %>%
  rename(P5=p5, P85=p85, P95=p95)

head(BMI)
##   age  sex       P5      P85      P95
## 1  24 Male 14.71929 18.11955 19.27890
## 2  25 Male 14.68361 18.03668 19.16466
## 3  26 Male 14.64843 17.95700 19.05567
## 4  27 Male 14.61379 17.88047 18.95187
## 5  28 Male 14.57969 17.80704 18.85317
## 6  29 Male 14.54615 17.73667 18.75949
tail(BMI)
##     age    sex       P5      P85      P95
## 429 235 Female 17.81852 26.33189 31.46583
## 430 236 Female 17.82119 26.36459 31.53085
## 431 237 Female 17.82256 26.39723 31.59664
## 432 238 Female 17.82259 26.42984 31.66324
## 433 239 Female 17.82127 26.46243 31.73069
## 434 240 Female 17.82009 26.47872 31.76474
# Data for 0-2 years obtained from WHO:
# https://www.who.int/toolkits/child-growth-standards/standards/body-mass-index-for-age-bmi-for-age

# Males
BMI_0_2_m <- read_excel("D:/Consults/Thirachit/Pharsai/bfa-boys-percentiles-expanded-tables.xlsx") %>%
  filter(Age %in% round(seq(0,931,30.4395))) %>%
  mutate(age=round(Age/30.4395), sex="Male") %>%
  select(age, sex, P5, P85, P95)

head(BMI_0_2_m)
## # A tibble: 6 × 5
##     age sex      P5   P85   P95
##   <dbl> <chr> <dbl> <dbl> <dbl>
## 1     0 Male   11.5  14.8  15.8
## 2     1 Male   12.8  16.4  17.3
## 3     2 Male   14.1  17.9  18.8
## 4     3 Male   14.7  18.4  19.4
## 5     4 Male   15.0  18.7  19.7
## 6     5 Male   15.1  18.9  19.8
tail(BMI_0_2_m)
## # A tibble: 6 × 5
##     age sex      P5   P85   P95
##   <dbl> <chr> <dbl> <dbl> <dbl>
## 1    25 Male   14.1  17.4  18.3
## 2    26 Male   14.1  17.3  18.2
## 3    27 Male   14.0  17.3  18.2
## 4    28 Male   14.0  17.2  18.1
## 5    29 Male   14.0  17.2  18.1
## 6    30 Male   13.9  17.2  18.0
# Females
BMI_0_2_f <- read_excel("D:/Consults/Thirachit/Pharsai/bfa-girls-percentiles-expanded-tables.xlsx") %>%
  filter(Age %in% round(seq(0,931,30.4395))) %>%
  mutate(age=round(Age/30.4395), sex="Female") %>%
  select(age, sex, P5, P85, P95)

head(BMI_0_2_f)
## # A tibble: 6 × 5
##     age sex       P5   P85   P95
##   <dbl> <chr>  <dbl> <dbl> <dbl>
## 1     0 Female  11.5  14.7  15.5
## 2     1 Female  12.4  16.0  16.9
## 3     2 Female  13.5  17.4  18.4
## 4     3 Female  14.0  18.0  19.0
## 5     4 Female  14.3  18.3  19.4
## 6     5 Female  14.5  18.5  19.6
tail(BMI_0_2_f)
## # A tibble: 6 × 5
##     age sex       P5   P85   P95
##   <dbl> <chr>  <dbl> <dbl> <dbl>
## 1    25 Female  13.7  17.1  18.1
## 2    26 Female  13.7  17.1  18.1
## 3    27 Female  13.7  17.1  18.0
## 4    28 Female  13.6  17.0  18.0
## 5    29 Female  13.6  17.0  18.0
## 6    30 Female  13.6  17.0  17.9
BMI_0_2 <- bind_rows(BMI_0_2_m, BMI_0_2_f) %>%
  filter()

head(BMI_0_2)
## # A tibble: 6 × 5
##     age sex      P5   P85   P95
##   <dbl> <chr> <dbl> <dbl> <dbl>
## 1     0 Male   11.5  14.8  15.8
## 2     1 Male   12.8  16.4  17.3
## 3     2 Male   14.1  17.9  18.8
## 4     3 Male   14.7  18.4  19.4
## 5     4 Male   15.0  18.7  19.7
## 6     5 Male   15.1  18.9  19.8
tail(BMI_0_2)
## # A tibble: 6 × 5
##     age sex       P5   P85   P95
##   <dbl> <chr>  <dbl> <dbl> <dbl>
## 1    25 Female  13.7  17.1  18.1
## 2    26 Female  13.7  17.1  18.1
## 3    27 Female  13.7  17.1  18.0
## 4    28 Female  13.6  17.0  18.0
## 5    29 Female  13.6  17.0  18.0
## 6    30 Female  13.6  17.0  17.9
# Combine 0-24 months (0-2 years) with 24-240 months (2-10 years)
BMI <- bind_rows(BMI_0_2, BMI)

# Omit the duplicates due to the extra rows in the BMI data (due to the overlap)
BMI <- group_by(BMI, age, sex) %>%
  slice(1) %>% ungroup()

head(BMI)
## # A tibble: 6 × 5
##     age sex       P5   P85   P95
##   <dbl> <chr>  <dbl> <dbl> <dbl>
## 1     0 Female  11.5  14.7  15.5
## 2     0 Male    11.5  14.8  15.8
## 3     1 Female  12.4  16.0  16.9
## 4     1 Male    12.8  16.4  17.3
## 5     2 Female  13.5  17.4  18.4
## 6     2 Male    14.1  17.9  18.8
tail(BMI)
## # A tibble: 6 × 5
##     age sex       P5   P85   P95
##   <dbl> <chr>  <dbl> <dbl> <dbl>
## 1   238 Female  17.8  26.4  31.7
## 2   238 Male    19.1  27.0  30.5
## 3   239 Female  17.8  26.5  31.7
## 4   239 Male    19.1  27.0  30.5
## 5   240 Female  17.8  26.5  31.8
## 6   240 Male    19.1  27.0  30.6
# Join to data on age and sex
data_BMI <- left_join(data, BMI, by=c("Age"="age", "sex"="sex")) %>%
  # Create BMI status based on percentiles
  mutate(bmi.gp=factor(
    case_when(bmi>=P95~1, # Obese
              bmi>=P85~2, # Overweight
              bmi>=P5~3,  # Normal (healthy)
              bmi<P5~4,   # Underweight
              TRUE~NA_real_), # Missing
    levels=1:4,
    labels=c('Obese','Overweight','Normal','Underweight')))


# Reorder BMI levels
data <- mutate(data_BMI, bmi.gp=factor(bmi.gp, levels=c('Underweight','Normal','Overweight','Obese')))

count(data, bmi.gp)
## # A tibble: 4 × 2
##   bmi.gp          n
##   <fct>       <int>
## 1 Underweight   212
## 2 Normal        392
## 3 Overweight     44
## 4 Obese          42
# Early complications
data <- mutate(data, 
               early_com=ifelse(TLS=="1. Yes" | 
                   Seizure=="1. Yes" | 
                   ICH=="1. Yes" | 
                   Ischemic_stroke=="1. Yes" | 
                   IICP=="1. Yes" | 
                   `Respiratory _status`=="1. Yes" | 
                   Pneumonia=="1. Yes" |
                   Pleural_effusion=="1. Yes" |
                   ARDS=="1. Yes" |
                   Pul_leuko=="1. Yes" |
                   `Pul_ hem`=="1. Yes" | 
                   DIC=="1. Yes" |
                   Renal_insuf=="1. Yes" |
                   RRT=="1. Yes" |
                   !is.na(RRT_med) |
                   Infect=="1. Yes" |
                   Sepsis=="1. Yes" |
                   !is.na(`Sepsis_ site`) |
                   Intubation=="1. Yes" |
                   ICU=="1. Yes", TRUE, FALSE))

data$early_com[is.na(data$early_com)] <- FALSE
count(data, early_com)
## # A tibble: 2 × 2
##   early_com     n
##   <lgl>     <int>
## 1 FALSE        96
## 2 TRUE        594
# Attach to the data to utilise epicalc functions
use(as.data.frame(data))
des()
##  
##  No. of observations =  690 
##     Variable                            Class           Description
## 1   1.site                              factor                     
## 2   Year_diag                           numeric                    
## 3   No_year                             numeric                    
## 4   2.register no                       numeric                    
## 5   HN                                  factor                     
## 6   Sex                                 numeric                    
## 7   place                               numeric                    
## 8   dob                                 POSIXct                    
## 9   A_ visit                            POSIXct                    
## 10  A_dx                                Date                       
## 11  Age                                 numeric                    
## 12  BW                                  numeric                    
## 13  HT                                  numeric                    
## 14  Right                               numeric                    
## 15  Right_other                         factor                     
## 16  Mor                                 factor                     
## 17  ALL                                 factor                     
## 18  13.2ANLL                            numeric                    
## 19  Chro                                numeric                    
## 20  Chro_Others                         factor                     
## 21  15.1st CBC                          logical                    
## 22  wbc                                 numeric                    
## 23  hb                                  numeric                    
## 24  hct                                 numeric                    
## 25  plt                                 numeric                    
## 26  blast                               numeric                    
## 27  16.1st Serum chem                   logical                    
## 28  bun                                 numeric                    
## 29  creatinine                          numeric                    
## 30  calcium                             numeric                    
## 31  phosphorus                          numeric                    
## 32  uric                                numeric                    
## 33  SGOT                                numeric                    
## 34  SGPT                                numeric                    
## 35  ALP                                 numeric                    
## 36  LDH                                 numeric                    
## 37  Mediastinal_mass                    factor                     
## 38  Fever                               factor                     
## 39  Arthitis                            factor                     
## 40  Arthitis_d                          factor                     
## 41  Xray                                factor                     
## 42  hepato                              factor                     
## 43  hepato_size                         numeric                    
## 44  Splenomegaly                        factor                     
## 45  Spleen_size                         numeric                    
## 46  lymphodenopathy                     factor                     
## 47  Exopthalmos                         factor                     
## 48  Skin_bleeding                       factor                     
## 49  Gum_hypertrophy                     factor                     
## 50  Bleed_gum                           factor                     
## 51  Jaundice                            factor                     
## 52  Underlying                          factor                     
## 53  Underlying_d                        factor                     
## 54  chemotherapy                        numeric                    
## 55  if yes (protocol)...56              factor                     
## 56  start date...57                     POSIXct                    
## 57  31.1 remission                      numeric                    
## 58  if yes (date)...59                  POSIXct                    
## 59  31.2 complete protocol              numeric                    
## 60  if yes (date)...61                  POSIXct                    
## 61  31.3 relapsed                       numeric                    
## 62  if yes (date)...63                  POSIXct                    
## 63  site...64                           numeric                    
## 64  if combined...65                    factor                     
## 65  32. 2nd protocol chemo.             numeric                    
## 66  if yes (protocol)...67              factor                     
## 67  start date...68                     POSIXct                    
## 68  32.1 remission                      numeric                    
## 69  if yes (date)...70                  POSIXct                    
## 70  32.2 complete protocol              numeric                    
## 71  if yes (date)...72                  numeric                    
## 72  32.3 relapsed                       numeric                    
## 73  if yes (date)...74                  POSIXct                    
## 74  site...75                           numeric                    
## 75  if combined...76                    factor                     
## 76  33. status                          numeric                    
## 77  if death, cause                     numeric                    
## 78  date of death                       POSIXct                    
## 79  34. Last FU                         POSIXct                    
## 80  ...81                               factor                     
## 81  start                               POSIXct                    
## 82  end                                 POSIXct                    
## 83  1. Site number                      logical                    
## 84  Dx                                  factor                     
## 85  Others...8                          logical                    
## 86  A_CMT                               factor                     
## 87  A_protocol                          factor                     
## 88  A_date                              POSIXct                    
## 89  A_Remiss                            factor                     
## 90  A_Remiss_day                        POSIXct                    
## 91  A_Comp                              factor                     
## 92  A_LastCMT                           POSIXct                    
## 93  A_Relapsed                          factor                     
## 94  A_Relapse_date                      POSIXct                    
## 95  A_Site_relapsed                     factor                     
## 96  A_BM                                numeric                    
## 97  A_CNS                               numeric                    
## 98  A_testis                            numeric                    
## 99  A_ Combine                          numeric                    
## 100 A_combine_data                      factor                     
## 101 A_type_relapsed                     factor                     
## 102 B_CMT                               factor                     
## 103 B_protocol                          factor                     
## 104 B_date                              POSIXct                    
## 105 B_Remiss                            factor                     
## 106 B_remiss_day                        POSIXct                    
## 107 B_Comp                              factor                     
## 108 B_LastCMT                           POSIXct                    
## 109 B_relapsed                          factor                     
## 110 B_relapsed_date                     POSIXct                    
## 111 B_Site_relapsed                     factor                     
## 112 B_BM                                numeric                    
## 113 B_CNS                               numeric                    
## 114 B_testis                            numeric                    
## 115 B_ Combine                          numeric                    
## 116 B_type_relapsed                     factor                     
## 117 C_CMT                               factor                     
## 118 C_protocol                          factor                     
## 119 C_date                              POSIXct                    
## 120 C_Remiss                            factor                     
## 121 C_remiss_day                        POSIXct                    
## 122 C_Comp                              factor                     
## 123 C_LastCMT                           POSIXct                    
## 124 C_relapsed                          factor                     
## 125 C_relapsed_date                     POSIXct                    
## 126 C_Site_relapsed                     factor                     
## 127 C_BM                                numeric                    
## 128 C_CNS                               numeric                    
## 129 C_testis                            numeric                    
## 130 C_ Combine                          numeric                    
## 131 C_type_relapsed                     factor                     
## 132 Status                              factor                     
## 133 death_cause                         factor                     
## 134 death_date                          Date                       
## 135 Last_date                           Date                       
## 136 CNS_diagnosis                       factor                     
## 137 67. Metabolic complication          logical                    
## 138 TLS                                 factor                     
## 139 68. Neurological complication...63  logical                    
## 140 Seizure                             factor                     
## 141 ICH                                 factor                     
## 142 Ischemic_stroke                     factor                     
## 143 IICP                                factor                     
## 144 Imaging                             factor                     
## 145 Surgery                             factor                     
## 146 Finding                             factor                     
## 147 Operation                           factor                     
## 148 69. Respiratory complication        logical                    
## 149 Respiratory _status                 factor                     
## 150 69.2 Diagnosis                      logical                    
## 151 Pneumonia                           factor                     
## 152 Pleural_effusion                    factor                     
## 153 ARDS                                factor                     
## 154 Pul_leuko                           factor                     
## 155 Pul_ hem                            factor                     
## 156 Others...80                         factor                     
## 157 Others_data                         factor                     
## 158 DIC                                 factor                     
## 159 71. Renal complication              logical                    
## 160 Renal_insuf                         factor                     
## 161 RRT                                 factor                     
## 162 RRT_med                             factor                     
## 163 72. Infection                       logical                    
## 164 Infect                              factor                     
## 165 Sepsis                              factor                     
## 166 Sepsis_ site                        factor                     
## 167 site_ CNS                           numeric                    
## 168 Site_respi                          numeric                    
## 169 Site_CVS                            numeric                    
## 170 Site_GI                             numeric                    
## 171 Site_GU                             numeric                    
## 172 Site_skin                           numeric                    
## 173 Site_others                         numeric                    
## 174 site_ CNS_d                         logical                    
## 175 Site_respi_d                        factor                     
## 176 Site_CVS_d                          logical                    
## 177 Site_GI_d                           factor                     
## 178 Site_GU_d                           factor                     
## 179 Site_skin_d                         factor                     
## 180 Site_others _d                      factor                     
## 181 73. Management                      logical                    
## 182 Intubation                          factor                     
## 183 ICU                                 factor                     
## 184 68. Neurological complication...108 logical                    
## 185 _id                                 numeric                    
## 186 _uuid                               factor                     
## 187 _submission_time                    POSIXct                    
## 188 _validation_status                  logical                    
## 189 _notes                              logical                    
## 190 _status                             factor                     
## 191 _submitted_by                       logical                    
## 192 _tags                               logical                    
## 193 _index                              numeric                    
## 194 hlk                                 factor                     
## 195 agegp                               factor                     
## 196 sex                                 character                  
## 197 rel_BM                              factor                     
## 198 rel_CNS                             factor                     
## 199 rel_Testis                          factor                     
## 200 rdate                               Date                       
## 201 outcome                             factor                     
## 202 sdate                               Date                       
## 203 stime                               numeric                    
## 204 event                               logical                    
## 205 edate                               Date                       
## 206 etime                               numeric                    
## 207 bmi                                 numeric                    
## 208 P5                                  numeric                    
## 209 P85                                 numeric                    
## 210 P95                                 numeric                    
## 211 bmi.gp                              factor                     
## 212 early_com                           logical
summ()
## 
## No. of observations = 690
## 
## 
##     Var. name                           obs. mean             median           s.d.            min.             max.            
## 1   1.site                              690  1                1                0               1                1               
## 2   Year_diag                           690  2550.05          2550             5.42            2541             2560            
## 3   No_year                             690  21.77            19.5             15.2            1                71              
## 4   2.register no                       619  1907369597783.05 1909803497561    405603052257.34 94021007440      5961200046342   
## 5   HN                                  690  345.5            345.5            199.33          1                690             
## 6   Sex                                 690  1.42             1                0.49            1                2               
## 7   place                               690  5.5              6                3.49            1                15              
## 8   dob                                 690  2544-07-24 12:35 2544-11-09 12:00 <NA>            2526-09-19 00:00 2559-03-13 00:00
## 9   A_ visit                            690  2550-07-10 19:09 2550-05-15 00:00 <NA>            2541-01-08 00:00 2560-12-26 00:00
## 10  A_dx                                690  2007-07-14       2007-05-16       <NA>            1998-01-09       2017-12-28      
## 11  Age                                 690  71.67            55.5             48              2                180             
## 12  BW                                  690  20.64            16               13.62           2.1              112             
## 13  HT                                  690  110.14           105              25.71           44               177             
## 14  Right                               690  1.1              1                0.34            1                4               
## 15  Right_other                         2    1.5              1.5              0.707           1                2               
## 16  Mor                                 690  1.3              1                0.459           1                2               
## 17  ALL                                 483  1.961            2                0.47            1                3               
## 18  13.2ANLL                            206  5.33             5                2               1                8               
## 19  Chro                                689  2.85             3                0.8             1                4               
## 20  Chro_Others                         143  71.154           71               40.856          1                141             
## 21  15.1st CBC                                                                                                                  
## 22  wbc                                 690  66157.2          17525            128839.25       600              1251700         
## 23  hb                                  690  7.42             7.3              3.19            1.4              64              
## 24  hct                                 690  22.46            22               7.03            4.3              43              
## 25  plt                                 690  72060.29         39500            92586.24        2000             823000          
## 26  blast                               689  48.15            49               34.85           0                100             
## 27  16.1st Serum chem                                                                                                           
## 28  bun                                 690  12.22            11.3             7.09            2.2              118.6           
## 29  creatinine                          690  0.55             0.5              0.36            0.13             6               
## 30  calcium                             690  9.48             9.4              1.01            4                18.4            
## 31  phosphorus                          690  4.77             4.8              1.29            0.31             19.3            
## 32  uric                                690  5.81             5.2              3.17            1.1              43.9            
## 33  SGOT                                690  51.31            32               74.2            7                1039            
## 34  SGPT                                690  30.99            15               57.89           2                640             
## 35  ALP                                 690  167.21           141.5            107.27          4                1055            
## 36  LDH                                 684  2555.35          1063.5           4643.32         11               46050           
## 37  Mediastinal_mass                    690  1.965            2                0.183           1                2               
## 38  Fever                               690  1.251            1                0.434           1                2               
## 39  Arthitis                            690  1.922            2                0.269           1                2               
## 40  Arthitis_d                          54   25.185           25.5             13.601          1                48              
## 41  Xray                                690  1.928            2                0.259           1                2               
## 42  hepato                              690  1.112            1                0.315           1                2               
## 43  hepato_size                         681  4.27             4                2.65            0                15              
## 44  Splenomegaly                        690  1.346            1                0.476           1                2               
## 45  Spleen_size                         635  3.41             3                3.18            0                16              
## 46  lymphodenopathy                     690  1.23             1                0.421           1                2               
## 47  Exopthalmos                         690  1.959            2                0.197           1                2               
## 48  Skin_bleeding                       690  1.59             2                0.492           1                2               
## 49  Gum_hypertrophy                     690  1.904            2                0.294           1                2               
## 50  Bleed_gum                           690  1.854            2                0.354           1                2               
## 51  Jaundice                            690  1.991            2                0.093           1                2               
## 52  Underlying                          690  1.943            2                0.231           1                2               
## 53  Underlying_d                        40   6.025            4                3.86            1                16              
## 54  chemotherapy                        690  1                1                0               1                1               
## 55  if yes (protocol)...56              690  24.965           21               13.268          1                48              
## 56  start date...57                     690  2550-07-26 07:39 2550-05-27 00:00 <NA>            2541-01-14 00:00 2560-12-28 00:00
## 57  31.1 remission                      690  1.35             1                0.84            1                4               
## 58  if yes (date)...59                  569  2550-09-30 04:13 2550-07-24 00:00 <NA>            2541-02-19 00:00 2561-02-05 00:00
## 59  31.2 complete protocol              690  1.52             2                0.5             1                2               
## 60  if yes (date)...61                  333  2553-10-23 21:58 2553-12-13 00:00 <NA>            2543-06-22 00:00 2563-04-27 00:00
## 61  31.3 relapsed                       690  1.67             2                0.47            1                2               
## 62  if yes (date)...63                  226  2551-10-30 11:15 2551-10-15 00:00 <NA>            2542-06-14 00:00 2562-09-23 00:00
## 63  site...64                           226  1.6              1                1.09            1                4               
## 64  if combined...65                    34   1.647            1                0.95            1                4               
## 65  32. 2nd protocol chemo.             688  1.75             2                0.43            1                2               
## 66  if yes (protocol)...67              175  23.623           22               11.591          1                45              
## 67  start date...68                     174  2551-11-20 11:26 2551-11-12 12:00 <NA>            2542-11-12 00:00 2562-10-01 00:00
## 68  32.1 remission                      173  1.91             2                1.09            1                4               
## 69  if yes (date)...70                  86   2552-08-12 02:30 2552-06-13 00:00 <NA>            2543-05-01 00:00 2562-12-11 00:00
## 70  32.2 complete protocol              169  1.92             2                0.28            1                2               
## 71  if yes (date)...72                  15   223210.47        238193           61771.87        2                241911          
## 72  32.3 relapsed                       167  1.65             2                0.48            1                2               
## 73  if yes (date)...74                  58   2552-01-25 22:20 2552-07-06 00:00 <NA>            2543-12-18 00:00 2563-01-27 00:00
## 74  site...75                           57   1.4              1                0.86            1                4               
## 75  if combined...76                    4    1.75             1.5              0.957           1                3               
## 76  33. status                          690  3.09             4                1.02            1                4               
## 77  if death, cause                     375  1.21             1                0.48            1                3               
## 78  date of death                       378  2551-08-10 03:29 2551-07-15 12:00 <NA>            2541-03-26 00:00 2563-05-30 00:00
## 79  34. Last FU                         690  2556-10-15 15:49 2558-11-12 00:00 <NA>            2541-03-26 00:00 2563-06-01 00:00
## 80  ...81                               9    1.333            1                0.707           1                3               
## 81  start                               690  2021-10-15 19:28 2021-10-09 14:01 <NA>            2021-09-25 23:33 2022-01-03 22:08
## 82  end                                 690  2021-10-16 03:47 2021-10-09 15:21 <NA>            2021-09-25 23:38 2022-01-03 22:11
## 83  1. Site number                                                                                                              
## 84  Dx                                  690  1.294            1                0.456           1                2               
## 85  Others...8                                                                                                                  
## 86  A_CMT                               690  1                1                0               1                1               
## 87  A_protocol                          690  11.88            6                10.943          1                39              
## 88  A_date                              687  2007-07-11 06:40 2007-05-11 00:00 <NA>            1991-05-30 00:00 2017-12-28 00:00
## 89  A_Remiss                            688  1.384            1                0.806           1                4               
## 90  A_Remiss_day                        536  2007-11-08 20:57 2007-08-28 00:00 <NA>            1998-02-19 00:00 2021-11-12 00:00
## 91  A_Comp                              689  1.511            2                0.5             1                2               
## 92  A_LastCMT                           337  2011-03-13 19:00 2011-04-04 00:00 <NA>            1998-09-21 00:00 2021-04-05 00:00
## 93  A_Relapsed                          689  1.673            2                0.469           1                2               
## 94  A_Relapse_date                      224  2009-02-07 16:55 2009-03-23 12:00 <NA>            1998-11-10 00:00 2022-04-05 00:00
## 95  A_Site_relapsed                     222  2.167            1                2.258           1                9               
## 96  A_BM                                222  0.87             1                0.34            0                1               
## 97  A_CNS                               222  0.23             0                0.42            0                1               
## 98  A_testis                            222  0.09             0                0.29            0                1               
## 99  A_ Combine                          222  0                0                0.07            0                1               
## 100 A_combine_data                      1    1                1                <NA>            1                1               
## 101 A_type_relapsed                     171  2.298            2                0.573           1                3               
## 102 B_CMT                               687  1.687            2                0.464           1                2               
## 103 B_protocol                          215  20.507           16               14.265          1                49              
## 104 B_date                              215  2008-03-16 08:22 2008-01-30 00:00 <NA>            1998-04-09 00:00 2021-05-18 00:00
## 105 B_Remiss                            215  1.967            2                1.02            1                4               
## 106 B_remiss_day                        93   2008-09-02 14:27 2007-11-20 00:00 <NA>            1998-05-26 00:00 2019-11-12 00:00
## 107 B_Comp                              214  1.888            2                0.316           1                2               
## 108 B_LastCMT                           24   2010-01-28 18:00 2008-12-15 00:00 <NA>            2002-09-02 00:00 2019-04-29 00:00
## 109 B_relapsed                          214  1.668            2                0.472           1                2               
## 110 B_relapsed_date                     69   2008-07-04 16:41 2007-11-20 00:00 <NA>            1999-06-14 00:00 2020-07-24 00:00
## 111 B_Site_relapsed                     71   1.592            1                1.214           1                5               
## 112 B_BM                                71   0.83             1                0.38            0                1               
## 113 B_CNS                               71   0.17             0                0.38            0                1               
## 114 B_testis                            71   0.04             0                0.2             0                1               
## 115 B_ Combine                          71   0                0                0               0                0               
## 116 B_type_relapsed                     71   2.873            3                0.861           1                4               
## 117 C_CMT                               688  1.929            2                0.257           1                2               
## 118 C_protocol                          49   13.531           13               9.06            1                30              
## 119 C_date                              46   2008-02-26 06:46 2007-07-17 00:00 <NA>            1999-07-21 00:00 2019-11-07 00:00
## 120 C_Remiss                            46   2.326            2                0.967           1                4               
## 121 C_remiss_day                        9    2007-08-27 00:00 2005-05-18 00:00 <NA>            2000-05-01 00:00 2019-12-16 00:00
## 122 C_Comp                              47   1.936            2                0.247           1                2               
## 123 C_LastCMT                           3    2009-11-14 00:00 2008-05-20 00:00 <NA>            2004-03-01 00:00 2017-01-23 00:00
## 124 C_relapsed                          47   1.787            2                0.414           1                2               
## 125 C_relapsed_date                     10   2007-06-27 00:00 2008-08-13 00:00 <NA>            2000-01-31 00:00 2013-10-06 00:00
## 126 C_Site_relapsed                     10   1.3              1                0.675           1                3               
## 127 C_BM                                10   0.9              1                0.32            0                1               
## 128 C_CNS                               10   0.2              0                0.42            0                1               
## 129 C_testis                            10   0                0                0               0                0               
## 130 C_ Combine                          10   0                0                0               0                0               
## 131 C_type_relapsed                     9    2                2                0.707           1                3               
## 132 Status                              690  3.061            4                1.005           1                4               
## 133 death_cause                         367  1.305            1                0.461           1                2               
## 134 death_date                          367  2009-02-10       2008-11-24       <NA>            1998-03-03       2021-07-28      
## 135 Last_date                           351  2020-04-03       2021-07-06       <NA>            1999-12-08       2021-12-27      
## 136 CNS_diagnosis                       659  2.625            1                1.915           1                5               
## 137 67. Metabolic complication                                                                                                  
## 138 TLS                                 668  1.883            2                0.321           1                2               
## 139 68. Neurological complication...63                                                                                          
## 140 Seizure                             662  1.956            2                0.205           1                2               
## 141 ICH                                 664  1.988            2                0.109           1                2               
## 142 Ischemic_stroke                     664  1.994            2                0.077           1                2               
## 143 IICP                                663  1.979            2                0.144           1                2               
## 144 Imaging                             664  1.955            2                0.208           1                2               
## 145 Surgery                             664  1.998            2                0.039           1                2               
## 146 Finding                             29   15               15               8.515           1                29              
## 147 Operation                           1    1                1                <NA>            1                1               
## 148 69. Respiratory complication                                                                                                
## 149 Respiratory _status                 664  1.866            2                0.341           1                2               
## 150 69.2 Diagnosis                                                                                                              
## 151 Pneumonia                           664  1.886            2                0.319           1                2               
## 152 Pleural_effusion                    665  1.964            2                0.187           1                2               
## 153 ARDS                                664  1.968            2                0.175           1                2               
## 154 Pul_leuko                           662  1                1                0               1                1               
## 155 Pul_ hem                            661  1.982            2                0.134           1                2               
## 156 Others...80                         664  1.989            2                0.102           1                2               
## 157 Others_data                         7    1.857            2                0.69            1                3               
## 158 DIC                                 664  2.233            2                0.723           1                3               
## 159 71. Renal complication                                                                                                      
## 160 Renal_insuf                         664  1.938            2                0.241           1                2               
## 161 RRT                                 665  1.989            2                0.102           1                2               
## 162 RRT_med                             7    1                1                0               1                1               
## 163 72. Infection                                                                                                               
## 164 Infect                              665  1.12             1                0.326           1                2               
## 165 Sepsis                              649  1.746            2                0.436           1                2               
## 166 Sepsis_ site                        585  22.456           26               6.011           1                26              
## 167 site_ CNS                           585  0                0                0               0                0               
## 168 Site_respi                          585  0.14             0                0.35            0                1               
## 169 Site_CVS                            585  0                0                0               0                0               
## 170 Site_GI                             585  0.15             0                0.36            0                1               
## 171 Site_GU                             585  0.06             0                0.24            0                1               
## 172 Site_skin                           585  0.19             0                0.4             0                1               
## 173 Site_others                         585  0.92             1                0.27            0                1               
## 174 site_ CNS_d                                                                                                                 
## 175 Site_respi_d                        82   19.951           24               7.586           1                32              
## 176 Site_CVS_d                                                                                                                  
## 177 Site_GI_d                           86   15.174           13.5             7.929           1                30              
## 178 Site_GU_d                           37   9.892            12               4.557           1                14              
## 179 Site_skin_d                         114  33.219           36               16.66           1                55              
## 180 Site_others _d                      538  36.4             30               18.779          1                104             
## 181 73. Management                                                                                                              
## 182 Intubation                          664  1.881            2                0.324           1                2               
## 183 ICU                                 665  1.856            2                0.352           1                2               
## 184 68. Neurological complication...108                                                                                         
## 185 _id                                 690  119355354.81     118275361.5      4223557.45      115986389        133448059       
## 186 _uuid                               690  345.5            345.5            199.33          1                690             
## 187 _submission_time                    690  2021-10-15 14:10 2021-10-09 07:04 <NA>            2021-09-25 16:38 2022-01-03 15:11
## 188 _validation_status                                                                                                          
## 189 _notes                                                                                                                      
## 190 _status                             690  1                1                0               1                1               
## 191 _submitted_by                                                                                                               
## 192 _tags                                                                                                                       
## 193 _index                              690  367.54           370              213.1           1                741             
## 194 hlk                                 690  1.177            1                0.382           1                2               
## 195 agegp                               690  2.771            3                1.042           1                4               
## 196 sex                                                                                                                         
## 197 rel_BM                              690  1.72             2                0.449           1                2               
## 198 rel_CNS                             690  1.926            2                0.262           1                2               
## 199 rel_Testis                          690  1.971            2                0.168           1                2               
## 200 rdate                               236  2008-11-14       2008-08-12       <NA>            1998-11-10       2022-04-05      
## 201 outcome                             690  1.532            2                0.499           1                2               
## 202 sdate                               690  2014-11-13       2017-11-10       <NA>            1998-03-03       2021-12-27      
## 203 stime                               690  7.33             4.36             7.08            0.01             23.58           
## 204 event                               690  0.57             1                0.49            0                1               
## 205 edate                               690  2014-02-13       2015-11-04       <NA>            1998-03-03       2022-04-05      
## 206 etime                               690  6.58             3.56             6.95            0                23.58           
## 207 bmi                                 690  15.64            14.94            3.47            2.77             44.46           
## 208 P5                                  690  14.19            14.01            0.67            13.42            16.55           
## 209 P85                                 690  18.3             17.36            1.91            16.76            23.99           
## 210 P95                                 690  20.04            18.46            2.82            17.82            28.05           
## 211 bmi.gp                              690  1.878            2                0.775           1                4               
## 212 early_com                           690  0.86             1                0.35            0                1
tableStack(vars=c(Age, agegp, sex, BW, HT, bmi.gp, 
                  Mor, ALL, 
                  hb:blast,
                  bun:LDH,
                  Mediastinal_mass:Arthitis,
                  Xray, hepato, Splenomegaly, lymphodenopathy, Exopthalmos,
                  Dx, # Why does this not correspond to Mor? 
                  A_CMT,
                  TLS,Seizure,ICH,Ischemic_stroke, IICP, `Respiratory _status`, Pneumonia, Pleural_effusion, ARDS, Pul_leuko, `Pul_ hem`, DIC, Renal_insuf, RRT, RRT_med, Infect, Sepsis, Intubation, ICU, early_com), 
           by="none")
##                     Total            
## Total               690              
##                                      
## Age                                  
##   Mean (SD)         71.7 (48.0)      
## agegp                                
##    0-23             89 (12.9)        
##    24-49            204 (29.6)       
##    48-91            173 (25.1)       
##    92+              224 (32.5)       
## sex                                  
##    Female           293 (42.5)       
##    Male             397 (57.5)       
## BW                                   
##   Mean (SD)         20.6 (13.6)      
## HT                                   
##   Mean (SD)         110.1 (25.7)     
## bmi.gp                               
##    Underweight      212 (30.7)       
##    Normal           392 (56.8)       
##    Overweight       44 (6.4)         
##    Obese            42 (6.1)         
## Mor                                  
##    ALL              483 (70.0)       
##    AML              207 (30.0)       
## ALL                                  
##    T_cell           63 (13.0)        
##    B-cell           376 (77.8)       
##    FAB              44 (9.1)         
## hb                                   
##   Mean (SD)         7.4 (3.2)        
## hct                                  
##   Mean (SD)         22.5 (7.0)       
## plt                                  
##   Mean (SD)         72060.3 (92586.2)
## blast                                
##   Mean (SD)         48.1 (34.9)      
## bun                                  
##   Mean (SD)         12.2 (7.1)       
## creatinine                           
##   Mean (SD)         0.5 (0.4)        
## calcium                              
##   Mean (SD)         9.5 (1.0)        
## phosphorus                           
##   Mean (SD)         4.8 (1.3)        
## uric                                 
##   Mean (SD)         5.8 (3.2)        
## SGOT                                 
##   Mean (SD)         51.3 (74.2)      
## SGPT                                 
##   Mean (SD)         31.0 (57.9)      
## ALP                                  
##   Mean (SD)         167.2 (107.3)    
## LDH                                  
##   Mean (SD)         2555.4 (4643.3)  
## Mediastinal_mass                     
##    Yes              24 (3.5)         
##    No               666 (96.5)       
## Fever                                
##    Yes              517 (74.9)       
##    No               173 (25.1)       
## Arthitis                             
##    Yes              54 (7.8)         
##    No               636 (92.2)       
## Xray                                 
##    Yes              50 (7.2)         
##    No               640 (92.8)       
## hepato                               
##    Yes              613 (88.8)       
##    No               77 (11.2)        
## Splenomegaly                         
##    Yes              451 (65.4)       
##    No               239 (34.6)       
## lymphodenopathy                      
##    Yes              531 (77.0)       
##    No               159 (23.0)       
## Exopthalmos                          
##    Yes              28 (4.1)         
##    No               662 (95.9)       
## Dx                                   
##    76.1. ALL        487 (70.6)       
##    76.2. AML        203 (29.4)       
## A_CMT                                
##    1. Yes           690 (100.0)      
## TLS                                  
##    1. Yes           78 (11.7)        
##    2. No            590 (88.3)       
## Seizure                              
##    1. Yes           29 (4.4)         
##    2. No            633 (95.6)       
## ICH                                  
##    1. Yes           8 (1.2)          
##    2. No            656 (98.8)       
## Ischemic_stroke                      
##    1. Yes           4 (0.6)          
##    2. No            660 (99.4)       
## IICP                                 
##    1. Yes           14 (2.1)         
##    2. No            649 (97.9)       
## Respiratory _status                  
##    1. Yes           89 (13.4)        
##    2. No            575 (86.6)       
## Pneumonia                            
##    1. Yes           76 (11.4)        
##    2. No            588 (88.6)       
## Pleural_effusion                     
##    1. Yes           24 (3.6)         
##    2. No            641 (96.4)       
## ARDS                                 
##    1. Yes           21 (3.2)         
##    2. No            643 (96.8)       
## Pul_leuko                            
##    2. No            662 (100.0)      
## Pul_ hem                             
##    1. Yes           12 (1.8)         
##    2. No            649 (98.2)       
## DIC                                  
##    70.1 Yes         114 (17.2)       
##    70.2 No          281 (42.3)       
##    70.3 No data     269 (40.5)       
## Renal_insuf                          
##    1. Yes           41 (6.2)         
##    2. No            623 (93.8)       
## RRT                                  
##    1. Yes           7 (1.1)          
##    2. No            658 (98.9)       
## RRT_med                              
##    71.2.2 CAPD      7 (100.0)        
## Infect                               
##    1. Yes           585 (88.0)       
##    2. No            80 (12.0)        
## Sepsis                               
##    1. Yes           165 (25.4)       
##    2. No            484 (74.6)       
## Intubation                           
##    1. Yes           79 (11.9)        
##    2. No            585 (88.1)       
## ICU                                  
##    1. Yes           96 (14.4)        
##    2. No            569 (85.6)       
## early_com                            
##    No               96 (13.9)        
##    Yes              594 (86.1)
# Six cases have conflicting data
count(data, Dx, Mor)
## # A tibble: 4 × 3
##   Dx        Mor       n
##   <fct>     <fct> <int>
## 1 76.1. ALL ALL     482
## 2 76.1. ALL AML       5
## 3 76.2. AML ALL       1
## 4 76.2. AML AML     202
filter(data, Dx=="76.1. ALL" & Mor=="AML" | Dx=="76.2. AML" & Mor=="ALL") %>%
  select(5:10, Dx, Mor, ALL)
## # A tibble: 6 × 9
##   HN        Sex place dob                 `A_ visit`          A_dx       Dx        Mor   ALL   
##   <fct>   <dbl> <dbl> <dttm>              <dttm>              <date>     <fct>     <fct> <fct> 
## 1 949283      2    11 2536-10-05 00:00:00 2543-03-24 00:00:00 2000-03-28 76.1. ALL AML   <NA>  
## 2 1155703     2     2 2538-08-18 00:00:00 2548-07-07 00:00:00 2005-07-11 76.1. ALL AML   <NA>  
## 3 1279281     1     1 2544-07-13 00:00:00 2548-08-15 00:00:00 2005-08-19 76.1. ALL AML   <NA>  
## 4 1298714     1    11 2539-10-21 00:00:00 2548-12-06 00:00:00 2005-12-07 76.2. AML ALL   B-cell
## 5 1337489     1     8 2547-06-04 00:00:00 2549-07-26 00:00:00 2006-07-26 76.1. ALL AML   <NA>  
## 6 1041881     1     3 2538-09-16 00:00:00 2552-01-20 00:00:00 2009-01-21 76.1. ALL AML   <NA>
count(data, RRT_med, hlk)
## # A tibble: 4 × 3
##   RRT_med     hlk       n
##   <fct>       <fct> <int>
## 1 71.2.2 CAPD No        4
## 2 71.2.2 CAPD Yes       3
## 3 <NA>        No      564
## 4 <NA>        Yes     119
count(data, Pul_leuko, hlk)
## # A tibble: 4 × 3
##   Pul_leuko hlk       n
##   <fct>     <fct> <int>
## 1 2. No     No      548
## 2 2. No     Yes     114
## 3 <NA>      No       20
## 4 <NA>      Yes       8
T1 <- tableStack(vars=c(Age,agegp,  sex, BW, HT, bmi.gp,
                        Mor, ALL,
                        hb:blast,
                        bun:LDH,
                        Mediastinal_mass:Arthitis,
                        Xray, hepato, Splenomegaly, lymphodenopathy, Exopthalmos, 
                        TLS,Seizure,ICH,Ischemic_stroke, IICP, `Respiratory _status`, Pneumonia, Pleural_effusion, ARDS, Pul_leuko, `Pul_ hem`, DIC, Renal_insuf, RRT, Infect, Sepsis, Intubation, ICU, early_com),
                 by=hlk, total.column = TRUE); T1
##                     No                      Yes                     Total                   Test stat.              P value
## Total               568                     122                     690                                                    
##                                                                                                                            
## Age                                                                                         Ranksum test            < 0.001
##   Median (IQR)      53.0 (33.8,98.2)        95.5 (34,147.5)         55.5 (34,105.0)                                        
## agegp                                                                                       Chisq. (3 df) = 41.16   < 0.001
##    0-23             67 (11.8)               22 (18.0)               89 (12.9)                                              
##    24-49            184 (32.4)              20 (16.4)               204 (29.6)                                             
##    48-91            158 (27.8)              15 (12.3)               173 (25.1)                                             
##    92+              159 (28.0)              65 (53.3)               224 (32.5)                                             
## sex                                                                                         Chisq. (1 df) = 0.12    0.732  
##    Female           239 (42.1)              54 (44.3)               293 (42.5)                                             
##    Male             329 (57.9)              68 (55.7)               397 (57.5)                                             
## BW                                                                                          Ranksum test            < 0.001
##   Median (IQR)      15.7 (12.2,22.1)        20.4 (12.2,37.9)        16.0 (12.2,24.4)                                       
## HT                                                                                          Ranksum test            < 0.001
##   Median (IQR)      104.0 (91,124.5)        122.9 (92.2,150.0)      105.0 (91,127.9)                                       
## bmi.gp                                                                                      Chisq. (3 df) = 1.99    0.574  
##    Underweight      179 (31.5)              33 (27.0)               212 (30.7)                                             
##    Normal           319 (56.2)              73 (59.8)               392 (56.8)                                             
##    Overweight       34 (6.0)                10 (8.2)                44 (6.4)                                               
##    Obese            36 (6.3)                6 (4.9)                 42 (6.1)                                               
## Mor                                                                                         Chisq. (1 df) = 1.14    0.286  
##    ALL              403 (71.0)              80 (65.6)               483 (70.0)                                             
##    AML              165 (29.0)              42 (34.4)               207 (30.0)                                             
## ALL                                                                                         Chisq. (2 df) = 28.59   < 0.001
##    T_cell           38 (9.4)                25 (31.2)               63 (13.0)                                              
##    B-cell           325 (80.6)              51 (63.7)               376 (77.8)                                             
##    FAB              40 (9.9)                4 (5.0)                 44 (9.1)                                               
## hb                                                                                          Ranksum test            0.09   
##   Median (IQR)      7.5 (5.7,9.1)           6.9 (5.5,8.4)           7.3 (5.6,9.1)                                          
## hct                                                                                         t-test  (688 df) = 1.22 0.222  
##   Mean (SD)         22.6 (7.0)              21.8 (7.0)              22.5 (7.0)                                             
## plt                                                                                         Ranksum test            0.01   
##   Median (IQR)      41000.0 (18000,94250.0) 35000.0 (20000,56000.0) 39500.0 (19000,86000.0)                                
## blast                                                                                       Ranksum test            < 0.001
##   Median (IQR)      38.0 (8,68.0)           93.0 (87,96.0)          49.0 (13,85.0)                                         
## bun                                                                                         Ranksum test            0.077  
##   Median (IQR)      11.4 (9,14.3)           10.4 (7.2,14.6)         11.3 (8.7,14.3)                                        
## creatinine                                                                                  Ranksum test            0.036  
##   Median (IQR)      0.5 (0.4,0.6)           0.6 (0.4,0.8)           0.5 (0.4,0.7)                                          
## calcium                                                                                     Ranksum test            < 0.001
##   Median (IQR)      9.5 (9,9.9)             9.2 (8.7,9.7)           9.4 (9,9.9)                                            
## phosphorus                                                                                  Ranksum test            < 0.001
##   Median (IQR)      4.9 (4.2,5.5)           4.1 (3.3,5.1)           4.8 (4.1,5.5)                                          
## uric                                                                                        Ranksum test            < 0.001
##   Median (IQR)      5.0 (3.8,6.6)           6.2 (4.6,9.0)           5.2 (3.9,6.8)                                          
## SGOT                                                                                        Ranksum test            < 0.001
##   Median (IQR)      31.0 (23,45.2)          40.5 (26,71.0)          32.0 (24,49.0)                                         
## SGPT                                                                                        Ranksum test            0.002  
##   Median (IQR)      15.0 (10,26.2)          19.5 (13,37.5)          15.0 (10,28.0)                                         
## ALP                                                                                         Ranksum test            < 0.001
##   Median (IQR)      138.0 (106.8,182.2)     177.0 (111.5,257.0)     141.5 (107,193.0)                                      
## LDH                                                                                         Ranksum test            < 0.001
##   Median (IQR)      928.0 (607,1921.0)      1968.0 (1175,4470.0)    1063.5 (644.5,2301.2)                                  
## Mediastinal_mass                                                                            Fisher's exact test     0.054  
##    Yes              16 (2.8)                8 (6.6)                 24 (3.5)                                               
##    No               552 (97.2)              114 (93.4)              666 (96.5)                                             
## Fever                                                                                       Chisq. (1 df) = 4.38    0.036  
##    Yes              416 (73.2)              101 (82.8)              517 (74.9)                                             
##    No               152 (26.8)              21 (17.2)               173 (25.1)                                             
## Arthitis                                                                                    Chisq. (1 df) = 6.86    0.009  
##    Yes              52 (9.2)                2 (1.6)                 54 (7.8)                                               
##    No               516 (90.8)              120 (98.4)              636 (92.2)                                             
## Xray                                                                                        Chisq. (1 df) = 5.96    0.015  
##    Yes              48 (8.5)                2 (1.6)                 50 (7.2)                                               
##    No               520 (91.5)              120 (98.4)              640 (92.8)                                             
## hepato                                                                                      Chisq. (1 df) = 6.61    0.01   
##    Yes              496 (87.3)              117 (95.9)              613 (88.8)                                             
##    No               72 (12.7)               5 (4.1)                 77 (11.2)                                              
## Splenomegaly                                                                                Chisq. (1 df) = 22.78   < 0.001
##    Yes              348 (61.3)              103 (84.4)              451 (65.4)                                             
##    No               220 (38.7)              19 (15.6)               239 (34.6)                                             
## lymphodenopathy                                                                             Chisq. (1 df) = 6.32    0.012  
##    Yes              426 (75.0)              105 (86.1)              531 (77.0)                                             
##    No               142 (25.0)              17 (13.9)               159 (23.0)                                             
## Exopthalmos                                                                                 Fisher's exact test     0.203  
##    Yes              26 (4.6)                2 (1.6)                 28 (4.1)                                               
##    No               542 (95.4)              120 (98.4)              662 (95.9)                                             
## TLS                                                                                         Chisq. (1 df) = 19.24   < 0.001
##    1. Yes           50 (9.1)                28 (23.9)               78 (11.7)                                              
##    2. No            501 (90.9)              89 (76.1)               590 (88.3)                                             
## Seizure                                                                                     Fisher's exact test     0.02   
##    1. Yes           19 (3.5)                10 (8.8)                29 (4.4)                                               
##    2. No            529 (96.5)              104 (91.2)              633 (95.6)                                             
## ICH                                                                                         Fisher's exact test     0.005  
##    1. Yes           3 (0.5)                 5 (4.4)                 8 (1.2)                                                
##    2. No            547 (99.5)              109 (95.6)              656 (98.8)                                             
## Ischemic_stroke                                                                             Fisher's exact test     1      
##    1. Yes           4 (0.7)                 0 (0.0)                 4 (0.6)                                                
##    2. No            546 (99.3)              114 (100.0)             660 (99.4)                                             
## IICP                                                                                        Fisher's exact test     0.073  
##    1. Yes           9 (1.6)                 5 (4.4)                 14 (2.1)                                               
##    2. No            541 (98.4)              108 (95.6)              649 (97.9)                                             
## Respiratory _status                                                                         Chisq. (1 df) = 2.49    0.115  
##    1. Yes           68 (12.4)               21 (18.4)               89 (13.4)                                              
##    2. No            482 (87.6)              93 (81.6)               575 (86.6)                                             
## Pneumonia                                                                                   Chisq. (1 df) = 0.63    0.428  
##    1. Yes           60 (10.9)               16 (14.0)               76 (11.4)                                              
##    2. No            490 (89.1)              98 (86.0)               588 (88.6)                                             
## Pleural_effusion                                                                            Fisher's exact test     0.161  
##    1. Yes           17 (3.1)                7 (6.1)                 24 (3.6)                                               
##    2. No            534 (96.9)              107 (93.9)              641 (96.4)                                             
## ARDS                                                                                        Fisher's exact test     0.383  
##    1. Yes           16 (2.9)                5 (4.4)                 21 (3.2)                                               
##    2. No            534 (97.1)              109 (95.6)              643 (96.8)                                             
## Pul_leuko                                                                                   Chisq. (1 df) = 284.53  < 0.001
##    2. No            548 (100.0)             114 (100.0)             662 (100.0)                                            
## Pul_ hem                                                                                    Fisher's exact test     0.444  
##    1. Yes           9 (1.6)                 3 (2.6)                 12 (1.8)                                               
##    2. No            538 (98.4)              111 (97.4)              649 (98.2)                                             
## DIC                                                                                         Chisq. (2 df) = 19.76   < 0.001
##    70.1 Yes         80 (14.5)               34 (29.8)               114 (17.2)                                             
##    70.2 No          231 (42.0)              50 (43.9)               281 (42.3)                                             
##    70.3 No data     239 (43.5)              30 (26.3)               269 (40.5)                                             
## Renal_insuf                                                                                 Chisq. (1 df) = 2.28    0.131  
##    1. Yes           30 (5.4)                11 (9.7)                41 (6.2)                                               
##    2. No            521 (94.6)              102 (90.3)              623 (93.8)                                             
## RRT                                                                                         Fisher's exact test     0.102  
##    1. Yes           4 (0.7)                 3 (2.6)                 7 (1.1)                                                
##    2. No            547 (99.3)              111 (97.4)              658 (98.9)                                             
## Infect                                                                                      Chisq. (1 df) = 5.21    0.023  
##    1. Yes           477 (86.6)              108 (94.7)              585 (88.0)                                             
##    2. No            74 (13.4)               6 (5.3)                 80 (12.0)                                              
## Sepsis                                                                                      Chisq. (1 df) = 4.2     0.04   
##    1. Yes           128 (23.7)              37 (33.6)               165 (25.4)                                             
##    2. No            411 (76.3)              73 (66.4)               484 (74.6)                                             
## Intubation                                                                                  Chisq. (1 df) = 6.36    0.012  
##    1. Yes           57 (10.4)               22 (19.3)               79 (11.9)                                              
##    2. No            493 (89.6)              92 (80.7)               585 (88.1)                                             
## ICU                                                                                         Chisq. (1 df) = 22.06   < 0.001
##    1. Yes           63 (11.4)               33 (28.9)               96 (14.4)                                              
##    2. No            488 (88.6)              81 (71.1)               569 (85.6)                                             
## early_com                                                                                   Chisq. (1 df) = 1.66    0.197  
##    No               84 (14.8)               12 (9.8)                96 (13.9)                                              
##    Yes              484 (85.2)              110 (90.2)              594 (86.1)
write.csv(T1, file="Table 1.csv")


count(data, Mor)
## # A tibble: 2 × 2
##   Mor       n
##   <fct> <int>
## 1 ALL     483
## 2 AML     207
# Subgroups

data.ALL <- filter(data, Mor=="ALL")
data.AML <- filter(data, Mor=="AML")

use(data.frame(data.ALL))

T2a <- tableStack(vars=c(Age,agegp, sex, BW, HT, bmi.gp),
               by=hlk, total.column = TRUE); T2a
##                No               Yes              Total              Test stat.            P value
## Total          403              80               483                                             
##                                                                                                  
## Age                                                                 Ranksum test          0.013  
##   Median (IQR) 52.0 (36,86.0)   91.0 (34,137.2)  54.0 (36,97.0)                                  
## agegp                                                               Chisq. (3 df) = 34.18 < 0.001
##    0-23        33 (8.2)         13 (16.2)        46 (9.5)                                        
##    24-49       145 (36.0)       16 (20.0)        161 (33.3)                                      
##    48-91       130 (32.3)       11 (13.8)        141 (29.2)                                      
##    92+         95 (23.6)        40 (50.0)        135 (28.0)                                      
## sex                                                                 Chisq. (1 df) = 1.88  0.171  
##    Female      170 (42.2)       41 (51.2)        211 (43.7)                                      
##    Male        233 (57.8)       39 (48.8)        272 (56.3)                                      
## BW                                                                  Ranksum test          0.006  
##   Median (IQR) 15.7 (12.6,21.0) 20.1 (13.2,31.5) 16.0 (12.6,22.4)                                
## HT                                                                  Ranksum test          0.009  
##   Median (IQR) 104.0 (92,119.8) 120.4 (93,140.2) 105.0 (92.5,124.8)                              
## bmi.gp                                                              Chisq. (3 df) = 4.09  0.251  
##    Underweight 128 (31.8)       18 (22.5)        146 (30.2)                                      
##    Normal      228 (56.6)       52 (65.0)        280 (58.0)                                      
##    Overweight  19 (4.7)         6 (7.5)          25 (5.2)                                        
##    Obese       28 (6.9)         4 (5.0)          32 (6.6)
write.csv(T2a, file="Table 2.csv")

T2b <- tableStack(vars=c(hb:blast,
                              bun:LDH,
                              Mediastinal_mass:Arthitis,
                              Xray, hepato, Splenomegaly, lymphodenopathy, Exopthalmos,
                              Mediastinal_mass:Arthitis,
                              Xray, hepato, Splenomegaly, lymphodenopathy, Exopthalmos),
                       by=hlk, total.column = TRUE, iqr="none"); T2b
##                  No                Yes               Total             Test stat.               P value
## Total            403               80                483                                               
##                                                                                                        
## hb                                                                     t-test  (481 df) = 1.22  0.223  
##   Mean (SD)      7.6 (3.7)         7.0 (2.7)         7.5 (3.5)                                         
## hct                                                                    t-test  (481 df) = 1.2   0.231  
##   Mean (SD)      22.7 (7.1)        21.6 (7.9)        22.5 (7.3)                                        
## plt                                                                    t-test  (481 df) = 4.04  < 0.001
##   Mean (SD)      82901.0 (98461.0) 37975.0 (29874.8) 75459.8 (92256.7)                                 
## blast                                                                  t-test  (480 df) = 13.23 < 0.001
##   Mean (SD)      42.2 (32.2)       90.4 (11.5)       50.2 (34.8)                                       
## bun                                                                    t-test  (481 df) = 0.06  0.952  
##   Mean (SD)      12.7 (7.8)        12.6 (7.7)        12.6 (7.7)                                        
## creatinine                                                             t-test  (481 df) = 0.66  0.512  
##   Mean (SD)      0.5 (0.4)         0.6 (0.3)         0.5 (0.4)                                         
## calcium                                                                t-test  (481 df) = 2.38  0.018  
##   Mean (SD)      9.6 (1.1)         9.3 (0.9)         9.6 (1.1)                                         
## phosphorus                                                             t-test  (481 df) = 5.12  < 0.001
##   Mean (SD)      5.0 (1.3)         4.1 (1.5)         4.8 (1.3)                                         
## uric                                                                   t-test  (481 df) = 3.91  < 0.001
##   Mean (SD)      5.9 (3.3)         7.5 (4.1)         6.1 (3.5)                                         
## SGOT                                                                   t-test  (481 df) = 3.17  0.002  
##   Mean (SD)      50.5 (69.8)       78.3 (82.2)       55.1 (72.6)                                       
## SGPT                                                                   t-test  (481 df) = 0.48  0.631  
##   Mean (SD)      32.2 (61.0)       35.6 (31.9)       32.8 (57.2)                                       
## ALP                                                                    t-test  (481 df) = 3.87  < 0.001
##   Mean (SD)      167.7 (112.8)     221.1 (112.7)     176.6 (114.4)                                     
## LDH                                                                    t-test  (478 df) = 4.03  < 0.001
##   Mean (SD)      2350.3 (4820.7)   4971.5 (7105.8)   2776.3 (5339.4)                                   
## Mediastinal_mass                                                       Fisher's exact test      0.037  
##    Yes           15 (3.7)          8 (10.0)          23 (4.8)                                          
##    No            388 (96.3)        72 (90.0)         460 (95.2)                                        
## Fever                                                                  Chisq. (1 df) = 0.28     0.599  
##    Yes           298 (73.9)        62 (77.5)         360 (74.5)                                        
##    No            105 (26.1)        18 (22.5)         123 (25.5)                                        
## Arthitis                                                               Chisq. (1 df) = 6.74     0.009  
##    Yes           46 (11.4)         1 (1.2)           47 (9.7)                                          
##    No            357 (88.6)        79 (98.8)         436 (90.3)                                        
## Xray                                                                   Chisq. (1 df) = 6.06     0.014  
##    Yes           43 (10.7)         1 (1.2)           44 (9.1)                                          
##    No            360 (89.3)        79 (98.8)         439 (90.9)                                        
## hepato                                                                 Chisq. (1 df) = 6.25     0.012  
##    Yes           368 (91.3)        80 (100.0)        448 (92.8)                                        
##    No            35 (8.7)          0 (0.0)           35 (7.2)                                          
## Splenomegaly                                                           Chisq. (1 df) = 19       < 0.001
##    Yes           260 (64.5)        72 (90.0)         332 (68.7)                                        
##    No            143 (35.5)        8 (10.0)          151 (31.3)                                        
## lymphodenopathy                                                        Chisq. (1 df) = 2.46     0.117  
##    Yes           319 (79.2)        70 (87.5)         389 (80.5)                                        
##    No            84 (20.8)         10 (12.5)         94 (19.5)                                         
## Exopthalmos                                                            Fisher's exact test      0.704  
##    Yes           12 (3.0)          1 (1.2)           13 (2.7)                                          
##    No            391 (97.0)        79 (98.8)         470 (97.3)                                        
## Mediastinal_mass                                                       Fisher's exact test      0.037  
##    Yes           15 (3.7)          8 (10.0)          23 (4.8)                                          
##    No            388 (96.3)        72 (90.0)         460 (95.2)                                        
## Fever                                                                  Chisq. (1 df) = 0.28     0.599  
##    Yes           298 (73.9)        62 (77.5)         360 (74.5)                                        
##    No            105 (26.1)        18 (22.5)         123 (25.5)                                        
## Arthitis                                                               Chisq. (1 df) = 6.74     0.009  
##    Yes           46 (11.4)         1 (1.2)           47 (9.7)                                          
##    No            357 (88.6)        79 (98.8)         436 (90.3)                                        
## Xray                                                                   Chisq. (1 df) = 6.06     0.014  
##    Yes           43 (10.7)         1 (1.2)           44 (9.1)                                          
##    No            360 (89.3)        79 (98.8)         439 (90.9)                                        
## hepato                                                                 Chisq. (1 df) = 6.25     0.012  
##    Yes           368 (91.3)        80 (100.0)        448 (92.8)                                        
##    No            35 (8.7)          0 (0.0)           35 (7.2)                                          
## Splenomegaly                                                           Chisq. (1 df) = 19       < 0.001
##    Yes           260 (64.5)        72 (90.0)         332 (68.7)                                        
##    No            143 (35.5)        8 (10.0)          151 (31.3)                                        
## lymphodenopathy                                                        Chisq. (1 df) = 2.46     0.117  
##    Yes           319 (79.2)        70 (87.5)         389 (80.5)                                        
##    No            84 (20.8)         10 (12.5)         94 (19.5)                                         
## Exopthalmos                                                            Fisher's exact test      0.704  
##    Yes           12 (3.0)          1 (1.2)           13 (2.7)                                          
##    No            391 (97.0)        79 (98.8)         470 (97.3)
write.csv(T2b, file="Table 2b.csv")

T2c <- tableStack(vars=c(early_com,
                         TLS,Seizure, ICH, Ischemic_stroke, IICP, `Respiratory._status`, 
                              Pneumonia, Pleural_effusion, ARDS, Pul_leuko, `Pul_.hem`, DIC, 
                              Renal_insuf, RRT, Infect, Sepsis, Intubation, ICU),
                       by=hlk, total.column = TRUE); T2c
##                     No          Yes        Total       Test stat.            P value
## Total               403         80         483                                      
##                                                                                     
## early_com                                              Chisq. (1 df) = 2.93  0.087  
##    FALSE            69 (17.1)   7 (8.8)    76 (15.7)                                
##    TRUE             334 (82.9)  73 (91.2)  407 (84.3)                               
## TLS                                                    Chisq. (1 df) = 19.83 < 0.001
##    1. Yes           43 (11.0)   24 (31.2)  67 (14.3)                                
##    2. No            349 (89.0)  53 (68.8)  402 (85.7)                               
## Seizure                                                Fisher's exact test   0.003  
##    1. Yes           12 (3.1)    9 (12.0)   21 (4.6)                                 
##    2. No            374 (96.9)  66 (88.0)  440 (95.4)                               
## ICH                                                    Fisher's exact test   0.056  
##    1. Yes           3 (0.8)     3 (4.0)    6 (1.3)                                  
##    2. No            386 (99.2)  72 (96.0)  458 (98.7)                               
## Ischemic_stroke                                        Fisher's exact test   1      
##    1. Yes           2 (0.5)     0 (0.0)    2 (0.4)                                  
##    2. No            386 (99.5)  75 (100.0) 461 (99.6)                               
## IICP                                                   Fisher's exact test   0.041  
##    1. Yes           5 (1.3)     4 (5.4)    9 (1.9)                                  
##    2. No            383 (98.7)  70 (94.6)  453 (98.1)                               
## Respiratory._status                                    Chisq. (1 df) = 8.27  0.004  
##    1. Yes           32 (8.2)    15 (20.0)  47 (10.2)                                
##    2. No            356 (91.8)  60 (80.0)  416 (89.8)                               
## Pneumonia                                              Chisq. (1 df) = 1.58  0.209  
##    1. Yes           27 (7.0)    9 (12.0)   36 (7.8)                                 
##    2. No            361 (93.0)  66 (88.0)  427 (92.2)                               
## Pleural_effusion                                       Fisher's exact test   0.054  
##    1. Yes           12 (3.1)    6 (8.0)    18 (3.9)                                 
##    2. No            377 (96.9)  69 (92.0)  446 (96.1)                               
## ARDS                                                   Fisher's exact test   0.21   
##    1. Yes           7 (1.8)     3 (4.0)    10 (2.2)                                 
##    2. No            381 (98.2)  72 (96.0)  453 (97.8)                               
## Pul_leuko                                              Chisq. (1 df) = 211.6 < 0.001
##    2. No            388 (100.0) 75 (100.0) 463 (100.0)                              
## Pul_.hem                                               Fisher's exact test   0.189  
##    1. Yes           3 (0.8)     2 (2.7)    5 (1.1)                                  
##    2. No            382 (99.2)  73 (97.3)  455 (98.9)                               
## DIC                                                    Chisq. (2 df) = 18.02 < 0.001
##    70.1 Yes         44 (11.3)   21 (28.0)  65 (14.0)                                
##    70.2 No          173 (44.5)  35 (46.7)  208 (44.8)                               
##    70.3 No data     172 (44.2)  19 (25.3)  191 (41.2)                               
## Renal_insuf                                            Fisher's exact test   0.025  
##    1. Yes           18 (4.6)    9 (12.2)   27 (5.8)                                 
##    2. No            371 (95.4)  65 (87.8)  436 (94.2)                               
## RRT                                                    Fisher's exact test   0.056  
##    1. Yes           3 (0.8)     3 (4.0)    6 (1.3)                                  
##    2. No            386 (99.2)  72 (96.0)  458 (98.7)                               
## Infect                                                 Chisq. (1 df) = 4.96  0.026  
##    1. Yes           327 (84.1)  71 (94.7)  398 (85.8)                               
##    2. No            62 (15.9)   4 (5.3)    66 (14.2)                                
## Sepsis                                                 Chisq. (1 df) = 6.83  0.009  
##    1. Yes           80 (21.1)   26 (36.1)  106 (23.5)                               
##    2. No            300 (78.9)  46 (63.9)  346 (76.5)                               
## Intubation                                             Chisq. (1 df) = 11.45 < 0.001
##    1. Yes           24 (6.2)    14 (18.7)  38 (8.2)                                 
##    2. No            365 (93.8)  61 (81.3)  426 (91.8)                               
## ICU                                                    Chisq. (1 df) = 29.78 < 0.001
##    1. Yes           28 (7.2)    22 (29.3)  50 (10.8)                                
##    2. No            361 (92.8)  53 (70.7)  414 (89.2)
write.csv(T2c, file="Table 2c.csv")


# Table 3 (ALL)
T3 <- tableStack(vars=c(A_Remiss, A_Relapsed, rel_BM, rel_CNS, rel_Testis, A_type_relapsed, Status, outcome),
                 by=hlk, total.column = TRUE); T3
## A_type_relapsed has zero count in at least one row
##                                        No         Yes       Total      Test stat.           P value
## Total                                  403        80        483                                    
##                                                                                                    
## A_Remiss                                                               Fisher's exact test  0.011  
##    1. Yes                              365 (90.8) 63 (79.7) 428 (89.0)                             
##    2. No                               8 (2.0)    6 (7.6)   14 (2.9)                               
##    3. Not access                       16 (4.0)   4 (5.1)   20 (4.2)                               
##    4. Death before complete induction  13 (3.2)   6 (7.6)   19 (4.0)                               
## A_Relapsed                                                             Chisq. (1 df) = 1.06 0.304  
##    1. Yes                              131 (32.5) 31 (39.2) 162 (33.6)                             
##    2. No                               272 (67.5) 48 (60.8) 320 (66.4)                             
## rel_BM                                                                 Chisq. (1 df) = 0.82 0.366  
##    Yes                                 108 (26.8) 26 (32.5) 134 (27.7)                             
##    No                                  295 (73.2) 54 (67.5) 349 (72.3)                             
## rel_CNS                                                                Chisq. (1 df) = 0.19 0.659  
##    Yes                                 36 (8.9)   9 (11.2)  45 (9.3)                               
##    No                                  367 (91.1) 71 (88.8) 438 (90.7)                             
## rel_Testis                                                             Fisher's exact test  0.339  
##    Yes                                 18 (4.5)   1 (1.2)   19 (3.9)                               
##    No                                  385 (95.5) 79 (98.8) 464 (96.1)                             
## A_type_relapsed                                                        Fisher's exact test  0.164  
##    1. T-cell                           7 (8.0)    3 (11.5)  10 (8.8)                               
##    2. B-cell                           79 (90.8)  21 (80.8) 100 (88.5)                             
##    3. Myeloid series                   1 (1.1)    2 (7.7)   3 (2.7)                                
##    4. N/A                              0 (0.0)    0 (0.0)   0 (0.0)                                
## Status                                                                 Fisher's exact test  < 0.001
##    1. Alive on chemotherapy            3 (0.7)    0 (0.0)   3 (0.6)                                
##    2. Alive off chemotherapy           246 (61.0) 26 (32.5) 272 (56.3)                             
##    3. Alive with alternative treatment 0 (0.0)    1 (1.2)   1 (0.2)                                
##    4. Death                            154 (38.2) 53 (66.2) 207 (42.9)                             
## outcome                                                                Chisq. (1 df) = 20.3 < 0.001
##    Alive                               249 (61.8) 27 (33.8) 276 (57.1)                             
##    Dead                                154 (38.2) 53 (66.2) 207 (42.9)
write.csv(T3, file="Table 3.csv")


#

use(data.frame(data.AML))

T4a <- tableStack(vars=c(Age,agegp, sex, BW, HT, bmi.gp),
                       by=hlk, total.column = TRUE); T4a
##                No               Yes                Total              Test stat.           P value
## Total          165              42                 207                                            
##                                                                                                   
## Age                                                                   Ranksum test         0.021  
##   Median (IQR) 58.0 (25,116.0)  137.0 (34,150.5)   65.0 (25.5,133.5)                              
## agegp                                                                 Chisq. (3 df) = 7.77 0.051  
##    0-23        34 (20.6)        9 (21.4)           43 (20.8)                                      
##    24-49       39 (23.6)        4 (9.5)            43 (20.8)                                      
##    48-91       28 (17.0)        4 (9.5)            32 (15.5)                                      
##    92+         64 (38.8)        25 (59.5)          89 (43.0)                                      
## sex                                                                   Chisq. (1 df) = 1.23 0.268  
##    Female      69 (41.8)        13 (31.0)          82 (39.6)                                      
##    Male        96 (58.2)        29 (69.0)          125 (60.4)                                     
## BW                                                                    Ranksum test         0.025  
##   Median (IQR) 15.8 (10.5,25.5) 27.2 (11,41.9)     16.9 (10.6,28.6)                               
## HT                                                                    Ranksum test         0.011  
##   Median (IQR) 103.0 (83,131.0) 138.0 (88.8,153.0) 109.0 (83.2,137.2)                             
## bmi.gp                                                                Fisher's exact test  0.921  
##    Underweight 51 (30.9)        15 (35.7)          66 (31.9)                                      
##    Normal      91 (55.2)        21 (50.0)          112 (54.1)                                     
##    Overweight  15 (9.1)         4 (9.5)            19 (9.2)                                       
##    Obese       8 (4.8)          2 (4.8)            10 (4.8)
write.csv(T4a, file="Table 4a.csv")

T4b <- tableStack(vars=c(hb:blast,
                              bun:LDH,
                              Mediastinal_mass:Arthitis,
                              Xray, hepato, Splenomegaly, lymphodenopathy, Exopthalmos,
                              Mediastinal_mass:Arthitis,
                              Xray, hepato, Splenomegaly, lymphodenopathy, Exopthalmos),
                       by=hlk, total.column = TRUE, iqr="none"); T4b
##                  No                 Yes               Total             Test stat.              P value
## Total            165                42                207                                              
##                                                                                                        
## hb                                                                      t-test  (205 df) = 0.55 0.585  
##   Mean (SD)      7.3 (2.3)          7.1 (1.6)         7.3 (2.2)                                        
## hct                                                                     t-test  (205 df) = 0.37 0.713  
##   Mean (SD)      22.4 (6.8)         22.0 (4.9)        22.3 (6.4)                                       
## plt                                                                     t-test  (205 df) = 1.1  0.272  
##   Mean (SD)      67724.2 (102520.2) 50000.0 (35262.8) 64128.0 (93091.5)                                
## blast                                                                   t-test  (205 df) = 9.2  < 0.001
##   Mean (SD)      33.9 (30.3)        80.5 (24.7)       43.4 (34.7)                                      
## bun                                                                     t-test  (205 df) = 2.14 0.033  
##   Mean (SD)      11.6 (5.1)         9.7 (5.0)         11.2 (5.1)                                       
## creatinine                                                              t-test  (205 df) = 1.71 0.089  
##   Mean (SD)      0.5 (0.2)          0.6 (0.3)         0.5 (0.2)                                        
## calcium                                                                 t-test  (205 df) = 2.33 0.021  
##   Mean (SD)      9.3 (0.7)          9.0 (0.9)         9.3 (0.7)                                        
## phosphorus                                                              t-test  (205 df) = 3.75 < 0.001
##   Mean (SD)      4.8 (1.0)          4.1 (1.4)         4.6 (1.2)                                        
## uric                                                                    t-test  (205 df) = 3.55 < 0.001
##   Mean (SD)      4.8 (1.9)          6.0 (2.6)         5.0 (2.1)                                        
## SGOT                                                                    t-test  (205 df) = 0.38 0.702  
##   Mean (SD)      43.6 (85.0)        38.4 (31.7)       42.5 (77.2)                                      
## SGPT                                                                    t-test  (205 df) = 1.04 0.302  
##   Mean (SD)      29.0 (66.0)        18.4 (13.2)       26.9 (59.3)                                      
## ALP                                                                     t-test  (205 df) = 1.6  0.112  
##   Mean (SD)      140.6 (63.8)       164.0 (139.1)     145.4 (84.7)                                     
## LDH                                                                     t-test  (202 df) = 1.91 0.058  
##   Mean (SD)      1888.6 (2135.3)    2619.7 (2425.5)   2035.6 (2210.0)                                  
## Mediastinal_mass                                                        Fisher's exact test     1      
##    Yes           1 (0.6)            0 (0.0)           1 (0.5)                                          
##    No            164 (99.4)         42 (100.0)        206 (99.5)                                       
## Fever                                                                   Chisq. (1 df) = 7.2     0.007  
##    Yes           118 (71.5)         39 (92.9)         157 (75.8)                                       
##    No            47 (28.5)          3 (7.1)           50 (24.2)                                        
## Arthitis                                                                Fisher's exact test     1      
##    Yes           6 (3.6)            1 (2.4)           7 (3.4)                                          
##    No            159 (96.4)         41 (97.6)         200 (96.6)                                       
## Xray                                                                    Fisher's exact test     1      
##    Yes           5 (3.0)            1 (2.4)           6 (2.9)                                          
##    No            160 (97.0)         41 (97.6)         201 (97.1)                                       
## hepato                                                                  Chisq. (1 df) = 1.69    0.194  
##    Yes           128 (77.6)         37 (88.1)         165 (79.7)                                       
##    No            37 (22.4)          5 (11.9)          42 (20.3)                                        
## Splenomegaly                                                            Chisq. (1 df) = 4.94    0.026  
##    Yes           88 (53.3)          31 (73.8)         119 (57.5)                                       
##    No            77 (46.7)          11 (26.2)         88 (42.5)                                        
## lymphodenopathy                                                         Chisq. (1 df) = 4.49    0.034  
##    Yes           107 (64.8)         35 (83.3)         142 (68.6)                                       
##    No            58 (35.2)          7 (16.7)          65 (31.4)                                        
## Exopthalmos                                                             Fisher's exact test     0.314  
##    Yes           14 (8.5)           1 (2.4)           15 (7.2)                                         
##    No            151 (91.5)         41 (97.6)         192 (92.8)                                       
## Mediastinal_mass                                                        Fisher's exact test     1      
##    Yes           1 (0.6)            0 (0.0)           1 (0.5)                                          
##    No            164 (99.4)         42 (100.0)        206 (99.5)                                       
## Fever                                                                   Chisq. (1 df) = 7.2     0.007  
##    Yes           118 (71.5)         39 (92.9)         157 (75.8)                                       
##    No            47 (28.5)          3 (7.1)           50 (24.2)                                        
## Arthitis                                                                Fisher's exact test     1      
##    Yes           6 (3.6)            1 (2.4)           7 (3.4)                                          
##    No            159 (96.4)         41 (97.6)         200 (96.6)                                       
## Xray                                                                    Fisher's exact test     1      
##    Yes           5 (3.0)            1 (2.4)           6 (2.9)                                          
##    No            160 (97.0)         41 (97.6)         201 (97.1)                                       
## hepato                                                                  Chisq. (1 df) = 1.69    0.194  
##    Yes           128 (77.6)         37 (88.1)         165 (79.7)                                       
##    No            37 (22.4)          5 (11.9)          42 (20.3)                                        
## Splenomegaly                                                            Chisq. (1 df) = 4.94    0.026  
##    Yes           88 (53.3)          31 (73.8)         119 (57.5)                                       
##    No            77 (46.7)          11 (26.2)         88 (42.5)                                        
## lymphodenopathy                                                         Chisq. (1 df) = 4.49    0.034  
##    Yes           107 (64.8)         35 (83.3)         142 (68.6)                                       
##    No            58 (35.2)          7 (16.7)          65 (31.4)                                        
## Exopthalmos                                                             Fisher's exact test     0.314  
##    Yes           14 (8.5)           1 (2.4)           15 (7.2)                                         
##    No            151 (91.5)         41 (97.6)         192 (92.8)
write.csv(T4b, file="Table 4b.csv")

T4c <- tableStack(vars=c(early_com,
                         TLS,Seizure, ICH, Ischemic_stroke, IICP, `Respiratory._status`, 
                              Pneumonia, Pleural_effusion, ARDS, Pul_leuko, `Pul_.hem`, DIC, 
                              Renal_insuf, RRT, Infect, Sepsis, Intubation, ICU),
                       by=hlk, total.column = TRUE); T4c
##                     No          Yes        Total       Test stat.            P value
## Total               165         42         207                                      
##                                                                                     
## early_com                                              Fisher's exact test   0.565  
##    FALSE            15 (9.1)    5 (11.9)   20 (9.7)                                 
##    TRUE             150 (90.9)  37 (88.1)  187 (90.3)                               
## TLS                                                    Fisher's exact test   0.236  
##    1. Yes           7 (4.4)     4 (10.0)   11 (5.5)                                 
##    2. No            152 (95.6)  36 (90.0)  188 (94.5)                               
## Seizure                                                Fisher's exact test   1      
##    1. Yes           7 (4.3)     1 (2.6)    8 (4.0)                                  
##    2. No            155 (95.7)  38 (97.4)  193 (96.0)                               
## ICH                                                    Fisher's exact test   0.037  
##    1. Yes           0 (0.0)     2 (5.1)    2 (1.0)                                  
##    2. No            161 (100.0) 37 (94.9)  198 (99.0)                               
## Ischemic_stroke                                        Fisher's exact test   1      
##    1. Yes           2 (1.2)     0 (0.0)    2 (1.0)                                  
##    2. No            160 (98.8)  39 (100.0) 199 (99.0)                               
## IICP                                                   Fisher's exact test   1      
##    1. Yes           4 (2.5)     1 (2.6)    5 (2.5)                                  
##    2. No            158 (97.5)  38 (97.4)  196 (97.5)                               
## Respiratory._status                                    Chisq. (1 df) = 0.52  0.469  
##    1. Yes           36 (22.2)   6 (15.4)   42 (20.9)                                
##    2. No            126 (77.8)  33 (84.6)  159 (79.1)                               
## Pneumonia                                              Chisq. (1 df) = 0.01  0.907  
##    1. Yes           33 (20.4)   7 (17.9)   40 (19.9)                                
##    2. No            129 (79.6)  32 (82.1)  161 (80.1)                               
## Pleural_effusion                                       Fisher's exact test   1      
##    1. Yes           5 (3.1)     1 (2.6)    6 (3.0)                                  
##    2. No            157 (96.9)  38 (97.4)  195 (97.0)                               
## ARDS                                                   Fisher's exact test   1      
##    1. Yes           9 (5.6)     2 (5.1)    11 (5.5)                                 
##    2. No            153 (94.4)  37 (94.9)  190 (94.5)                               
## Pul_leuko                                              Chisq. (1 df) = 73.57 < 0.001
##    2. No            160 (100.0) 39 (100.0) 199 (100.0)                              
## Pul_.hem                                               Fisher's exact test   1      
##    1. Yes           6 (3.7)     1 (2.6)    7 (3.5)                                  
##    2. No            156 (96.3)  38 (97.4)  194 (96.5)                               
## DIC                                                    Chisq. (2 df) = 3.04  0.219  
##    70.1 Yes         36 (22.4)   13 (33.3)  49 (24.5)                                
##    70.2 No          58 (36.0)   15 (38.5)  73 (36.5)                                
##    70.3 No data     67 (41.6)   11 (28.2)  78 (39.0)                                
## Renal_insuf                                            Fisher's exact test   1      
##    1. Yes           12 (7.4)    2 (5.1)    14 (7.0)                                 
##    2. No            150 (92.6)  37 (94.9)  187 (93.0)                               
## RRT                                                    Fisher's exact test   1      
##    1. Yes           1 (0.6)     0 (0.0)    1 (0.5)                                  
##    2. No            161 (99.4)  39 (100.0) 200 (99.5)                               
## Infect                                                 Fisher's exact test   1      
##    1. Yes           150 (92.6)  37 (94.9)  187 (93.0)                               
##    2. No            12 (7.4)    2 (5.1)    14 (7.0)                                 
## Sepsis                                                 Chisq. (1 df) = 0     1      
##    1. Yes           48 (30.2)   11 (28.9)  59 (29.9)                                
##    2. No            111 (69.8)  27 (71.1)  138 (70.1)                               
## Intubation                                             Chisq. (1 df) = 0     1      
##    1. Yes           33 (20.5)   8 (20.5)   41 (20.5)                                
##    2. No            128 (79.5)  31 (79.5)  159 (79.5)                               
## ICU                                                    Chisq. (1 df) = 0.45  0.504  
##    1. Yes           35 (21.6)   11 (28.2)  46 (22.9)                                
##    2. No            127 (78.4)  28 (71.8)  155 (77.1)
write.csv(T4c, file="Table 4c.csv")


T5 <- tableStack(vars=c(A_Remiss, A_Relapsed, rel_BM, rel_CNS, rel_Testis, A_type_relapsed, Status, outcome),
                     by=hlk, total.column = TRUE); T5
## A_type_relapsed has zero count in at least one row
## Status has zero count in at least one row
##                                        No         Yes        Total      Test stat.           P value
## Total                                  165        42         207                                    
##                                                                                                     
## A_Remiss                                                                Chisq. (3 df) = 3.76 0.289  
##    1. Yes                              90 (54.5)  19 (45.2)  109 (52.7)                             
##    2. No                               40 (24.2)  13 (31.0)  53 (25.6)                              
##    3. Not access                       29 (17.6)  6 (14.3)   35 (16.9)                              
##    4. Death before complete induction  6 (3.6)    4 (9.5)    10 (4.8)                               
## A_Relapsed                                                              Chisq. (1 df) = 0.01 0.915  
##    1. Yes                              51 (30.9)  12 (28.6)  63 (30.4)                              
##    2. No                               114 (69.1) 30 (71.4)  144 (69.6)                             
## rel_BM                                                                  Chisq. (1 df) = 0.03 0.857  
##    Yes                                 48 (29.1)  11 (26.2)  59 (28.5)                              
##    No                                  117 (70.9) 31 (73.8)  148 (71.5)                             
## rel_CNS                                                                 Fisher's exact test  0.604  
##    Yes                                 4 (2.4)    2 (4.8)    6 (2.9)                                
##    No                                  161 (97.6) 40 (95.2)  201 (97.1)                             
## rel_Testis                                                              Fisher's exact test  1      
##    Yes                                 1 (0.6)    0 (0.0)    1 (0.5)                                
##    No                                  164 (99.4) 42 (100.0) 206 (99.5)                             
## A_type_relapsed                                                         Fisher's exact test  1      
##    1. T-cell                           0 (0.0)    0 (0.0)    0 (0.0)                                
##    2. B-cell                           0 (0.0)    0 (0.0)    0 (0.0)                                
##    3. Myeloid series                   46 (100.0) 12 (100.0) 58 (100.0)                             
##    4. N/A                              0 (0.0)    0 (0.0)    0 (0.0)                                
## Status                                                                  Fisher's exact test  0.68   
##    1. Alive on chemotherapy            0 (0.0)    0 (0.0)    0 (0.0)                                
##    2. Alive off chemotherapy           39 (23.6)  8 (19.0)   47 (22.7)                              
##    3. Alive with alternative treatment 0 (0.0)    0 (0.0)    0 (0.0)                                
##    4. Death                            126 (76.4) 34 (81.0)  160 (77.3)                             
## outcome                                                                 Chisq. (1 df) = 0.18 0.669  
##    Alive                               39 (23.6)  8 (19.0)   47 (22.7)                              
##    Dead                                126 (76.4) 34 (81.0)  160 (77.3)
write.csv(T5, file="Table 5.csv")


#

#

detachAllData()

#
# Overall survival
#
count(data, outcome)
## # A tibble: 2 × 2
##   outcome     n
##   <fct>   <int>
## 1 Alive     323
## 2 Dead      367
os.fit <- survfit(Surv(stime, outcome=="Dead")~1, data=data)
os.fit
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ 1, data = data)
## 
##        n events median 0.95LCL 0.95UCL
## [1,] 690    367   6.72     4.3    13.7
# The median survival time for the whole group was 4.32 years (3.31 - 9.21).
summary(os.fit, times=c(5,10))
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ 1, data = data)
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5    326     328    0.523  0.0191        0.486        0.561
##    10    241      26    0.477  0.0194        0.441        0.517
# The 5- and 10-year survival rates were 48.9% and 44.9%, respectively.

ggsurvplot(os.fit, data=data,
           legend="none",
           xlab="Time (years)",
           xlim=c(0,20))

# Late deaths (1 case remains. Death 22.8 years after diagnosis)
filter(data, stime>20 & outcome=="Dead") %>%
  select(HN, A_dx, rdate, sdate, stime, death_cause)
## # A tibble: 2 × 6
##   HN     A_dx       rdate      sdate      stime death_cause      
##   <fct>  <date>     <date>     <date>     <dbl> <fct>            
## 1 839791 1998-05-06 2003-12-15 2021-07-28  23.2 1. Cancer related
## 2 865930 1998-10-15 1999-09-29 2021-07-27  22.8 1. Cancer related
# Stratified by hlk
os.fit.hlk <- survfit(Surv(stime, outcome=="Dead")~hlk, data=data)
os.fit.hlk
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ hlk, data = data)
## 
##           n events median 0.95LCL 0.95UCL
## hlk=No  568    280  13.58    6.89      NA
## hlk=Yes 122     87   1.59    1.25    2.17
summary(os.fit.hlk, times=c(5,10))
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ hlk, data = data)
## 
##                 hlk=No 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5    293     244    0.568  0.0209        0.529        0.611
##    10    217      23    0.520  0.0214        0.479        0.563
## 
##                 hlk=Yes 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     33      84    0.309  0.0421        0.236        0.403
##    10     24       3    0.280  0.0413        0.210        0.374
# Survival curves
ggsurvplot(os.fit.hlk, data=data,
           legend.title="Hyperleukocytosis",
           legend.labs=c('No','Yes'),
           xlab="Time (years)",
           xlim=c(0,20),
           pval=TRUE)

#
# Event-free survival
#

efs.fit <- survfit(Surv(etime, event)~1, data=data)
efs.fit
## Call: survfit(formula = Surv(etime, event) ~ 1, data = data)
## 
##        n events median 0.95LCL 0.95UCL
## [1,] 690    396   3.62    2.84    5.11
summary(efs.fit, times=c(5,10))
## Call: survfit(formula = Surv(etime, event) ~ 1, data = data)
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5    289     370    0.462  0.0191        0.426        0.501
##    10    215      17    0.432  0.0191        0.396        0.472
ggsurvplot(efs.fit, data=data,
           legend="none",
           xlab="Time (years)",
           xlim=c(0,20))

# Stratify by hlk
efs.fit.hlk <- survfit(Surv(etime, event)~hlk, data=data)
efs.fit.hlk
## Call: survfit(formula = Surv(etime, event) ~ hlk, data = data)
## 
##           n events median 0.95LCL 0.95UCL
## hlk=No  568    307   4.98   3.696   11.25
## hlk=Yes 122     89   1.26   0.723    1.81
# Rates
summary(efs.fit.hlk, times=c(5,10))
## Call: survfit(formula = Surv(etime, event) ~ hlk, data = data)
## 
##                 hlk=No 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5    257     283    0.499  0.0211        0.460        0.543
##    10    191      15    0.468  0.0213        0.428        0.511
## 
##                 hlk=Yes 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     32      87    0.286  0.0410        0.216        0.379
##    10     24       2    0.268  0.0403        0.200        0.360
ggsurvplot(efs.fit.hlk, data=data,
           legend.title="Hyperleukocytefsis",
           legend.labs=c('No','Yes'),
           xlab="Time (years)",
           xlim=c(0,20),
           pval=TRUE)

# Change reference group for clinical presentation variables
data <- mutate(data, 
               Mediastinal_mass=relevel(Mediastinal_mass, ref="No"), 
               Fever=relevel(Fever, ref="No"),
               Arthitis=relevel(Arthitis, ref="No"), 
               Xray=relevel(Xray, ref="No"),
               hepato=relevel(hepato, ref="No"), 
               Splenomegaly=relevel(Splenomegaly, ref="No"), 
               lymphodenopathy=relevel(lymphodenopathy, ref="No"),
               Exopthalmos=relevel(Exopthalmos, ref="No"))

# Compare outcomes between HLK within ALL and AML subgroups
data.ALL <- filter(data, Mor=="ALL")
data.AML <- filter(data, Mor=="AML")


#
# Overall survival
#

# OS among ALL subgroup
os.fit.all.hlk <- survfit(Surv(stime, outcome=="Dead")~hlk, data=data.ALL)
os.fit.all.hlk
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ hlk, data = data.ALL)
## 
##           n events median 0.95LCL 0.95UCL
## hlk=No  403    154  23.23   22.78      NA
## hlk=Yes  80     53   1.84    1.51    4.28
# Rates
summary(os.fit.all.hlk, times=c(5,10))
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ hlk, data = data.ALL)
## 
##                 hlk=No 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5    249     129    0.678  0.0234        0.633        0.725
##    10    184      19    0.621  0.0248        0.574        0.672
## 
##                 hlk=Yes 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     26      50    0.372  0.0544        0.279        0.495
##    10     18       3    0.327  0.0536        0.237        0.451
ggsurvplot(os.fit.all.hlk, data=data.ALL,
           legend.title="Hyperleukocytefsis",
           legend.labs=c('No','Yes'),
           xlab="Time (years)",
           xlim=c(0,20),
           pval=TRUE)

ggsave(file="Figures/HLK_Figure_ALL_OS.png", width=6, height=6)


# OS among AML subgroup
os.fit.aml.hlk <- survfit(Surv(stime, outcome=="Dead")~hlk, data=data.AML)
os.fit.aml.hlk
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ hlk, data = data.AML)
## 
##           n events median 0.95LCL 0.95UCL
## hlk=No  165    126   1.11   0.868    1.76
## hlk=Yes  42     34   0.83   0.331    2.03
# Rates
summary(os.fit.aml.hlk, times=c(5,10))
## Call: survfit(formula = Surv(stime, outcome == "Dead") ~ hlk, data = data.AML)
## 
##                 hlk=No 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     44     115    0.302  0.0358        0.239        0.381
##    10     33       4    0.272  0.0353        0.211        0.351
## 
##                 hlk=Yes 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5      7      34     0.19  0.0606        0.102        0.355
##    10      6       0     0.19  0.0606        0.102        0.355
ggsurvplot(os.fit.aml.hlk, data=data.AML,
           legend.title="Hyperleukocytefsis",
           legend.labs=c('No','Yes'),
           xlab="Time (years)",
           xlim=c(0,20),
           pval=TRUE,
           pval.coord=c(0,0.1))

ggsave(file="Figures/HLK_Figure_AML_OS.png", width=6, height=6)


#
# Event-free survival
#

# EFS among ALL subgroup
efs.fit.all.hlk <- survfit(Surv(etime, event)~hlk, data=data.ALL)
efs.fit.all.hlk
## Call: survfit(formula = Surv(etime, event) ~ hlk, data = data.ALL)
## 
##           n events median 0.95LCL 0.95UCL
## hlk=No  403    180     NA   12.75      NA
## hlk=Yes  80     55   1.53    1.24    2.97
# Rates
summary(efs.fit.all.hlk, times=c(5,10))
## Call: survfit(formula = Surv(etime, event) ~ hlk, data = data.ALL)
## 
##                 hlk=No 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5    217     164    0.591  0.0246        0.545        0.641
##    10    160      13    0.552  0.0252        0.505        0.604
## 
##                 hlk=Yes 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     25      53    0.337  0.0529        0.247        0.458
##    10     18       2    0.310  0.0520        0.223        0.430
ggsurvplot(efs.fit.all.hlk, data=data.ALL,
           legend.title="Hyperleukocytefsis",
           legend.labs=c('No','Yes'),
           xlab="Time (years)",
           xlim=c(0,20),
           pval=TRUE)

ggsave(file="Figures/HLK_Figure_ALL_efs.png", width=6, height=6)


# efs among AML subgroup
efs.fit.aml.hlk <- survfit(Surv(etime, event)~hlk, data=data.AML)
efs.fit.aml.hlk
## Call: survfit(formula = Surv(etime, event) ~ hlk, data = data.AML)
## 
##           n events median 0.95LCL 0.95UCL
## hlk=No  165    127  0.827   0.665    1.36
## hlk=Yes  42     34  0.579   0.331    1.51
# Rates
summary(efs.fit.aml.hlk, times=c(5,10))
## Call: survfit(formula = Surv(etime, event) ~ hlk, data = data.AML)
## 
##                 hlk=No 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     40     119    0.277  0.0350        0.216        0.355
##    10     31       2    0.262  0.0347        0.202        0.339
## 
##                 hlk=Yes 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5      7      34     0.19  0.0606        0.102        0.355
##    10      6       0     0.19  0.0606        0.102        0.355
ggsurvplot(efs.fit.aml.hlk, data=data.AML,
           legend.title="Hyperleukocytefsis",
           legend.labs=c('No','Yes'),
           xlab="Time (years)",
           xlim=c(0,20),
           pval=TRUE,
           pval.coord=c(0,0.1))

ggsave(file="Figures/HLK_Figure_AML_EFS.png", width=6, height=6)





# Risk factors associated with death


#
# Univariate analysis
#
# In cox regression analysis, could we divide age group in 2 group: <= 10 years(120 month) and > 10 years?
data <- mutate(data, agegp=cut(Age, br=c(0,120,Inf), labels=c("<=10",">10")))
count(data, agegp)
## # A tibble: 2 × 2
##   agegp     n
##   <fct> <int>
## 1 <=10    555
## 2 >10     135
#
# Stratify by Morphology (ALL/AML)
#

# ALL subgroup

tbl_6a <-
  tbl_uvregression(
    data.ALL[c("hlk", "outcome", "agegp", "sex", 
           "Mediastinal_mass", "Fever", "Arthitis", "Xray",
           "hepato", "Splenomegaly", "lymphodenopathy",
           "Exopthalmos", "early_com", "stime")],
    method = coxph,
    y = Surv(stime, outcome=="Dead"),
    exponentiate = TRUE,
    pvalue_fun = function(x) style_pvalue(x, digits = 2)
  )

t6a <- bold_labels(tbl_6a) 


# Multivariate
cox1 <- coxph(Surv(stime, outcome=="Dead")~hlk+agegp+sex+
                Mediastinal_mass+Fever+Arthitis+Xray+
                hepato+Splenomegaly+lymphodenopathy+
                Exopthalmos+early_com, data=data.ALL)


t6b <- tbl_regression(cox1, expo=TRUE) %>%
  bold_labels() 

tbl_merge(
  tbls = list(t6a, t6b),
  tab_spanner = c("**Univariate**", "**Multivariate**")) %>%
  as_flex_table() %>%
  save_as_html(
    path="Table 6.html",
    encoding = "utf-8")


# AML subgroup

tbl_9a <-
  tbl_uvregression(
    data.AML[c("hlk", "outcome", "agegp", "sex", 
               "Mediastinal_mass", "Fever", "Arthitis", "Xray",
               "hepato", "Splenomegaly", "lymphodenopathy",
               "Exopthalmos", "early_com", "stime")],
    method = coxph,
    y = Surv(stime, outcome=="Dead"),
    exponentiate = TRUE,
    pvalue_fun = function(x) style_pvalue(x, digits = 2)
  )

t9a <- bold_labels(tbl_9a) 


# Multivariate
cox2 <- coxph(Surv(stime, outcome=="Dead")~hlk+agegp+sex+
                Mediastinal_mass+Fever+Arthitis+Xray+
                hepato+Splenomegaly+lymphodenopathy+
                Exopthalmos+early_com, data=data.AML)


t9b <- tbl_regression(cox2, expo=TRUE) %>%
  bold_labels() 

tbl_merge(
  tbls = list(t9a, t9b),
  tab_spanner = c("**Univariate**", "**Multivariate**")) %>%
  as_flex_table() %>%
  save_as_html(
    path="Table 9.html",
    encoding = "utf-8")


#
# Event-free survival
#

tbl_8a <-
  tbl_uvregression(
    data.ALL[c("hlk", "event", "agegp", "sex", 
               "Mediastinal_mass", "Fever", "Arthitis", "Xray",
               "hepato", "Splenomegaly", "lymphodenopathy",
               "Exopthalmos", "early_com", "etime")],
    method = coxph,
    y = Surv(etime, event),
    exponentiate = TRUE,
    pvalue_fun = function(x) style_pvalue(x, digits = 2)
  )

t8a <- bold_labels(tbl_8a) 


# Multivariate
cox1 <- coxph(Surv(etime, event)~hlk+agegp+sex+
                Mediastinal_mass+Fever+Arthitis+Xray+
                hepato+Splenomegaly+lymphodenopathy+
                Exopthalmos+early_com, data=data.ALL)


t8b <- tbl_regression(cox1, expo=TRUE) %>%
  bold_labels() 

tbl_merge(
  tbls = list(t8a, t8b),
  tab_spanner = c("**Univariate**", "**Multivariate**")) %>%
  as_flex_table() %>%
  save_as_html(
    path="Table 8.html",
    encoding = "utf-8")


# AML subgroup

tbl_9a <-
  tbl_uvregression(
    data.AML[c("hlk", "event", "agegp", "sex", 
               "Mediastinal_mass", "Fever", "Arthitis", "Xray",
               "hepato", "Splenomegaly", "lymphodenopathy",
               "Exopthalmos", "early_com", "stime")],
    method = coxph,
    y = Surv(stime, event),
    exponentiate = TRUE,
    pvalue_fun = function(x) style_pvalue(x, digits = 2)
  )

t9a <- bold_labels(tbl_9a) 


# Multivariate
cox2 <- coxph(Surv(etime, event)~hlk+agegp+sex+
                Mediastinal_mass+Fever+Arthitis+Xray+
                hepato+Splenomegaly+lymphodenopathy+
                Exopthalmos+early_com, data=data.AML)


t9b <- tbl_regression(cox2, expo=TRUE) %>%
  bold_labels() 

tbl_merge(
  tbls = list(t9a, t9b),
  tab_spanner = c("**Univariate**", "**Multivariate**")) %>%
  as_flex_table() %>%
  save_as_html(
    path="Table 9.html",
    encoding = "utf-8")