
Get Survival Predict Types
surv_return.Rd
Internal helper function to easily return the correct survival predict types.
This should be used by all survival mlr3 learners, to ensure that the crank
prediction type is always included.
Usage
surv_return(
times = NULL,
surv = NULL,
crank = NULL,
lp = NULL,
response = NULL,
data_type = "surv",
interp_meth = "const_surv"
)
Arguments
- times
(
numeric()
|NULL
)
Vector of survival times. If times isNULL
, it will be inferred fromcolnames(surv)
.- surv
(
matrix()
)
Matrix of predicted survival probabilities, rows (1st dimension) are observations, columns (2nd dimension) are time points. Number of columns should be equal to length oftimes
. Several checks are performed with assert_prob_matrix.- crank
(
numeric()
)
Relative risk/continuous ranking. Higher value is associated with higher risk. IfNULL
then either set as-response
if available orlp
if available (this assumes that thelp
prediction comes from a PH type model - in case of an AFT model the user should provide-lp
). In case neitherresponse
orlp
are provided, thencrank
is calculated as the sum of the cumulative hazard function (expected mortality) derived from the predicted survival function (surv
), see get_mortality.- lp
(
numeric()
)
Predicted linear predictor, used to imputecrank
if the latter isNULL
.- response
(
numeric()
)
Predicted survival time, passed through function without modification.- data_type
(
character(1)
)
Data type forsurv
. Either"surv"
for survival or"haz"
for hazard matrices.- interp_meth
(
character(1)
)
Interpolation type; one of"const_surv"
,"linear_surv"
, or"const_haz"
. This is needed for conversion to a survDistr object.
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.9955685 0.9610665 0.9554548 0.9447273 0.9409626 0.9234934 0.9153843
#> [2,] 0.9920600 0.9759655 0.9539570 0.9156397 0.9146216 0.8803851 0.8610232
#> [3,] 0.9081511 0.8835293 0.8660587 0.8512050 0.8014918 0.7920109 0.7692867
#> [4,] 0.9934460 0.9603136 0.9198523 0.9125235 0.8451152 0.8427400 0.8284433
#> [5,] 0.9813836 0.9694043 0.9025228 0.9023741 0.8356563 0.7942700 0.7529222
#> [6,] 0.9964527 0.9884856 0.9848897 0.9375977 0.9157621 0.8889144 0.8790108
#> [7,] 0.9610269 0.9524263 0.9383424 0.9283513 0.8825434 0.8707106 0.8631875
#> [8,] 0.9898985 0.9849717 0.9461507 0.9190942 0.9180984 0.9092984 0.8877611
#> [9,] 0.9802742 0.9711406 0.9408706 0.9294002 0.8900840 0.8327498 0.8268267
#> [10,] 0.9236206 0.9130971 0.8749583 0.8647057 0.8187860 0.8162950 0.8072527
#> 8 9 10 11 12 13 14
#> [1,] 0.8941871 0.8860739 0.8742160 0.8674180 0.8410537 0.8198010 0.7905741
#> [2,] 0.8024997 0.7853741 0.7730265 0.7574071 0.7531261 0.7474663 0.7281897
#> [3,] 0.7224122 0.7102736 0.6902049 0.6899707 0.6495377 0.6405322 0.6308578
#> [4,] 0.8239078 0.8128987 0.8097331 0.7997705 0.7691553 0.7480038 0.7121408
#> [5,] 0.7408466 0.7315666 0.7250983 0.7230918 0.7204667 0.7022510 0.6846876
#> [6,] 0.8246223 0.8141156 0.8132309 0.8030951 0.7856838 0.7816516 0.7642949
#> [7,] 0.8539796 0.8506568 0.8478029 0.8299002 0.8067556 0.7954503 0.7505098
#> [8,] 0.8555177 0.8464711 0.8453551 0.7713282 0.7353102 0.7088059 0.7061405
#> [9,] 0.7950654 0.7696836 0.7677212 0.7636785 0.7306002 0.7287163 0.7094167
#> [10,] 0.7967724 0.7910454 0.7523420 0.7363630 0.7299699 0.7277044 0.7112888
#> 15 16 17 18 19 20 21
#> [1,] 0.7891494 0.7832957 0.7831176 0.7320725 0.7196247 0.7184372 0.7129650
#> [2,] 0.7088456 0.7036071 0.6942461 0.6613904 0.6602534 0.6218533 0.6137436
#> [3,] 0.6191940 0.6143616 0.6032756 0.5422356 0.4896883 0.4698937 0.4685521
#> [4,] 0.6988863 0.6727037 0.6706232 0.6579245 0.6309976 0.5926344 0.5453928
#> [5,] 0.6520543 0.6469287 0.6274892 0.6062636 0.5996537 0.5551521 0.5507743
#> [6,] 0.7232087 0.6964027 0.6853092 0.6642060 0.6616399 0.6414824 0.6412099
#> [7,] 0.7320372 0.6980541 0.6866767 0.6537801 0.6451109 0.6435746 0.6340869
#> [8,] 0.6991565 0.6630767 0.6043650 0.5891424 0.5541124 0.5511978 0.4852220
#> [9,] 0.6863414 0.6794863 0.6275976 0.6223692 0.6171816 0.5965949 0.5745329
#> [10,] 0.6939081 0.6832633 0.6790934 0.6721112 0.6619993 0.6567115 0.6453137
#> 22 23 24 25 26 27 28
#> [1,] 0.6893664 0.6794794 0.6776885 0.6591100 0.6590830 0.6296410 0.5883703
#> [2,] 0.5922085 0.5801826 0.5194664 0.4980553 0.4919785 0.4466452 0.4272466
#> [3,] 0.4556346 0.4480606 0.3856759 0.3819871 0.3641701 0.3583615 0.3451553
#> [4,] 0.5416781 0.5135930 0.5123930 0.5113113 0.4919778 0.4800047 0.4793860
#> [5,] 0.5372664 0.5195376 0.4928372 0.4848310 0.4072438 0.4014449 0.3758337
#> [6,] 0.6397781 0.5933208 0.5616738 0.5254825 0.5150504 0.5052717 0.4565150
#> [7,] 0.6189364 0.6054746 0.6008156 0.5655572 0.4913631 0.4700358 0.4433637
#> [8,] 0.4814955 0.4356070 0.4329577 0.4134435 0.3576113 0.3532520 0.3386323
#> [9,] 0.5711436 0.5424376 0.5017637 0.4978482 0.4733496 0.4702427 0.4681660
#> [10,] 0.6375847 0.6193783 0.6006700 0.5611449 0.5553265 0.5484002 0.5424417
#> 29 30 31 32 33 34 35
#> [1,] 0.5618213 0.4919181 0.3905596 0.3527889 0.3129833 0.2854513 0.2645395
#> [2,] 0.4094238 0.4091068 0.4069197 0.3862684 0.3574005 0.3370598 0.3328170
#> [3,] 0.3156294 0.2915964 0.2857769 0.2792125 0.2488301 0.2302948 0.2279427
#> [4,] 0.4513757 0.4481785 0.4353852 0.4344885 0.4176636 0.3525640 0.3115541
#> [5,] 0.3440131 0.3253989 0.3141783 0.2960687 0.2739955 0.2433217 0.2202471
#> [6,] 0.4516670 0.4380257 0.4308361 0.4260052 0.3298215 0.3062329 0.2981795
#> [7,] 0.4168978 0.4056787 0.3995427 0.3813287 0.3794024 0.3650372 0.3126142
#> [8,] 0.3254810 0.3061836 0.2705845 0.2635015 0.2624227 0.2478493 0.2337346
#> [9,] 0.4637440 0.4611052 0.4600282 0.4431284 0.3727811 0.3548055 0.3449816
#> [10,] 0.4197834 0.3781626 0.3603482 0.3302254 0.3013423 0.2704266 0.2327210
#> 36 37 38 39 40 41 42
#> [1,] 0.1945824 0.1925987 0.1606107 0.1516763 0.1369292 0.09768965 0.09394605
#> [2,] 0.3106024 0.3087645 0.2927387 0.2688736 0.2588608 0.22030637 0.21324813
#> [3,] 0.2269574 0.2249139 0.2201635 0.2139898 0.2066011 0.14721379 0.14672756
#> [4,] 0.3063278 0.3012543 0.2365209 0.2200902 0.1936979 0.19244075 0.17295454
#> [5,] 0.2009283 0.1897889 0.1809801 0.1763501 0.1719961 0.16122764 0.14869215
#> [6,] 0.2954092 0.2797018 0.2614054 0.2263226 0.2161579 0.20345269 0.17611169
#> [7,] 0.3008545 0.2822754 0.2416559 0.2326897 0.2090516 0.20517822 0.19822812
#> [8,] 0.2083617 0.2083168 0.1789609 0.1713852 0.1682065 0.14983401 0.13431220
#> [9,] 0.3050461 0.3050290 0.2892817 0.2693460 0.2580382 0.25282988 0.24252311
#> [10,] 0.2256654 0.1947964 0.1720781 0.1701358 0.1660048 0.15376385 0.13416555
#> 43 44 45 46 47 48
#> [1,] 0.09189839 0.08932224 0.07009002 0.06551847 0.04839823 0.02179576
#> [2,] 0.21251409 0.17147829 0.14050187 0.11076174 0.10590877 0.05871596
#> [3,] 0.14119609 0.12243392 0.10452654 0.06459299 0.05912893 0.04781616
#> [4,] 0.16481841 0.15062109 0.12640189 0.07758068 0.06544498 0.05018036
#> [5,] 0.13918238 0.07432107 0.07183850 0.06493924 0.06336721 0.06013087
#> [6,] 0.13753298 0.13350623 0.12529780 0.11076854 0.11075900 0.06637027
#> [7,] 0.19560864 0.17457635 0.12544662 0.10769635 0.09983606 0.08827764
#> [8,] 0.12764111 0.08005309 0.06906200 0.06165471 0.04055868 0.02154078
#> [9,] 0.22826087 0.18670618 0.18133320 0.13096989 0.10037212 0.02994646
#> [10,] 0.07532395 0.06820935 0.06262987 0.06116300 0.05044214 0.02753508
#> 49 50
#> [1,] 0.01622159 0.0009579435
#> [2,] 0.02370328 0.0180867743
#> [3,] 0.01935947 0.0024007875
#> [4,] 0.02764830 0.0084195950
#> [5,] 0.04722266 0.0436023297
#> [6,] 0.03718221 0.0146303358
#> [7,] 0.08054491 0.0361323683
#> [8,] 0.02015370 0.0070707756
#> [9,] 0.02510966 0.0250663597
#> [10,] 0.02280205 0.0109879340
#>
#> $crank
#> [1] 55.30503 46.52040 60.49891 49.56963 54.97303 46.80817 44.34776 59.74379
#> [9] 46.11500 55.68412
#>
#> $lp
#> NULL
#>
#> $response
#> NULL
#>
#> attr(,"data_type")
#> [1] "surv"
#> attr(,"interp_meth")
#> [1] "const_surv"
# 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.9955685 0.9610665 0.9554548 0.9447273 0.9409626 0.9234934 0.9153843
#> [2,] 0.9920600 0.9759655 0.9539570 0.9156397 0.9146216 0.8803851 0.8610232
#> [3,] 0.9081511 0.8835293 0.8660587 0.8512050 0.8014918 0.7920109 0.7692867
#> [4,] 0.9934460 0.9603136 0.9198523 0.9125235 0.8451152 0.8427400 0.8284433
#> [5,] 0.9813836 0.9694043 0.9025228 0.9023741 0.8356563 0.7942700 0.7529222
#> [6,] 0.9964527 0.9884856 0.9848897 0.9375977 0.9157621 0.8889144 0.8790108
#> [7,] 0.9610269 0.9524263 0.9383424 0.9283513 0.8825434 0.8707106 0.8631875
#> [8,] 0.9898985 0.9849717 0.9461507 0.9190942 0.9180984 0.9092984 0.8877611
#> [9,] 0.9802742 0.9711406 0.9408706 0.9294002 0.8900840 0.8327498 0.8268267
#> [10,] 0.9236206 0.9130971 0.8749583 0.8647057 0.8187860 0.8162950 0.8072527
#> 8 9 10 11 12 13 14
#> [1,] 0.8941871 0.8860739 0.8742160 0.8674180 0.8410537 0.8198010 0.7905741
#> [2,] 0.8024997 0.7853741 0.7730265 0.7574071 0.7531261 0.7474663 0.7281897
#> [3,] 0.7224122 0.7102736 0.6902049 0.6899707 0.6495377 0.6405322 0.6308578
#> [4,] 0.8239078 0.8128987 0.8097331 0.7997705 0.7691553 0.7480038 0.7121408
#> [5,] 0.7408466 0.7315666 0.7250983 0.7230918 0.7204667 0.7022510 0.6846876
#> [6,] 0.8246223 0.8141156 0.8132309 0.8030951 0.7856838 0.7816516 0.7642949
#> [7,] 0.8539796 0.8506568 0.8478029 0.8299002 0.8067556 0.7954503 0.7505098
#> [8,] 0.8555177 0.8464711 0.8453551 0.7713282 0.7353102 0.7088059 0.7061405
#> [9,] 0.7950654 0.7696836 0.7677212 0.7636785 0.7306002 0.7287163 0.7094167
#> [10,] 0.7967724 0.7910454 0.7523420 0.7363630 0.7299699 0.7277044 0.7112888
#> 15 16 17 18 19 20 21
#> [1,] 0.7891494 0.7832957 0.7831176 0.7320725 0.7196247 0.7184372 0.7129650
#> [2,] 0.7088456 0.7036071 0.6942461 0.6613904 0.6602534 0.6218533 0.6137436
#> [3,] 0.6191940 0.6143616 0.6032756 0.5422356 0.4896883 0.4698937 0.4685521
#> [4,] 0.6988863 0.6727037 0.6706232 0.6579245 0.6309976 0.5926344 0.5453928
#> [5,] 0.6520543 0.6469287 0.6274892 0.6062636 0.5996537 0.5551521 0.5507743
#> [6,] 0.7232087 0.6964027 0.6853092 0.6642060 0.6616399 0.6414824 0.6412099
#> [7,] 0.7320372 0.6980541 0.6866767 0.6537801 0.6451109 0.6435746 0.6340869
#> [8,] 0.6991565 0.6630767 0.6043650 0.5891424 0.5541124 0.5511978 0.4852220
#> [9,] 0.6863414 0.6794863 0.6275976 0.6223692 0.6171816 0.5965949 0.5745329
#> [10,] 0.6939081 0.6832633 0.6790934 0.6721112 0.6619993 0.6567115 0.6453137
#> 22 23 24 25 26 27 28
#> [1,] 0.6893664 0.6794794 0.6776885 0.6591100 0.6590830 0.6296410 0.5883703
#> [2,] 0.5922085 0.5801826 0.5194664 0.4980553 0.4919785 0.4466452 0.4272466
#> [3,] 0.4556346 0.4480606 0.3856759 0.3819871 0.3641701 0.3583615 0.3451553
#> [4,] 0.5416781 0.5135930 0.5123930 0.5113113 0.4919778 0.4800047 0.4793860
#> [5,] 0.5372664 0.5195376 0.4928372 0.4848310 0.4072438 0.4014449 0.3758337
#> [6,] 0.6397781 0.5933208 0.5616738 0.5254825 0.5150504 0.5052717 0.4565150
#> [7,] 0.6189364 0.6054746 0.6008156 0.5655572 0.4913631 0.4700358 0.4433637
#> [8,] 0.4814955 0.4356070 0.4329577 0.4134435 0.3576113 0.3532520 0.3386323
#> [9,] 0.5711436 0.5424376 0.5017637 0.4978482 0.4733496 0.4702427 0.4681660
#> [10,] 0.6375847 0.6193783 0.6006700 0.5611449 0.5553265 0.5484002 0.5424417
#> 29 30 31 32 33 34 35
#> [1,] 0.5618213 0.4919181 0.3905596 0.3527889 0.3129833 0.2854513 0.2645395
#> [2,] 0.4094238 0.4091068 0.4069197 0.3862684 0.3574005 0.3370598 0.3328170
#> [3,] 0.3156294 0.2915964 0.2857769 0.2792125 0.2488301 0.2302948 0.2279427
#> [4,] 0.4513757 0.4481785 0.4353852 0.4344885 0.4176636 0.3525640 0.3115541
#> [5,] 0.3440131 0.3253989 0.3141783 0.2960687 0.2739955 0.2433217 0.2202471
#> [6,] 0.4516670 0.4380257 0.4308361 0.4260052 0.3298215 0.3062329 0.2981795
#> [7,] 0.4168978 0.4056787 0.3995427 0.3813287 0.3794024 0.3650372 0.3126142
#> [8,] 0.3254810 0.3061836 0.2705845 0.2635015 0.2624227 0.2478493 0.2337346
#> [9,] 0.4637440 0.4611052 0.4600282 0.4431284 0.3727811 0.3548055 0.3449816
#> [10,] 0.4197834 0.3781626 0.3603482 0.3302254 0.3013423 0.2704266 0.2327210
#> 36 37 38 39 40 41 42
#> [1,] 0.1945824 0.1925987 0.1606107 0.1516763 0.1369292 0.09768965 0.09394605
#> [2,] 0.3106024 0.3087645 0.2927387 0.2688736 0.2588608 0.22030637 0.21324813
#> [3,] 0.2269574 0.2249139 0.2201635 0.2139898 0.2066011 0.14721379 0.14672756
#> [4,] 0.3063278 0.3012543 0.2365209 0.2200902 0.1936979 0.19244075 0.17295454
#> [5,] 0.2009283 0.1897889 0.1809801 0.1763501 0.1719961 0.16122764 0.14869215
#> [6,] 0.2954092 0.2797018 0.2614054 0.2263226 0.2161579 0.20345269 0.17611169
#> [7,] 0.3008545 0.2822754 0.2416559 0.2326897 0.2090516 0.20517822 0.19822812
#> [8,] 0.2083617 0.2083168 0.1789609 0.1713852 0.1682065 0.14983401 0.13431220
#> [9,] 0.3050461 0.3050290 0.2892817 0.2693460 0.2580382 0.25282988 0.24252311
#> [10,] 0.2256654 0.1947964 0.1720781 0.1701358 0.1660048 0.15376385 0.13416555
#> 43 44 45 46 47 48
#> [1,] 0.09189839 0.08932224 0.07009002 0.06551847 0.04839823 0.02179576
#> [2,] 0.21251409 0.17147829 0.14050187 0.11076174 0.10590877 0.05871596
#> [3,] 0.14119609 0.12243392 0.10452654 0.06459299 0.05912893 0.04781616
#> [4,] 0.16481841 0.15062109 0.12640189 0.07758068 0.06544498 0.05018036
#> [5,] 0.13918238 0.07432107 0.07183850 0.06493924 0.06336721 0.06013087
#> [6,] 0.13753298 0.13350623 0.12529780 0.11076854 0.11075900 0.06637027
#> [7,] 0.19560864 0.17457635 0.12544662 0.10769635 0.09983606 0.08827764
#> [8,] 0.12764111 0.08005309 0.06906200 0.06165471 0.04055868 0.02154078
#> [9,] 0.22826087 0.18670618 0.18133320 0.13096989 0.10037212 0.02994646
#> [10,] 0.07532395 0.06820935 0.06262987 0.06116300 0.05044214 0.02753508
#> 49 50
#> [1,] 0.01622159 0.0009579435
#> [2,] 0.02370328 0.0180867743
#> [3,] 0.01935947 0.0024007875
#> [4,] 0.02764830 0.0084195950
#> [5,] 0.04722266 0.0436023297
#> [6,] 0.03718221 0.0146303358
#> [7,] 0.08054491 0.0361323683
#> [8,] 0.02015370 0.0070707756
#> [9,] 0.02510966 0.0250663597
#> [10,] 0.02280205 0.0109879340
#>
#> $crank
#> [1] 0.70988511 -2.38630624 1.40426569 -0.20961072 -0.99024937 1.96196662
#> [7] 0.59404591 -0.28169565 -0.56878596 -0.06509551
#>
#> $lp
#> NULL
#>
#> $response
#> NULL
#>
#> attr(,"data_type")
#> [1] "surv"
#> attr(,"interp_meth")
#> [1] "const_surv"
# lp = crank
surv_return(lp = rnorm(n))
#> $distr
#> NULL
#>
#> $crank
#> [1] -0.96074408 1.16969956 0.01865945 1.85665562 -1.11318445 -0.25276798
#> [7] -1.39607224 -0.61915155 0.26331401 -1.40401480
#>
#> $lp
#> [1] -0.96074408 1.16969956 0.01865945 1.85665562 -1.11318445 -0.25276798
#> [7] -1.39607224 -0.61915155 0.26331401 -1.40401480
#>
#> $response
#> NULL
#>
#> attr(,"data_type")
#> [1] "surv"
#> attr(,"interp_meth")
#> [1] "const_surv"
# if response is set and no crank, crank = -response
surv_return(response = sample(1:100, n))
#> $distr
#> NULL
#>
#> $crank
#> [1] -3 -84 -5 -23 -10 -78 -71 -91 -34 -60
#>
#> $lp
#> NULL
#>
#> $response
#> [1] 3 84 5 23 10 78 71 91 34 60
#>
#> attr(,"data_type")
#> [1] "surv"
#> attr(,"interp_meth")
#> [1] "const_surv"
# if both are set, they are not overwritten
surv_return(crank = rnorm(n), response = sample(1:100, n))
#> $distr
#> NULL
#>
#> $crank
#> [1] -0.39575701 0.25298543 -0.08484486 0.64585268 -1.38928361 1.48373145
#> [7] -0.06463873 -0.80526765 -1.93488547 0.08484413
#>
#> $lp
#> NULL
#>
#> $response
#> [1] 8 90 5 4 2 1 80 11 79 50
#>
#> attr(,"data_type")
#> [1] "surv"
#> attr(,"interp_meth")
#> [1] "const_surv"