Example of logistic regression

The following script contains an example of build, apply, and test tasks for the Logistic Regression module in AdvancedMiner. Please note that this script does not include the data required to run the script in AdvancedMiner Client. The full source with the data can be found in the Appendix Examples.

Example 38.1. Logistic regression

if not tableExists('pima_diabetes'):
 raise "Table 'pima_diabetes' does not exists. Please run pima_diabetes.py script from data directory first"

pd = PhysicalData('pima_diabetes')
ld = LogicalData(pd)

fs = ClassificationFunctionSettings()

#------ logistic regression algorithm settings ------
as = LogisticRegressionSettings()

as.estimationMethod = LogisticEstimationMethod.newton
as.positiveTargetCategory = 'tested_positive'
as.linkFunctionType = LinkFunctionType.logit
as.preselection = TRUE
as.intercept = TRUE
# as.positiveCategoryApriori = 0.35

opt = OptimizationAlgorithmSettings()
opt.convergenceCriterion = ConvergenceCriterion.likelihood
opt.convergenceThreshold = 0.1
opt.iterMax = 20

vss = VariableSelectionSettings()
vss.variableSelectionMethod = VariableSelectionMethod.forward
vss.modelEntryLevel = 0.15

as.variableSelectionSettings = vss
as.optimizationAlgorithmSettings = opt

fs.algorithmSettings = as
fs.logicalData = ld
fs.targetAttributeName = 'Class'

save('diabetes_pd', pd)
save('diabetes_ld', ld)
save('logreg_settings', fs)

#------ building the logistic model ------
bt = MiningBuildTask('diabetes_pd', 'logreg_settings', 'logreg_model')
save('diabetes_build', bt)
execute('diabetes_build')

#------ testing the logistic model ------
tt = ClassificationTestTask('diabetes_pd', 'logreg_model', 'diabetes_out')
tt.testDataTargetAttributeName = 'Class'
tt.positiveTargetValue = 'tested_positive'
save('diabetes_test', tt)
execute('diabetes_test')

#------ applying the logistic model ------
pdout = PhysicalData('diabetes_apply')
save('diabetes_pd_apply', pdout)

at = MiningApplyTask()
at.modelName = 'logreg_model'
at.sourceDataName = 'diabetes_pd'
at.targetDataName = 'diabetes_pd_apply'
at.replaceExistingData = TRUE

directMapping = java.util.ArrayList()
asi = ApplySourceItem()
asi.sourceName = 'Class'
asi.destinationName = 'Class'
directMapping.add(asi)
at.setDirectMapping(directMapping)

ao = ClassificationApplyOutput()
ao.item.add(ClassificationRankItem('predicted_target', ClassificationOutputType.\
predictedCategory , 0))

ao.item.add(ClassificationRankItem('prediction_probability', ClassificationOutputType.\
probability , 0))

at.applyOutput = ao

save('logreg_apply', at)
execute('logreg_apply') 

# print model fit

statModel = load('logreg_model').getModelStatistics()
statNames = statModel.getModelStatNames()
print "Model statistics \t value"
for row in range(len(statNames)):
    print statNames[row], " \t ", statModel.getModelStatValue(row)

print    
statModel = load('logreg_model')
varStats =statModel.getModelStatistics().getVariableStatistics()
print "Variable \t Coeff \t Wald Pr>ChiSq"
for v in varStats.getNames():
    print v, "\t", varStats.getVarStatValue(v,"Coeff"), "\t", varStats.getVarStatValue(v,"Wald Pr>ChiSq")
    
    

Output:

Model statistics 	 value
LikelihoodRatio Stat  	  268.0153231411516
LikelihoodRatio Pr>ChiSq  	  0.0
Score Stat  	  280.70602064058977
Score Pr>ChiSq  	  0.0
Wald Stat  	  197.16538747970228
Wald Pr>ChiSq  	  0.0
Hosmer-Lemeshow Stat  	  5.502652326558107
Hosmer-Lemeshow Pr>ChiSq  	  0.7027461211662231
Deviance Residual  	  725.4685875260167
Deviance Pr>ChiSq  	  0.8181082400653304
Concordant  	  0.8375074626865672
Discordant  	  0.1609179104477612
Ties  	  0.0015746268656716418
Pairs  	  134000.0
Sommers' D  	  0.676589552238806
Goodman-Kruskal Gamma  	  0.6776566085403135
Kendall's Tau-a  	  0.307824722946545
ROC c  	  0.838294776119403
Rsq  	  0.2697731893425104

Variable 	 Coeff 	 Wald Pr>ChiSq
Intercept 	 -8.260706943350403 	 0.0
preg 	 0.1254554228868913 	 8.772799138223597E-5
plas 	 0.033440006471360854 	 0.0
pres 	 -0.01350445385203633 	 0.008310735053548624
mass 	 0.08810607587571508 	 7.022885606389195E-10
pedi 	 0.8870372830280561 	 0.0026299182994015124
age 	 0.01667736631080239 	 0.07108043725106061