Example of Survival Analysis

The following scripts present the functionality of the Survival Analysis module in AdvancedMiner. Please note that these scripts do not include the data required to run the scripts in AdvancedMiner Client. The full source with the data can be found in the Appendix Examples.

Example 39.1. Cox semi-parametric model example

# Cox semi-parametric model example
table "HIV":
    censor days dose treatment weight
    
    N 30.0 3.081430851090935 0.0 57.8738112449646
    Y 33.0 2.2284185921454207 1.0 46.98548197746277
    N 36.0 2.793160256900953 1.0 40.64843571186066
    N 31.0 2.3161954166339696 1.0 40.122660636901855
    N 50.0 3.1277873810530066 0.0 47.098684549331665
    Y 28.0 2.6280503396985995 1.0 43.85288977622986
    N 36.0 2.437778201298561 1.0 46.10991942882538
    N 37.0 2.714954640472462 1.0 42.934664607048035
    N 30.0 2.825249956361242 0.0 58.074954867362976
    N 39.0 2.444908655624817 0.0 58.468533754348755
    Y 31.0 2.7114104899340448 1.0 39.142784118652344
    N 41.0 2.94848220027547 0.0 44.23114502429962
    N 28.0 2.9645756487077244 1.0 55.35672903060913
    N 41.0 2.290834441296651 1.0 42.474403500556946
    N 63.0 3.0564012778980416 0.0 46.09067142009735
    N 30.0 2.948842929873205 0.0 46.39939904212952
    N 32.0 2.417541672572897 0.0 65.35233283042908
    Y 45.0 3.185570719084413 0.0 45.58563852310181
    Y 30.0 2.7404453142255814 1.0 54.944403529167175
    N 30.0 2.34792207236493 0.0 48.73584043979645
    Y 28.0 2.6066313731805923 0.0 50.85618436336517
    N 33.0 3.147555289426225 1.0 45.98667299747467
    Y 88.0 2.8853177676167308 0.0 35.09775495529175
    N 38.0 2.5345985397824777 0.0 43.1633358001709
    Y 29.0 2.320861461672114 1.0 53.95890474319458
    N 33.0 2.7948169349758176 1.0 57.79575431346893
    Y 32.0 2.5992757005792857 0.0 41.27472746372223
    N 28.0 3.136323450462654 1.0 57.88637900352478
    N 41.0 2.6741280259338156 0.0 52.05712568759918
    N 30.0 2.8173865700918084 1.0 50.10614264011383
    N 28.0 2.2408500642157927 0.0 53.18464469909668
    N 28.0 2.5509714116969633 0.0 52.71421265602112
    N 55.0 2.8213830565294984 0.0 40.885963916778564
    N 32.0 2.4273371494971934 0.0 48.997780203819275
    N 31.0 2.6487828638066757 1.0 52.48666775226593
    N 33.0 2.498272122845316 1.0 41.266563296318054
    N 32.0 2.9660948924938135 1.0 50.418524622917175
    N 36.0 2.7394662336137823 0.0 42.07624423503876
    N 30.0 2.7452705362811454 1.0 62.820916295051575
    N 34.0 3.087627224486434 0.0 47.21555209159851
    N 64.0 2.37063068259187 0.0 54.45838928222656
    N 31.0 2.3214752107658216 0.0 50.81758153438568
    N 36.0 3.0645516154652377 0.0 44.36666679382324
    N 29.0 2.507903009710647 1.0 55.339086294174194
    N 62.0 3.222180386251486 0.0 43.327778458595276
    N 35.0 3.0075439228834555 0.0 38.84279119968414
    N 39.0 3.119852909901356 0.0 41.8588570356369
    Y 82.0 3.0418112274741143 1.0 31.74041712284088
    Y 29.0 3.0952031905202606 0.0 55.899375677108765
    N 30.0 2.428066420544726 1.0 49.83421802520752
    N 43.0 2.7461049250314398 0.0 44.55719482898712
    N 30.0 2.685063480009806 0.0 42.86260807514191
    N 38.0 2.673262196321976 0.0 43.42743194103241
    N 30.0 2.6029907504661254 1.0 61.44343388080597
    N 35.0 2.3166010581839807 1.0 46.22520351409912
    N 32.0 3.0095231192909937 1.0 40.053192257881165
    N 29.0 2.5733932135819324 1.0 53.496418595314026
    N 28.0 2.279215674998498 1.0 42.93415677547455
    N 60.0 2.492899053446145 0.0 44.43672692775726
    N 30.0 2.476461302031735 1.0 48.018630623817444
    Y 37.0 2.8520655533096235 0.0 44.8764009475708
    N 37.0 3.1518045857517154 1.0 49.3680135011673
    N 30.0 2.9153954819696715 1.0 49.525230050086975
    N 32.0 2.637453694206634 1.0 43.58605468273163
    N 31.0 2.4376940380600063 1.0 45.57033693790436
    N 58.0 2.461523257765543 0.0 45.864428758621216
    N 32.0 3.0699319938588903 1.0 56.160651445388794
    N 87.0 2.6465096577784455 0.0 33.01168072223663
    N 29.0 2.295246680407634 1.0 55.48183524608612
    Y 31.0 2.3054889809583967 0.0 47.76137626171112
    N 70.0 2.2985271026542557 0.0 36.922224164009094
    N 29.0 2.4566889312587827 0.0 48.11109435558319
    N 31.0 2.3262493123463654 0.0 42.92390561103821
    N 79.0 3.160247682801543 0.0 45.728721022605896
    N 40.0 2.6820998534275016 0.0 40.03557217121124
    N 32.0 2.4004851865292722 1.0 46.45250082015991
    N 82.0 2.23333776855838 0.0 31.5962792634964
    N 28.0 2.8828017121825145 1.0 37.30770468711853
    N 26.0 2.7895431128242967 1.0 43.97993278503418
    N 36.0 3.001367858915769 0.0 53.60692238807678
    N 33.0 3.213561996595554 1.0 50.63715600967407
    N 30.0 2.771378506406407 1.0 47.741469502449036
    N 30.0 2.753735868832637 0.0 57.45537531375885
    N 30.0 2.3618389146055723 1.0 42.4123272895813
    Y 34.0 2.3200263447155756 1.0 52.94650888442993
    Y 27.0 2.5502785209347234 1.0 56.603357911109924
    N 38.0 3.177853644572297 1.0 37.21322977542877
    Y 52.0 3.2831628197807117 0.0 40.17420041561127
    N 34.0 2.6228052088840226 1.0 43.38765358924866
    Y 39.0 3.055756520792758 1.0 42.641565918922424
    N 32.0 3.00239502396637 0.0 46.75193762779236
    N 86.0 2.4905506830766013 0.0 47.11822819709778
    N 27.0 2.7037077801923677 1.0 51.02326464653015
    Y 38.0 2.597713133947351 1.0 46.52934992313385
    N 34.0 3.001039147303028 1.0 45.40196716785431
    N 30.0 2.6868492495016354 1.0 44.355523109436035
    N 32.0 2.9580542489999537 0.0 38.836639165878296
    Y 87.0 2.4924213929461847 0.0 40.57176160812378
    Y 27.0 3.28069196967279 1.0 46.625693678855896
    N 30.0 2.820234781890454 1.0 44.84109568595886
    
pd = PhysicalData('HIV')
ld = LogicalData(pd)

#------ algorithm settings (Cox algorithm) ------
fs = SurvivalFunctionSettings()
as = CoxSettings()

oas = as.getOptimizationAlgorithmSettings()
oas.setConvergenceThreshold(0.1)
oas.setIterMax(10)
as.preselection = TRUE

vss = VariableSelectionSettings()
vss.variableSelectionMethod = VariableSelectionMethod.stepwise
vss.modelEntryLevel = 0.15
vss.modelLeaveLevel = 0.2

as.variableSelectionSettings = vss
fs.algorithmSettings = as


#------ survival function settings ------
fs.logicalData = ld
fs.targetAttributeName = 'days'
fs.censorName = 'censor'
fs.censoredCategory = 'Y'     # censor value for the censored
                              # observation

save('hiv_pd', pd)
save('hiv_ld', ld)
save('cox_settings', fs)


#------ model building (Cox model) ------
bt = MiningBuildTask('hiv_pd', 'cox_settings', 'cox_model')

save('cox_build', bt)
execute('cox_build')


#------ model testing ------
tt = SurvivalTestTask('hiv_pd', 'cox_model', 'hiv_out')
tt.testDataTargetAttributeName = 'days'
tt.censorName = 'censor'
tt.censoredCategory = 'Y'     # censor value for the censored
                              # observation

tt.numberOfLiftQuantiles = 10

save('cox_test', tt)
execute('cox_test')


