0.1 Load data from spreadsheet

data <- read.csv("/Users/carolinaferreiraatuesta/Documents/UCL/Reserach Project/Thesis Databases/Database_thesis_final_R.csv")
dataset <- saveRDS(data, "data.rds")

ddist<-datadist(data)
options(datadist='ddist')

0.2 Create Table 1

tab1 <-
  CreateTableOne(data = data,
                 testApprox = chisq.test,
                 testExact = fisher.test)
cat <-
  c("sz",
    "auras",
    "drugs",
    "gtcs",
    "temp",
    "nosz",
    "MRI",
    "complete",
    "pathology_HS")
cont <- c("time_sz", "time_begin", "duration")
tab2 <- print(
  tab1,
  nonnormal = cont,
  smd = TRUE,
  missing = TRUE,
  testNormal = TRUE
)
##                                       
##                                        Overall              Missing
##   n                                      350                       
##   auras (mean (SD))                     0.06 (0.24)         0.0    
##   time_begin (median [IQR])             1.31 [0.88, 3.00]   0.0    
##   sz (mean (SD))                        0.30 (0.46)         0.0    
##   time_sz (median [IQR])                5.13 [2.05, 9.79]   0.0    
##   all (mean (SD))                       0.40 (0.49)         0.0    
##   all2 (mean (SD))                      0.60 (0.49)         0.0    
##   surgery_to_withdrawalall (mean (SD))  5.04 (3.91)         0.0    
##   begin_all_follow (mean (SD))          4.12 (3.90)         0.0    
##   duration (median [IQR])              21.74 [13.21, 29.74] 0.0    
##   duration_strat (mean (SD))            2.75 (1.25)         0.0    
##   drugs (mean (SD))                     2.38 (0.83)         0.0    
##   gtcs (mean (SD))                      0.67 (0.47)         0.0    
##   nosz (mean (SD))                     24.01 (53.45)        0.0    
##   temp (mean (SD))                      0.11 (0.31)         0.0    
##   MRI (mean (SD))                       0.96 (0.19)         0.0    
##   complete (mean (SD))                  0.00 (0.05)         0.0    
##   pathology_HS (mean (SD))              0.68 (0.47)         0.0    
##   withdrawalall (mean (SD))             0.40 (0.49)         0.0    
##   psychiatric_pre_any (mean (SD))       0.43 (0.50)         0.0    
##   age_onset (mean (SD))                12.37 (10.16)        0.0    
##   age_at_surgery (mean (SD))           34.89 (10.60)        0.0    
##   cure_last (mean (SD))                 0.33 (0.47)         0.0    
##   years_follow_up (mean (SD))          11.28 (6.20)         0.0

1 Predicting seizure relapse after AED withdrawal after surgery

Cohort: Seizure free before reduction Outcome: Seizure relapse

model1 <-
  coxph(
    Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + data$duration + data$drugs + data$gtcs + data$nosz +
      data$temp + data$MRI  + data$pathology_HS,
    x = TRUE,
    y = TRUE,
    data = data
  )
print(model1)
## Call:
## coxph(formula = Surv(time = data$time_sz, event = data$sz) ~ 
##     data$auras + data$time_begin + data$duration + data$drugs + 
##         data$gtcs + data$nosz + data$temp + data$MRI + data$pathology_HS, 
##     data = data, x = TRUE, y = TRUE)
## 
##                         coef  exp(coef)   se(coef)      z        p
## data$auras         1.344e+00  3.834e+00  3.159e-01  4.255 2.09e-05
## data$time_begin   -3.520e-02  9.654e-01  2.684e-02 -1.311    0.190
## data$duration      8.171e-03  1.008e+00  8.664e-03  0.943    0.346
## data$drugs         1.854e-01  1.204e+00  1.238e-01  1.498    0.134
## data$gtcs          3.492e-01  1.418e+00  2.265e-01  1.541    0.123
## data$nosz          5.138e-05  1.000e+00  1.971e-03  0.026    0.979
## data$temp         -3.173e-01  7.281e-01  4.145e-01 -0.766    0.444
## data$MRI          -5.347e-02  9.479e-01  6.170e-01 -0.087    0.931
## data$pathology_HS -7.492e-02  9.278e-01  2.747e-01 -0.273    0.785
## 
## Likelihood ratio test=21.11  on 9 df, p=0.01218
## n= 350, number of events= 106

1.1 Stepwise reduction using AIC

step(model1, direction = "both")
## Start:  AIC=1136.43
## Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + 
##     data$duration + data$drugs + data$gtcs + data$nosz + data$temp + 
##     data$MRI + data$pathology_HS
## 
##                     Df    AIC
## - data$nosz          1 1134.4
## - data$MRI           1 1134.4
## - data$pathology_HS  1 1134.5
## - data$temp          1 1135.0
## - data$duration      1 1135.3
## - data$time_begin    1 1136.3
## <none>                 1136.4
## - data$drugs         1 1136.6
## - data$gtcs          1 1136.9
## - data$auras         1 1148.7
## 
## Step:  AIC=1134.43
## Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + 
##     data$duration + data$drugs + data$gtcs + data$temp + data$MRI + 
##     data$pathology_HS
## 
##                     Df    AIC
## - data$MRI           1 1132.4
## - data$pathology_HS  1 1132.5
## - data$temp          1 1133.0
## - data$duration      1 1133.3
## - data$time_begin    1 1134.3
## <none>                 1134.4
## - data$drugs         1 1134.6
## - data$gtcs          1 1134.9
## + data$nosz          1 1136.4
## - data$auras         1 1146.7
## 
## Step:  AIC=1132.44
## Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + 
##     data$duration + data$drugs + data$gtcs + data$temp + data$pathology_HS
## 
##                     Df    AIC
## - data$pathology_HS  1 1130.5
## - data$temp          1 1131.1
## - data$duration      1 1131.3
## - data$time_begin    1 1132.3
## <none>                 1132.4
## - data$drugs         1 1132.6
## - data$gtcs          1 1133.0
## + data$MRI           1 1134.4
## + data$nosz          1 1134.4
## - data$auras         1 1144.7
## 
## Step:  AIC=1130.53
## Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + 
##     data$duration + data$drugs + data$gtcs + data$temp
## 
##                     Df    AIC
## - data$temp          1 1129.1
## - data$duration      1 1129.3
## - data$time_begin    1 1130.5
## <none>                 1130.5
## - data$drugs         1 1130.7
## - data$gtcs          1 1131.1
## + data$pathology_HS  1 1132.4
## + data$MRI           1 1132.5
## + data$nosz          1 1132.5
## - data$auras         1 1142.7
## 
## Step:  AIC=1129.09
## Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + 
##     data$duration + data$drugs + data$gtcs
## 
##                     Df    AIC
## - data$duration      1 1128.0
## - data$time_begin    1 1128.9
## - data$drugs         1 1128.9
## <none>                 1129.1
## - data$gtcs          1 1129.7
## + data$temp          1 1130.5
## + data$MRI           1 1131.1
## + data$pathology_HS  1 1131.1
## + data$nosz          1 1131.1
## - data$auras         1 1141.2
## 
## Step:  AIC=1128.05
## Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin + 
##     data$drugs + data$gtcs
## 
##                     Df    AIC
## <none>                 1128.0
## - data$drugs         1 1128.1
## - data$time_begin    1 1128.3
## - data$gtcs          1 1129.0
## + data$duration      1 1129.1
## + data$temp          1 1129.3
## + data$pathology_HS  1 1129.9
## + data$nosz          1 1130.0
## + data$MRI           1 1130.0
## - data$auras         1 1141.2
## Call:
## coxph(formula = Surv(time = data$time_sz, event = data$sz) ~ 
##     data$auras + data$time_begin + data$drugs + data$gtcs, data = data, 
##     x = TRUE, y = TRUE)
## 
##                     coef exp(coef) se(coef)      z        p
## data$auras       1.37550   3.95704  0.30905  4.451 8.56e-06
## data$time_begin -0.03769   0.96301  0.02636 -1.430   0.1528
## data$drugs       0.17264   1.18844  0.11772  1.467   0.1425
## data$gtcs        0.37628   1.45685  0.22418  1.678   0.0933
## 
## Likelihood ratio test=19.49  on 4 df, p=0.0006307
## n= 350, number of events= 106

