Skip to contents

Helper function to more 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(
  surv = NULL,
  times = NULL,
  crank = NULL,
  lp = NULL,
  response = NULL,
  interp_method = "const_surv"
)

Arguments

surv

(matrix())
Matrix of predicted survival probabilities (rows = observations, columns = time points). Number of columns should be equal to length of times, if provided. Several checks are performed with assert_prob.

times

(numeric()|NULL)
Vector of survival times. If NULL, extracted from colnames(surv).

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.

lp

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

response

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

interp_method

(character(1))
Interpolation method; one of "const_surv" (default), "const_dens" (alias: "linear_surv") and "const_haz" (alias: "exp_surv"). For more details and available options, see survdistr::interp()

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(surv = surv_mat, times = 1:k)
#> $distr
#>               1         2         3         4         5         6         7
#>  [1,] 0.9889192 0.9690369 0.9683199 0.9437308 0.9218756 0.9212826 0.9178706
#>  [2,] 0.9804811 0.9786097 0.9530340 0.9423625 0.9268608 0.9127213 0.9065461
#>  [3,] 0.9619562 0.9567793 0.9484422 0.9473683 0.9353635 0.9064602 0.8535766
#>  [4,] 0.9868869 0.9800092 0.9260390 0.8944599 0.8878286 0.8873375 0.8803836
#>  [5,] 0.9959625 0.9919731 0.9850511 0.9751168 0.9139310 0.8575062 0.8352656
#>  [6,] 0.9920600 0.9585321 0.9328572 0.9270148 0.9155436 0.8925789 0.8906556
#>  [7,] 0.9671381 0.9596100 0.9590034 0.9034411 0.8789390 0.8674794 0.8476126
#>  [8,] 0.9745263 0.9500633 0.9339483 0.9214927 0.8921737 0.8199200 0.7955550
#>  [9,] 0.9955748 0.9813836 0.9424059 0.9310599 0.9198801 0.8868092 0.8614284
#> [10,] 0.9610608 0.9163004 0.8873296 0.8814146 0.8677521 0.8596251 0.8472173
#>               8         9        10        11        12        13        14
#>  [1,] 0.9038860 0.8440998 0.8440193 0.8421653 0.8205010 0.7970515 0.7961235
#>  [2,] 0.8369869 0.8159506 0.8114278 0.7713282 0.7683000 0.7656792 0.7456198
#>  [3,] 0.8335778 0.7704878 0.7636785 0.7590858 0.7586447 0.6778735 0.6759110
#>  [4,] 0.8725791 0.8187860 0.8052743 0.7490236 0.7327437 0.7149037 0.7112888
#>  [5,] 0.8296154 0.8247190 0.8229460 0.8038033 0.7949596 0.7611123 0.7610534
#>  [6,] 0.8778933 0.8523680 0.8299951 0.8286271 0.8269547 0.8268593 0.8165666
#>  [7,] 0.8255570 0.7829180 0.7701585 0.7538819 0.7449058 0.7237593 0.7183186
#>  [8,] 0.7535412 0.7255402 0.7086418 0.6988863 0.6766600 0.6759594 0.6451981
#>  [9,] 0.8500275 0.8452710 0.8432447 0.8263765 0.8027989 0.7820211 0.7419539
#> [10,] 0.8101817 0.7890479 0.7816516 0.7637358 0.7603732 0.7131149 0.6701602
#>              15        16        17        18        19        20        21
#>  [1,] 0.7866777 0.7576211 0.7327638 0.6951274 0.6340869 0.6118011 0.6117696
#>  [2,] 0.7395815 0.7172991 0.7052175 0.7020355 0.7013903 0.6991565 0.6808708
#>  [3,] 0.6603287 0.6543678 0.6458006 0.6294073 0.6259621 0.5754043 0.5630118
#>  [4,] 0.6655357 0.6483364 0.6188366 0.6027495 0.5986539 0.5982999 0.5973761
#>  [5,] 0.7462186 0.7408126 0.7348783 0.6979899 0.6705935 0.6590275 0.6443476
#>  [6,] 0.7563185 0.6689565 0.5971393 0.5928493 0.5879468 0.5755505 0.5617880
#>  [7,] 0.7042313 0.6781008 0.6632329 0.6372512 0.6299828 0.6039721 0.5614104
#>  [8,] 0.6321782 0.6072117 0.6042523 0.5872954 0.5597414 0.5440445 0.5070840
#>  [9,] 0.6973787 0.6940696 0.6881147 0.6846876 0.6641716 0.6190444 0.6082372
#> [10,] 0.6642060 0.6547804 0.6462127 0.6383965 0.6080354 0.6049454 0.6004056
#>              22        23        24        25        26        27        28
#>  [1,] 0.6054746 0.6038457 0.5998603 0.5890975 0.5507951 0.5136861 0.4742308
#>  [2,] 0.6493836 0.6321850 0.6215600 0.6163354 0.5750370 0.5655163 0.5644536
#>  [3,] 0.5224107 0.5017637 0.4740490 0.4661924 0.3441610 0.3136254 0.3120600
#>  [4,] 0.5956799 0.5359932 0.5232697 0.5226437 0.5095380 0.4616787 0.4185217
#>  [5,] 0.6405922 0.5998696 0.5965282 0.5948208 0.4628729 0.4595849 0.4318412
#>  [6,] 0.5292034 0.4762322 0.4739241 0.4719390 0.4591184 0.4536133 0.4480053
#>  [7,] 0.5482236 0.5338073 0.5244011 0.4761861 0.4756676 0.4661243 0.4551315
#>  [8,] 0.4657682 0.4405450 0.4344885 0.4294308 0.4176636 0.3851899 0.3810109
#>  [9,] 0.5982019 0.5074436 0.4711367 0.4500197 0.4307965 0.3890885 0.3716696
#> [10,] 0.5207486 0.5034499 0.4893102 0.4845777 0.4814087 0.4290002 0.4274280
#>              29        30        31        32        33        34        35
#>  [1,] 0.4683029 0.4593261 0.4433637 0.4344531 0.4297519 0.4267803 0.4169858
#>  [2,] 0.5413156 0.5361411 0.4889994 0.4843596 0.4145562 0.4136132 0.3387799
#>  [3,] 0.3078428 0.2758815 0.2730494 0.2693460 0.2662559 0.2425231 0.2382044
#>  [4,] 0.3779098 0.3496356 0.3464580 0.3462587 0.3371671 0.3020716 0.2811637
#>  [5,] 0.4002238 0.3931054 0.3867357 0.3837898 0.3824884 0.3451422 0.3449301
#>  [6,] 0.4408169 0.4221801 0.4041968 0.4035552 0.3992167 0.3473396 0.3149803
#>  [7,] 0.4457041 0.3909496 0.3813734 0.3054336 0.2882499 0.2663287 0.2578534
#>  [8,] 0.3661185 0.3561430 0.3559751 0.3192830 0.3191377 0.3175392 0.2781942
#>  [9,] 0.3682134 0.3639770 0.3504167 0.3461421 0.3440131 0.3130251 0.3122014
#> [10,] 0.3912177 0.3895407 0.3681659 0.3531725 0.3386629 0.3298215 0.2684298
#>              36        37        38        39        40        41        42
#>  [1,] 0.3995427 0.3957093 0.3662304 0.3346222 0.3267946 0.2847507 0.2646712
#>  [2,] 0.3280287 0.3061836 0.2887916 0.2548883 0.2479501 0.2126810 0.2083617
#>  [3,] 0.2134377 0.2123919 0.2123529 0.2103326 0.1872994 0.1872581 0.1721756
#>  [4,] 0.2704266 0.2555720 0.2408527 0.2334880 0.2051377 0.1865900 0.1798543
#>  [5,] 0.3432095 0.3243905 0.2994848 0.2775082 0.2729664 0.2726121 0.1683404
#>  [6,] 0.2953447 0.2943441 0.2855340 0.2306713 0.1815383 0.1809239 0.1741410
#>  [7,] 0.2399940 0.2249139 0.2066011 0.2053978 0.1908249 0.1697801 0.1357010
#>  [8,] 0.2743648 0.2698786 0.2399817 0.2372413 0.2021869 0.1923918 0.1731028
#>  [9,] 0.3054173 0.2907716 0.2679083 0.2477650 0.1867189 0.1823221 0.1757924
#> [10,] 0.1950225 0.1921941 0.1604869 0.1596419 0.1458227 0.1432455 0.1296418
#>              43         44         45         46         47         48
#>  [1,] 0.2602322 0.22741486 0.21357079 0.19049296 0.18565151 0.13296582
#>  [2,] 0.1307439 0.09567142 0.08413627 0.08390764 0.07016841 0.03923126
#>  [3,] 0.1610261 0.13281461 0.09488274 0.08158873 0.08042770 0.08015722
#>  [4,] 0.1442918 0.09241899 0.08920410 0.06826615 0.05767730 0.04913684
#>  [5,] 0.1635163 0.09768965 0.09189839 0.07105661 0.07009002 0.06869184
#>  [6,] 0.1059088 0.09956634 0.08747629 0.06834881 0.03031139 0.02370328
#>  [7,] 0.1045265 0.08237326 0.08134637 0.07775419 0.06273725 0.02711444
#>  [8,] 0.1679829 0.09864674 0.09678465 0.08882124 0.08590772 0.07927434
#>  [9,] 0.1643405 0.11910933 0.11050873 0.10516616 0.06121978 0.05953748
#> [10,] 0.1200995 0.10108347 0.09712604 0.08428220 0.06989352 0.06929129
#>                49          50
#>  [1,] 0.132306410 0.089174358
#>  [2,] 0.027205368 0.016294998
#>  [3,] 0.071973418 0.023566173
#>  [4,] 0.027535082 0.008714871
#>  [5,] 0.026502171 0.008762787
#>  [6,] 0.009083197 0.002873588
#>  [7,] 0.017161721 0.008509290
#>  [8,] 0.040086601 0.022777462
#>  [9,] 0.044282167 0.025840241
#> [10,] 0.049804813 0.016614835
#> 
#> $crank
#>  [1] 35.90414 45.95965 52.82426 52.38759 47.72183 54.80388 54.98869 52.30626
#>  [9] 48.88587 53.17314
#> 
#> $lp
#> NULL
#> 
#> $response
#> NULL
#> 
#> attr(,"interp_method")
#> [1] "const_surv"

