import javastat.multivariate.DiscriminantAnalysis;
import javastat.util.DataManager;
/**
*
* <p>Example: class DiscriminantAnalysis.</p>
* <p>Data Source: Fisher's iris
data</p>
*/
dm = new DataManager();
testdata = new double[4][];
dm.scanFileToMatrix(System.getProperty("user.dir")
+
System.getProperty("file.separator") +
"beanshell_examples"
+
System.getProperty("file.separator") + "javastat"
+
System.getProperty("file.separator") +
"iris.txt", testdata, 4);
testgroup = new double[150];
for (int j = 0; j < 3; j++)
{
for
(int i = 0; i < 50; i++)
{
testgroup[j
* 50 + i] = j + 1;
}
}
testclass1
= new DiscriminantAnalysis(testgroup, testdata);
linearDiscriminants1
= testclass1.linearDiscriminants;
print("The first discriminant (non-null
constructor) constructor = ["
+ linearDiscriminants1[0][0] + " , " +
linearDiscriminants1[0][1] +
" , "
+ linearDiscriminants1[0][2] + " , "
+ linearDiscriminants1[0][3]
+ "]");
print("The second discriminant (non-null
constructor) constructor = ["
+ linearDiscriminants1[1][0] + " , " +
linearDiscriminants1[1][1] +
" , "
+ linearDiscriminants1[1][2] + " , "
+ linearDiscriminants1[1][3]
+ "]");
testclass2
= new DiscriminantAnalysis();
linearDiscriminants2
= testclass2.linearDiscriminants(testgroup,
testdata);
print("The first discriminant (null
constructor) constructor = ["
+ linearDiscriminants1[0][0] + " , " +
linearDiscriminants1[0][1] +
" , "
+ linearDiscriminants1[0][2] + " , "
+ linearDiscriminants1[0][3]
+ "]");
print("The second discriminant (null
constructor) constructor = ["
+ linearDiscriminants1[1][0] + " , " +
linearDiscriminants1[1][1] +
" , "
+ linearDiscriminants1[1][2] + " , "
+ linearDiscriminants1[1][3]
+ "]");
predata = new double[4][];
dm.scanFileToMatrix(System.getProperty("user.dir")
+
System.getProperty("file.separator") +
"beanshell_examples"
+
System.getProperty("file.separator") + "javastat"
+
System.getProperty("file.separator") +
"iris2.txt", predata, 4);
predictedGroup2
= testclass2.predictedGroup(testgroup,
testdata, predata);
errorRate = 0.0;
for (int i = 0; i < predictedGroup2.length; i++)
{
if
((int) testgroup[i] != predictedGroup2[i])
{
errorRate += 1.0;
}
}
errorRate /= predictedGroup2.length;
print("The error rate (null constructor)
=" + errorRate);
testclass3
= new DiscriminantAnalysis(testgroup, testdata,
testdata);
predictedGroup3
= testclass3.predictedGroup;
preg = predictedGroup3[predictedGroup3.length - 1];
print("The last observation of Iris data was assigned to = group " + preg);
Results:
The
first discriminant based on non-null constructor =
[-0.946
, -1.803 , 1.523 , 4.002]
The
second discriminant based on non-null constructor =
[0.031 ,
1.382 , -1.551 , 4.32]
The
first discriminant based on null constructor = [-0.946 , -1.803 , 1.523 , 4.002]
The
first discriminant based on null constructor = [0.031 , 1.382 , -1.551 , 4.32]
The
error rate based on null constructor = 0.02
The
last observation of Iris data was assigned to = group 3.0