In [1]:
using DataFrames, RDatasets

In [2]:
vn = dataset("Ecdat","VietNamI")

Out[2]:
PharvisLnhhExpAgeSexMarriedEducIllnessInjuryIlldaysActdaysInsuranceCommune
102.7303633.7612male1210700192
202.7372482.944439female0010400167
302.2669352.56495male040000176
412.3927533.637586female1310301123
513.1053353.295837male13101000148
603.7608843.367296male190000120
703.1556093.663562female120000140
803.7246822.197225male050000157
922.8616913.7612female122040049
1032.6150774.234107male1010700170
1112.6532432.772589male041010040
1212.1398573.663562female1220500127
1322.6256833.555348female1310300106
1412.7677461.94591female0520300168
1502.8712422.302585male0000000185
1621.9838113.135494male1430100041
1701.2602013.091043male0400000106
1802.2981783.401197male1200000141
1932.1325083.332205male1430100061
2002.0002313.258096male1300000159
2102.0183753.218876male1610700121
2211.8861422.639057female021030056
23102.9531251.098612female022040034
2402.3784813.526361male1000000127
2501.5743763.555348male1200000135
2632.4955143.637586female1310500147
2712.3180773.7612male141050094
2802.0290454.248495female13103000125
2911.7887543.610918female133030079
3002.0911072.079442female0000000143

# Predicting "Days Ill" based on historic data¶

In [6]:
days_ill = vn[:Illdays]

Out[6]:
27765-element DataArray{Int32,1}:
7
4
0
3
10
0
0
0
4
7
1
5
3
⋮
3
10
0
1
0
0
30
3
0
0
20
7

### We've isolated the signal we want to analyze, now we remove it from the feature set¶

In [7]:
delete!(vn,:Illdays)

Out[7]:
PharvisLnhhExpAgeSexMarriedEducIllnessInjuryActdaysInsuranceCommune
102.7303633.7612male121000192
202.7372482.944439female001000167
302.2669352.56495male04000176
412.3927533.637586female131001123
513.1053353.295837male131000148
603.7608843.367296male19000120
703.1556093.663562female12000140
803.7246822.197225male05000157
922.8616913.7612female12200049
1032.6150774.234107male101000170
1112.6532432.772589male04100040
1212.1398573.663562female122000127
1322.6256833.555348female131000106
1412.7677461.94591female052000168
1502.8712422.302585male000000185
1621.9838113.135494male14300041
1701.2602013.091043male040000106
1802.2981783.401197male120000141
1932.1325083.332205male14300061
2002.0002313.258096male130000159
2102.0183753.218876male161000121
2211.8861422.639057female02100056
23102.9531251.098612female02200034
2402.3784813.526361male100000127
2501.5743763.555348male120000135
2632.4955143.637586female131000147
2712.3180773.7612male14100094
2802.0290454.248495female131000125
2911.7887543.610918female13300079
3002.0911072.079442female000000143
In [12]:
# Pkg.add("DecisionTree")
using DecisionTree


### The DecisionTree package works with Julia Arrays, so we make those conversions¶

In [19]:
signals = convert(Array,days_ill)
features = convert(Array,vn);

In [13]:
methods(build_forest)

Out[13]:
5 methods for generic function build_forest:

# Random Forest¶

### build_forest parameters are:¶

signals _array of the signal we want to calculate
features the corresponding feature array that indicates those signals
festures used the number features for the each split or branch of the tree
number of trees trees in the forrest, larger takes longer, but could be more accurate.
sampling rate number lowered from 1.0 to favor minority signals

In [20]:
model = build_forest(signals,features,int(sqrt(length(features[1,:]))),10,.9)

Out[20]:
Ensemble of Decision Trees
Trees:      10
Avg Leaves: 3674.7
Avg Depth:  24.3

### Apply the algorithm to any 2D Matix of features¶

In [26]:
predictions = apply_forest(model,features)

Out[26]:
27765-element Array{Any,1}:
7
3
0
3
10
0
0
0
4
7
2
5
3
⋮
3
10
0
1
0
0
30
3
0
0
20
7
In [24]:
confusion_matrix(signals,predictions)

32x32 Array{Int64,2}:
16566    0     5    10     1    1    0  …  0  0  0   0  0  0    0  0    1  0
1  290    68    68    16    5    0     0  0  0   0  0  0    0  0    0  0
1    4  1383   114    41    7    0     0  0  0   1  0  0    0  0    0  0
0    2    87  1856    45    7    2     0  0  0   0  0  0    1  0    0  0
1    1    32    95  1129    4    1     0  0  0   1  0  0    1  0    0  0
1    0    56   143    46  841    1  …  0  0  0   0  0  0    1  0    0  0
1    0    23    55    26    8  287     0  0  0   0  0  0    2  0    0  0
1    0    24    66    16    8    0     0  0  0   0  0  0    0  0    0  0
0    0     4    16    12    3    1     0  0  0   0  0  0    0  0    0  0
0    0     1     6     0    0    0     0  0  0   0  0  0    0  0    0  0
0    0    12    40    18    5    1  …  0  0  0   0  0  0    0  0    0  0
0    0     0     1     0    1    0     0  0  0   0  0  0    0  0    0  0
1    0     2     4     6    1    0     0  0  0   0  0  0    1  0    0  0
⋮                           ⋮       ⋱            ⋮                  ⋮
1    3     7    23    13    6    0  …  0  0  0   1  0  0    3  0    0  0
0    0     0     1     1    0    0     0  0  0   0  0  0    0  0    0  0
0    0     0     0     0    0    0     1  0  0   0  0  0    0  0    0  0
0    0     0     0     0    0    0     0  3  0   0  0  0    0  0    0  0
0    0     0     0     0    0    0     0  0  3   0  0  0    0  0    0  0
0    0     0     0     1    2    0  …  0  0  0  56  0  0    0  0    0  0
0    0     1     1     0    1    0     0  0  0   0  2  0    0  0    0  0
0    0     0     0     0    0    0     0  0  0   0  0  3    0  0    0  0
0    0    13    22     4    3    0     0  0  0   0  0  0  274  0    0  0
0    0     0     0     0    0    0     0  0  0   0  0  0    0  1    0  0
0    1     7    18     6    4    1  …  0  0  0   0  0  0    0  0  153  0
0    0     0     0     1    0    0     0  0  0   0  0  0    0  0    0  0
Out[24]:
Classes:  {0,1,2,3,4,5,6,7,8,9  …  22,23,24,25,26,27,28,29,30,60}
Matrix:
Accuracy: 0.9280388978930308
Kappa:    0.8846820611436211
In [ ]: