library(readxl)
library(tidyverse)
library(ggsci)
library(gridExtra)
library(scales)
library(tableone)
library(knitr)
library(kableExtra)
library(gridExtra)
library(lemon)
library(extrafont)
#font_import()
loadfonts(device = "win")

windowsFonts(OpenSans = windowsFont("Open Sans"))
fct_case_when <- function(...) {
  args <- as.list(match.call())
  levels <- sapply(args[-1], function(f) f[[3]])  # extract RHS of formula
  levels <- levels[!is.na(levels)]
  factor(dplyr::case_when(...), levels=levels)
}
ds_raw <- read_excel("../data/data_ISP.xlsx", range = "A5:BW160", col_names = F)
ds_preproc_names <- ds_raw %>%
  rename_all(~ c("id", "dob", "sex", "date_event", "date_adm", "etiology", "isp_icu", "diagnosis_t0", "crs_r_t0", "lcf_t0", "therapy_adm", "tracheo_t0", "breath_t0", "feed_t0", "cateth_t0", "bedsore_t0", "fractures_t0", "asworth_t0", "poa_t0", "craniolac_t0","hydrocef_t0", "erbi_sub_a_t0", "erbi_sub_b_t0", "psh_cfs_t0", "psh_dlt_t0", "psh_tot_t0", "nr_crisis_t0", "isp_treat_t0", "psh_cfs_t1", "psh_dlt_t1", "psh_tot_t1", "nr_crisis_t1", "isp_treat_t1", "diagnosis_t2", "crs_r_t2", "lcf_t2", "tracheo_t2", "breath_t2", "feed_t2", "cateth_t2", "bedsore_t2", "fractures_t2", "asworth_t2", "poa_t2", "craniolac_t2","hydrocef_t2", "erbi_sub_a_t2", "erbi_sub_b_t2", "psh_cfs_t2", "psh_dlt_t2", "psh_tot_t2", "nr_crisis_t2", "isp_treat_t2", "date_disch", "diagnosis_t3", "crs_r_t3", "lcf_t3", "tracheo_t3", "breath_t3", "feed_t3", "cateth_t3", "bedsore_t3", "asworth_t3", "poa_t3", "craniolac_t3","hydrocef_t3", "erbi_sub_a_t3", "erbi_sub_b_t3", "psh_cfs_t3", "psh_dlt_t3", "psh_tot_t3", "nr_crisis_t3", "isp_treat_t3", "therapy_disch", "gos")) %>%
   mutate(id = str_remove(id, " "))