1.2 Final model

model1f <-
  coxph(
    Surv(time = data$time_sz, event = data$sz) ~ data$auras + data$time_begin +  data$drugs + data$gtcs
  )
summary(model1f)
## Call:
## coxph(formula = Surv(time = data$time_sz, event = data$sz) ~ 
##     data$auras + data$time_begin + data$drugs + data$gtcs)
## 
##   n= 350, number of events= 106 
## 
##                     coef exp(coef) se(coef)      z Pr(>|z|)    
## data$auras       1.37550   3.95704  0.30905  4.451 8.56e-06 ***
## data$time_begin -0.03769   0.96301  0.02636 -1.430   0.1528    
## data$drugs       0.17264   1.18844  0.11772  1.467   0.1425    
## data$gtcs        0.37628   1.45685  0.22418  1.678   0.0933 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                 exp(coef) exp(-coef) lower .95 upper .95
## data$auras          3.957     0.2527    2.1593     7.252
## data$time_begin     0.963     1.0384    0.9145     1.014
## data$drugs          1.188     0.8414    0.9436     1.497
## data$gtcs           1.457     0.6864    0.9388     2.261
## 
## Concordance= 0.627  (se = 0.03 )
## Likelihood ratio test= 19.49  on 4 df,   p=6e-04
## Wald test            = 23.84  on 4 df,   p=9e-05
## Score (logrank) test = 25.38  on 4 df,   p=4e-05

``{r} library(Hmisc) x <- rcorrcens(formula = Surv(time_sz,sz)~ data\(auras + data\)time_begin + data\(drugs + data\)gtcs ,data=data) x

CstatisticCI <- function(x) { se <- x[“S.D.”]/sqrt(x[“n”]) Low95 <- x[“C Index”] - 1.96se Upper95 <- x[“C Index”] + 1.96se cbind(x[“C Index”], Low95, Upper95) }

CstatisticCI(model1fr)

set.seed(1)
x <- round(rnorm(200))
y <- rnorm(200)
rcorr.cens(x, y, outx=TRUE)   # can correlate non-censored variables
library(survival)
age <- rnorm(400, 50, 10)
bp  <- rnorm(400,120, 15)
bp[1]  <- NA
d.time <- rexp(400)
cens   <- runif(400,.5,2)
death  <- d.time <= cens
d.time <- pmin(d.time, cens)

out.rcorr <- rcorr.cens(age, Surv(d.time, death))  

CstatisticCI(out.rcorr)


1.3 Internal validadion

1.3.1 Discrimination

fitmodel1f <-
  cph(
    Surv(time = time_sz, event = sz) ~ auras + time_begin + drugs + gtcs,
    data = data,
    x = TRUE,
    y = TRUE,
    surv = TRUE,
    time.inc = 2
  )
fitmodel1f
## Cox Proportional Hazards Model
##  
##  cph(formula = Surv(time = time_sz, event = sz) ~ auras + time_begin + 
##      drugs + gtcs, data = data, x = TRUE, y = TRUE, surv = TRUE, 
##      time.inc = 2)
##  
##                      Model Tests       Discrimination    
##                                           Indexes        
##  Obs       350    LR chi2     19.49    R2       0.056    
##  Events    106    d.f.            4    Dxy      0.253    
##  Center 0.6434    Pr(> chi2) 0.0006    g        0.399    
##                   Score chi2  25.38    gr       1.490    
##                   Pr(> chi2) 0.0000                      
##  
##             Coef    S.E.   Wald Z Pr(>|Z|)
##  auras       1.3755 0.3090  4.45  <0.0001 
##  time_begin -0.0377 0.0264 -1.43  0.1528  
##  drugs       0.1726 0.1177  1.47  0.1425  
##  gtcs        0.3763 0.2242  1.68  0.0933  
## 
rms::validate(fitmodel1f, dxy = TRUE, B = 1000)
##       index.orig training   test optimism index.corrected    n
## Dxy       0.2530   0.2555 0.2264   0.0291          0.2239 1000
## R2        0.0563   0.0671 0.0484   0.0187          0.0376 1000
## Slope     1.0000   1.0000 0.8882   0.1118          0.8882 1000
## D         0.0162   0.0198 0.0138   0.0060          0.0102 1000
## U        -0.0018  -0.0018 0.0018  -0.0036          0.0019 1000
## Q         0.0180   0.0216 0.0119   0.0097          0.0083 1000
## g         0.3987   0.4422 0.3752   0.0670          0.3317 1000

