Skip to contents

Internal helper function to easily return the correct survival predict types.

Usage

.surv_return(
  times = NULL,
  surv = NULL,
  crank = NULL,
  lp = NULL,
  response = NULL,
  which.curve = NULL
)

Arguments

times

(numeric())
Vector of survival times.

surv

(matrix()|array())
Matrix or array of predicted survival probabilities, rows (1st dimension) are observations, columns (2nd dimension) are times and in the case of an array there should be one more dimension. Number of columns should be equal to length of times. In case a numeric() vector is provided, it is converted to a single row (one observation) matrix.

crank

(numeric())
Relative risk/continuous ranking. Higher value is associated with higher risk. If NULL then either set as -response if available or lp if available (this assumes that the lp prediction comes from a PH type model - in case of an AFT model the user should provide -lp). In case neither response or lp are provided, then crank is calculated as the sum of the cumulative hazard function (expected mortality) derived from the predicted survival function (surv), see get_mortality. In case surv is a 3d array, we use the which.curve parameter to decide which survival matrix (index in the 3rd dimension) will be chosen for the calculation of crank.

lp

(numeric())
Predicted linear predictor, used to impute crank if NULL.

response

(numeric())
Predicted survival time, passed through function without modification.

which.curve

Which curve (3rd dimension) should the crank be calculated for, in case surv is an array? If between (0,1) it is taken as the quantile of the curves otherwise if greater than 1 it is taken as the curve index. It can also be 'mean' and the survival probabilities are averaged across the 3rd dimension. Default value (NULL) is the 0.5 quantile which is the median across the 3rd dimension of the survival array.

References

Sonabend, Raphael, Bender, Andreas, Vollmer, Sebastian (2022). “Avoiding C-hacking when evaluating survival distribution predictions with discrimination measures.” Bioinformatics. ISSN 1367-4803, doi:10.1093/BIOINFORMATICS/BTAC451 , https://academic.oup.com/bioinformatics/advance-article/doi/10.1093/bioinformatics/btac451/6640155.

Examples

n = 10 # number of observations
k = 50 # time points

# Create the matrix with random values between 0 and 1
mat = matrix(runif(n * k, min = 0, max = 1), nrow = n, ncol = k)

# transform it to a survival matrix
surv_mat = t(apply(mat, 1L, function(row) sort(row, decreasing = TRUE)))