# if crank is set, it's not overwritten
surv_return(surv = surv_mat, times = 1:k, crank = rnorm(n))
#> $distr
#>               1         2         3         4         5         6         7
#>  [1,] 0.9889192 0.9690369 0.9683199 0.9437308 0.9218756 0.9212826 0.9178706
#>  [2,] 0.9804811 0.9786097 0.9530340 0.9423625 0.9268608 0.9127213 0.9065461
#>  [3,] 0.9619562 0.9567793 0.9484422 0.9473683 0.9353635 0.9064602 0.8535766
#>  [4,] 0.9868869 0.9800092 0.9260390 0.8944599 0.8878286 0.8873375 0.8803836
#>  [5,] 0.9959625 0.9919731 0.9850511 0.9751168 0.9139310 0.8575062 0.8352656
#>  [6,] 0.9920600 0.9585321 0.9328572 0.9270148 0.9155436 0.8925789 0.8906556
#>  [7,] 0.9671381 0.9596100 0.9590034 0.9034411 0.8789390 0.8674794 0.8476126
#>  [8,] 0.9745263 0.9500633 0.9339483 0.9214927 0.8921737 0.8199200 0.7955550
#>  [9,] 0.9955748 0.9813836 0.9424059 0.9310599 0.9198801 0.8868092 0.8614284
#> [10,] 0.9610608 0.9163004 0.8873296 0.8814146 0.8677521 0.8596251 0.8472173
#>               8         9        10        11        12        13        14
#>  [1,] 0.9038860 0.8440998 0.8440193 0.8421653 0.8205010 0.7970515 0.7961235
#>  [2,] 0.8369869 0.8159506 0.8114278 0.7713282 0.7683000 0.7656792 0.7456198
#>  [3,] 0.8335778 0.7704878 0.7636785 0.7590858 0.7586447 0.6778735 0.6759110
#>  [4,] 0.8725791 0.8187860 0.8052743 0.7490236 0.7327437 0.7149037 0.7112888
#>  [5,] 0.8296154 0.8247190 0.8229460 0.8038033 0.7949596 0.7611123 0.7610534
#>  [6,] 0.8778933 0.8523680 0.8299951 0.8286271 0.8269547 0.8268593 0.8165666
#>  [7,] 0.8255570 0.7829180 0.7701585 0.7538819 0.7449058 0.7237593 0.7183186
#>  [8,] 0.7535412 0.7255402 0.7086418 0.6988863 0.6766600 0.6759594 0.6451981
#>  [9,] 0.8500275 0.8452710 0.8432447 0.8263765 0.8027989 0.7820211 0.7419539
#> [10,] 0.8101817 0.7890479 0.7816516 0.7637358 0.7603732 0.7131149 0.6701602
#>              15        16        17        18        19        20        21
#>  [1,] 0.7866777 0.7576211 0.7327638 0.6951274 0.6340869 0.6118011 0.6117696
#>  [2,] 0.7395815 0.7172991 0.7052175 0.7020355 0.7013903 0.6991565 0.6808708
#>  [3,] 0.6603287 0.6543678 0.6458006 0.6294073 0.6259621 0.5754043 0.5630118
#>  [4,] 0.6655357 0.6483364 0.6188366 0.6027495 0.5986539 0.5982999 0.5973761
#>  [5,] 0.7462186 0.7408126 0.7348783 0.6979899 0.6705935 0.6590275 0.6443476
#>  [6,] 0.7563185 0.6689565 0.5971393 0.5928493 0.5879468 0.5755505 0.5617880
#>  [7,] 0.7042313 0.6781008 0.6632329 0.6372512 0.6299828 0.6039721 0.5614104
#>  [8,] 0.6321782 0.6072117 0.6042523 0.5872954 0.5597414 0.5440445 0.5070840
#>  [9,] 0.6973787 0.6940696 0.6881147 0.6846876 0.6641716 0.6190444 0.6082372
#> [10,] 0.6642060 0.6547804 0.6462127 0.6383965 0.6080354 0.6049454 0.6004056
#>              22        23        24        25        26        27        28
#>  [1,] 0.6054746 0.6038457 0.5998603 0.5890975 0.5507951 0.5136861 0.4742308
#>  [2,] 0.6493836 0.6321850 0.6215600 0.6163354 0.5750370 0.5655163 0.5644536
#>  [3,] 0.5224107 0.5017637 0.4740490 0.4661924 0.3441610 0.3136254 0.3120600
#>  [4,] 0.5956799 0.5359932 0.5232697 0.5226437 0.5095380 0.4616787 0.4185217
#>  [5,] 0.6405922 0.5998696 0.5965282 0.5948208 0.4628729 0.4595849 0.4318412
#>  [6,] 0.5292034 0.4762322 0.4739241 0.4719390 0.4591184 0.4536133 0.4480053
#>  [7,] 0.5482236 0.5338073 0.5244011 0.4761861 0.4756676 0.4661243 0.4551315
#>  [8,] 0.4657682 0.4405450 0.4344885 0.4294308 0.4176636 0.3851899 0.3810109
#>  [9,] 0.5982019 0.5074436 0.4711367 0.4500197 0.4307965 0.3890885 0.3716696
#> [10,] 0.5207486 0.5034499 0.4893102 0.4845777 0.4814087 0.4290002 0.4274280
#>              29        30        31        32        33        34        35
#>  [1,] 0.4683029 0.4593261 0.4433637 0.4344531 0.4297519 0.4267803 0.4169858
#>  [2,] 0.5413156 0.5361411 0.4889994 0.4843596 0.4145562 0.4136132 0.3387799
#>  [3,] 0.3078428 0.2758815 0.2730494 0.2693460 0.2662559 0.2425231 0.2382044
#>  [4,] 0.3779098 0.3496356 0.3464580 0.3462587 0.3371671 0.3020716 0.2811637
#>  [5,] 0.4002238 0.3931054 0.3867357 0.3837898 0.3824884 0.3451422 0.3449301
#>  [6,] 0.4408169 0.4221801 0.4041968 0.4035552 0.3992167 0.3473396 0.3149803
#>  [7,] 0.4457041 0.3909496 0.3813734 0.3054336 0.2882499 0.2663287 0.2578534
#>  [8,] 0.3661185 0.3561430 0.3559751 0.3192830 0.3191377 0.3175392 0.2781942
#>  [9,] 0.3682134 0.3639770 0.3504167 0.3461421 0.3440131 0.3130251 0.3122014
#> [10,] 0.3912177 0.3895407 0.3681659 0.3531725 0.3386629 0.3298215 0.2684298
#>              36        37        38        39        40        41        42
#>  [1,] 0.3995427 0.3957093 0.3662304 0.3346222 0.3267946 0.2847507 0.2646712
#>  [2,] 0.3280287 0.3061836 0.2887916 0.2548883 0.2479501 0.2126810 0.2083617
#>  [3,] 0.2134377 0.2123919 0.2123529 0.2103326 0.1872994 0.1872581 0.1721756
#>  [4,] 0.2704266 0.2555720 0.2408527 0.2334880 0.2051377 0.1865900 0.1798543
#>  [5,] 0.3432095 0.3243905 0.2994848 0.2775082 0.2729664 0.2726121 0.1683404
#>  [6,] 0.2953447 0.2943441 0.2855340 0.2306713 0.1815383 0.1809239 0.1741410
#>  [7,] 0.2399940 0.2249139 0.2066011 0.2053978 0.1908249 0.1697801 0.1357010
#>  [8,] 0.2743648 0.2698786 0.2399817 0.2372413 0.2021869 0.1923918 0.1731028
#>  [9,] 0.3054173 0.2907716 0.2679083 0.2477650 0.1867189 0.1823221 0.1757924
#> [10,] 0.1950225 0.1921941 0.1604869 0.1596419 0.1458227 0.1432455 0.1296418
#>              43         44         45         46         47         48
#>  [1,] 0.2602322 0.22741486 0.21357079 0.19049296 0.18565151 0.13296582
#>  [2,] 0.1307439 0.09567142 0.08413627 0.08390764 0.07016841 0.03923126
#>  [3,] 0.1610261 0.13281461 0.09488274 0.08158873 0.08042770 0.08015722
#>  [4,] 0.1442918 0.09241899 0.08920410 0.06826615 0.05767730 0.04913684
#>  [5,] 0.1635163 0.09768965 0.09189839 0.07105661 0.07009002 0.06869184
#>  [6,] 0.1059088 0.09956634 0.08747629 0.06834881 0.03031139 0.02370328
#>  [7,] 0.1045265 0.08237326 0.08134637 0.07775419 0.06273725 0.02711444
#>  [8,] 0.1679829 0.09864674 0.09678465 0.08882124 0.08590772 0.07927434
#>  [9,] 0.1643405 0.11910933 0.11050873 0.10516616 0.06121978 0.05953748
#> [10,] 0.1200995 0.10108347 0.09712604 0.08428220 0.06989352 0.06929129
#>                49          50
#>  [1,] 0.132306410 0.089174358
#>  [2,] 0.027205368 0.016294998
#>  [3,] 0.071973418 0.023566173
#>  [4,] 0.027535082 0.008714871
#>  [5,] 0.026502171 0.008762787
#>  [6,] 0.009083197 0.002873588
#>  [7,] 0.017161721 0.008509290
#>  [8,] 0.040086601 0.022777462
#>  [9,] 0.044282167 0.025840241
#> [10,] 0.049804813 0.016614835
#> 
#> $crank
#>  [1] -1.769955151 -3.237400211  0.307004133 -0.174265752  0.657495410
#>  [6] -0.008831523  0.421837595  0.650005772  0.390177072 -0.768557294
#> 
#> $lp
#> NULL
#> 
#> $response
#> NULL
#> 
#> attr(,"interp_method")
#> [1] "const_surv"