ds_preproc <- ds_preproc_names  %>%
  filter(!(is.na(id))) %>%
  mutate(sex = factor(sex),
         #diagnosis = factor(recode(diagnosis, "EMERSIONE" = "Emersion", "SMC" = "MCS", "SCM" = "MCS", "SV" = "VS")),
         date_event = replace(date_event, id == "A43", as.Date("2018-11-05")),
         date_event = replace(date_event, id == "B28", as.Date("2018-12-29")),
         date_adm = as.Date(as.numeric(replace(date_adm, id == "A48", 43493)), origin = "1899-12-30"),
         date_adm = replace(date_adm, id == "A43", as.Date("2019-01-08")),
         date_disch = replace(date_disch, id == "N14", as.Date("2018-10-18")),
         date_disch = replace(date_disch, id == "O17", as.Date("2019-04-18")),
         date_disch = replace(date_disch, id == "F5", as.Date("2019-06-19")),
         date_disch = replace(date_disch, id == "M3", as.Date("2019-05-05")),
         etiology = fct_case_when(
           etiology == 1 ~ "TBI",
           etiology == 2 ~ "Vascular",
           etiology == 3 ~ "Anoxic",
           etiology == 4 ~ "Infective",
           etiology == 5 ~ "Neoplastic",
           etiology == 6 ~ "Mixed"),
         psh_tot_t3 = as.numeric(recode(psh_tot_t3, "<8" = "1"))) %>%
  mutate_at(.vars = vars(contains("diagnosis")), 
            .funs = list(~factor(recode(., "EMERSIONE" = "Emersion", "SMC" = "MCS", "SCM" = "MCS", "SV" = "VS", "DIMESSO" = "Discharged",  "DIMESSA"  = "Discharged", "DECESSO" = "Dead")))) %>%
  mutate_at(.vars = vars(contains("crs_r")), 
            .funs = list(as.numeric)) %>%
  mutate_at(.vars = vars(contains("erbi_sub_a")),
            .funs = list(~case_when(
              . > 0 ~ .*-1,
              TRUE ~ .))) %>%
  mutate_at(.vars = vars(contains("breath")),
            .funs = list(~factor(case_when(
              . == 1 ~ "Autonomous",
              . == 2 ~ "Autonomous + O2",
              . == 3 ~ "Mechanical"), levels=c("Autonomous", "Autonomous + O2", "Mechanical")))) %>%
  mutate_at(.vars = vars(contains("feed")),
            .funs = list(~fct_case_when(
           . == 4 ~ "Oral",
           . == 1 ~ "Parenteral",
           . == 2 ~ "NG-tube",
           . == 3 ~ "PEG"))) %>%
  mutate_at(.vars = vars(therapy_adm, isp_treat_t0, isp_treat_t1, isp_treat_t2, isp_treat_t3, therapy_disch), 
            .funs = list(~str_replace_all(., c("[\\.]|[;]" = ",", "O" = "0", ",$" = "", "4,5999999999999996" = "4,6", "4.5999999999999996" = "4.6")))) %>%
  mutate_at(.vars = vars(dob, date_event, date_disch), 
            .funs = list(as.Date.POSIXct)) %>%
  mutate_at(.vars = vars(contains("nr_crisis")),
            .funs = list(~fct_case_when(
              . == 0 ~ "none",
              . == 1 ~ "<3",
              . == 2 ~ "3-5",
              . == 3 ~ ">5"))) %>%
  mutate_at(.vars = vars(isp_icu, tracheo_t0, cateth_t0:hydrocef_t0, tracheo_t2,  cateth_t2:hydrocef_t2, tracheo_t3, cateth_t3:hydrocef_t3),
            .funs = list(~factor(recode(., "N0" = "no", "NO" = "no", "SI," = "yes", "SI" = "yes", "si" = "yes", "SI, NO" = "no", "BO" = "no", "NO DVP" = "no", "SI DVP" = "yes"))))
ds_therapy <- ds_preproc %>%
  select(id, therapy_adm, isp_treat_t0, isp_treat_t1, isp_treat_t2, isp_treat_t3, therapy_disch) %>%
  separate(therapy_adm, into = paste("adm", 1:5, sep = "_")) %>%
  separate(isp_treat_t0, into = paste("t0", 1:5, sep = "_")) %>%
  separate(isp_treat_t1, into = paste("t1", 1:5, sep = "_")) %>%
  separate(isp_treat_t2, into = paste("t2", 1:5, sep = "_")) %>%
  separate(isp_treat_t3, into = paste("t3", 1:5, sep = "_")) %>%
  separate(therapy_disch, into = paste("disch", 1:5, sep = "_")) %>%
  gather(treatment, n, adm_1:disch_5, na.rm = T) %>%
  mutate(n = fct_case_when(
    n == 0 ~ "none",
    n == 1 ~ "opioids",
    n == 2 ~ "benzos",
    n == 3 ~ "barbiturate",
    n == 4 ~ "beta_blockers",
    n == 5 ~ "alpha_blockers",
    n == 6 ~ "dopa_antagonist",
    n == 7 ~ "baclofen",
    n == 8 ~ "other"), 
    treatment = str_replace(treatment, pattern = '_.*', replacement = ''), 
    val = 1) 
ds_treatments <- ds_therapy %>%
  expand(id, treatment, n) %>%
  unite(treat, treatment, n, sep = "_") %>%
  anti_join(ds_therapy %>%
              select(-val) %>%
              unite(treat, treatment, n, sep = "_")) %>%
  mutate(val = 0)