1.3.2 Calibration plots

1.4 Nomogram

fitmodel1f$Design$label <-
  c(
    "SPSs before withdrawal (Yes=1, No=0)",
    "Time to begin withdrawal (Years from surgery)",
    "Number of AEDs at time of surgery",
    "GTCS before surgery (Yes=1, No=0)"
  )
surv.fitmodel1f <- Survival(fitmodel1f)
nom.cox1 <-
  nomogram(
    fitmodel1f,
    fun = list(function(x)
      surv.fitmodel1f(2, x), function(x)
        surv.fitmodel1f(5, x)),
    funlabel = c("2-year seizure freedom", "5-year seizure freedom"),
    lp = F,
    fun.at = c(0.99, 0.95, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2)
  )

tiff(
  "nomo1.tiff",
  width = cm(17),
  height = cm(10),
  units = 'cm',
  res = 300
)
nomo1<- plot(
  nom.cox1,
  cex.axis = 1,
  cex.var = 1,
  col.grid = gray(c(0.8, 0.95)),
  theme(text = element_text(
    family = "Times New Roman",
    face = "bold",
    size = 20
  ))
)
title(main = "Nomogram predicting risk of seizure relapse after beginnign AED withdrawal after epilepsy surgery", theme(text =
                                                                                                                          element_text(
                                                                                                                            family = "Times New Roman",
                                                                                                                            face = "bold",
                                                                                                                            size = 20
                                                                                                                          )))
dev.off()
## quartz_off_screen 
##                 2
nomo1
## NULL

1.5 Dynamic Nomogram

ddist <- datadist(data)
options(datadist = 'ddist')
web <-
  cph(Surv(time = time_sz, event = sz) ~ auras + time_begin + drugs + gtcs ,
      data = data)
DNbuilder(
  web,
  data = data,
  clevel = 0.95,
  covariate = c("numeric"),
  ptype = c("st")
)
## creating new directory: /Users/carolinaferreiraatuesta/R/DynNomapp
## Export dataset: /Users/carolinaferreiraatuesta/R/DynNomapp/dataset.RData
## Export functions: /Users/carolinaferreiraatuesta/R/DynNomapp/functions.R
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/README.txt
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/ui.R
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/server.R
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/global.R

2 Predicting chances of complete AED withdrawal after epilepsy surgery

Cohort: Seizure free before reduction Outcome: withdrawal all

model2 <-
  coxph(
    Surv(time = begin_all_follow, event = all) ~ auras + age_at_surgery + drugs + time_begin + pathology_HS + gtcs + MRI + psychiatric_pre_any + duration,
    data = data,
    x = TRUE,
    y = TRUE
  )
print(model2)
## Call:
## coxph(formula = Surv(time = begin_all_follow, event = all) ~ 
##     auras + age_at_surgery + drugs + time_begin + pathology_HS + 
##         gtcs + MRI + psychiatric_pre_any + duration, data = data, 
##     x = TRUE, y = TRUE)
## 
##                          coef exp(coef)  se(coef)      z       p
## auras               -0.497195  0.608234  0.439740 -1.131 0.25820
## age_at_surgery      -0.028425  0.971975  0.011276 -2.521 0.01171
## drugs               -0.360725  0.697171  0.117311 -3.075 0.00211
## time_begin          -0.066724  0.935453  0.032535 -2.051 0.04029
## pathology_HS        -0.085182  0.918345  0.194728 -0.437 0.66179
## gtcs                -0.046131  0.954917  0.180559 -0.255 0.79834
## MRI                  0.695083  2.003875  0.599016  1.160 0.24590
## psychiatric_pre_any -0.320041  0.726119  0.177625 -1.802 0.07158
## duration             0.009447  1.009492  0.010032  0.942 0.34631
## 
## Likelihood ratio test=31.41  on 9 df, p=0.0002516
## n= 350, number of events= 141
summary(model2)
## Call:
## coxph(formula = Surv(time = begin_all_follow, event = all) ~ 
##     auras + age_at_surgery + drugs + time_begin + pathology_HS + 
##         gtcs + MRI + psychiatric_pre_any + duration, data = data, 
##     x = TRUE, y = TRUE)
## 
##   n= 350, number of events= 141 
## 
##                          coef exp(coef)  se(coef)      z Pr(>|z|)   
## auras               -0.497195  0.608234  0.439740 -1.131  0.25820   
## age_at_surgery      -0.028425  0.971975  0.011276 -2.521  0.01171 * 
## drugs               -0.360725  0.697171  0.117311 -3.075  0.00211 **
## time_begin          -0.066724  0.935453  0.032535 -2.051  0.04029 * 
## pathology_HS        -0.085182  0.918345  0.194728 -0.437  0.66179   
## gtcs                -0.046131  0.954917  0.180559 -0.255  0.79834   
## MRI                  0.695083  2.003875  0.599016  1.160  0.24590   
## psychiatric_pre_any -0.320041  0.726119  0.177625 -1.802  0.07158 . 
## duration             0.009447  1.009492  0.010032  0.942  0.34631   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                     exp(coef) exp(-coef) lower .95 upper .95
## auras                  0.6082     1.6441    0.2569    1.4401
## age_at_surgery         0.9720     1.0288    0.9507    0.9937
## drugs                  0.6972     1.4344    0.5540    0.8774
## time_begin             0.9355     1.0690    0.8777    0.9970
## pathology_HS           0.9183     1.0889    0.6270    1.3451
## gtcs                   0.9549     1.0472    0.6703    1.3604
## MRI                    2.0039     0.4990    0.6194    6.4827
## psychiatric_pre_any    0.7261     1.3772    0.5126    1.0285
## duration               1.0095     0.9906    0.9898    1.0295
## 
## Concordance= 0.649  (se = 0.024 )
## Likelihood ratio test= 31.41  on 9 df,   p=3e-04
## Wald test            = 28.55  on 9 df,   p=8e-04
## Score (logrank) test = 29.22  on 9 df,   p=6e-04

2.1 Stepwise reduction using AIC