#------ model application ------
pdout = PhysicalData('hiv_apply')
save('hiv_pd_apply', pdout)

at = MiningApplyTask()
at.modelName = 'cox_model'
at.sourceDataName = 'hiv_pd'
at.targetDataName = 'hiv_pd_apply'
at.replaceExistingData = TRUE

asi = ApplySourceItem()
asi.sourceName = 'days'
asi.destinationName = 'actual_days'
at.directMapping.add(asi)
asi = ApplySourceItem()
asi.sourceName = 'censor'
asi.destinationName = 'censor'
at.directMapping.add(asi)

ao = SurvivalApplyOutput()
ao.firstTimePoint = 25
ao.lastTimePoint = 85
ao.numberOfTimePoints = 13
at.applyOutput = ao

save('cox_apply', at)
execute('cox_apply')

# print model statistics
statModel = load('cox_model').getModelStatistics()
varStat = statModel.variableStatistics
statNames = varStat.varStatNames
varNames=statModel.varNames

print " "*5,
for colName in statNames:
    print "%10s"%colName[0:10],
print

for row in range(0,len(varNames)):
        print "%5s"%varNames[row][0:20],
        for col in range(0,len(statNames)):
            value = (str(varStat.getVarStatValue(varNames[row],col)))
            if value == "NaN": value = "%10s" % value
            else: value = "%10.4f" % value
            print value,
        print

Output

           Coeff Univariate Lower Conf Upper Conf     StdErr  Wald Test Wald Pr>Ch Schoenfeld Scaled Sch
treatment     0.9698     0.0004     0.4638     1.4757     0.2581    14.1137     0.0002     0.0000     0.0000
weight     0.0800     0.0000     0.0468     0.1131     0.0169    22.2849     0.0000    -0.0003    -0.0000

Non-parametric survival model example

Example 39.2. Non-parametric survival model example