ds_therapy_wide <- ds_therapy %>%
  unite(treat, treatment, n, sep = "_") %>%
  bind_rows(ds_treatments) %>%
  spread(key = treat, value = val, fill = 0) %>%
  select(id:adm_other, t0_alpha_blockers:t3_other, everything())
ds_proc <- ds_preproc %>%
  select(-c(therapy_adm, isp_treat_t0, isp_treat_t1, isp_treat_t2, isp_treat_t3, therapy_disch)) %>%
  left_join(ds_therapy_wide) %>%
  mutate_at(vars(contains("psh_tot")), 
            .funs = list(cat = ~cut(., breaks = c(0,7,16,100), labels = c("Unlikely", "Possible", "Probable")))) %>%
  mutate(etiology = factor(etiology),
         days_icu = as.numeric(date_adm - date_event),
         days_rehab = as.numeric(date_disch - date_adm),
         days_event = as.numeric(date_disch - date_event),
         rehab_center = factor(str_sub(id, end = 1)), 
         age_event = round(as.numeric(date_event - dob)/365.25, 1),
         gos_cat = fct_case_when(
           gos == 1 ~ "Death",
           gos == 2 ~ "Vegetative State",
           gos == 3 ~ "Severe Disability",
           gos == 4 ~ "Moderate Disability",
           gos == 5 ~ "Low Disability"
         )) %>%
  filter(days_icu < 93)
ds_isp <- ds_proc %>%
  select(id, rehab_center, age_event, sex, days_icu, etiology, isp_icu, adm_alpha_blockers:adm_other, diagnosis_t0:psh_tot_t0, psh_tot_t0_cat, nr_crisis_t0, t0_alpha_blockers:t0_other, psh_cfs_t1:psh_tot_t1, psh_tot_t1_cat, nr_crisis_t1, t1_alpha_blockers:t1_other, diagnosis_t2:psh_tot_t2, psh_tot_t2_cat, nr_crisis_t2, t2_alpha_blockers:t2_other, diagnosis_t3:psh_tot_t3, psh_tot_t3_cat, nr_crisis_t3, t3_alpha_blockers:t3_other, days_rehab, gos_cat, disch_alpha_blockers:disch_other)

Data description

format_table = function(data) {
  data %>%
    mutate(
      var = trimws(var),
      var = str_replace_all(var, "_", " "),
      var = str_to_title(var),
      var = str_remove(var, " T0"),
      var = str_remove(var, " T2"),
      var = str_remove(var, " T3"),
      var = as.character(var),
      var = case_when(
        var == "Age Event (Mean (Sd))" ~ "Age Event (years)",
        var == "Days Icu (Mean (Sd))" ~ "Length of stay ICU (days)",
        var == "Days Rehab (Mean (Sd))" ~ "Length of stay IRU (days)",
        var == "Lcf (Median [Iqr])" ~ "LCF Score",
        str_detect(var,"Tracheo") ~ "Tracheostomy (%)",
        var == "Breath (%)" ~ "Breathing (%)",
        var == "Feed (%)" ~ "Feeding (%)",
        str_detect(var,"Cateth") ~ "Catether (%)",
        str_detect(var,"Asworth") ~ "Spasticity (%)",
        str_detect(var,"Craniolac") ~ "DC (%)",
        str_detect(var, "Mcs") ~ "MCS",
        str_detect(var, "Vs") ~ "VS",
        str_detect(var, "Tbi") ~ "TBI",
        str_detect(var , "Ng-Tube") ~ "NG-Tube",
        str_detect(var, "Peg") ~ "PEG",
        str_detect(var, "Psh Tot Cat") ~ "PSH-AM",
        TRUE ~ str_remove(var, "= Yes ")))
}
vars_tab1 <- c("age_event", "sex", "days_icu", "days_rehab", "etiology", "diagnosis_t0", "lcf_t0", "tracheo_t0", "breath_t0", "feed_t0", "cateth_t0", "bedsore_t0", "asworth_t0", "craniolac_t0", "psh_tot_t0_cat", "nr_crisis_t0")
ds_therapy_tab <- as.data.frame(
  print(CreateTableOne(
    vars = "treatment", 
    strata = "time", 
    data = ds_therapy %>% 
      rename(time = treatment, treatment = n) %>% 
      mutate(time = factor(time, labels = c("Admission", "Week 1", "Week 2", "Month 4", "Discharge-ISP", "Discharge")),
             treatment = factor(treatment, labels = str_to_title(str_replace_all(levels(treatment), "_", " ")))), 
    factorVars = "treatment", test = F), 
    catDigits = 1, contDigits = 1, noSpaces = T, missing = F, printToggle = F)[-1,]) %>%
  rename_at(1:6, ~ rep("Overall",6))