# lp = crank
surv_return(lp = rnorm(n))
#> $distr
#> NULL
#> 
#> $crank
#>  [1]  0.85694240 -0.35334899  0.40663320  0.18289146 -1.87671538 -0.51074152
#>  [7] -0.15800012  1.21515166  0.04221889  0.39740704
#> 
#> $lp
#>  [1]  0.85694240 -0.35334899  0.40663320  0.18289146 -1.87671538 -0.51074152
#>  [7] -0.15800012  1.21515166  0.04221889  0.39740704
#> 
#> $response
#> NULL
#> 
#> attr(,"interp_method")
#> [1] "const_surv"

# if response is set and no crank, crank = -response
surv_return(response = sample(1:100, n))
#> $distr
#> NULL
#> 
#> $crank
#>  [1] -77 -56 -20 -65 -42 -49 -94 -53 -81 -50
#> 
#> $lp
#> NULL
#> 
#> $response
#>  [1] 77 56 20 65 42 49 94 53 81 50
#> 
#> attr(,"interp_method")
#> [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.09450402  0.03255579  0.46129012  1.38140030 -0.41647627  0.68094267
#>  [7] -0.41437304 -0.51834551 -0.68401973 -0.88564860
#> 
#> $lp
#> NULL
#> 
#> $response
#>  [1]  7 98 88 20 99 39 25  3 87 19
#> 
#> attr(,"interp_method")
#> [1] "const_surv"