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 GLM.</p>

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

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

 */

 

public class GLMExample

{

 

    public static void main(String[] args)

    {

        String[][] nominalCovariate = { {"a", "a", "b", "b", "c", "c", "a", "a",

"b", "c", "c", "c"},

                                  {"T", "T", "T", "F", "T", "F", "T",

"F", "F", "T", "F", "F"} };

        String[] binaryResponse = {"s", "d", "s", "d", "s", "d", "d", "s", "d",

                               "s", "d", "d"};

        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();

        Hashtable argument = new Hashtable();

        argument.put(REGRESSION_TYPE, "Logistic");

        StatisticalAnalysis testclass1 =

                new GLM(argument, binaryResponse, nominalCovariate).

                statisticalAnalysis;

        double[] coefficients =

(double[]) testclass1.output.get(COEFFICIENTS);

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

                CONFIDENCE_INTERVAL);

        double[] testStatistic = (double[]) testclass1.output.get(

                TEST_STATISTIC);

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

        double[][] devianceTable = (double[][]) testclass1.output.get(

                DEVIANCE_TABLE);

 

        argument.put(REGRESSION_TYPE, "Log_Linear");

        StatisticalAnalysis testclass2 =

                new GLM(argument, damageNumber, offset, shipData).

                statisticalAnalysis;

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

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

                CONFIDENCE_INTERVAL);

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

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

        devianceTable =

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

 

    }

 

}

 

Results:

The estimated coefficients based on non-null constructor                   

= [-1.928 , 0.259 , 0.447 , 2.436]

The t statistics based on non-null constructor                             

= [-1.118 , 0.135 , 0.269 , 1.594]

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

= [0.263 , 0.893 , 0.788 , 0.111]

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

= [-5.306 , 1.451]

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

= [-3.511 , 4.029]

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

= [-2.81 , 3.704]

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

= [-0.56 , 5.431]

 

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

Null              0.0             0.0           11.0         12.482

Factor A          2.0             0.206          9.0         12.275

Factor A+B        1.0             3.123          8.0          9.152

 

 

{PVALUE=[D@a59698, FITTED_VALUES=[D@141d683,

DEVIANCE_TABLE=[[D@16a55fa, CONFIDENCE_INTERVAL=[[D@32c41a,

DEVIANCE_RESIDUALS=[D@e89b94, TEST_STATISTIC=[D@13e205f,

RESPONSE_RESIDUALS=[D@1bf73fa, RESPONSE_VARIANCE=[D@5740bb,

MEANS=[D@5ac072, PEARSON_RESIDUALS=[D@109a4c,

COEFFICIENTS=[D@201f9}

 

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

 

 

{PVALUE=[D@17a8913, DEVIANCE_TABLE=[[D@1bd747e,

CONFIDENCE_INTERVAL=[[D@12b3374, TEST_STATISTIC=[D@13f3789,

RESPONSE_RESIDUALS=[D@1571886, MEANS=[D@10b4199,

RESPONSE_VARIANCE=[D@1ffb8dc, PEARSON_RESIDUALS=[D@1c184f4,

COEFFICIENTS=[D@1ffbd68}