ds_tab1 <- as.data.frame(print(CreateTableOne(vars = vars_tab1, data = ds_isp), catDigits = 1, contDigits = 1, noSpaces = T, missing = F, printToggle = F, nonnormal = "lcf_t0")) %>%
  #mutate_all(as.character) %>%
  mutate(var = row.names(.),
         Overall = case_when(
           str_detect(var, "mean") ~ str_replace(str_replace(as.character(Overall), "\\(", "? "), "\\)", ""),
           str_detect(var, "median") ~ str_replace(as.character(Overall), "\\,", " -"),
           TRUE ~ as.character(Overall))) %>%
  format_table() %>%
  bind_rows(ds_therapy_tab[1] %>% mutate(var = str_to_title(row.names(.)))) %>%
  select(var, everything()) %>%
  rename(Admission = Overall)

# kable(ds_tab1, caption = "Table 1. Charasteristics of patients at admission in rehab centers") %>%
#   kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
#   add_indent(c(6:11, 13:15, 19:21, 23:26, 32:34, 36:39, 41:49))
vars_tab2 <- c("diagnosis_t2", "lcf_t2", "tracheo_t2", "breath_t2", "feed_t2", "cateth_t2", "bedsore_t2", "asworth_t2", "craniolac_t2", "psh_tot_t2_cat", "nr_crisis_t2")
ds_tab2 <- as.data.frame(print(CreateTableOne(vars = vars_tab2, data = ds_isp  %>% filter(!diagnosis_t2 %in% c("Dead", "Discharged")) %>% mutate(diagnosis_t2 = factor(diagnosis_t2))), catDigits = 1, contDigits = 1, noSpaces = T, missing = F, printToggle = F, nonnormal = "lcf_t2")) %>%
  mutate(var = row.names(.),
         Overall = case_when(
           str_detect(var, "mean") ~ str_replace(str_replace(as.character(Overall), "\\(", "? "), "\\)", ""),
           str_detect(var, "median") ~ str_replace(as.character(Overall), "\\,", " -"),
           TRUE ~ as.character(Overall))) %>%
  format_table() %>%
  bind_rows(ds_therapy_tab[2] %>% mutate(var = str_to_title(row.names(.)))) %>%
  select(var, everything()) %>%
  rename(`4th Month` = Overall)

# kable(ds_tab2, caption = "Table 2. Charasteristics of patients after 4 months from rehab start") %>%
#   kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
#   add_indent(c(3:5,9:11,13:16, 22:24, 26:29, 31:39))
vars_tab3 <- c("diagnosis_t3", "lcf_t3", "tracheo_t3", "breath_t3", "feed_t3", "cateth_t3", "bedsore_t3", "asworth_t3", "craniolac_t3", "psh_tot_t3_cat", "nr_crisis_t3", "days_rehab", "gos_cat")
ds_tab3 <- as.data.frame(print(CreateTableOne(vars = vars_tab3, data = ds_isp %>% filter(!diagnosis_t3 %in% c("Dead", "Discharged")) %>% mutate(diagnosis_t3 = factor(diagnosis_t3))), catDigits = 1, contDigits = 1, noSpaces = T, missing = F, printToggle = F, nonnormal = "lcf_t3")) %>%
  mutate(var = row.names(.),
         Overall = case_when(
           str_detect(var, "mean") ~ str_replace(str_replace(as.character(Overall), "\\(", "? "), "\\)", ""),
           str_detect(var, "median") ~ str_replace(as.character(Overall), "\\,", " -"),
           TRUE ~ as.character(Overall))) %>%
  format_table() %>%
  bind_rows(ds_therapy_tab[6] %>% mutate(var = str_to_title(row.names(.)))) %>%
  select(var, everything()) %>%
  rename(Discharge = Overall)

