Introduction:

This is a survival analysis for death in HF patients. the analysis concluded that age, male gender, length of stay, prior missed outpatient appointments and many comorbidities have evidence that they affect death rate in heart failure.

Load Packages:

library(survival)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
library(survminer)
## Warning: package 'survminer' was built under R version 4.0.5
## Loading required package: ggpubr
## Warning: package 'ggpubr' was built under R version 4.0.5
library(gmodels)
## Warning: package 'gmodels' was built under R version 4.0.5

Load Data:

filename <- "HF.csv"
fileURL <- "https://d3c33hcgiwev3.cloudfront.net/6AiBbg-BEem6Gg6vVM6M8A_e872b4600f8111e9b2f4133a1edfbb40_simulated-HF-mort-data-for-GMPH-_1K_-final-_2_.csv?Expires=1628294400&Signature=W6JL~~LOJRnDWbOy80-uNeDtawKxqe~CwY9OUiOepmB2ALINxQN3ZlrsOzFegePzP6DXpQqnp3LavLWlHSnBIx6vNPfkhv6dWo8MElYTKll-dne15MYRKS0Xs8bw~Dx7xWxAj0WEDX4XGKIeHjudu4hgVQqawwtsq53A8x-~D3o_&Key-Pair-Id=APKAJLTNE6QMUY6HBC5A"
if (!file.exists(filename)){
download.file(fileURL, filename)
}

HF <- read.csv("HF.csv")

Define Variables:

gender <- as.factor(HF[,"gender"]) #  categorical variable 
fu_time <- HF[,"fu_time"] # continuous variable (numeric) 
death <- HF[,"death"] # binary variable (numeric) 
age <- HF[,"age"]
ethnicgroup <- factor(HF[,"ethnicgroup"]) 
prior_dnas <- HF[,"prior_dnas"]
prior_dnas_c <- factor(HF[,"prior_dnas"])
copd <- factor(HF[,"copd"])
quintile <- factor(HF[, "quintile"])
los <- HF[,"los"]
ihd <- factor(HF[,"ihd"])
stroke <- factor(HF[,"stroke"])
arrhythmias <- factor(HF[,"arrhythmias"])
vd <-  factor(HF[,"valvular_disease"])
pvd <- factor(HF[,"pvd"])
pneumonia <- factor(HF[,"pneumonia"])
renal <-  factor(HF[,"renal_disease"])
htn <- factor(HF[,"hypertension"])
cancer <- factor(HF[,"cancer"])
met_cancer <-  factor(HF[,"metastatic_cancer"])
dementia <-  factor(HF[,"dementia"])
senile <- factor(HF[,"senile"])
cog_imp <- as.factor(ifelse(HF$dementia == 1 | HF$senile == 1, 1, 0))
mental <- factor(HF[,"mental_health"])
prior_attend <- HF[,"prior_appts_attended"]
cabg <- factor(HF[,"cabg"])
defib <- factor(HF[,"defib"])

describe variables:

dim(HF)
## [1] 1000   31
table(gender, useNA = "always")
## gender
##    1    2 <NA> 
##  548  452    0
summary(fu_time)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0   117.8   475.0   466.0   743.2  1107.0
table(death, useNA = "always")
## death
##    0    1 <NA> 
##  508  492    0
summary(age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   29.00   73.00   80.00   78.73   87.00  102.00
hist(age)

table(ethnicgroup, useNA = "always")
## ethnicgroup
##    1    2    3    9 <NA> 
##  889   17   34   17   43
summary(prior_dnas)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   0.501   1.000  10.000
table(prior_dnas_c)
## prior_dnas_c
##   0   1   2   3   4   5   6   7   8  10 
## 732 156  50  34  17   3   3   2   1   2
CrossTable(copd, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##         copd |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       393 |       365 |       758 | 
##              |     0.164 |     0.169 |           | 
##              |     0.518 |     0.482 |     0.758 | 
##              |     0.774 |     0.742 |           | 
##              |     0.393 |     0.365 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       115 |       127 |       242 | 
##              |     0.512 |     0.529 |           | 
##              |     0.475 |     0.525 |     0.242 | 
##              |     0.226 |     0.258 |           | 
##              |     0.115 |     0.127 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
table(quintile, useNA = "always")
## quintile
##    0    1    2    3    4    5 <NA> 
##    4  138  205  211  220  216    6
# there is an error here ( the 0 value).  we can't use it as the reference value. so I am going to change it. 

summary(los)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    3.00    7.00   10.77   13.00   89.00
CrossTable(ihd, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##          ihd |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       266 |       239 |       505 | 
##              |     0.349 |     0.360 |           | 
##              |     0.527 |     0.473 |     0.505 | 
##              |     0.524 |     0.486 |           | 
##              |     0.266 |     0.239 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       242 |       253 |       495 | 
##              |     0.356 |     0.367 |           | 
##              |     0.489 |     0.511 |     0.495 | 
##              |     0.476 |     0.514 |           | 
##              |     0.242 |     0.253 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(stroke, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##       stroke |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       500 |       480 |       980 | 
##              |     0.009 |     0.010 |           | 
##              |     0.510 |     0.490 |     0.980 | 
##              |     0.984 |     0.976 |           | 
##              |     0.500 |     0.480 |           | 
## -------------|-----------|-----------|-----------|
##            1 |         8 |        12 |        20 | 
##              |     0.459 |     0.474 |           | 
##              |     0.400 |     0.600 |     0.020 | 
##              |     0.016 |     0.024 |           | 
##              |     0.008 |     0.012 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(arrhythmias, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##  arrhythmias |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       249 |       261 |       510 | 
##              |     0.392 |     0.405 |           | 
##              |     0.488 |     0.512 |     0.510 | 
##              |     0.490 |     0.530 |           | 
##              |     0.249 |     0.261 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       259 |       231 |       490 | 
##              |     0.408 |     0.421 |           | 
##              |     0.529 |     0.471 |     0.490 | 
##              |     0.510 |     0.470 |           | 
##              |     0.259 |     0.231 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(vd, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##           vd |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       380 |       376 |       756 | 
##              |     0.043 |     0.044 |           | 
##              |     0.503 |     0.497 |     0.756 | 
##              |     0.748 |     0.764 |           | 
##              |     0.380 |     0.376 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       128 |       116 |       244 | 
##              |     0.132 |     0.136 |           | 
##              |     0.525 |     0.475 |     0.244 | 
##              |     0.252 |     0.236 |           | 
##              |     0.128 |     0.116 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(pvd, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##          pvd |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       454 |       446 |       900 | 
##              |     0.022 |     0.023 |           | 
##              |     0.504 |     0.496 |     0.900 | 
##              |     0.894 |     0.907 |           | 
##              |     0.454 |     0.446 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        54 |        46 |       100 | 
##              |     0.202 |     0.208 |           | 
##              |     0.540 |     0.460 |     0.100 | 
##              |     0.106 |     0.093 |           | 
##              |     0.054 |     0.046 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(pneumonia, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##    pneumonia |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       457 |       426 |       883 | 
##              |     0.159 |     0.164 |           | 
##              |     0.518 |     0.482 |     0.883 | 
##              |     0.900 |     0.866 |           | 
##              |     0.457 |     0.426 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        51 |        66 |       117 | 
##              |     1.197 |     1.236 |           | 
##              |     0.436 |     0.564 |     0.117 | 
##              |     0.100 |     0.134 |           | 
##              |     0.051 |     0.066 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(renal, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##        renal |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       394 |       357 |       751 | 
##              |     0.409 |     0.422 |           | 
##              |     0.525 |     0.475 |     0.751 | 
##              |     0.776 |     0.726 |           | 
##              |     0.394 |     0.357 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       114 |       135 |       249 | 
##              |     1.234 |     1.274 |           | 
##              |     0.458 |     0.542 |     0.249 | 
##              |     0.224 |     0.274 |           | 
##              |     0.114 |     0.135 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(htn, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##          htn |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       187 |       192 |       379 | 
##              |     0.159 |     0.164 |           | 
##              |     0.493 |     0.507 |     0.379 | 
##              |     0.368 |     0.390 |           | 
##              |     0.187 |     0.192 |           | 
## -------------|-----------|-----------|-----------|
##            1 |       321 |       300 |       621 | 
##              |     0.097 |     0.100 |           | 
##              |     0.517 |     0.483 |     0.621 | 
##              |     0.632 |     0.610 |           | 
##              |     0.321 |     0.300 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(cancer, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##       cancer |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       488 |       461 |       949 | 
##              |     0.072 |     0.075 |           | 
##              |     0.514 |     0.486 |     0.949 | 
##              |     0.961 |     0.937 |           | 
##              |     0.488 |     0.461 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        20 |        31 |        51 | 
##              |     1.347 |     1.391 |           | 
##              |     0.392 |     0.608 |     0.051 | 
##              |     0.039 |     0.063 |           | 
##              |     0.020 |     0.031 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(met_cancer, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##   met_cancer |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       507 |       484 |       991 | 
##              |     0.025 |     0.026 |           | 
##              |     0.512 |     0.488 |     0.991 | 
##              |     0.998 |     0.984 |           | 
##              |     0.507 |     0.484 |           | 
## -------------|-----------|-----------|-----------|
##            1 |         1 |         8 |         9 | 
##              |     2.791 |     2.881 |           | 
##              |     0.111 |     0.889 |     0.009 | 
##              |     0.002 |     0.016 |           | 
##              |     0.001 |     0.008 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(dementia, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##     dementia |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       497 |       458 |       955 | 
##              |     0.290 |     0.299 |           | 
##              |     0.520 |     0.480 |     0.955 | 
##              |     0.978 |     0.931 |           | 
##              |     0.497 |     0.458 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        11 |        34 |        45 | 
##              |     6.153 |     6.353 |           | 
##              |     0.244 |     0.756 |     0.045 | 
##              |     0.022 |     0.069 |           | 
##              |     0.011 |     0.034 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(senile, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##       senile |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       497 |       459 |       956 | 
##              |     0.265 |     0.274 |           | 
##              |     0.520 |     0.480 |     0.956 | 
##              |     0.978 |     0.933 |           | 
##              |     0.497 |     0.459 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        11 |        33 |        44 | 
##              |     5.765 |     5.953 |           | 
##              |     0.250 |     0.750 |     0.044 | 
##              |     0.022 |     0.067 |           | 
##              |     0.011 |     0.033 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(cog_imp, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##      cog_imp |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       488 |       433 |       921 | 
##              |     0.866 |     0.894 |           | 
##              |     0.530 |     0.470 |     0.921 | 
##              |     0.961 |     0.880 |           | 
##              |     0.488 |     0.433 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        20 |        59 |        79 | 
##              |    10.099 |    10.428 |           | 
##              |     0.253 |     0.747 |     0.079 | 
##              |     0.039 |     0.120 |           | 
##              |     0.020 |     0.059 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(mental, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##       mental |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       454 |       457 |       911 | 
##              |     0.167 |     0.172 |           | 
##              |     0.498 |     0.502 |     0.911 | 
##              |     0.894 |     0.929 |           | 
##              |     0.454 |     0.457 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        54 |        35 |        89 | 
##              |     1.708 |     1.764 |           | 
##              |     0.607 |     0.393 |     0.089 | 
##              |     0.106 |     0.071 |           | 
##              |     0.054 |     0.035 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
summary(prior_attend)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   1.000   3.000   5.538   8.000  62.000
hist(prior_attend)

CrossTable(cabg, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##         cabg |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       495 |       491 |       986 | 
##              |     0.069 |     0.071 |           | 
##              |     0.502 |     0.498 |     0.986 | 
##              |     0.974 |     0.998 |           | 
##              |     0.495 |     0.491 |           | 
## -------------|-----------|-----------|-----------|
##            1 |        13 |         1 |        14 | 
##              |     4.875 |     5.033 |           | 
##              |     0.929 |     0.071 |     0.014 | 
##              |     0.026 |     0.002 |           | 
##              |     0.013 |     0.001 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
CrossTable(defib, death)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  1000 
## 
##  
##              | death 
##        defib |         0 |         1 | Row Total | 
## -------------|-----------|-----------|-----------|
##            0 |       504 |       490 |       994 | 
##              |     0.002 |     0.002 |           | 
##              |     0.507 |     0.493 |     0.994 | 
##              |     0.992 |     0.996 |           | 
##              |     0.504 |     0.490 |           | 
## -------------|-----------|-----------|-----------|
##            1 |         4 |         2 |         6 | 
##              |     0.297 |     0.307 |           | 
##              |     0.667 |     0.333 |     0.006 | 
##              |     0.008 |     0.004 |           | 
##              |     0.004 |     0.002 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       508 |       492 |      1000 | 
##              |     0.508 |     0.492 |           | 
## -------------|-----------|-----------|-----------|
## 
## 

Kaplan-Meier Plot:

km_fit <- survfit(Surv(fu_time, death) ~ 1)
plot(km_fit)

summary(km_fit, times = c(1:7,30,60,90*(1:10)))
## Call: survfit(formula = Surv(fu_time, death) ~ 1)
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     1    992      12    0.988 0.00346        0.981        0.995
##     2    973       7    0.981 0.00435        0.972        0.989
##     3    963       5    0.976 0.00489        0.966        0.985
##     4    954       6    0.970 0.00546        0.959        0.980
##     5    945       5    0.964 0.00590        0.953        0.976
##     6    938       1    0.963 0.00598        0.952        0.975
##     7    933       1    0.962 0.00606        0.951        0.974
##    30    865      39    0.921 0.00865        0.905        0.939
##    60    809      28    0.891 0.01010        0.871        0.911
##    90    770      24    0.864 0.01117        0.843        0.887
##   180    698      43    0.815 0.01282        0.790        0.841
##   270    653      24    0.787 0.01363        0.760        0.814
##   360    619      21    0.761 0.01428        0.733        0.789
##   450    525      44    0.705 0.01554        0.675        0.736
##   540    429      47    0.639 0.01681        0.607        0.673
##   630    362      32    0.589 0.01765        0.556        0.625
##   720    266      43    0.514 0.01876        0.479        0.552
##   810    190      31    0.448 0.01979        0.411        0.488
##   900    126      26    0.378 0.02098        0.339        0.421
# split the curve by gender:
km_gender_fit <- survfit(Surv(fu_time, death) ~ gender) 

plot(km_gender_fit)

survdiff(Surv(fu_time, death) ~ gender, rho=0)  ##logrank test
## Call:
## survdiff(formula = Surv(fu_time, death) ~ gender, rho = 0)
## 
##            N Observed Expected (O-E)^2/E (O-E)^2/V
## gender=1 548      268      271    0.0365     0.082
## gender=2 452      224      221    0.0448     0.082
## 
##  Chisq= 0.1  on 1 degrees of freedom, p= 0.8
# split the curve by age categories: 
age_categorised <- ifelse(age < 65, "under 65",  
                                              ifelse(age >= 65, "65 or above", NA)) 
age_categorised <- factor(age_categorised)                                                     
table(age_categorised, useNA = "always")
## age_categorised
## 65 or above    under 65        <NA> 
##         885         115           0
km_age_fit <- survfit(Surv(fu_time, death) ~ age_categorised)
plot(km_age_fit)

survdiff(Surv(fu_time, death) ~ age_categorised, rho=0)
## Call:
## survdiff(formula = Surv(fu_time, death) ~ age_categorised, rho = 0)
## 
##                               N Observed Expected (O-E)^2/E (O-E)^2/V
## age_categorised=65 or above 885      474      425      5.65      41.7
## age_categorised=under 65    115       18       67     35.85      41.7
## 
##  Chisq= 41.7  on 1 degrees of freedom, p= 1e-10

Simple Cox Regression:

cox <- coxph(Surv(fu_time, death) ~ ethnicgroup) 
summary(cox)
## Call:
## coxph(formula = Surv(fu_time, death) ~ ethnicgroup)
## 
##   n= 957, number of events= 471 
##    (43 observations deleted due to missingness)
## 
##                  coef exp(coef) se(coef)      z Pr(>|z|)   
## ethnicgroup2 -0.06428   0.93774  0.32000 -0.201  0.84078   
## ethnicgroup3 -1.19586   0.30244  0.41108 -2.909  0.00362 **
## ethnicgroup9  0.07394   1.07674  0.35706  0.207  0.83596   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##              exp(coef) exp(-coef) lower .95 upper .95
## ethnicgroup2    0.9377     1.0664    0.5008    1.7558
## ethnicgroup3    0.3024     3.3064    0.1351    0.6769
## ethnicgroup9    1.0767     0.9287    0.5348    2.1679
## 
## Concordance= 0.516  (se = 0.006 )
## Likelihood ratio test= 12.99  on 3 df,   p=0.005
## Wald test            = 8.55  on 3 df,   p=0.04
## Score (logrank) test = 9.61  on 3 df,   p=0.02
# add missing ethnic groups to new "unknown" category:
levels(ethnicgroup) <- c(levels(ethnicgroup),"8") # add level 8 to the factor

ethnicgroup[is.na(ethnicgroup)] <- "8" # Change NA to "None"

cox <- coxph(Surv(fu_time, death) ~ ethnicgroup) 
summary(cox)
## Call:
## coxph(formula = Surv(fu_time, death) ~ ethnicgroup)
## 
##   n= 1000, number of events= 492 
## 
##                  coef exp(coef) se(coef)      z Pr(>|z|)   
## ethnicgroup2 -0.06573   0.93638  0.31999 -0.205  0.83725   
## ethnicgroup3 -1.19368   0.30310  0.41107 -2.904  0.00369 **
## ethnicgroup9  0.08160   1.08502  0.35706  0.229  0.81923   
## ethnicgroup8 -0.02353   0.97675  0.22363 -0.105  0.91621   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##              exp(coef) exp(-coef) lower .95 upper .95
## ethnicgroup2    0.9364     1.0679    0.5001    1.7532
## ethnicgroup3    0.3031     3.2992    0.1354    0.6784
## ethnicgroup9    1.0850     0.9216    0.5389    2.1846
## ethnicgroup8    0.9767     1.0238    0.6301    1.5140
## 
## Concordance= 0.518  (se = 0.008 )
## Likelihood ratio test= 12.95  on 4 df,   p=0.01
## Wald test            = 8.53  on 4 df,   p=0.07
## Score (logrank) test = 9.58  on 4 df,   p=0.05

Multiple Cox Regression Model:

reference article for affecting factor: https://www.ncbi.nlm.nih.gov/books/NBK513479/

mcox <- coxph(Surv(fu_time, death) ~ age + gender +  ethnicgroup + los + ihd + stroke + arrhythmias + vd + pvd + copd + pneumonia + renal + htn + cancer + met_cancer + cog_imp + mental + prior_attend + prior_dnas + cabg + defib)
summary(mcox)
## Call:
## coxph(formula = Surv(fu_time, death) ~ age + gender + ethnicgroup + 
##     los + ihd + stroke + arrhythmias + vd + pvd + copd + pneumonia + 
##     renal + htn + cancer + met_cancer + cog_imp + mental + prior_attend + 
##     prior_dnas + cabg + defib)
## 
##   n= 1000, number of events= 492 
## 
##                   coef exp(coef)  se(coef)      z Pr(>|z|)    
## age           0.057228  1.058897  0.005805  9.859  < 2e-16 ***
## gender2      -0.245671  0.782180  0.098309 -2.499 0.012456 *  
## ethnicgroup2 -0.210588  0.810108  0.357098 -0.590 0.555379    
## ethnicgroup3 -0.798948  0.449802  0.416732 -1.917 0.055216 .  
## ethnicgroup9  0.432757  1.541501  0.365419  1.184 0.236304    
## ethnicgroup8 -0.131358  0.876904  0.232841 -0.564 0.572650    
## los           0.011399  1.011465  0.003286  3.469 0.000522 ***
## ihd1          0.200584  1.222117  0.096332  2.082 0.037323 *  
## stroke1       0.029573  1.030015  0.303815  0.097 0.922457    
## arrhythmias1 -0.143632  0.866206  0.095310 -1.507 0.131809    
## vd1           0.202418  1.224359  0.108949  1.858 0.063182 .  
## pvd1          0.039183  1.039960  0.163059  0.240 0.810101    
## copd1         0.084882  1.088588  0.108785  0.780 0.435230    
## pneumonia1    0.294409  1.342332  0.141618  2.079 0.037627 *  
## renal1        0.149282  1.161000  0.110007  1.357 0.174774    
## htn1         -0.039846  0.960937  0.096734 -0.412 0.680404    
## cancer1       0.313144  1.367719  0.209448  1.495 0.134890    
## met_cancer1   2.191424  8.947943  0.400448  5.472 4.44e-08 ***
## cog_imp1      0.290274  1.336793  0.150246  1.932 0.053361 .  
## mental1      -0.061308  0.940534  0.182404 -0.336 0.736788    
## prior_attend -0.009295  0.990748  0.008091 -1.149 0.250644    
## prior_dnas    0.126733  1.135114  0.043682  2.901 0.003716 ** 
## cabg1        -1.805277  0.164429  1.004864 -1.797 0.072409 .  
## defib1       -0.601418  0.548034  0.723481 -0.831 0.405813    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##              exp(coef) exp(-coef) lower .95 upper .95
## age             1.0589     0.9444   1.04692    1.0710
## gender2         0.7822     1.2785   0.64510    0.9484
## ethnicgroup2    0.8101     1.2344   0.40233    1.6312
## ethnicgroup3    0.4498     2.2232   0.19875    1.0180
## ethnicgroup9    1.5415     0.6487   0.75318    3.1549
## ethnicgroup8    0.8769     1.1404   0.55559    1.3840
## los             1.0115     0.9887   1.00497    1.0180
## ihd1            1.2221     0.8183   1.01185    1.4761
## stroke1         1.0300     0.9709   0.56785    1.8683
## arrhythmias1    0.8662     1.1545   0.71861    1.0441
## vd1             1.2244     0.8168   0.98894    1.5158
## pvd1            1.0400     0.9616   0.75548    1.4316
## copd1           1.0886     0.9186   0.87956    1.3473
## pneumonia1      1.3423     0.7450   1.01699    1.7718
## renal1          1.1610     0.8613   0.93582    1.4404
## htn1            0.9609     1.0407   0.79498    1.1615
## cancer1         1.3677     0.7311   0.90722    2.0620
## met_cancer1     8.9479     0.1118   4.08189   19.6148
## cog_imp1        1.3368     0.7481   0.99581    1.7945
## mental1         0.9405     1.0632   0.65783    1.3447
## prior_attend    0.9907     1.0093   0.97516    1.0066
## prior_dnas      1.1351     0.8810   1.04198    1.2366
## cabg1           0.1644     6.0817   0.02294    1.1785
## defib1          0.5480     1.8247   0.13273    2.2627
## 
## Concordance= 0.713  (se = 0.012 )
## Likelihood ratio test= 237.2  on 24 df,   p=<2e-16
## Wald test            = 223.8  on 24 df,   p=<2e-16
## Score (logrank) test = 244.5  on 24 df,   p=<2e-16
# backward deletion:
mcox2 <- coxph(Surv(fu_time, death) ~ age + gender  + los + ihd  + vd +   pneumonia +  met_cancer + cog_imp  + prior_dnas )
summary(mcox2)
## Call:
## coxph(formula = Surv(fu_time, death) ~ age + gender + los + ihd + 
##     vd + pneumonia + met_cancer + cog_imp + prior_dnas)
## 
##   n= 1000, number of events= 492 
## 
##                  coef exp(coef)  se(coef)      z Pr(>|z|)    
## age          0.060630  1.062505  0.005684 10.667  < 2e-16 ***
## gender2     -0.266403  0.766130  0.095243 -2.797 0.005157 ** 
## los          0.011838  1.011908  0.003218  3.679 0.000234 ***
## ihd1         0.188195  1.207069  0.093562  2.011 0.044278 *  
## vd1          0.201040  1.222674  0.107514  1.870 0.061497 .  
## pneumonia1   0.322339  1.380352  0.138607  2.326 0.020042 *  
## met_cancer1  2.457014 11.669914  0.364843  6.734 1.65e-11 ***
## cog_imp1     0.292821  1.340203  0.146576  1.998 0.045744 *  
## prior_dnas   0.106943  1.112871  0.037477  2.854 0.004323 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##             exp(coef) exp(-coef) lower .95 upper .95
## age            1.0625    0.94117    1.0507    1.0744
## gender2        0.7661    1.30526    0.6357    0.9234
## los            1.0119    0.98823    1.0055    1.0183
## ihd1           1.2071    0.82845    1.0048    1.4500
## vd1            1.2227    0.81788    0.9904    1.5095
## pneumonia1     1.3804    0.72445    1.0520    1.8112
## met_cancer1   11.6699    0.08569    5.7084   23.8574
## cog_imp1       1.3402    0.74616    1.0056    1.7862
## prior_dnas     1.1129    0.89858    1.0341    1.1977
## 
## Concordance= 0.704  (se = 0.012 )
## Likelihood ratio test= 215.3  on 9 df,   p=<2e-16
## Wald test            = 208.9  on 9 df,   p=<2e-16
## Score (logrank) test = 223.9  on 9 df,   p=<2e-16
# check the proportionality assumption:
zph <- cox.zph(mcox2, transform="km", global=TRUE)
zph
##             chisq df      p
## age         0.547  1 0.4596
## gender      1.482  1 0.2234
## los         0.216  1 0.6423
## ihd         7.699  1 0.0055
## vd          0.539  1 0.4629
## pneumonia   1.234  1 0.2666
## met_cancer  0.488  1 0.4848
## cog_imp     0.350  1 0.5539
## prior_dnas  0.171  1 0.6790
## GLOBAL     12.332  9 0.1953
ggcoxzph(zph)

# p-value of ihd is too low. I am going to remove it. 

mcox2 <- coxph(Surv(fu_time, death) ~ age + gender  + los   + vd +   pneumonia +  met_cancer + cog_imp  + prior_dnas)
summary(mcox2)
## Call:
## coxph(formula = Surv(fu_time, death) ~ age + gender + los + vd + 
##     pneumonia + met_cancer + cog_imp + prior_dnas)
## 
##   n= 1000, number of events= 492 
## 
##                  coef exp(coef)  se(coef)      z Pr(>|z|)    
## age          0.060063  1.061904  0.005632 10.665  < 2e-16 ***
## gender2     -0.290035  0.748237  0.094303 -3.076 0.002101 ** 
## los          0.011124  1.011187  0.003209  3.466 0.000528 ***
## vd1          0.198727  1.219849  0.107519  1.848 0.064558 .  
## pneumonia1   0.349217  1.417956  0.138080  2.529 0.011436 *  
## met_cancer1  2.501949 12.206264  0.364470  6.865 6.67e-12 ***
## cog_imp1     0.289850  1.336226  0.146679  1.976 0.048146 *  
## prior_dnas   0.115907  1.122892  0.037286  3.109 0.001880 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##             exp(coef) exp(-coef) lower .95 upper .95
## age            1.0619    0.94170    1.0502    1.0737
## gender2        0.7482    1.33647    0.6220    0.9001
## los            1.0112    0.98894    1.0048    1.0176
## vd1            1.2198    0.81977    0.9881    1.5060
## pneumonia1     1.4180    0.70524    1.0818    1.8586
## met_cancer1   12.2063    0.08193    5.9751   24.9356
## cog_imp1       1.3362    0.74838    1.0024    1.7813
## prior_dnas     1.1229    0.89056    1.0438    1.2080
## 
## Concordance= 0.699  (se = 0.012 )
## Likelihood ratio test= 211.3  on 8 df,   p=<2e-16
## Wald test            = 206  on 8 df,   p=<2e-16
## Score (logrank) test = 221.9  on 8 df,   p=<2e-16

Results:

In this survival analysis, factors affecting survival in Heart failure patients are: age (hazard: 1.06 per year, CI: 1.05 - 1.07, p-value: < 2e-16), gender-male as reference (hazard: 0.75 per year, CI: 0.62 - 0.9, p-value: 0.002), length of stay (los) (hazard: 1.01, CI: 1 - 1.02, p-value: 0.000), prior missed appointments (hazard: 1.12, CI: 1.04 - 1.2, p-value: 0.0019) and comorbidites: pneumonia (hazard: 0.35, CI: 1.08 - 1.86, p-value: 0.011), metastatic cancer (hazard: 12.21, CI: 5.98 - 24.94, p-value: 6.67e-12) and cognitive impairment (dementia and senility) (hazard: 1.34, CI: 1 - 1.78, p-value: 0.048).