# Non-parametric survival model example
table "HIV":
    censor days dose treatment weight
    
    N 30.0 3.081430851090935 0.0 57.8738112449646
    Y 33.0 2.2284185921454207 1.0 46.98548197746277
    N 36.0 2.793160256900953 1.0 40.64843571186066
    N 31.0 2.3161954166339696 1.0 40.122660636901855
    N 50.0 3.1277873810530066 0.0 47.098684549331665
    Y 28.0 2.6280503396985995 1.0 43.85288977622986
    N 36.0 2.437778201298561 1.0 46.10991942882538
    N 37.0 2.714954640472462 1.0 42.934664607048035
    N 30.0 2.825249956361242 0.0 58.074954867362976
    N 39.0 2.444908655624817 0.0 58.468533754348755
    Y 31.0 2.7114104899340448 1.0 39.142784118652344
    N 41.0 2.94848220027547 0.0 44.23114502429962
    N 28.0 2.9645756487077244 1.0 55.35672903060913
    N 41.0 2.290834441296651 1.0 42.474403500556946
    N 63.0 3.0564012778980416 0.0 46.09067142009735
    N 30.0 2.948842929873205 0.0 46.39939904212952
    N 32.0 2.417541672572897 0.0 65.35233283042908
    Y 45.0 3.185570719084413 0.0 45.58563852310181
    Y 30.0 2.7404453142255814 1.0 54.944403529167175
    N 30.0 2.34792207236493 0.0 48.73584043979645
    Y 28.0 2.6066313731805923 0.0 50.85618436336517
    N 33.0 3.147555289426225 1.0 45.98667299747467
    Y 88.0 2.8853177676167308 0.0 35.09775495529175
    N 38.0 2.5345985397824777 0.0 43.1633358001709
    Y 29.0 2.320861461672114 1.0 53.95890474319458
    N 33.0 2.7948169349758176 1.0 57.79575431346893
    Y 32.0 2.5992757005792857 0.0 41.27472746372223
    N 28.0 3.136323450462654 1.0 57.88637900352478
    N 41.0 2.6741280259338156 0.0 52.05712568759918
    N 30.0 2.8173865700918084 1.0 50.10614264011383
    N 28.0 2.2408500642157927 0.0 53.18464469909668
    N 28.0 2.5509714116969633 0.0 52.71421265602112
    N 55.0 2.8213830565294984 0.0 40.885963916778564
    N 32.0 2.4273371494971934 0.0 48.997780203819275
    N 31.0 2.6487828638066757 1.0 52.48666775226593
    N 33.0 2.498272122845316 1.0 41.266563296318054
    N 32.0 2.9660948924938135 1.0 50.418524622917175
    N 36.0 2.7394662336137823 0.0 42.07624423503876
    N 30.0 2.7452705362811454 1.0 62.820916295051575
    N 34.0 3.087627224486434 0.0 47.21555209159851
    N 64.0 2.37063068259187 0.0 54.45838928222656
    N 31.0 2.3214752107658216 0.0 50.81758153438568
    N 36.0 3.0645516154652377 0.0 44.36666679382324
    N 29.0 2.507903009710647 1.0 55.339086294174194
    N 62.0 3.222180386251486 0.0 43.327778458595276
    N 35.0 3.0075439228834555 0.0 38.84279119968414
    N 39.0 3.119852909901356 0.0 41.8588570356369
    Y 82.0 3.0418112274741143 1.0 31.74041712284088
    Y 29.0 3.0952031905202606 0.0 55.899375677108765
    N 30.0 2.428066420544726 1.0 49.83421802520752
    N 43.0 2.7461049250314398 0.0 44.55719482898712
    N 30.0 2.685063480009806 0.0 42.86260807514191
    N 38.0 2.673262196321976 0.0 43.42743194103241
    N 30.0 2.6029907504661254 1.0 61.44343388080597
    N 35.0 2.3166010581839807 1.0 46.22520351409912
    N 32.0 3.0095231192909937 1.0 40.053192257881165
    N 29.0 2.5733932135819324 1.0 53.496418595314026
    N 28.0 2.279215674998498 1.0 42.93415677547455
    N 60.0 2.492899053446145 0.0 44.43672692775726
    N 30.0 2.476461302031735 1.0 48.018630623817444
    Y 37.0 2.8520655533096235 0.0 44.8764009475708
    N 37.0 3.1518045857517154 1.0 49.3680135011673
    N 30.0 2.9153954819696715 1.0 49.525230050086975
    N 32.0 2.637453694206634 1.0 43.58605468273163
    N 31.0 2.4376940380600063 1.0 45.57033693790436
    N 58.0 2.461523257765543 0.0 45.864428758621216
    N 32.0 3.0699319938588903 1.0 56.160651445388794
    N 87.0 2.6465096577784455 0.0 33.01168072223663
    N 29.0 2.295246680407634 1.0 55.48183524608612
    Y 31.0 2.3054889809583967 0.0 47.76137626171112
    N 70.0 2.2985271026542557 0.0 36.922224164009094
    N 29.0 2.4566889312587827 0.0 48.11109435558319
    N 31.0 2.3262493123463654 0.0 42.92390561103821
    N 79.0 3.160247682801543 0.0 45.728721022605896
    N 40.0 2.6820998534275016 0.0 40.03557217121124
    N 32.0 2.4004851865292722 1.0 46.45250082015991
    N 82.0 2.23333776855838 0.0 31.5962792634964
    N 28.0 2.8828017121825145 1.0 37.30770468711853
    N 26.0 2.7895431128242967 1.0 43.97993278503418
    N 36.0 3.001367858915769 0.0 53.60692238807678
    N 33.0 3.213561996595554 1.0 50.63715600967407
    N 30.0 2.771378506406407 1.0 47.741469502449036
    N 30.0 2.753735868832637 0.0 57.45537531375885
    N 30.0 2.3618389146055723 1.0 42.4123272895813
    Y 34.0 2.3200263447155756 1.0 52.94650888442993
    Y 27.0 2.5502785209347234 1.0 56.603357911109924
    N 38.0 3.177853644572297 1.0 37.21322977542877
    Y 52.0 3.2831628197807117 0.0 40.17420041561127
    N 34.0 2.6228052088840226 1.0 43.38765358924866
    Y 39.0 3.055756520792758 1.0 42.641565918922424
    N 32.0 3.00239502396637 0.0 46.75193762779236
    N 86.0 2.4905506830766013 0.0 47.11822819709778
    N 27.0 2.7037077801923677 1.0 51.02326464653015
    Y 38.0 2.597713133947351 1.0 46.52934992313385
    N 34.0 3.001039147303028 1.0 45.40196716785431
    N 30.0 2.6868492495016354 1.0 44.355523109436035
    N 32.0 2.9580542489999537 0.0 38.836639165878296
    Y 87.0 2.4924213929461847 0.0 40.57176160812378
    Y 27.0 3.28069196967279 1.0 46.625693678855896
    N 30.0 2.820234781890454 1.0 44.84109568595886
    