# kable(ds_tab3, caption = "Table 3. Charasteristics of patients at discharge") %>%
#   kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
#   add_indent(c(3:5,9:11,13:16, 22:24, 26:29, 32:36, 38:46))
ds_tab123 <- ds_tab1 %>%
  left_join(ds_tab2) %>%
  left_join(ds_tab3) %>%
  replace_na(list(`4th Month` = "", Discharge = ""))
  
kable(ds_tab123, caption = "Table 1. Charasteristics of patients at admission in rehab centers") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  add_indent(c(7:11, 13:15, 19:21, 23:26, 32:34, 36:39, 41:49))
Table 1. Charasteristics of patients at admission in rehab centers
var Admission 4th Month Discharge
N 152 138 148
Age Event (years) 52.3 ? 18.0
Sex = M (%) 95 (62.5)
Length of stay ICU (days) 45.4 ? 20.1
Length of stay IRU (days) 164.4 ? 75.9 165.6 ? 75.6
Etiology (%)
TBI 54 (35.5)
Vascular 77 (50.7)
Anoxic 15 (9.9)
Infective 2 (1.3)
Neoplastic 4 (2.6)
Diagnosis (%)
Emersion 39 (25.7) 76 (55.1) 95 (64.2)
MCS 54 (35.5) 32 (23.2) 34 (23.0)
VS 59 (38.8) 30 (21.7) 19 (12.8)
LCF Score 3.0 [2.0 - 4.0] 4.0 [3.0 - 6.0] 5.0 [3.0 - 7.0]
Tracheostomy (%) 135 (88.8) 78 (56.5) 54 (36.5)
Breathing (%)
Autonomous 58 (40.6) 112 (81.8) 126 (85.1)
Autonomous + O2 80 (55.9) 25 (18.2) 21 (14.2)
Mechanical 5 (3.5) 0 (0.0) 1 (0.7)
Feeding (%)
Oral 11 (7.3) 55 (39.9) 74 (50.7)
Parenteral 2 (1.3) 1 (0.7) 1 (0.7)
NG-Tube 63 (41.7) 11 (8.0) 3 (2.1)
PEG 75 (49.7) 71 (51.4) 68 (46.6)
Catether (%) 147 (96.7) 60 (43.5) 44 (29.7)
Bedsore (%) 55 (36.2) 38 (27.5) 21 (14.2)
Spasticity (%) 31 (20.4) 45 (32.6) 43 (29.1)
DC (%) 32 (21.1) 23 (16.7) 13 (8.8)
PSH-AM
Unlikely 105 (69.5) 114 (83.2) 137 (93.2)
Possible 30 (19.9) 13 (9.5) 5 (3.4)
Probable 16 (10.6) 10 (7.3) 5 (3.4)
Nr Crisis (%)
None 113 (79.6) 101 (79.5) 116 (83.5)
<3 14 (9.9) 16 (12.6) 15 (10.8)
3-5 14 (9.9) 4 (3.1) 5 (3.6)
>5 1 (0.7) 6 (4.7) 3 (2.2)
Treatment (%)
None 24 (10.8) 32 (12.7) 119 (77.8)
Opioids 10 (4.5) 2 (0.8) 0 (0.0)
Benzos 22 (9.9) 35 (13.9) 12 (7.8)
Barbiturate 6 (2.7) 3 (1.2) 0 (0.0)
Beta Blockers 69 (31.1) 45 (17.9) 11 (7.2)
Alpha Blockers 16 (7.2) 4 (1.6) 1 (0.7)
Dopa Antagonist 3 (1.4) 24 (9.6) 0 (0.0)
Baclofen 17 (7.7) 22 (8.8) 6 (3.9)
Other 55 (24.8) 84 (33.5) 4 (2.6)

