Examples

Standard linear regression example

The following script presents the abilities of the Linear 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 37.1. Regresja linear


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

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

#------ Approximation function settings ------

fs = ApproximationFunctionSettings()
fs.logicalData = ld
fs.targetAttributeName = 'TE'
fs.attributeUsageSet.getAttribute('season').setUsage(UsageOption.inactive)

#------ Linear Regression algorithm settings ------

as = LinearRegressionSettings()
as.preselection = TRUE
as.intercept = TRUE

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

as.variableSelectionSettings = vss
fs.algorithmSettings = as

save('cloud_pd', pd)
save('cloud_ld', ld)
save('reg_settings', fs)

#------ model building ------

bt = MiningBuildTask('cloud_pd', 'reg_settings', 'reg_model')
save('cloud_build', bt)
execute('cloud_build')

#------ model testing ------

tt = ApproximationTestTask('cloud_pd', 'reg_model', 'cloud_out')
tt.testDataTargetAttributeName = 'TE'
save('cloud_test', tt)
execute('cloud_test')

#----- model application ------

pdout = PhysicalData('cloud_apply')
save('cloud_pd_apply', pdout)
at = MiningApplyTask()
at.modelName = 'reg_model'
at.sourceDataName = 'cloud_pd'
at.targetDataName = 'cloud_pd_apply'
at.replaceExistingData = TRUE

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

ao = ApproximationApplyOutput()
aai = ApproximationOutputItem()
aai.setDestinationName('predicted_target')
aai.setOutputType(ApproximationOutputType.predictedValue)
ao.item.add(aai)

at.applyOutput = ao

save('cloud_apply', at)
execute('cloud_apply')

# print model fit

statModel = load('reg_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('reg_model')
varStats =statModel.getModelStatistics().getVariableStatistics()
print "Variable \t Coeff \t VIF"
for v in varStats.getNames():
    print v, "\t", varStats.getVarStatValue(v,"Coeff"), "\t", varStats.getVarStatValue(v,"VIF")


Output:

Model statistics 	 value
dfR  	  3.0
SSR  	  109.91431878975642
MSR  	  36.63810626325214
dfE  	  104.0
SSE  	  14.2393552843176
MSE  	  0.13691687773382308
dfT  	  107.0
SST  	  124.15367407407402
F-test  	  267.59379025922163
Pr>F  	  0.0
s  	  0.37002280704548884
Rsq  	  0.8853086274690353
ADJRsq  	  0.8820002224921806

Variable 	 Coeff 	 VIF
Intercept 	 -0.04717301845975744 	 NaN
NC 	 0.47842241886110154 	 3.7075209337995614
SC 	 0.7328626001274345 	 1.9650100440149518
NWC 	 -0.14680322892118228 	 4.256510917786886

IRLS regression example

The following script presents the abilities of IRLS Regression. 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 37.2. Regresja IRLS

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

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

#------ Approximation function settings ------

fs = ApproximationFunctionSettings()
fs.logicalData = ld
fs.targetAttributeName = 'TE'
fs.attributeUsageSet.getAttribute('season').setUsage(UsageOption.inactive)
fs.attributeUsageSet.getAttribute('seeded').setUsage(UsageOption.inactive)

#------ Linear Regression algorithm settings ------

as = IRLSSettings()
as.preselection = TRUE
as.intercept = TRUE

vss = VariableSelectionSettings()
vss.variableSelectionMethod = VariableSelectionMethod.full


as.variableSelectionSettings = vss
fs.algorithmSettings = as

save('cloud_pd', pd)
save('cloud_ld', ld)
save('reg_settings', fs)

#------ model building ------

bt = MiningBuildTask('cloud_pd', 'reg_settings', 'reg_model')
save('cloud_build', bt)
execute('cloud_build')

#------ model testing ------

tt = ApproximationTestTask('cloud_pd', 'reg_model', 'cloud_out')
tt.testDataTargetAttributeName = 'TE'
save('cloud_test', tt)
execute('cloud_test')

#----- model application ------

pdout = PhysicalData('cloud_apply')
save('cloud_pd_apply', pdout)
at = MiningApplyTask()
at.modelName = 'reg_model'
at.sourceDataName = 'cloud_pd'
at.targetDataName = 'cloud_pd_apply'
at.replaceExistingData = TRUE

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

ao = ApproximationApplyOutput()
aai = ApproximationOutputItem()
aai.setDestinationName('predicted_target')
aai.setOutputType(ApproximationOutputType.predictedValue)
ao.item.add(aai)

at.applyOutput = ao

save('cloud_apply', at)
execute('cloud_apply')

# print model fit


statModel = load('reg_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('reg_model')
varStats =statModel.getModelStatistics().getVariableStatistics()
print "Variable \t Coeff \t VIF"
for v in varStats.getNames():
    print v, "\t", varStats.getVarStatValue(v,"Coeff"), "\t", varStats.getVarStatValue(v,"VIF")

Output:

Model statistics 	 value
dfR  	  4.0
SSR  	  92.55068427595378
MSR  	  23.137671068988446
dfE  	  103.0
SSE  	  6.71296171838018
MSE  	  0.06517438561534156
dfT  	  107.0
SST  	  99.26364599433397
F-test  	  355.011725090079
Pr>F  	  0.0
s  	  0.255292744932835
Rsq  	  0.9323724043063725
ADJRsq  	  0.9297460899105034

Variable 	 Coeff 	 VIF
Intercept 	 -0.066814006025922 	 NaN
period 	 4.707606205758679E-4 	 1.1405808437704104
NC 	 0.472707106434072 	 4.1130343485278
SC 	 0.637350132259273 	 2.777231115978932
NWC 	 -0.10911174793879069 	 5.334078732767622