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 oftimes. In case anumeric()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. IfNULLthen either set as-responseif available orlpif available (this assumes that thelpprediction comes from a PH type model - in case of an AFT model the user should provide-lp). In case neitherresponseorlpare provided, thencrankis calculated as the sum of the cumulative hazard function (expected mortality) derived from the predicted survival function (surv), see get_mortality. In casesurvis a 3d array, we use thewhich.curveparameter to decide which survival matrix (index in the 3rd dimension) will be chosen for the calculation ofcrank.- lp
(
numeric())
Predicted linear predictor, used to imputecrankifNULL.- response
(
numeric())
Predicted survival time, passed through function without modification.- which.curve
Which curve (3rd dimension) should the
crankbe calculated for, in casesurvis anarray? 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.9601220 0.9495659 0.9310957 0.9297803 0.9089725 0.9059234 0.8659402
#> [2,] 0.9909094 0.9711749 0.9581259 0.9530731 0.9479940 0.9216620 0.9101897
#> [3,] 0.9906870 0.9716406 0.9438836 0.9218941 0.9204260 0.9083477 0.8793162
#> [4,] 0.9759593 0.9563315 0.9376486 0.9339008 0.8849987 0.8796140 0.8763845
#> [5,] 0.9767449 0.9743528 0.9531728 0.9221718 0.9170467 0.8730264 0.8084914
#> [6,] 0.9516306 0.9338806 0.8878508 0.8756263 0.8708534 0.8285446 0.8207543
#> [7,] 0.9998447 0.9724284 0.9543264 0.9491880 0.9466628 0.9327012 0.9173127
#> [8,] 0.9675390 0.9142221 0.9027982 0.8899683 0.8669590 0.8573240 0.8519055
#> [9,] 0.9890795 0.9858608 0.9654728 0.9448536 0.9364885 0.9060088 0.8906392
#> [10,] 0.9730560 0.9487160 0.9428271 0.8863515 0.8691271 0.8617795 0.8433454
#> 8 9 10 11 12 13 14
#> [1,] 0.7845105 0.7808230 0.7722442 0.7568174 0.7449017 0.7434366 0.7299066
#> [2,] 0.8859200 0.8701071 0.8629188 0.8469389 0.7878905 0.7713004 0.7679921
#> [3,] 0.8777703 0.8706598 0.8702529 0.8655641 0.8636567 0.8618446 0.8303266
#> [4,] 0.8761189 0.8617826 0.8450412 0.7866003 0.7776508 0.7620051 0.7428142
#> [5,] 0.8022366 0.7925900 0.7505408 0.6651548 0.6597830 0.6439160 0.6341326
#> [6,] 0.8184838 0.7978042 0.7948741 0.7571857 0.7384526 0.7304661 0.6924004
#> [7,] 0.8908974 0.8901029 0.8789508 0.8037802 0.8019599 0.7530992 0.7356779
#> [8,] 0.8446583 0.8209465 0.8145745 0.8139582 0.7860832 0.7801263 0.7538980
#> [9,] 0.8769484 0.8678448 0.8513832 0.8342614 0.8155443 0.7738239 0.7723029
#> [10,] 0.8323254 0.8031215 0.7921495 0.7740206 0.7284863 0.7267992 0.7254787
#> 15 16 17 18 19 20 21
#> [1,] 0.7278502 0.7234531 0.7093392 0.6701434 0.6657371 0.6510970 0.6427980
#> [2,] 0.7413305 0.6865578 0.6800588 0.6695930 0.6687771 0.6097963 0.5976811
#> [3,] 0.7970099 0.7892563 0.7668244 0.7220036 0.6992279 0.6910307 0.6775991
#> [4,] 0.7265740 0.7201042 0.6947003 0.6810780 0.6742850 0.6703581 0.6642856
#> [5,] 0.6306245 0.6162566 0.6000014 0.5999056 0.5799658 0.5774133 0.5653502
#> [6,] 0.6799750 0.6768262 0.6596608 0.6564364 0.6482446 0.6364941 0.6063566
#> [7,] 0.7345690 0.7079882 0.7026167 0.7008201 0.6832273 0.6724583 0.6697346
#> [8,] 0.7456405 0.7409866 0.7335197 0.7171166 0.7097568 0.7029901 0.6264608
#> [9,] 0.7627677 0.7535266 0.7464305 0.7181508 0.7128408 0.7109861 0.6836407
#> [10,] 0.7202877 0.7200691 0.6875649 0.6678413 0.6427999 0.6356405 0.6339617
#> 22 23 24 25 26 27 28
#> [1,] 0.5918039 0.5407953 0.5359964 0.5132487 0.5118275 0.5017347 0.4959497
#> [2,] 0.5937847 0.5668495 0.5442056 0.5400753 0.4287755 0.4015737 0.3988924
#> [3,] 0.6176610 0.6006347 0.5468032 0.5403987 0.5360398 0.5266994 0.5265647
#> [4,] 0.6590303 0.6539911 0.6335639 0.6334734 0.5785536 0.5665604 0.5397839
#> [5,] 0.5533765 0.5188431 0.5051660 0.4706471 0.4670220 0.4182991 0.4027881
#> [6,] 0.5993099 0.5769722 0.5730236 0.5101347 0.4854569 0.4735402 0.4656578
#> [7,] 0.6557504 0.6552430 0.6535167 0.6023105 0.5844572 0.5584800 0.5514303
#> [8,] 0.5890740 0.5562464 0.5365677 0.5348811 0.5014067 0.4563751 0.4435792
#> [9,] 0.6361196 0.6157642 0.6034804 0.5856238 0.5825802 0.5328941 0.5242507
#> [10,] 0.5766697 0.5687415 0.5612325 0.5579791 0.4728650 0.4569324 0.4429400
#> 29 30 31 32 33 34 35
#> [1,] 0.4952824 0.4818725 0.4362780 0.3862390 0.3831350 0.3764240 0.3740602
#> [2,] 0.3933907 0.3909816 0.3880226 0.3836235 0.3725195 0.3703788 0.3533510
#> [3,] 0.5160231 0.5146263 0.4804464 0.4298022 0.4258216 0.3732882 0.3696564
#> [4,] 0.5182212 0.5047098 0.4873358 0.4777484 0.4726850 0.4131903 0.4003503
#> [5,] 0.4014162 0.3974846 0.3400726 0.2937668 0.2896496 0.2450084 0.2333460
#> [6,] 0.4648227 0.4549245 0.4113649 0.4103955 0.3881242 0.3751256 0.3658381
#> [7,] 0.5400497 0.5189208 0.4671363 0.4579581 0.3577982 0.3562040 0.3260143
#> [8,] 0.4413669 0.4340734 0.4325529 0.3813428 0.3676868 0.3409037 0.3250229
#> [9,] 0.4888591 0.4886697 0.4425418 0.4373961 0.4315009 0.4126689 0.3994742
#> [10,] 0.4188823 0.4134633 0.3879167 0.3877588 0.3635898 0.3613822 0.3285043
#> 36 37 38 39 40 41 42
#> [1,] 0.3724099 0.3239719 0.3216327 0.3182635 0.3013624 0.2232145 0.2198372
#> [2,] 0.3453656 0.3348520 0.3220942 0.2889920 0.2683252 0.2594262 0.2525974
#> [3,] 0.3671074 0.3302739 0.3143060 0.2804843 0.2395154 0.2378750 0.2104764
#> [4,] 0.3806560 0.3656421 0.3394798 0.2887132 0.2840410 0.2748368 0.2655549
#> [5,] 0.2320586 0.2106654 0.1950008 0.1790624 0.1697489 0.1582246 0.1547823
#> [6,] 0.3183309 0.2741671 0.2691391 0.2553422 0.1667015 0.1652511 0.1564482
#> [7,] 0.3250826 0.2523032 0.2113746 0.2083448 0.1875051 0.1530335 0.1446564
#> [8,] 0.2993645 0.2934558 0.2327759 0.2085001 0.2074240 0.1376902 0.1296571
#> [9,] 0.3961166 0.3459975 0.3400762 0.3218799 0.3187240 0.3164540 0.2510513
#> [10,] 0.3268932 0.3198173 0.3171567 0.3129703 0.2539149 0.2283809 0.2277949
#> 43 44 45 46 47 48
#> [1,] 0.2128982 0.20968921 0.18608095 0.13460232 0.13380890 0.08492077
#> [2,] 0.2455210 0.17148838 0.11294029 0.10099474 0.08856742 0.07020072
#> [3,] 0.1729949 0.16037901 0.10945988 0.07211711 0.01594701 0.01586487
#> [4,] 0.2526115 0.23996621 0.22342360 0.21542467 0.18668669 0.15412276
#> [5,] 0.1227994 0.11767881 0.07375568 0.05480521 0.03106433 0.02641530
#> [6,] 0.1494964 0.12622446 0.11845434 0.08512465 0.07912264 0.07774896
#> [7,] 0.1201565 0.08823581 0.08636744 0.06434619 0.05062812 0.01537042
#> [8,] 0.1198864 0.11920347 0.11850907 0.08127596 0.04255011 0.04026191
#> [9,] 0.2341565 0.10724452 0.07099984 0.07028271 0.06303511 0.02982706
#> [10,] 0.2185542 0.17954757 0.14382768 0.13430510 0.13334875 0.13306397
#> 49 50
#> [1,] 0.025457193 0.009273979
#> [2,] 0.049794795 0.014983522
#> [3,] 0.003352694 0.001643273
#> [4,] 0.075303067 0.002765834
#> [5,] 0.009306584 0.002801292
#> [6,] 0.048779228 0.029700955
#> [7,] 0.014147086 0.008609672
#> [8,] 0.033057781 0.032142362
#> [9,] 0.010023806 0.004280336
#> [10,] 0.085862566 0.072264937
#>
#> $crank
#> [1] 44.02741 44.85181 51.32371 39.55984 59.43421 47.36076 50.77521 49.21592
#> [9] 46.67155 41.95995
#>
#> $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.9601220 0.9495659 0.9310957 0.9297803 0.9089725 0.9059234 0.8659402
#> [2,] 0.9909094 0.9711749 0.9581259 0.9530731 0.9479940 0.9216620 0.9101897
#> [3,] 0.9906870 0.9716406 0.9438836 0.9218941 0.9204260 0.9083477 0.8793162
#> [4,] 0.9759593 0.9563315 0.9376486 0.9339008 0.8849987 0.8796140 0.8763845
#> [5,] 0.9767449 0.9743528 0.9531728 0.9221718 0.9170467 0.8730264 0.8084914
#> [6,] 0.9516306 0.9338806 0.8878508 0.8756263 0.8708534 0.8285446 0.8207543
#> [7,] 0.9998447 0.9724284 0.9543264 0.9491880 0.9466628 0.9327012 0.9173127
#> [8,] 0.9675390 0.9142221 0.9027982 0.8899683 0.8669590 0.8573240 0.8519055
#> [9,] 0.9890795 0.9858608 0.9654728 0.9448536 0.9364885 0.9060088 0.8906392
#> [10,] 0.9730560 0.9487160 0.9428271 0.8863515 0.8691271 0.8617795 0.8433454
#> 8 9 10 11 12 13 14
#> [1,] 0.7845105 0.7808230 0.7722442 0.7568174 0.7449017 0.7434366 0.7299066
#> [2,] 0.8859200 0.8701071 0.8629188 0.8469389 0.7878905 0.7713004 0.7679921
#> [3,] 0.8777703 0.8706598 0.8702529 0.8655641 0.8636567 0.8618446 0.8303266
#> [4,] 0.8761189 0.8617826 0.8450412 0.7866003 0.7776508 0.7620051 0.7428142
#> [5,] 0.8022366 0.7925900 0.7505408 0.6651548 0.6597830 0.6439160 0.6341326
#> [6,] 0.8184838 0.7978042 0.7948741 0.7571857 0.7384526 0.7304661 0.6924004
#> [7,] 0.8908974 0.8901029 0.8789508 0.8037802 0.8019599 0.7530992 0.7356779
#> [8,] 0.8446583 0.8209465 0.8145745 0.8139582 0.7860832 0.7801263 0.7538980
#> [9,] 0.8769484 0.8678448 0.8513832 0.8342614 0.8155443 0.7738239 0.7723029
#> [10,] 0.8323254 0.8031215 0.7921495 0.7740206 0.7284863 0.7267992 0.7254787
#> 15 16 17 18 19 20 21
#> [1,] 0.7278502 0.7234531 0.7093392 0.6701434 0.6657371 0.6510970 0.6427980
#> [2,] 0.7413305 0.6865578 0.6800588 0.6695930 0.6687771 0.6097963 0.5976811
#> [3,] 0.7970099 0.7892563 0.7668244 0.7220036 0.6992279 0.6910307 0.6775991
#> [4,] 0.7265740 0.7201042 0.6947003 0.6810780 0.6742850 0.6703581 0.6642856
#> [5,] 0.6306245 0.6162566 0.6000014 0.5999056 0.5799658 0.5774133 0.5653502
#> [6,] 0.6799750 0.6768262 0.6596608 0.6564364 0.6482446 0.6364941 0.6063566
#> [7,] 0.7345690 0.7079882 0.7026167 0.7008201 0.6832273 0.6724583 0.6697346
#> [8,] 0.7456405 0.7409866 0.7335197 0.7171166 0.7097568 0.7029901 0.6264608
#> [9,] 0.7627677 0.7535266 0.7464305 0.7181508 0.7128408 0.7109861 0.6836407
#> [10,] 0.7202877 0.7200691 0.6875649 0.6678413 0.6427999 0.6356405 0.6339617
#> 22 23 24 25 26 27 28
#> [1,] 0.5918039 0.5407953 0.5359964 0.5132487 0.5118275 0.5017347 0.4959497
#> [2,] 0.5937847 0.5668495 0.5442056 0.5400753 0.4287755 0.4015737 0.3988924
#> [3,] 0.6176610 0.6006347 0.5468032 0.5403987 0.5360398 0.5266994 0.5265647
#> [4,] 0.6590303 0.6539911 0.6335639 0.6334734 0.5785536 0.5665604 0.5397839
#> [5,] 0.5533765 0.5188431 0.5051660 0.4706471 0.4670220 0.4182991 0.4027881
#> [6,] 0.5993099 0.5769722 0.5730236 0.5101347 0.4854569 0.4735402 0.4656578
#> [7,] 0.6557504 0.6552430 0.6535167 0.6023105 0.5844572 0.5584800 0.5514303
#> [8,] 0.5890740 0.5562464 0.5365677 0.5348811 0.5014067 0.4563751 0.4435792
#> [9,] 0.6361196 0.6157642 0.6034804 0.5856238 0.5825802 0.5328941 0.5242507
#> [10,] 0.5766697 0.5687415 0.5612325 0.5579791 0.4728650 0.4569324 0.4429400
#> 29 30 31 32 33 34 35
#> [1,] 0.4952824 0.4818725 0.4362780 0.3862390 0.3831350 0.3764240 0.3740602
#> [2,] 0.3933907 0.3909816 0.3880226 0.3836235 0.3725195 0.3703788 0.3533510
#> [3,] 0.5160231 0.5146263 0.4804464 0.4298022 0.4258216 0.3732882 0.3696564
#> [4,] 0.5182212 0.5047098 0.4873358 0.4777484 0.4726850 0.4131903 0.4003503
#> [5,] 0.4014162 0.3974846 0.3400726 0.2937668 0.2896496 0.2450084 0.2333460
#> [6,] 0.4648227 0.4549245 0.4113649 0.4103955 0.3881242 0.3751256 0.3658381
#> [7,] 0.5400497 0.5189208 0.4671363 0.4579581 0.3577982 0.3562040 0.3260143
#> [8,] 0.4413669 0.4340734 0.4325529 0.3813428 0.3676868 0.3409037 0.3250229
#> [9,] 0.4888591 0.4886697 0.4425418 0.4373961 0.4315009 0.4126689 0.3994742
#> [10,] 0.4188823 0.4134633 0.3879167 0.3877588 0.3635898 0.3613822 0.3285043
#> 36 37 38 39 40 41 42
#> [1,] 0.3724099 0.3239719 0.3216327 0.3182635 0.3013624 0.2232145 0.2198372
#> [2,] 0.3453656 0.3348520 0.3220942 0.2889920 0.2683252 0.2594262 0.2525974
#> [3,] 0.3671074 0.3302739 0.3143060 0.2804843 0.2395154 0.2378750 0.2104764
#> [4,] 0.3806560 0.3656421 0.3394798 0.2887132 0.2840410 0.2748368 0.2655549
#> [5,] 0.2320586 0.2106654 0.1950008 0.1790624 0.1697489 0.1582246 0.1547823
#> [6,] 0.3183309 0.2741671 0.2691391 0.2553422 0.1667015 0.1652511 0.1564482
#> [7,] 0.3250826 0.2523032 0.2113746 0.2083448 0.1875051 0.1530335 0.1446564
#> [8,] 0.2993645 0.2934558 0.2327759 0.2085001 0.2074240 0.1376902 0.1296571
#> [9,] 0.3961166 0.3459975 0.3400762 0.3218799 0.3187240 0.3164540 0.2510513
#> [10,] 0.3268932 0.3198173 0.3171567 0.3129703 0.2539149 0.2283809 0.2277949
#> 43 44 45 46 47 48
#> [1,] 0.2128982 0.20968921 0.18608095 0.13460232 0.13380890 0.08492077
#> [2,] 0.2455210 0.17148838 0.11294029 0.10099474 0.08856742 0.07020072
#> [3,] 0.1729949 0.16037901 0.10945988 0.07211711 0.01594701 0.01586487
#> [4,] 0.2526115 0.23996621 0.22342360 0.21542467 0.18668669 0.15412276
#> [5,] 0.1227994 0.11767881 0.07375568 0.05480521 0.03106433 0.02641530
#> [6,] 0.1494964 0.12622446 0.11845434 0.08512465 0.07912264 0.07774896
#> [7,] 0.1201565 0.08823581 0.08636744 0.06434619 0.05062812 0.01537042
#> [8,] 0.1198864 0.11920347 0.11850907 0.08127596 0.04255011 0.04026191
#> [9,] 0.2341565 0.10724452 0.07099984 0.07028271 0.06303511 0.02982706
#> [10,] 0.2185542 0.17954757 0.14382768 0.13430510 0.13334875 0.13306397
#> 49 50
#> [1,] 0.025457193 0.009273979
#> [2,] 0.049794795 0.014983522
#> [3,] 0.003352694 0.001643273
#> [4,] 0.075303067 0.002765834
#> [5,] 0.009306584 0.002801292
#> [6,] 0.048779228 0.029700955
#> [7,] 0.014147086 0.008609672
#> [8,] 0.033057781 0.032142362
#> [9,] 0.010023806 0.004280336
#> [10,] 0.085862566 0.072264937
#>
#> $crank
#> [1] 0.71347890 0.29203135 -0.62077077 1.13898311 0.08287237 -1.06315674
#> [7] -0.77896631 0.07137724 -0.86559484 -1.12121210
#>
#> $lp
#> NULL
#>
#> $response
#> NULL
#>
# lp = crank
surv_return(lp = rnorm(n))
#> $distr
#> NULL
#>
#> $crank
#> [1] 1.3862977 1.5070593 0.4138559 -0.2266490 0.3427536 -1.1833491
#> [7] -0.2199551 2.2557620 -2.3596604 1.6125885
#>
#> $lp
#> [1] 1.3862977 1.5070593 0.4138559 -0.2266490 0.3427536 -1.1833491
#> [7] -0.2199551 2.2557620 -2.3596604 1.6125885
#>
#> $response
#> NULL
#>
# if response is set and no crank, crank = -response
surv_return(response = sample(1:100, n))
#> $distr
#> NULL
#>
#> $crank
#> [1] -74 -54 -28 -13 -49 -98 -69 -21 -95 -67
#>
#> $lp
#> NULL
#>
#> $response
#> [1] 74 54 28 13 49 98 69 21 95 67
#>
# if both are set, they are not overwritten
surv_return(crank = rnorm(n), response = sample(1:100, n))
#> $distr
#> NULL
#>
#> $crank
#> [1] 0.03569200 0.77028323 0.34967678 -0.04091514 -0.20360167 0.67497409
#> [7] -0.07562396 -0.06232395 -2.23704331 -0.84981797
#>
#> $lp
#> NULL
#>
#> $response
#> [1] 97 80 17 22 49 11 2 44 12 72
#>