Descriptive Plots (Captions are below the figures)

cbPalette <- c(pal_nejm("default", alpha = 1)(8), "#000000")
ds_plot <- ds_isp %>%
  select(id, c(vars_tab1, vars_tab2, vars_tab3), psh_tot_t1_cat, nr_crisis_t1) %>%
  select(-c(age_event, sex, days_icu, etiology, days_rehab, gos_cat)) %>%
  rename(psh_t0 = psh_tot_t0_cat, psh_t1 = psh_tot_t1_cat, psh_t2 = psh_tot_t2_cat, psh_t3 = psh_tot_t3_cat) %>%
  gather(var, value, -id, na.rm = T, factor_key = T) %>%
  separate(var, c("var", "time"), sep = "_t") %>%
  mutate(time = fct_case_when(
    time == "0" ~ "Admission",
    time == "1" ~ "2nd Week",
    time == "2" ~ "4th month",
    time == "3" ~ "Discharge"
  ))
g <- ggplot(data = ds_plot %>% 
              filter(var %in% c("psh", "nr_crisis")) %>%
              spread(var,value) %>%
              group_by(time, nr_crisis, psh) %>% 
              summarise(n = n()) %>%
              mutate(freq = n/sum(n)) %>%
              ungroup() %>%
              mutate(nr_crisis = factor(nr_crisis, levels = c("none", "<3", "3-5", ">5")),
                     psh = factor(psh, levels = c("Unlikely", "Possible", "Probable"))) %>%
              filter(!is.na(nr_crisis)) %>%
              filter(!is.na(psh)) %>%
              complete(nr_crisis, nesting(time, psh), fill = list(n = 0, freq = 0)),
             aes(x = psh, y = n, fill = nr_crisis))

g + geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(name = "Nr. ISP Crisis", values = cbPalette) +
  #scale_y_continuous(trans='log2') +
  labs(x = "PSH Total Score", y = "nr. of Patients") +
  facet_wrap(~ time) +
  theme_bw(base_family="OpenSans") +
  theme(text = element_text(size=16),
        axis.text.x = element_text(size=12),
        legend.position = "bottom")
Figure 8. Number of ISP crisis by PSH scale and time

Figure 8. Number of ISP crisis by PSH scale and time

ds_out <- ds_isp %>%
  select(id, contains("nr_crisis"),contains("psh_tot"), contains("gos")) %>%
  mutate(isp_crisis = ds_isp %>%
           select(contains("nr_crisis")) %>%
           mutate_all(.funs = list(~ as.numeric(.) - 1)) %>%
           mutate(isp = rowSums(., na.rm = T),
                  isp = factor(isp>0, c(F,T), c("none", "yes"))) %>%
           pull(isp),
         psh_positive = ds_isp %>%
           select(contains("psh_tot")) %>%
           select(contains("cat")) %>%
           mutate_all(.funs = list(~ as.numeric(.) - 1)) %>%
           mutate(psh = rowSums(., na.rm = T),
                  psh = factor(psh>0, c(F,T), c("negative", "positive"))) %>%
           pull(psh),
         gos_cat = fct_rev(gos_cat),
         gos = as.numeric(gos_cat))
library(descr)

ct_t0 <- crosstab(ds_out$psh_tot_t0_cat, ds_out$nr_crisis_t0,
dnn = c("PSH", "Nr.Crisis"),
drop.levels = F, expected = F, prop.c = F, prop.r = TRUE, fisher = T, plot = FALSE)

ds_xtab_t0 <- as.data.frame(ct_t0$tab) %>%
  spread(Nr.Crisis,Freq) %>%
  mutate_if(is.numeric, as.character) %>%
  bind_rows(as.data.frame(ct_t0$prop.row) %>%
              spread(Nr.Crisis,Freq) %>%
              mutate_if(is.numeric, .funs = list(~paste(round(.*100,2),"%", sep="")))) %>%
  arrange(PSH)

kable(ds_xtab_t0, caption = "Table 2. Numbers of ISP Crisis by PSH Score at Admission") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  add_header_above(c(" ", "Nr. Crisis" = 4))
Table 2. Numbers of ISP Crisis by PSH Score at Admission
Nr. Crisis
PSH none <3 3-5 >5
Unlikely 104 1 0 0
99.05% 0.95% 0% 0%
Possible 8 12 7 0
29.63% 44.44% 25.93% 0%
Probable 0 1 7 1
0% 11.11% 77.78% 11.11%
ct_t1 <- crosstab(ds_out$psh_tot_t1_cat, ds_out$nr_crisis_t1,
dnn = c("PSH", "Nr.Crisis"),
drop.levels = F, expected = F, prop.c = F, prop.r = TRUE, fisher = T, plot = FALSE)

ds_xtab_t1 <- as.data.frame(ct_t1$tab) %>%
  spread(Nr.Crisis,Freq) %>%
  mutate_if(is.numeric, as.character) %>%
  bind_rows(as.data.frame(ct_t1$prop.row) %>%
              spread(Nr.Crisis,Freq) %>%
              mutate_if(is.numeric, .funs = list(~paste(round(.*100,2),"%", sep="")))) %>%
  arrange(PSH)

kable(ds_xtab_t1, caption = "Table 2. Numbers of ISP Crisis by PSH Score at Admission") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  add_header_above(c(" ", "Nr. Crisis" = 4))
Table 2. Numbers of ISP Crisis by PSH Score at Admission
Nr. Crisis
PSH none <3 3-5 >5
Unlikely 115 1 0 0
99.14% 0.86% 0% 0%
Possible 7 10 3 0
35% 50% 15% 0%
Probable 0 2 5 0
0% 28.57% 71.43% 0%
ct_t2 <- crosstab(ds_out$psh_tot_t2_cat, ds_out$nr_crisis_t2,
dnn = c("PSH", "Nr.Crisis"),
drop.levels = F, expected = F, prop.c = F, prop.r = TRUE, fisher = T, plot = FALSE)

ds_xtab_t2 <- as.data.frame(ct_t2$tab) %>%
  spread(Nr.Crisis,Freq) %>%
  mutate_if(is.numeric, as.character) %>%
  bind_rows(as.data.frame(ct_t2$prop.row) %>%
              spread(Nr.Crisis,Freq) %>%
              mutate_if(is.numeric, .funs = list(~paste(round(.*100,2),"%", sep="")))) %>%
  arrange(PSH)

kable(ds_xtab_t2, caption = "Table 2. Numbers of ISP Crisis by PSH Score at Admission") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  add_header_above(c(" ", "Nr. Crisis" = 4))
Table 2. Numbers of ISP Crisis by PSH Score at Admission
Nr. Crisis
PSH none <3 3-5 >5
Unlikely 99 13 0 0
88.39% 11.61% 0% 0%
Possible 1 3 2 2
12.5% 37.5% 25% 25%
Probable 0 0 2 4
0% 0% 33.33% 66.67%
ct_t3 <- crosstab(ds_out$psh_tot_t3_cat, ds_out$nr_crisis_t3,
dnn = c("PSH", "Nr.Crisis"),
drop.levels = F, expected = F, prop.c = F, prop.r = TRUE, fisher = T, plot = FALSE)

ds_xtab_t3 <- as.data.frame(ct_t3$tab) %>%
  spread(Nr.Crisis,Freq) %>%
  mutate_if(is.numeric, as.character) %>%
  bind_rows(as.data.frame(ct_t3$prop.row) %>%
              spread(Nr.Crisis,Freq) %>%
              mutate_if(is.numeric, .funs = list(~paste(round(.*100,2),"%", sep="")))) %>%
  arrange(PSH)