pd = PhysicalData('HIV')
ld = LogicalData(pd)

fs = SurvivalFunctionSettings()

#----- Nonparametric Survival algorithm settings
as = NonparametricSurvivalSettings()
as.survivalEstimator = NonparametricSurvivalEstimator.lifeTable

fs.algorithmSettings = as


#----- survival function settings
fs.logicalData = ld
fs.targetAttributeName = 'days'
fs.censorName = 'censor'
fs.censoredCategory = 'Y'     # censor category for censored observation

save('hiv_pd', pd)
save('hiv_ld', ld)
save('nonparam_settings', fs)


#----- building Nonparametric Survival model
bt = MiningBuildTask('hiv_pd', 'nonparam_settings', 'nonparam_model')

save('nonparam_build', bt)
execute('nonparam_build')


model=load('nonparam_model')

strFormat = "%s %.4f %.4f %.4f"
for t in range(25,90,5):
    print strFormat % ("\nS(%d) : " % t, model.getS0(t), model.getS0StdErr(t), model.getS0ConfidenceInterval(t))
    print strFormat % ("H(%d) : " %t, model.getH0(t), model.getH0StdErr(t), model.getH0ConfidenceInterval(t))
    print strFormat % ("h(%d) : " %t, model.geth0(t), model.geth0StdErr(t), model.geth0ConfidenceInterval(t))
    print strFormat % ("f(%d) : " %t, model.getPDF(t), model.getPDFStdErr(t),  model.getPDFConfidenceInterval(t))

Output

S(25) :  1.0000 0.0000 0.0000
H(25) :  -0.0000 0.0000 0.0000
h(25) :  0.0000 0.0000 0.0000
f(25) :  0.0000 0.0000 0.0000

S(30) :  0.8758 0.0336 0.0658
H(30) :  0.1326 0.0383 0.0751
h(30) :  0.2177 0.0541 0.1060
f(30) :  0.0403 0.0621 0.1216

S(35) :  0.4658 0.0526 0.1031
H(35) :  0.7639 0.1129 0.2212
h(35) :  0.0526 0.0372 0.0729
f(35) :  0.0329 0.0593 0.1163

S(40) :  0.2941 0.0494 0.0969
H(40) :  1.2237 0.1681 0.3295
h(40) :  0.0465 0.0465 0.0911
f(40) :  0.0240 0.0545 0.1067

S(45) :  0.2273 0.0464 0.0909
H(45) :  1.4815 0.2040 0.3999
h(45) :  0.0000 0.0000 0.0000
f(45) :  0.0063 0.0171 0.0335

S(50) :  0.2273 0.0464 0.0909
H(50) :  1.4815 0.2040 0.3999
h(50) :  0.0129 0.0129 0.0253
f(50) :  0.0000 0.0000 0.0000

S(55) :  0.2131 0.0456 0.0894
H(55) :  1.5461 0.2140 0.4194
h(55) :  0.0247 0.0247 0.0484
f(55) :  0.0000 0.0000 0.0000

S(60) :  0.1826 0.0439 0.0860
H(60) :  1.7002 0.2402 0.4708
h(60) :  0.0435 0.0434 0.0851
f(60) :  0.0070 0.0224 0.0439

S(65) :  0.1218 0.0384 0.0752
H(65) :  2.1057 0.3152 0.6178
h(65) :  0.1176 0.1174 0.2302
f(65) :  0.0023 0.0084 0.0164

S(70) :  0.1218 0.0384 0.0752
H(70) :  2.1057 0.3152 0.6178
h(70) :  0.0222 0.0222 0.0435
f(70) :  0.0023 0.0084 0.0164

S(75) :  0.1065 0.0365 0.0715
H(75) :  2.2392 0.3424 0.6710
h(75) :  0.0222 0.0222 0.0435
f(75) :  0.0015 0.0057 0.0111

S(80) :  0.0913 0.0343 0.0672
H(80) :  2.3934 0.3755 0.7361
h(80) :  0.0171 0.0170 0.0334
f(80) :  0.0043 0.0172 0.0336

S(85) :  0.0747 0.0318 0.0624
H(85) :  2.5940 0.4260 0.8349
h(85) :  0.0667 0.0663 0.1300
f(85) :  0.0034 0.0138 0.0270