import static java.lang.System.out;

import java.util.*;

 

import javastat.*;

import javastat.multivariate.*;

import static javastat.util.Argument.*;

import static javastat.util.Output.*;

import javastat.util.*;

 

/**

 *

 * <p>Example: class PCA.</p>

 * <p>Data Source: S-PLUS 6 for Windows Guide to Statistics, Volume 2,

 *    Insightful Corporation, Seattle, WA.,  pp. 40-41.</p>

 */

 

public class PCAExample

{

 

    public static void main(String arg[])

    {

        double[][] testscores = { {36, 62, 31, 76, 46, 12, 39, 30, 22, 9, 32,

                              40, 64, 36, 24, 50, 42, 2, 56, 59, 28, 19,

                              36, 54, 14},

                             {58, 54, 42, 78, 56, 42, 46, 51, 32, 40, 49,

                              62, 75, 38, 46, 50, 42, 35, 53, 72, 50, 46,

                              56, 57, 35},

                             {43, 50, 41, 69, 52, 38, 51, 54, 43, 47, 54,

                              51, 70, 58, 44, 54, 52, 32, 42, 70, 50, 49,

                              56, 59, 38},

                             {36, 46, 40, 66, 56, 38, 54, 52, 28, 30, 37,

                              40, 66, 62, 55, 52, 38, 22, 40, 66, 42, 40,

                              54, 62, 29},

                             {37, 52, 29, 81, 40, 28, 41, 32, 22, 24, 52,

                              49, 63, 62, 49, 51, 50, 16, 32, 62, 63, 30,

                              52, 58, 20} };

        DataManager dm = new DataManager();

 

        PCA testclass1 = new PCA(0.95, "covariance", testscores);

 

        PCA testclass2 = new PCA();

        double[][] principalComponents = testclass2.principalComponents(

                testscores);

        double[] variance = testclass2.variance(testscores);

 

        Hashtable argument1 = new Hashtable();

        argument1.put(LEVEL, 0.95);

        argument1.put(COVARIANCE_CHOICE, "covariance");

        StatisticalAnalysis testclass3 = new PCA(argument1, testscores).

                                         statisticalAnalysis;

        principalComponents = (double[][]) testclass3.output.get(

                PRINCIPAL_COMPONENTS);

        variance =

(double[]) testclass3.output.get(COMPONENT_VARIANCE);

 

        Hashtable argument2 = new Hashtable();

        PCA testclass4 = new PCA(argument2, null);

        principalComponents = testclass4.principalComponents(argument2,

                testscores);

        variance = testclass4.variance(argument2, testscores);

    }

 

}

 

Results:

Covariance Matrix:

1st component vector       =0.598  0.361  0.302  0.389  0.519

2nd component vector      = -0.675  -0.245  0.214  0.338  0.57

3rd component vector      = -0.185  0.249  0.211  0.7  -0.607

4th component vector      = 0.386  -0.829  -0.135  0.375  -0.072

variances of components   = 845.481  85.041  45.388  39.188

 

{COMPONENT_VARIANCE=[D@1270b73,

PRINCIPAL_COMPONENTS=[[D@60aeb0}

 

Correlation Matrix:

1st component vector      = 0.443  0.437  0.462  0.445  0.449

2nd component vector     = -0.473  -0.616  0.276  0.47  0.317

3rd component vector     = -0.352  0.368  0.21  0.47  -0.69

4th component vector     = 0.615  -0.424  -0.338  0.476  -0.318

5th component vector     = -0.279  0.339  -0.743  0.367  0.347

variances of components  = 4.081  0.374  0.217  0.205  0.124

 

{COMPONENT_VARIANCE=[D@16caf43,

PRINCIPAL_COMPONENTS=[[D@66848c}