import static java.lang.System.out;

import java.util.*;

 

import javastat.*;

import javastat.regression.lm.*;

import static javastat.util.Argument.*;

import static javastat.util.Output.*;

import javastat.util.*;

 

/**

 *

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

 * <p>Data Source: Anderson, D. R., Sweeney, D. J. and Williams, T. A. (2001).

 *    Contemporary Business Statistics with Microsoft Excel.

 *    South-Western, p. 468, p.530.

 </p>

 */

 

public class LinearRegressionExample

{

 

    public static void main(String arg[])

    {

        double[] pizzaResponse = {58, 105, 88, 118, 117, 137, 157, 169, 149,

                              202};

        double[] pizzaCovariate = {2, 6, 8, 8, 12, 16, 20, 20, 22, 26};

        double[] hellerResponse = {102, 100, 120, 77, 46, 93, 26, 69, 65, 85};

        double[] hellerCovariate1 = {120, 140, 190, 130, 155, 175, 125, 145,

                                180, 150};

        double[] hellerCovariate2 = {100, 110, 90, 150, 210, 150, 250, 270, 300,

                                250};

        DataManager dm = new DataManager();

        LinearRegression myClass1 = new LinearRegression(pizzaResponse,

                pizzaCovariate);

        double[] coefficients = myClass1.coefficients;

        double[] fittedValues = myClass1.fittedValues;

        double[] residuals = myClass1.residuals;

        double[] testStatistic = myClass1.testStatistic;

        double[] pValue = myClass1.pValue;

        double[][] confidenceInterval = {myClass1.confidenceInterval[0],

                                    myClass1.confidenceInterval[1]};

        double rSquare = myClass1.rSquare;

        double testFStatistic = myClass1.testFStatistic;

        double fPValue = myClass1.fPValue;

 

        LinearRegression myClass2 = new LinearRegression();

        coefficients = myClass2.coefficients(hellerResponse, hellerCovariate1,

                                       hellerCovariate2);

        confidenceInterval = myClass2.confidenceInterval(0.05, hellerResponse,

hellerCovariate1, hellerCovariate2);

        testStatistic = myClass2.testStatistic(hellerResponse, hellerCovariate1,

                                       hellerCovariate2);

        pValue = myClass2.pValue(hellerResponse, hellerCovariate1,

                               hellerCovariate2);

        testFStatistic = myClass2.testFStatistic(hellerResponse,

                                          hellerCovariate1,

                                          hellerCovariate2);

        fPValue = myClass2.fPValue(hellerResponse, hellerCovariate1,

                                 hellerCovariate2);

        confidenceInterval = myClass2.confidenceInterval(hellerResponse,

                hellerCovariate1, hellerCovariate2);

 

        Hashtable argument1 = new Hashtable();

        argument1.put(ALPHA, 0.05);

        StatisticalAnalysis myClass3 = new LinearRegression(argument1,

                pizzaResponse, pizzaCovariate).statisticalAnalysis;

        confidenceInterval =

            (double[][]) myClass3.output.get(CONFIDENCE_INTERVAL);

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

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

        rSquare = (Double) myClass3.output.get(R_SQUARE);

        testFStatistic = (Double) myClass3.output.get(F_STATISTIC);

        fPValue = (Double) myClass3.output.get(F_PVALUE);

        residuals = (double[]) myClass3.output.get(RESIDUALS);

        fittedValues = (double[]) myClass3.output.get(FITTED_VALUES);

 

        Hashtable argument2 = new Hashtable();

        LinearRegression myClass4 = new LinearRegression(argument2, null);

        coefficients = myClass4.coefficients(

           argument2, hellerResponse, hellerCovariate1, hellerCovariate2);

        argument2.put(ALPHA, 0.05);

        confidenceInterval = myClass4.confidenceInterval(argument2,

                hellerResponse, hellerCovariate1, hellerCovariate2);

        testStatistic = myClass4.testStatistic(argument2, hellerResponse,

                                       hellerCovariate1,

                                       hellerCovariate2);

        pValue = myClass4.pValue(argument2, hellerResponse,

hellerCovariate1, hellerCovariate2);

        testFStatistic = myClass4.testFStatistic(argument2, hellerResponse,

                                          hellerCovariate1,

                                          hellerCovariate2);

        fPValue = myClass4.fPValue(argument2, hellerResponse,

hellerCovariate1, hellerCovariate2);

    }

 

}

 

Results:

The estimated coefficients based on non-null constructor      = [60.0 , 5.0]

The t statistics based on non-null constructor                = [6.503 , 8.617]

The p-values for the t statistics based on non-null constructor  = [0.0 , 0.0]

The confidence interval for the intercept based on non-null constructor =

[41.917 , 78.083]

The confidence interval for the slope based on non-null constructor    =

[3.863 , 6.137]

The r-square based on non-null constructor                  =  0.903

The f statistic based on non-null constructor                 =  74.248

The p-value for the F statistic based on non-null constructor    =  0.0

 

{FITTED_VALUES=[D@e89b94, RESIDUALS=[D@13e205f,

RSS=1530.00, CONFIDENCE_INTERVAL=[[D@1bf73fa,

MSE=191.25, HAT_MATRIX=[[D@5740bb, R_SQUARE=0.9027, SSR=14200,

COEFFICIENT_VARIANCE=[[D@5ac072, F_PVALUE=2.549E-5,

PVALUE=[D@109a4c, TEST_STATISTIC=[D@201f9,

DEGREE_OF_FREEDOM=[D@1cf8583, F_STATISTIC=74.248,

COEFFICIENTS=[D@14693c7, SST=15730.0, SSE=1530.00}

 

The estimated coefficients based on null constructor    = [66.518 , 0.414 , -0.27]

The t statistics based on null constructor              = [1.588 , 1.59 , -3.335]

The p-values for the t statistics based on null constructor = [0.156 , 0.156 , 0.013]

The confidence interval for parameter 1 based on null constructor  =

 [-15.56 , 148.595]

The confidence interval for parameter 2 based on null constructor =

 [-0.096 , 0.924]

The confidence interval for parameter 3 based on null constructor =

[-0.428 , -0.111]

The f statistic based on null constructor                =  6.579

The p-value for the f statistic based on null constructor   =  0.025

The predicted quantity sold                          =  93.718

 

{FITTED_VALUES=[D@901887, RESIDUALS=[D@3a6727, RSS=2457.311,

CONFIDENCE_INTERVAL=[[D@4a65e0, MSE=351.044,

HAT_MATRIX=[[D@665753, R_SQUARE=0.653, SSR=4618.789,

COEFFICIENT_VARIANCE=[[D@ef22f8, F_PVALUE=0.0247,

PVALUE=[D@1e0cf70, TEST_STATISTIC=[D@52fe85,

DEGREE_OF_FREEDOM=[D@c40c80, F_STATISTIC=6.579,

COEFFICIENTS=[D@110d81b, SST=7076.100, SSE=2457.311}