step(model2, direction = "both")
## Start:  AIC=1505.92
## Surv(time = begin_all_follow, event = all) ~ auras + age_at_surgery + 
##     drugs + time_begin + pathology_HS + gtcs + MRI + psychiatric_pre_any + 
##     duration
## 
##                       Df    AIC
## - gtcs                 1 1504.0
## - pathology_HS         1 1504.1
## - duration             1 1504.8
## - auras                1 1505.4
## - MRI                  1 1505.6
## <none>                   1505.9
## - psychiatric_pre_any  1 1507.2
## - time_begin           1 1508.7
## - age_at_surgery       1 1510.8
## - drugs                1 1513.9
## 
## Step:  AIC=1503.98
## Surv(time = begin_all_follow, event = all) ~ auras + age_at_surgery + 
##     drugs + time_begin + pathology_HS + MRI + psychiatric_pre_any + 
##     duration
## 
##                       Df    AIC
## - pathology_HS         1 1502.2
## - duration             1 1502.9
## - auras                1 1503.4
## - MRI                  1 1503.7
## <none>                   1504.0
## - psychiatric_pre_any  1 1505.3
## + gtcs                 1 1505.9
## - time_begin           1 1506.8
## - age_at_surgery       1 1508.8
## - drugs                1 1512.0
## 
## Step:  AIC=1502.19
## Surv(time = begin_all_follow, event = all) ~ auras + age_at_surgery + 
##     drugs + time_begin + MRI + psychiatric_pre_any + duration
## 
##                       Df    AIC
## - duration             1 1500.9
## - auras                1 1501.6
## - MRI                  1 1501.7
## <none>                   1502.2
## - psychiatric_pre_any  1 1503.7
## + pathology_HS         1 1504.0
## + gtcs                 1 1504.1
## - time_begin           1 1505.1
## - age_at_surgery       1 1506.8
## - drugs                1 1510.1
## 
## Step:  AIC=1500.89
## Surv(time = begin_all_follow, event = all) ~ auras + age_at_surgery + 
##     drugs + time_begin + MRI + psychiatric_pre_any
## 
##                       Df    AIC
## - auras                1 1500.2
## - MRI                  1 1500.6
## <none>                   1500.9
## + duration             1 1502.2
## - psychiatric_pre_any  1 1502.2
## + gtcs                 1 1502.8
## + pathology_HS         1 1502.9
## - time_begin           1 1503.5
## - age_at_surgery       1 1505.4
## - drugs                1 1508.2
## 
## Step:  AIC=1500.22
## Surv(time = begin_all_follow, event = all) ~ age_at_surgery + 
##     drugs + time_begin + MRI + psychiatric_pre_any
## 
##                       Df    AIC
## - MRI                  1 1500.0
## <none>                   1500.2
## + auras                1 1500.9
## + duration             1 1501.6
## - psychiatric_pre_any  1 1502.0
## + pathology_HS         1 1502.2
## + gtcs                 1 1502.2
## - time_begin           1 1505.5
## - age_at_surgery       1 1505.7
## - drugs                1 1506.9
## 
## Step:  AIC=1500
## Surv(time = begin_all_follow, event = all) ~ age_at_surgery + 
##     drugs + time_begin + psychiatric_pre_any
## 
##                       Df    AIC
## <none>                   1500.0
## + MRI                  1 1500.2
## + auras                1 1500.6
## + duration             1 1501.1
## - psychiatric_pre_any  1 1501.5
## + pathology_HS         1 1502.0
## + gtcs                 1 1502.0
## - time_begin           1 1505.3
## - age_at_surgery       1 1505.6
## - drugs                1 1507.0
## Call:
## coxph(formula = Surv(time = begin_all_follow, event = all) ~ 
##     age_at_surgery + drugs + time_begin + psychiatric_pre_any, 
##     data = data, x = TRUE, y = TRUE)
## 
##                          coef exp(coef)  se(coef)      z       p
## age_at_surgery      -0.023985  0.976301  0.008915 -2.691 0.00713
## drugs               -0.333390  0.716491  0.113360 -2.941 0.00327
## time_begin          -0.074550  0.928161  0.030598 -2.436 0.01483
## psychiatric_pre_any -0.322484  0.724347  0.175853 -1.834 0.06668
## 
## Likelihood ratio test=27.33  on 4 df, p=1.707e-05
## n= 350, number of events= 141

2.2 Final Model

model2f <-
  coxph(
    Surv(time = begin_all_follow, event = all) ~ age_at_surgery + drugs + time_begin + psychiatric_pre_any,
    data = data
  )
summary(model2f)
## Call:
## coxph(formula = Surv(time = begin_all_follow, event = all) ~ 
##     age_at_surgery + drugs + time_begin + psychiatric_pre_any, 
##     data = data)
## 
##   n= 350, number of events= 141 
## 
##                          coef exp(coef)  se(coef)      z Pr(>|z|)   
## age_at_surgery      -0.023985  0.976301  0.008915 -2.691  0.00713 **
## drugs               -0.333390  0.716491  0.113360 -2.941  0.00327 **
## time_begin          -0.074550  0.928161  0.030598 -2.436  0.01483 * 
## psychiatric_pre_any -0.322484  0.724347  0.175853 -1.834  0.06668 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                     exp(coef) exp(-coef) lower .95 upper .95
## age_at_surgery         0.9763      1.024    0.9594    0.9935
## drugs                  0.7165      1.396    0.5737    0.8948
## time_begin             0.9282      1.077    0.8741    0.9855
## psychiatric_pre_any    0.7243      1.381    0.5132    1.0224
## 
## Concordance= 0.64  (se = 0.025 )
## Likelihood ratio test= 27.33  on 4 df,   p=2e-05
## Wald test            = 25.38  on 4 df,   p=4e-05
## Score (logrank) test = 25.83  on 4 df,   p=3e-05

