import static java.lang.System.out;

import java.util.*;

 

import javastat.*;

import javastat.regression.glm.*;

import static javastat.util.Argument.*;

import static javastat.util.Output.*;

import javastat.util.*;

 

/**

 *

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

 * <p>Data Source: McCullagh, P. and Nelder, J. A. (1989).

 *    Generalized Linear Models. New York: Chapman and Hall, p. 205. </p>

 */

 

public class LogLinearRegressionExample

{

 

    public static void main(String[] args)

    {

        String[][] shipData = { {"a", "a", "a", "a", "a", "a", "a", "a",

                            "b", "b", "b", "b", "b", "b", "b", "b",

                            "c", "c", "c", "c", "c", "c", "c", "c",

                            "d", "d", "d", "d", "d", "d", "d", "d",

                            "e", "e", "e", "e", "e", "e", "e", "e"},

                           {"1960-64", "1960-64", "1965-69", "1965-69",

                            "1970-74", "1970-74", "1975-79", "1975-79",

                            "1960-64", "1960-64", "1965-69", "1965-69",

                            "1970-74", "1970-74", "1975-79", "1975-79",

                            "1960-64", "1960-64", "1965-69", "1965-69",

                            "1970-74", "1970-74", "1975-79", "1975-79",

                            "1960-64", "1960-64", "1965-69", "1965-69",

                            "1970-74", "1970-74", "1975-79", "1975-79",

                            "1960-64", "1960-64", "1965-69", "1965-69",

                            "1970-74", "1970-74", "1975-79", "1975-79"},

                           {"1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79",

                            "1960-74", "1975-79", "1960-74", "1975-79"} };

 

        double[] offset = {127, 63, 1095, 1095, 1512, 3353, 0, 2244, 44882,

                       17176, 28609, 20370, 7064, 13099, 0, 7177, 1179,

                       552, 781, 676, 783, 1948, 0, 274, 251, 105, 288,

                       192, 349, 1208, 0, 2051, 45, 0, 789, 437, 1157,

                       2161, 0, 542};

 

        double[] damageNumber = {0, 0, 3, 4, 6, 18, 0, 11, 39, 29, 58, 53, 12,

                                44, 0, 18, 1, 1, 0, 1, 6, 2, 0, 1, 0, 0, 0, 0,

                                2, 11, 0, 4, 0, 0, 7, 7, 5, 12, 0, 1};

        DataManager dm = new DataManager();

        LogLinearRegression testclass1 =

new LogLinearRegression(damageNumber, offset, shipData);

        double[] coefficients = testclass1.coefficients;

        double[][] confidenceInterval = testclass1.confidenceInterval;

        double[] testStatistic = testclass1.testStatistic;

        double[] pValue = testclass1.pValue;

        double[][] devianceTable = testclass1.devianceTable;

 

        LogLinearRegression testclass2 = new LogLinearRegression();

        coefficients = testclass2.coefficients(damageNumber, offset, shipData);

        confidenceInterval = testclass2.confidenceInterval(0.1, damageNumber,

                offset, shipData);

        testStatistic =

                testclass2.testStatistic(damageNumber, offset, shipData);

        pValue = testclass2.pValue(damageNumber, offset, shipData);

        devianceTable =

                testclass2.devianceTable(damageNumber, offset, shipData);

 

        Hashtable argument1 = new Hashtable();

        StatisticalAnalysis testclass3 =

                new LogLinearRegression(argument1, damageNumber, offset,

                                       shipData).statisticalAnalysis;

        coefficients = (double[]) testclass3.output.get(COEFFICIENTS);

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

                CONFIDENCE_INTERVAL);

        testStatistic = (double[]) testclass3.output.get(TEST_STATISTIC);

        pValue = (double[]) testclass3.output.get(PVALUE);

        devianceTable =

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

 

        Hashtable argument2 = new Hashtable();

        LogLinearRegression testclass4 =

                new LogLinearRegression(argument2, null);

        coefficients = testclass4.coefficients(argument2, damageNumber, offset,

                                       shipData);

        argument2.put(ALPHA, 0.1);

        confidenceInterval = testclass4.confidenceInterval(argument2,

                damageNumber, offset, shipData);

        testStatistic = testclass4.testStatistic(argument2, damageNumber,

                                       offset, shipData);

        pValue = testclass4.pValue(argument2, damageNumber, offset, shipData);

        devianceTable = testclass4.devianceTable(argument2, damageNumber,

                                            offset, shipData);

    }

 

}

 