kable(ds_xtab_t3, caption = "Table 2. Numbers of ISP Crisis by PSH Score at Admission") %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed")) %>%
  collapse_rows(columns = 1, valign = "top") %>%
  add_header_above(c(" ", "Nr. Crisis" = 4))
Table 2. Numbers of ISP Crisis by PSH Score at Admission
Nr. Crisis
PSH none <3 3-5 >5
Unlikely 115 12 3 1
87.79% 9.16% 2.29% 0.76%
Possible 0 3 1 0
0% 75% 25% 0%
Probable 0 0 1 2
0% 0% 33.33% 66.67%
g1 <- ggplot(data = ds_out %>%
               select(nr_crisis_t0, gos_cat) %>%
               filter(!is.na(nr_crisis_t0)) %>%
               group_by(nr_crisis_t0, gos_cat) %>%
               summarise(n = n()) %>%
               mutate(freq = n/sum(n)) %>%
               complete(nr_crisis_t0, nesting(gos_cat), fill = list(n = 0, freq = 0)),
               aes(x = nr_crisis_t0, y = n, fill = gos_cat)) + geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(name = "GOS", values = cbPalette) +
  labs(title = "A", x = "ISP Crisis during Week 1", y = "nr. of Patients") +
  theme_bw(base_family="OpenSans") +
  theme(text = element_text(size=16))

g2 <- ggplot(data = ds_out %>%
               select(psh_tot_t0_cat, gos_cat) %>%
               filter(!is.na(psh_tot_t0_cat)) %>%
               group_by(psh_tot_t0_cat, gos_cat) %>%
               summarise(n = n()) %>%
               mutate(freq = n/sum(n)) %>%
               complete(psh_tot_t0_cat, nesting(gos_cat), fill = list(n = 0, freq = 0)),
               aes(x = psh_tot_t0_cat, y = n, fill = gos_cat)) + geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(name = "GOS", values = cbPalette) +
  labs(title = "B", x = "PSH results during Week 1", y = "nr. of Patients") +
  theme_bw(base_family="OpenSans") +
  theme(text = element_text(size=16),
        legend.text = element_text(size=11),
        legend.title = element_text(size=12))

grid_arrange_shared_legend(g1,g2, ncol = 1, nrow = 2, position = "bottom")
Figure 9. GOS by Nr. ISP Crisis (A) and PSH score results (B) during Week 1

Figure 9. GOS by Nr. ISP Crisis (A) and PSH score results (B) during Week 1

g1 <- ggplot(data = ds_out %>%
               select(isp_crisis, gos_cat) %>%
               group_by(isp_crisis, gos_cat) %>%
               summarise(n = n()) %>%
               mutate(freq = n/sum(n)) %>%
               complete(isp_crisis, nesting(gos_cat), fill = list(n = 0, freq = 0)),
               aes(x = isp_crisis, y = n, fill = gos_cat)) + geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(name = "GOS", values = cbPalette) +
  labs(title = "A", x = "ISP Crisis during follow-up", y = "nr. of Patients") +
  theme_bw(base_family="OpenSans") +
  theme(text = element_text(size=16))

g2 <- ggplot(data = ds_out %>%
               select(psh_positive, gos_cat) %>%
               group_by(psh_positive, gos_cat) %>%
               summarise(n = n()) %>%
               mutate(freq = n/sum(n)) %>%
               complete(psh_positive, nesting(gos_cat), fill = list(n = 0, freq = 0)),
               aes(x = psh_positive, y = n, fill = gos_cat)) + geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(name = "GOS", values = cbPalette) +
  labs(title = "B", x = "PSH positivity during study", y = "nr. of Patients") +
  theme_bw(base_family="OpenSans") +
  theme(text = element_text(size=16),
        legend.text = element_text(size=11),
        legend.title = element_text(size=12))

grid_arrange_shared_legend(g1,g2, ncol = 1, nrow = 2, position = "bottom")
Figure 10. GOS by Nr. ISP Crisis (A) and PSH score results (B) anytime during follow-up

Figure 10. GOS by Nr. ISP Crisis (A) and PSH score results (B) anytime during follow-up