# crank is expected mortality, distr is the survival matrix
.surv_return(times = 1:k, surv = surv_mat)
#> $distr
#>               1         2         3         4         5         6         7
#>  [1,] 0.9190243 0.9094204 0.9023741 0.8963784 0.8821565 0.8383750 0.8315819
#>  [2,] 0.9651191 0.9624163 0.9357124 0.9241712 0.8790108 0.8457036 0.7856838
#>  [3,] 0.9964129 0.9610269 0.9528096 0.9383424 0.9162296 0.9038774 0.8843691
#>  [4,] 0.9960334 0.9646256 0.9092984 0.8464711 0.8170736 0.7781341 0.7608931
#>  [5,] 0.9835116 0.9711406 0.9408706 0.9392060 0.8505534 0.8327498 0.8175131
#>  [6,] 0.9634940 0.9513960 0.9142980 0.8784999 0.8749583 0.8647057 0.7910454
#>  [7,] 0.9941947 0.9925807 0.9615714 0.9524294 0.9234934 0.9224745 0.9098439
#>  [8,] 0.9759655 0.9710409 0.9496892 0.9349984 0.9237896 0.8809570 0.8741768
#>  [9,] 0.9473172 0.9453343 0.9341725 0.9333584 0.8985678 0.8810192 0.8512050
#> [10,] 0.9934460 0.9317959 0.9136854 0.8737398 0.8703245 0.8474800 0.8427400
#>               8         9        10        11        12        13        14
#>  [1,] 0.8150977 0.7721615 0.7426239 0.7408466 0.7315666 0.7195228 0.7022510
#>  [2,] 0.7654656 0.7642949 0.7214050 0.6616399 0.6189772 0.5933208 0.5257314
#>  [3,] 0.8539796 0.8509742 0.8478029 0.7847097 0.7478482 0.7320372 0.7099068
#>  [4,] 0.7539531 0.7460717 0.7429240 0.7353102 0.7344441 0.7303385 0.7044640
#>  [5,] 0.7987365 0.7748016 0.7730603 0.7487380 0.7329705 0.7306002 0.7234506
#>  [6,] 0.7730277 0.7724088 0.7596780 0.7299699 0.7279052 0.7015503 0.6832633
#>  [7,] 0.8941871 0.8878725 0.8852476 0.8674180 0.8549595 0.8161877 0.8140452
#>  [8,] 0.8392650 0.8150232 0.7853741 0.7730265 0.7574071 0.7474663 0.7337463
#>  [9,] 0.8014918 0.7920109 0.7773062 0.7595991 0.7395896 0.7371237 0.7283057
#> [10,] 0.8284433 0.8128987 0.8097331 0.7997705 0.7927355 0.7691553 0.7392508
#>              15        16        17        18        19        20        21
#>  [1,] 0.6909178 0.6907191 0.6713953 0.6568564 0.6520543 0.6476439 0.6437388
#>  [2,] 0.5052717 0.4841195 0.4565847 0.4516670 0.4308361 0.3966740 0.3936717
#>  [3,] 0.6866767 0.6738564 0.6716483 0.6524795 0.6345200 0.6269666 0.6160618
#>  [4,] 0.6630767 0.6504853 0.5891424 0.5420758 0.5228954 0.5014234 0.4852220
#>  [5,] 0.7158094 0.6904052 0.6900072 0.6863414 0.6615740 0.6275976 0.6171816
#>  [6,] 0.6790934 0.6619993 0.6413834 0.6375847 0.6145241 0.5745623 0.5611449
#>  [7,] 0.7905741 0.7837275 0.7821541 0.7690480 0.7184372 0.7129650 0.6893664
#>  [8,] 0.7089125 0.7046719 0.7036071 0.6865140 0.6602534 0.6364466 0.6137436
#>  [9,] 0.6902049 0.6495377 0.6191940 0.6056477 0.5715608 0.5463676 0.5338427
#> [10,] 0.7087320 0.6799206 0.6779881 0.6579245 0.6370154 0.6356681 0.6022224
#>              22        23        24        25        26        27        28
#>  [1,] 0.6274892 0.6123770 0.6062636 0.5996537 0.5404310 0.5372664 0.5121368
#>  [2,] 0.3914532 0.3734108 0.3123294 0.3109595 0.2992035 0.2644819 0.2548184
#>  [3,] 0.5906431 0.5843084 0.5745444 0.5674063 0.5655572 0.4959910 0.4832982
#>  [4,] 0.4665975 0.4621038 0.4402233 0.4356070 0.4329577 0.4245976 0.3959981
#>  [5,] 0.5745329 0.5711436 0.5671314 0.5454252 0.5215289 0.5040124 0.4702427
#>  [6,] 0.5317419 0.4785984 0.4513363 0.4376551 0.4335948 0.3693408 0.3610061
#>  [7,] 0.6794794 0.6590830 0.6520002 0.6147532 0.6131014 0.5677488 0.5457234
#>  [8,] 0.6065589 0.5873467 0.5784610 0.5662714 0.5459154 0.4814845 0.4466452
#>  [9,] 0.5289571 0.5190610 0.5174852 0.4896883 0.4497768 0.4075939 0.4066971
#> [10,] 0.5926344 0.5416781 0.4800047 0.4665048 0.4481785 0.4213711 0.4142487
#>              29        30        31        32        33        34        35
#>  [1,] 0.4848310 0.4265054 0.3993062 0.3751405 0.3625163 0.3507959 0.3382199
#>  [2,] 0.2547738 0.2533116 0.2470779 0.2383798 0.2324572 0.2263226 0.2166260
#>  [3,] 0.4775874 0.4740412 0.4700358 0.4437336 0.4329308 0.4168978 0.4116800
#>  [4,] 0.3954480 0.3712700 0.3576113 0.3282673 0.2874570 0.2804005 0.2764568
#>  [5,] 0.4665094 0.4637440 0.4611052 0.4237044 0.3773430 0.3727811 0.3050461
#>  [6,] 0.3603482 0.3273132 0.3157314 0.3059903 0.2683308 0.2417888 0.2327210
#>  [7,] 0.4919181 0.4739806 0.3946066 0.3494275 0.3264299 0.3129833 0.2929688
#>  [8,] 0.4455378 0.4069197 0.3862684 0.3685591 0.3563146 0.3359689 0.3328170
#>  [9,] 0.3961379 0.3451553 0.3373486 0.3309320 0.3293803 0.3156294 0.3153969
#> [10,] 0.4124729 0.3758752 0.3187564 0.3063278 0.3062813 0.3033107 0.2598182
#>              36        37        38        39        40        41        42
#>  [1,] 0.3141783 0.2983446 0.2829591 0.2739955 0.1763501 0.1719961 0.1612276
#>  [2,] 0.2101353 0.2005600 0.1854464 0.1761117 0.1509289 0.1375330 0.1335062
#>  [3,] 0.3794024 0.3264005 0.3019302 0.3008545 0.2960282 0.2416559 0.2051782
#>  [4,] 0.2705845 0.2635015 0.2396383 0.2096216 0.1713852 0.1702637 0.1389265
#>  [5,] 0.2937588 0.2892817 0.1929435 0.1833201 0.1813332 0.1748682 0.1661618
#>  [6,] 0.2256654 0.2068019 0.1947964 0.1947595 0.1856538 0.1791275 0.1720781
#>  [7,] 0.2854513 0.2657936 0.2645395 0.2129675 0.1438034 0.1379704 0.1369292
#>  [8,] 0.3106024 0.3087645 0.2865388 0.2651872 0.2629099 0.2588608 0.2203064
#>  [9,] 0.3038334 0.2915964 0.2895593 0.2415937 0.2392738 0.2269574 0.2201635
#> [10,] 0.2433926 0.2284658 0.2169959 0.1936979 0.1777593 0.1729545 0.1143787
#>               43         44         45         46         47         48
#>  [1,] 0.15922928 0.14294366 0.10894967 0.07134267 0.06493924 0.04360233
#>  [2,] 0.12529780 0.11076854 0.11075900 0.10768937 0.07961901 0.06637027
#>  [3,] 0.19916067 0.19822812 0.19560864 0.12004143 0.11178991 0.09983606
#>  [4,] 0.13431220 0.12764111 0.12179664 0.09900971 0.08772024 0.08005309
#>  [5,] 0.16528379 0.16219870 0.13935932 0.10774453 0.09239664 0.03814079
#>  [6,] 0.15149556 0.13416555 0.10733189 0.07532395 0.06820935 0.04355700
#>  [7,] 0.11032985 0.08932224 0.07299444 0.07154974 0.04839823 0.02211705
#>  [8,] 0.21943974 0.21324813 0.18081600 0.11076174 0.10404637 0.07916641
#>  [9,] 0.19052512 0.14846305 0.14721379 0.14672756 0.12243392 0.05912893
#> [10,] 0.09120098 0.08443613 0.07758068 0.05018036 0.03852641 0.02764830
#>                49           50
#>  [1,] 0.034052273 0.0189061700
#>  [2,] 0.018531433 0.0146303358
#>  [3,] 0.097978055 0.0361323683
#>  [4,] 0.020153696 0.0070707756
#>  [5,] 0.029946458 0.0075055535
#>  [6,] 0.006291386 0.0024924283
#>  [7,] 0.021795763 0.0009579435
#>  [8,] 0.056558586 0.0150322786
#>  [9,] 0.019359473 0.0024007875
#> [10,] 0.019695889 0.0040819810
#> 
#> $crank
#>  [1] 48.38907 62.20738 40.80732 54.33467 49.11489 57.89427 52.62349 44.18092
#>  [9] 50.79840 56.84568
#> 
#> $lp
#> NULL
#> 
#> $response
#> NULL
#> 

# if crank is set, it's not overwritten
.surv_return(times = 1:k, surv = surv_mat, crank = rnorm(n))
#> $distr
#>               1         2         3         4         5         6         7
#>  [1,] 0.9190243 0.9094204 0.9023741 0.8963784 0.8821565 0.8383750 0.8315819
#>  [2,] 0.9651191 0.9624163 0.9357124 0.9241712 0.8790108 0.8457036 0.7856838
#>  [3,] 0.9964129 0.9610269 0.9528096 0.9383424 0.9162296 0.9038774 0.8843691
#>  [4,] 0.9960334 0.9646256 0.9092984 0.8464711 0.8170736 0.7781341 0.7608931
#>  [5,] 0.9835116 0.9711406 0.9408706 0.9392060 0.8505534 0.8327498 0.8175131
#>  [6,] 0.9634940 0.9513960 0.9142980 0.8784999 0.8749583 0.8647057 0.7910454
#>  [7,] 0.9941947 0.9925807 0.9615714 0.9524294 0.9234934 0.9224745 0.9098439
#>  [8,] 0.9759655 0.9710409 0.9496892 0.9349984 0.9237896 0.8809570 0.8741768
#>  [9,] 0.9473172 0.9453343 0.9341725 0.9333584 0.8985678 0.8810192 0.8512050
#> [10,] 0.9934460 0.9317959 0.9136854 0.8737398 0.8703245 0.8474800 0.8427400
#>               8         9        10        11        12        13        14
#>  [1,] 0.8150977 0.7721615 0.7426239 0.7408466 0.7315666 0.7195228 0.7022510
#>  [2,] 0.7654656 0.7642949 0.7214050 0.6616399 0.6189772 0.5933208 0.5257314
#>  [3,] 0.8539796 0.8509742 0.8478029 0.7847097 0.7478482 0.7320372 0.7099068
#>  [4,] 0.7539531 0.7460717 0.7429240 0.7353102 0.7344441 0.7303385 0.7044640
#>  [5,] 0.7987365 0.7748016 0.7730603 0.7487380 0.7329705 0.7306002 0.7234506
#>  [6,] 0.7730277 0.7724088 0.7596780 0.7299699 0.7279052 0.7015503 0.6832633
#>  [7,] 0.8941871 0.8878725 0.8852476 0.8674180 0.8549595 0.8161877 0.8140452
#>  [8,] 0.8392650 0.8150232 0.7853741 0.7730265 0.7574071 0.7474663 0.7337463
#>  [9,] 0.8014918 0.7920109 0.7773062 0.7595991 0.7395896 0.7371237 0.7283057
#> [10,] 0.8284433 0.8128987 0.8097331 0.7997705 0.7927355 0.7691553 0.7392508
#>              15        16        17        18        19        20        21
#>  [1,] 0.6909178 0.6907191 0.6713953 0.6568564 0.6520543 0.6476439 0.6437388
#>  [2,] 0.5052717 0.4841195 0.4565847 0.4516670 0.4308361 0.3966740 0.3936717
#>  [3,] 0.6866767 0.6738564 0.6716483 0.6524795 0.6345200 0.6269666 0.6160618
#>  [4,] 0.6630767 0.6504853 0.5891424 0.5420758 0.5228954 0.5014234 0.4852220
#>  [5,] 0.7158094 0.6904052 0.6900072 0.6863414 0.6615740 0.6275976 0.6171816
#>  [6,] 0.6790934 0.6619993 0.6413834 0.6375847 0.6145241 0.5745623 0.5611449
#>  [7,] 0.7905741 0.7837275 0.7821541 0.7690480 0.7184372 0.7129650 0.6893664
#>  [8,] 0.7089125 0.7046719 0.7036071 0.6865140 0.6602534 0.6364466 0.6137436
#>  [9,] 0.6902049 0.6495377 0.6191940 0.6056477 0.5715608 0.5463676 0.5338427
#> [10,] 0.7087320 0.6799206 0.6779881 0.6579245 0.6370154 0.6356681 0.6022224
#>              22        23        24        25        26        27        28
#>  [1,] 0.6274892 0.6123770 0.6062636 0.5996537 0.5404310 0.5372664 0.5121368
#>  [2,] 0.3914532 0.3734108 0.3123294 0.3109595 0.2992035 0.2644819 0.2548184
#>  [3,] 0.5906431 0.5843084 0.5745444 0.5674063 0.5655572 0.4959910 0.4832982
#>  [4,] 0.4665975 0.4621038 0.4402233 0.4356070 0.4329577 0.4245976 0.3959981
#>  [5,] 0.5745329 0.5711436 0.5671314 0.5454252 0.5215289 0.5040124 0.4702427
#>  [6,] 0.5317419 0.4785984 0.4513363 0.4376551 0.4335948 0.3693408 0.3610061
#>  [7,] 0.6794794 0.6590830 0.6520002 0.6147532 0.6131014 0.5677488 0.5457234
#>  [8,] 0.6065589 0.5873467 0.5784610 0.5662714 0.5459154 0.4814845 0.4466452
#>  [9,] 0.5289571 0.5190610 0.5174852 0.4896883 0.4497768 0.4075939 0.4066971
#> [10,] 0.5926344 0.5416781 0.4800047 0.4665048 0.4481785 0.4213711 0.4142487
#>              29        30        31        32        33        34        35
#>  [1,] 0.4848310 0.4265054 0.3993062 0.3751405 0.3625163 0.3507959 0.3382199
#>  [2,] 0.2547738 0.2533116 0.2470779 0.2383798 0.2324572 0.2263226 0.2166260
#>  [3,] 0.4775874 0.4740412 0.4700358 0.4437336 0.4329308 0.4168978 0.4116800
#>  [4,] 0.3954480 0.3712700 0.3576113 0.3282673 0.2874570 0.2804005 0.2764568
#>  [5,] 0.4665094 0.4637440 0.4611052 0.4237044 0.3773430 0.3727811 0.3050461
#>  [6,] 0.3603482 0.3273132 0.3157314 0.3059903 0.2683308 0.2417888 0.2327210
#>  [7,] 0.4919181 0.4739806 0.3946066 0.3494275 0.3264299 0.3129833 0.2929688
#>  [8,] 0.4455378 0.4069197 0.3862684 0.3685591 0.3563146 0.3359689 0.3328170
#>  [9,] 0.3961379 0.3451553 0.3373486 0.3309320 0.3293803 0.3156294 0.3153969
#> [10,] 0.4124729 0.3758752 0.3187564 0.3063278 0.3062813 0.3033107 0.2598182
#>              36        37        38        39        40        41        42
#>  [1,] 0.3141783 0.2983446 0.2829591 0.2739955 0.1763501 0.1719961 0.1612276
#>  [2,] 0.2101353 0.2005600 0.1854464 0.1761117 0.1509289 0.1375330 0.1335062
#>  [3,] 0.3794024 0.3264005 0.3019302 0.3008545 0.2960282 0.2416559 0.2051782
#>  [4,] 0.2705845 0.2635015 0.2396383 0.2096216 0.1713852 0.1702637 0.1389265
#>  [5,] 0.2937588 0.2892817 0.1929435 0.1833201 0.1813332 0.1748682 0.1661618
#>  [6,] 0.2256654 0.2068019 0.1947964 0.1947595 0.1856538 0.1791275 0.1720781
#>  [7,] 0.2854513 0.2657936 0.2645395 0.2129675 0.1438034 0.1379704 0.1369292
#>  [8,] 0.3106024 0.3087645 0.2865388 0.2651872 0.2629099 0.2588608 0.2203064
#>  [9,] 0.3038334 0.2915964 0.2895593 0.2415937 0.2392738 0.2269574 0.2201635
#> [10,] 0.2433926 0.2284658 0.2169959 0.1936979 0.1777593 0.1729545 0.1143787
#>               43         44         45         46         47         48
#>  [1,] 0.15922928 0.14294366 0.10894967 0.07134267 0.06493924 0.04360233
#>  [2,] 0.12529780 0.11076854 0.11075900 0.10768937 0.07961901 0.06637027
#>  [3,] 0.19916067 0.19822812 0.19560864 0.12004143 0.11178991 0.09983606
#>  [4,] 0.13431220 0.12764111 0.12179664 0.09900971 0.08772024 0.08005309
#>  [5,] 0.16528379 0.16219870 0.13935932 0.10774453 0.09239664 0.03814079
#>  [6,] 0.15149556 0.13416555 0.10733189 0.07532395 0.06820935 0.04355700
#>  [7,] 0.11032985 0.08932224 0.07299444 0.07154974 0.04839823 0.02211705
#>  [8,] 0.21943974 0.21324813 0.18081600 0.11076174 0.10404637 0.07916641
#>  [9,] 0.19052512 0.14846305 0.14721379 0.14672756 0.12243392 0.05912893
#> [10,] 0.09120098 0.08443613 0.07758068 0.05018036 0.03852641 0.02764830
#>                49           50
#>  [1,] 0.034052273 0.0189061700
#>  [2,] 0.018531433 0.0146303358
#>  [3,] 0.097978055 0.0361323683
#>  [4,] 0.020153696 0.0070707756
#>  [5,] 0.029946458 0.0075055535
#>  [6,] 0.006291386 0.0024924283
#>  [7,] 0.021795763 0.0009579435
#>  [8,] 0.056558586 0.0150322786
#>  [9,] 0.019359473 0.0024007875
#> [10,] 0.019695889 0.0040819810
#> 
#> $crank
#>  [1] -0.3164416  1.6688489 -0.1430424  1.5957467 -0.1114378 -1.0840001
#>  [7] -0.2386753 -1.9588298  0.6190933  0.3341261
#> 
#> $lp
#> NULL
#> 
#> $response
#> NULL
#> 

# lp = crank
.surv_return(lp = rnorm(n))
#> $distr
#> NULL
#> 
#> $crank
#>  [1] -1.0420591  1.0392545 -0.6656110  0.8034734 -1.5173197 -0.8383099
#>  [7]  0.3955462  0.2445431  2.6172969 -0.7378764
#> 
#> $lp
#>  [1] -1.0420591  1.0392545 -0.6656110  0.8034734 -1.5173197 -0.8383099
#>  [7]  0.3955462  0.2445431  2.6172969 -0.7378764
#> 
#> $response
#> NULL
#> 

# if response is set and no crank, crank = -response
.surv_return(response = sample(1:100, n))
#> $distr
#> NULL
#> 
#> $crank
#>  [1] -18 -47 -76 -94 -70 -55 -14 -68 -91 -87
#> 
#> $lp
#> NULL
#> 
#> $response
#>  [1] 18 47 76 94 70 55 14 68 91 87
#> 

# if both are set, they are not overwritten
.surv_return(crank = rnorm(n), response = sample(1:100, n))
#> $distr
#> NULL
#> 
#> $crank
#>  [1]  0.4612446 -1.0749610 -0.2496090 -0.4884539  0.4662627  0.2233549
#>  [7] -0.5657645  0.1276178  0.3680300  0.6089351
#> 
#> $lp
#> NULL
#> 
#> $response
#>  [1]  95  37  34  30 100  97  56  43  16  80
#>