Results:

The estimated coefficients based on non-null constructor                   

= [-6.405 , -0.545 , -0.688 , -0.075 , 0.325 , 0.697 , 0.818 , 0.449 , 0.384]

The t statistics based on non-null constructor                             

= [-29.457 , -3.068 , -2.091 , -0.258 , 1.378 , 4.658 , 4.817 , 1.926 , 3.25]

The p-values for the t statistics based on non-null constructor            

= [0.0 , 0.0020 , 0.036 , 0.796 , 0.168 , 0.0 , 0.0 , 0.054 , 0.0010]

The 95% confidence interval for parameter 1 based on non-null constructor  

= [-6.831 , -5.978]

The 95% confidence interval for parameter 2 based on non-null constructor  

= [-0.893 , -0.197]

The 95% confidence interval for parameter 3 based on non-null constructor  

= [-1.333 , -0.043]

The 95% confidence interval for parameter 4 based on non-null constructor  

= [-0.645 , 0.494]

The 95% confidence interval for parameter 5 based on non-null constructor  

= [-0.137 , 0.787]

The 95% confidence interval for parameter 6 based on non-null constructor  

= [0.404 , 0.99]

The 95% confidence interval for parameter 7 based on non-null constructor  

= [0.485 , 1.151]

The 95% confidence interval for parameter 8 based on non-null constructor  

= [-0.0080 , 0.906]

The 95% confidence interval for parameter 9 based on non-null constructor  

= [0.153 , 0.616]

 

          D.F. Difference   Deviation Difference    D.F.     Deviance

Null              0.0         0.0                39.0         146.287

Factor A          4.0        55.481              35.0          90.807

Factor A+B        3.0        41.49               32.0         49.316

Factor A+B+C      1.0       10.658              31.0         38.658

 

Factor A: Ship Type;  Factor B: Year of Construction;  Factor C: Period of Operation

 

{MEANS=[D@119cca4,

PEARSON_RESIDUALS=[D@ca2dce, RESPONSE_RESIDUALS=[D@18558d2,

CONFIDENCE_INTERVAL=[[D@18a47e0, PVALUE=[D@174cc1f,

TEST_STATISTIC=[D@1050e1f, RESPONSE_VARIANCE=[D@e24e2a,

DEVIANCE_TABLE=[[D@179c285, COEFFICIENTS=[D@d1e604}

 

The estimated coefficients based on null constructor                   

= [-6.405 , -0.545 , -0.688 , -0.075 , 0.325 , 0.697 , 0.818 , 0.449 , 0.384]

The z statistics based on null constructor                              

= [-29.457 , -3.068 , -2.091 , -0.258 , 1.378 , 4.658 , 4.817 , 1.926 , 3.25]

The p-values for the t statistics based on null constructor            

= [0.0 , 0.0020 , 0.036 , 0.796 , 0.168 , 0.0 , 0.0 , 0.054 , 0.0010]

The 90% confidence interval for parameter 1 based on null constructor  

= [-6.762 , -6.047]

The 90% confidence interval for parameter 2 based on null constructor  

= [-0.837 , -0.253]

The 90% confidence interval for parameter 3 based on null constructor  

= [-1.229 , -0.147]

The 90% confidence interval for parameter 4 based on null constructor  

= [-0.553 , 0.403]

The 90% confidence interval for parameter 5 based on null constructor  

= [-0.063 , 0.713]

The 90% confidence interval for parameter 6 based on null constructor  

= [0.451 , 0.943]

The 90% confidence interval for parameter 7 based on null constructor  

= [0.539 , 1.097]

The 90% confidence interval for parameter 8 based on null constructor  

= [0.065 , 0.832]

The 90% confidence interval for parameter 9 based on null constructor  

= [0.19 , 0.579]

 

          D.F. Difference   Deviation Difference    D.F.     Deviance

Null              0.0         0.0                39.0         146.287

Factor A          4.0        55.481              35.0          90.807

Factor A+B        3.0        41.49               32.0         49.316

Factor A+B+C      1.0       10.658              31.0         38.658

 

Factor A: Ship Type;  Factor B: Year of Construction;  Factor C: Period of Operation

 

{CONFIDENCE_INTERVAL=[[D@dd5b, PVALUE=[D@c4bcdc,

TEST_STATISTIC=[D@4b4333, COEFFICIENTS=[D@128e20a,

DEVIANCE_TABLE=[[D@1100d7a, MEANS=[D@e4f972}