2.3 Internal validation

2.3.1 Discrimination

fitmodel2f <-
  cph(
    Surv(time = begin_all_follow, event = all) ~ age_at_surgery + drugs + time_begin + psychiatric_pre_any,
    data = data,
    surv = TRUE,
    x = TRUE,
    y = TRUE
  )
rms::validate(fitmodel2f,
              dxy = TRUE,
              B = 1000,
              u = 5)
##       index.orig training   test optimism index.corrected    n
## Dxy       0.2794   0.2900 0.2636   0.0264          0.2530 1000
## R2        0.0761   0.0873 0.0685   0.0189          0.0572 1000
## Slope     1.0000   1.0000 0.9058   0.0942          0.9058 1000
## D         0.0173   0.0202 0.0155   0.0048          0.0126 1000
## U        -0.0013  -0.0013 0.0010  -0.0023          0.0010 1000
## Q         0.0186   0.0216 0.0145   0.0070          0.0116 1000
## g         0.5577   0.5989 0.5260   0.0729          0.4848 1000

2.3.2 Calibration plots

2.4 Nonomogram 2

ddist <- datadist(data)
options(datadist = 'ddist')
fitmodel2f$Design$label <-
  c(
    "Age at surgery (Years)",
    "Number of AEDs at time of surgery",
    "Time to begin withdrawal (Years from surgery)",
    "Presurgical psychiatric comorbidity (Yes=1, No=0)"
  )
surv.fitmodel2f <- Survival(fitmodel2f)

nomo2 <-
  nomogram(
    fitmodel2f,
    fun = list(function(x)
      surv.fitmodel2f(2, x), function(x)
        surv.fitmodel2f(5, x)),
    funlabel = c(
      "2-years AED freedom (= 1-value)",
      "5-years AED freedom (= 1-value)"
    ),
    lp = F,
    fun.at = c(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2)
  )
tiff(
  "nomo2.2.tiff",
  width = cm(17),
  height = cm(10),
  units = 'cm',
  res = 300
)

plot(
  nomo2,
  cex.axis = 1,
  cex.var = 1,
  col.grid = gray(c(0.8, 0.95)),
  theme(text = element_text(
    family = "Times New Roman",
    face = "bold",
    size = 20
  ))
)
title(main = "Nomogram predicting risk of achieving complete AED withdrawal after epielpsy surgery", theme(text =
                                                                                                             element_text(
                                                                                                               family = "Times New Roman",
                                                                                                               face = "bold",
                                                                                                               size = 20
                                                                                                             )))
dev.off
## function (which = dev.cur()) 
## {
##     if (which == 1) 
##         stop("cannot shut down device 1 (the null device)")
##     .External(C_devoff, as.integer(which))
##     dev.cur()
## }
## <bytecode: 0x7fd90585d4f8>
## <environment: namespace:grDevices>

2.5 Dynamic Nomogram

web2 <-
  cph(
    Surv(time = begin_all_follow, event = all) ~ age_at_surgery + drugs + time_begin + psychiatric_pre_any,
    data = data
  )
DNbuilder(
  web2,
  data = data,
  clevel = 0.95,
  covariate = c("numeric"),
  ptype = c("1-st")
)
## creating new directory: /Users/carolinaferreiraatuesta/R/DynNomapp
## Export dataset: /Users/carolinaferreiraatuesta/R/DynNomapp/dataset.RData
## Export functions: /Users/carolinaferreiraatuesta/R/DynNomapp/functions.R
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/README.txt
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/ui.R
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/server.R
## writing file: /Users/carolinaferreiraatuesta/R/DynNomapp/global.R