/*
*   CLASS:      ANOVA
*
*   USAGE:      Class for performing a one way ANOVA
*
*   WRITTEN BY: Dr Michael Thomas Flanagan
*
*   DATE:       December 2011 - January 2012
*   AMENDED:    7 January 2012 
*
*   DOCUMENTATION:
*   See Michael Thomas Flanagan's Java library on-line web pages:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*   http://www.ee.ucl.ac.uk/~mflanaga/java/ANOVA.html
*
*   Copyright (c) 2011-2012  Michael Thomas Flanagan
*
*   PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/


package flanagan.analysis;

import java.util.*;
import java.math.*;
import java.text.*;

import flanagan.math.*;
import flanagan.io.*;

public class ANOVA{
 
    private String[] title = new String[3];                                         // title
    private int nTitle = 0;                                                         // number of title lines
    private boolean titleCheck = false;                                             // = true when title entered
  
    private String inputFilename = null;                                            // input file name if input data read from file
    private String outputFilename = null;                                           // output file name if output written to file
    private int fileOption = 1;                                                     // type of file option
                                                                                    // option = 1 - text file  (.txt)
                                                                                    // option = 2 - MS Excel file (.xls)
    private boolean fileOptionSet = false;                                          // = true if fileOption changed by user
  
    private int trunc = 6;                                                          // number of decimal places in output data
    private int fieldD = 13;                                                        // decimal and String field length in output files
                                                                                    // overriden by the precision of the input data if this is greater
    private boolean truncAll = false;                                               // if true - the above truncation is not overriden by the precision of the input data if this is greater

    private int originalDataType = -1;                                              // = 1  - String[][]  (including read from file);
                                                                                    // = 2  - double[][]
                                                                                    // = 3  - Matrix
                                                                                    // = 4  - float[][]
                                                                                    // = 5  - int[][]
                                                                                    // = 6  - char[][]
                                                                                    // = 7  - boolean[][]
                                                                                    // = 8  - long[][]
                                                                                    // = 9  - BigDecimal[][]
                                                                                    // = 10 = BigInteger[][]

    private Object originalData = null;                                             // Original data as entered

    private double[][]responses0 = null;                                            // individual responses as double
    private BigDecimal[][]responsesBD = null;                                       // individual responses as BigDecimal                                                                                   // arranged as rows of responses for each group
                                                                                    // e.g. responses0[0][0] to responses0[0][nIndividuals-1] =  responses in turn for the first group
                                                                                    //      responses0[1][0] to responses0[1][nIndividuals-1] =  responses in turn for the second group
                                                                                    // etc.
    
    private boolean dataEntered = false;                                            // = true when responses entered
    private boolean bigDecimal = false;                                             // = true if data entered a BigDecimal or BigInteger
   
    private int nGroups = 0;                                                        // number of groups
    private String[] groupNames = null;                                             // names of the groups
    private boolean groupNamesSet = false;                                          // = true when group names entered

    private int nTotalResponses = 0;                                                // total number of responses
    private int[] nResponsesPerGroup = null;                                        // number of responses for each group
    private int[] typePerGroup = null;                                              // data type for each group
    
    private String[] dichotomousS = {"yes", "no", "y", "n", "true", "false"};       // dichotomous options as words
    private char[] dichotomousC = {'y', 'n'}; 
    private double dichotTrue = 1.0;                                                // value of true, yes, y ... in a dichotomous pait 
    private double dichotFalse = -1.0;// dichotomous options as char
    private double[] dichotomousDoubleS = {dichotTrue, dichotFalse, dichotTrue, dichotFalse, dichotTrue, dichotFalse};  // double equivalents of the word pairs    
    private double[] dichotomousDoubleC = {dichotTrue, dichotFalse};                // double equivalents of the char pairs
    private int nDichotomousS = 6;                                                  // number of dichotomous words
    private int nDichotomousC = 2;                                                  // number of dichotomous char    private double dichotFalse = -1.0;                                              // value of flase, no, n ... in a dichotomous pait 
      
    private boolean nFactorOption = false;                                          // = true  varaiance, covariance and standard deviation denominator = n
                                                                                    // = false varaiance, covariance and standard deviation denominator = n-1
    private double[] groupMeans = null;                                             // group means as double
    private BigDecimal[] groupMeansBD = null;                                       // group means as bigDecimal
    private double[] groupSD = null;                                                // group standard deviations
    private double[] groupSE = null;                                                // group standard errors
    private double[] groupClb = null;                                               // lower bound of the group mean entered confidence limit
    private double[] groupCub = null;                                               // upper bound of the group mean entered confidence limit
    private double[] groupMinimum = null;                                           // group minimum as double
    private BigDecimal[] groupMinimumBD = null;                                     // group minimum as BigDecimal
    private double[] groupMaximum = null;                                           // group maximum as double
    private BigDecimal[] groupMaximumBD = null;                                     // group maximum as BigDecimal
    private double[] groupSS = null;                                                // group sum of squares as double
    private BigDecimal[] groupSSBD = null;                                          // group sum of squares as BigDecimal
    private double[] groupMedians = null;                                           // group medians as double
    private BigDecimal[] groupMediansBD = null;                                     // group medians as BigDecimal
    private double[] groupMomentSkewness = null;                                    // group moment skewnesses as double 
    private double[] groupMedianSkewness = null;                                    // group median skewnesses as double 
    private double[] groupQuartileSkewness = null;                                  // group quartile skewnesses as double 
    private BigDecimal[] groupQuartileSkewnessBD = null;                            // group quartile skewnesses as BigDecimal 
    private double[] groupKurtosis = null;                                          // group kurtoses as double 
    private BigDecimal[] groupKurtosisBD = null;                                    // group kurtoses as BigDecimal  
    private double[] groupExcessKurtosis = null;                                    // group excess kurtoses as double 
    private BigDecimal[] groupExcessKurtosisBD = null;                              // group excess kurtoses as BigDecimal 
    private double[] groupProbPlotR = null;                                         // group gaussian probability plot correlation coefficients
    private double[] groupProbPlotGradient = null;                                  // group gaussian probability plot gradients
    private double[] groupProbPlotIntercept = null;                                 // group gaussian probability plot correlation intercepts
    private double[] groupProbPlotMu = null;                                        // group gaussian probability plot mu
    private double[] groupProbPlotSigma = null;                                     // group gaussian probability plot sigma
    private boolean groupStatsDone = false;                                         // = true when group statistics have been performed
    
    private double totalMean = 0.0;                                                 // total mean as double
    private BigDecimal totalMeanBD = new BigDecimal(0.0);                           // total mean as BigDecimal
    private double totalSD = 0.0;                                                   // total standard deviation
    private double totalSE = 0.0;                                                   // total error
    private double totalClb = 0.0;                                                  // lower bound of the total mean entered confidence limit
    private double totalCub = 0.0;                                                  // upper bound of the total mean entered confidence limit
    private double totalMinimum = 0.0;                                              // total minimum as double
    private BigDecimal totalMinimumBD = new BigDecimal("0.0");                      // total minimum as BigDecimal
    private double totalMaximum = 0.0;                                              // total maximum as double
    private BigDecimal totalMaximumBD = new BigDecimal("0.0");                      // total maximum as BigDecimal
    private double totalSS = 0.0;                                                   // total sum of squares as double
    private BigDecimal totalSSBD = new BigDecimal("0.0");                           // total sum of squares as BigDecimal
    private double totalMedian = 0.0;                                               // total medians as double
    private BigDecimal totalMedianBD = new BigDecimal("0.0");                       // total medians as BigDecimal
    private double totalMomentSkewness = 0.0;                                       // total moment skewnesses as double 
    private double totalMedianSkewness = 0.0;                                       // total median skewnesses as double 
    private double totalQuartileSkewness = 0.0;                                     // total quartile skewnesses as double 
    private BigDecimal totalQuartileSkewnessBD = new BigDecimal("0.0");             // total quartile skewnesses as BigDecimal 
    private double totalKurtosis = 0.0;                                             // total kurtoses as double 
    private BigDecimal totalKurtosisBD = new BigDecimal("0.0");                     // total kurtoses as BigDecimal  
    private double totalExcessKurtosis = 0.0;                                       // total excess kurtoses as double 
    private BigDecimal totalExcessKurtosisBD = new BigDecimal("0.0");               // total excess kurtoses as BigDecimal 
    private double totalProbPlotR = 0.0;                                            // total gaussian probability plot correlation coefficient
    private double totalProbPlotGradient = 0.0;                                     // total gaussian probability plot gradient
    private double totalProbPlotIntercept = 0.0;                                    // total gaussian probability plot correlation intercept
    private double totalProbPlotMu = 0.0;                                           // total gaussian probability plot mu
    private double totalProbPlotSigma = 0.0;                                        // total gaussian probability plot sigma
 
                                             
    private int dofTotal = 0;                                                       // total degrees of freedom
    private int dofWithinGroups = 0;                                                // degrees of freedom within groups
    private int dofBetweenGroups = 0;                                               // degrees of freedom between groups
  
    private double ssTotal = 0.0;                                                   // total sum of squares as double
    private double ssWithin = 0.0;                                                  // sum of squares within groups as double
    private double ssBetween = 0.0;                                                 // sum of squares between groups as double
    private BigDecimal ssTotalBD = new BigDecimal("0.0");                           // total sum of squares as BigDecimal
    private BigDecimal ssWithinBD = new BigDecimal("0.0");                          // sum of squares within groups as BigDecimal
    private BigDecimal ssBetweenBD = new BigDecimal("0.0");                         // sum of squares between groups as BigDecimal
   
    private double meanSquareTotal = 0.0;                                           // total  mean square as double
    private double meanSquareWithin = 0.0;                                          // mean square within groups as double
    private double meanSquareBetween = 0.0;                                         // mean square between groups as double
    private BigDecimal meanSquareTotalBD = new BigDecimal("0.0");                   // total mean square as BigDecimal
    private BigDecimal meanSquareWithinBD = new BigDecimal("0.0");                  // mean square within groups as BigDecimal
    private BigDecimal meanSquareBetweenBD = new BigDecimal("0.0");                 // mean square between groups as BigDecimal
    
    private double fRatio = 0.0;                                                    // one way ANOVA F-ratio as double
    private BigDecimal fRatioBD = new BigDecimal("0.0");                            // one way ANOVA F-ratio as BigDecimal
    private double fRatioP = 0.0;                                                   // one way ANOVA F-ratio probability
    private boolean oneWayDone = false;                                             // = true when one way ANOVA complete
    private double criticalSignificance = 0.95;                                      // critical probability
    private String criticalSignificanceS = "95%";                                    // critical probability as percentage as String;
    private String criticalSignificanceF = "5%";                                     // 1 - critical probability as percentage as String;
    private double criticalFratio = 0.0;                                            // critical F-ratio
    
    private boolean[] compGroups = null;                                            // element = true if group to be included in the group comparisons
    private boolean comparison = false;                                             // = true if comparison between pairs of groups required
    private int nComparisons = 0;                                                   // number of pair comparisons
    private int[][] pairIndices = null;                                             // indices of comparison pairs
    private double[] meanSquareTotalG = null;                                       // total  mean square of pair comparisons as double
    private double[] meanSquareWithinG = null;                                      // mean square within groups of pair comparisons as double
    private double[] meanSquareBetweenG = null;                                     // mean square between groups of pair comparisons as double
    private BigDecimal[] meanSquareTotalBDG = null;                                 // total mean square of pair comparisons as BigDecimal
    private BigDecimal[] meanSquareWithinBDG = null;                                // mean square within groups of pair comparisons as BigDecimal
    private BigDecimal[] meanSquareBetweenBDG =null;                                // mean square between groups of pair comparisons as BigDecimal
    private double[] fRatioG = null;                                                // one way ANOVA F-ratio of pair comparisons as double
    private BigDecimal[] fRatioBDG = null;                                          // one way ANOVA F-ratio of pair comparisons as BigDecimal
    private double[] fRatioPG = null;                                               // one way ANOVA F-ratio probability of pair comparisons
    private double[] criticalFratioG = null;                                        // critical F-ratio of pair comparisons
    private double[] ssTotalG = null;                                               // total sum of squares of pair comparisons                  
    private double[] ssWithinG = null;                                              // within groups sum of squares of pair comparisons   
    private double[] ssBetweenG = null;                                             // between groups sum of squares of pair comparisons   
    private BigDecimal[] ssTotalBDG = null;                                         // total sum of squares of pair comparisons as BigDecimal                 
    private BigDecimal[] ssWithinBDG = null;                                        // within groups sum of squares of pair comparisons as BigDecimal  
    private BigDecimal[] ssBetweenBDG = null;                                       // between groups sum of squares of pair comparisons as BigDecimal     
    private int[] dofTotalG = null;                                                 // total degrees of freedom of pair comparisons
    private int[] dofBetweenG = null;                                               // between groups degrees of freedom of pair comparisons
    private int[] dofWithinG = null;                                                // within groups degrees of freedom of pair comparisons
    
    private boolean allCalcn = true;                                                // = false if only pair comparisons required
    
    // CONSTRUCTOR
    public ANOVA(){
        this.setTitle("Untitled data", false);
    }
    
    // Constuctor setting data title
    public ANOVA(String dataTitle){
        this.setTitle(dataTitle, true);
    }
    
    // TITLE
    // Enter title (optional)
    public void enterTitle(String title){
        this.setTitle(title, true);
    }
    
    // Set title 
    private void setTitle(String title, boolean check){
        if(!check){
            this.title[0] = title;
            this.nTitle++;
            this.titleCheck = check;
        }
        else{
            if(!this.titleCheck){
                this.title[0] = title;
                this.nTitle++;
                this.titleCheck = check;
            }
            else{
                this.titleCheck = check;
                this.title[this.nTitle] = title;
                this.nTitle++;
            }
        }
    }

    
    // CRITICAL PROBABILITY
    // entered as a fraction
    public void setCriticalSignificance(double critProb){
        this.criticalSignificance = 1.0 - critProb;
        this.criticalSignificanceF = Double.toString(100.0*critProb) + "%";
        this.criticalSignificanceS = Double.toString(100.0*(1.0-critProb)) + "%";
    }
    
    // Return the criticalSignificance
    public double getCriticalSignificance(){
        return 1.0 - this.criticalSignificance;
    }

    // DICHOTOMOUS PAIRS    
    // Add dichotomous word pairs to default list
    public void addDichotomousPair(String trueSign, String falseSign){
        String[] holdS = (String[])this.dichotomousS.clone();
        double[] holdD = (double[])this.dichotomousDoubleS.clone();
        this.dichotomousS = new String[this.nDichotomousS+2];
        this.dichotomousDoubleS = new double[this.nDichotomousS+2];
        for(int i=0; i64 && iC<91){
                    test1 = true;
                }
                else{
                    if(iC>96 && iC<123){
                        test1 = true;
                    }
                }
            }
            if(test1)nA++;
        }
        if(nA==n)test0 = true;
        return test0;
    }
    
    // Test for alphabetic char data
    private boolean testIfAlphabetic(char[] responses){
        boolean test0 = false;
        int n = responses.length;
        int nA = 0;
        
        for(int i=0; i64 && iC<91){
                test1 = true;
            }
            else{
                if(iC>96 && iC<123){
                    test1 = true;
                }
            }
            if(test1)nA++;
        }
        if(nA==n)test0 = true;
        return test0;
    }
    
    // Convert dichotomous Strings to double
    private double[] dichotStringToDouble(String[] responses){
        boolean test0 = false;
        int n = responses.length;
        double[] converted = new double[n];
        for(int i=0; i96.0){
                converted[i] = holdi-96.0;
            }
            else{
                converted[i] = holdi-64.0;
            }
        }     
        return converted;
    }

    // Convert single letter alphabetic String to double
    private double[] alphabeticToDouble(char[] responses){
        int n = responses.length;
        double[] converted = new double[n];
        for(int i=0; i96.0){
                converted[i] = holdi-96.0;
            }
            else{
                converted[i] = holdi-64.0;
            }
        }     
        return converted;
    }
    
    // Convert 'numeric' String to double
    private double[] stringToDouble(String[] responses){
        int n = responses.length;
        double[] converted = new double[n];
        for(int i=0; i96.0){
                converted[i] = holdi-96.0;
            }
            else{
                converted[i] = holdi-64.0;
            }
        }     
        return converted;
    }

    // TRIM RESPONSES
    // Trim all elements of leading and trailing spaces
    private String[][] trimResponses(String[][] responses){
        String[][] responsesT = (String[][])responses.clone();
        int n = responses.length;
        int m = responses[0].length;
        for(int i=0; ithis.nGroups)throw new IllegalArgumentException("The group index, " + ii + ", must be greater than 0 and less than " + (this.nGroups+1));
        if(jj<1 || jj>this.nGroups)throw new IllegalArgumentException("The group index, " + jj + ", must be greater than 0 and less than " + (this.nGroups+1));
        this.comparison = true;
        this.compGroups = new boolean[this.nGroups];
        this.nComparisons = 1;
        ii--;
        jj--;
        for(int i=0;ithis.nGroups)throw new IllegalArgumentException("The group index, " + ii + ", must be greater than 0 and less than " + (this.nGroups+1));
        if(jj<1 || jj>this.nGroups)throw new IllegalArgumentException("The group index, " + jj + ", must be greater than 0 and less than " + (this.nGroups+1));
        this.comparison = true;
        this.compGroups = new boolean[this.nGroups];
        this.nComparisons = 1;
        ii--;
        jj--;
        for(int i=0;ithis.nGroups)throw new IllegalArgumentException("The group index, " + ii + ", must be greater than 0 and less than " + (this.nGroups+1));
        if(jj<1 || jj>this.nGroups)throw new IllegalArgumentException("The group index, " + jj + ", must be greater than 0 and less than " + (this.nGroups+1));
        this.comparison = true;
        this.compGroups = new boolean[this.nGroups];
        this.nComparisons = 1;
        ii--;
        jj--;
        for(int i=0;i2 && this.comparison){
            this.meanSquareTotalG = new double[this.nComparisons];                          // total  mean square of pair comparisons as double
            this.meanSquareWithinG = new double[this.nComparisons];                         // mean square within groups of pair comparisons as double
            this.meanSquareBetweenG  = new double[this.nComparisons];                       // mean square between groups of pair comparisons as double
            this.meanSquareTotalBDG  = new BigDecimal[this.nComparisons];                   // total mean square of pair comparisons as BigDecimal
            this.meanSquareWithinBDG  = new BigDecimal[this.nComparisons];                  // mean square within groups of pair comparisons as BigDecimal
            this.meanSquareBetweenBDG  = new BigDecimal[this.nComparisons];                 // mean square between groups of pair comparisons as BigDecimal
            this.fRatioG  = new double[this.nComparisons];                                  // one way ANOVA F-ratio of pair comparisons as double
            this.fRatioBDG  = new BigDecimal[this.nComparisons];                            // one way ANOVA F-ratio of pair comparisons as BigDecimal
            this.fRatioPG = new double[this.nComparisons];                                  // one way ANOVA F-ratio probability of pair comparisons
            this.criticalFratioG  = new double[this.nComparisons];                          // critical F-ratio of pair comparisons
            this.ssTotalG = new double[this.nComparisons];                       
            this.ssWithinG = new double[this.nComparisons];
            this.ssBetweenG = new double[this.nComparisons];
            this.ssTotalBDG = new BigDecimal[this.nComparisons];                       
            this.ssWithinBDG = new BigDecimal[this.nComparisons]; 
            this.ssBetweenBDG = new BigDecimal[this.nComparisons]; 
            this.dofTotalG = new int[this.nComparisons];  
            this.dofBetweenG = new int[this.nComparisons]; 
            this.dofWithinG = new int[this.nComparisons];
            this.pairIndices = new int[this.nComparisons][2];
                    
            double[][] comp = new double[2][]; 
            int counter = 0;
            for(int i=0;  i2 && this.comparison){
            this.meanSquareTotalG = new double[this.nComparisons];                          // total  mean square of pair comparisons as double
            this.meanSquareWithinG = new double[this.nComparisons];                         // mean square within groups of pair comparisons as double
            this.meanSquareBetweenG  = new double[this.nComparisons];                       // mean square between groups of pair comparisons as double
            this.meanSquareTotalBDG  = new BigDecimal[this.nComparisons];                   // total mean square of pair comparisons as BigDecimal
            this.meanSquareWithinBDG  = new BigDecimal[this.nComparisons];                  // mean square within groups of pair comparisons as BigDecimal
            this.meanSquareBetweenBDG  = new BigDecimal[this.nComparisons];                 // mean square between groups of pair comparisons as BigDecimal
            this.fRatioG  = new double[this.nComparisons];                                  // one way ANOVA F-ratio of pair comparisons as double
            this.fRatioBDG  = new BigDecimal[this.nComparisons];                            // one way ANOVA F-ratio of pair comparisons as BigDecimal
            this.fRatioPG = new double[this.nComparisons];                                  // one way ANOVA F-ratio probability of pair comparisons
            this.criticalFratioG  = new double[this.nComparisons];                          // critical F-ratio of pair comparisons
            this.ssTotalG = new double[this.nComparisons];                       
            this.ssWithinG = new double[this.nComparisons];
            this.ssBetweenG = new double[this.nComparisons];
            this.ssTotalBDG = new BigDecimal[this.nComparisons];                       
            this.ssWithinBDG = new BigDecimal[this.nComparisons]; 
            this.ssBetweenBDG = new BigDecimal[this.nComparisons]; 
            this.dofTotalG = new int[this.nComparisons];  
            this.dofBetweenG = new int[this.nComparisons]; 
            this.dofWithinG = new int[this.nComparisons];
            this.pairIndices = new int[this.nComparisons][2];
                    
            BigDecimal[][] comp = new BigDecimal[2][]; 
            int counter = 0;
            for(int i=0;  i=this.nComparisons){
                        throw new IllegalArgumentException("No index found " + ii + " and " + jj);
                    }
                }
            }
        }
        return ret;
    }
    
    
    // OUTPUT THE ANALYSIS
    
    // Output the one way analysis
    // No input file name entered via method argument list
    public void outputAnalysis(){
        if(!this.oneWayDone){
            System.out.println("Method outputAnalysis: As no ANOVA peforming method had been called a ANOVA of all groups, withour pair comparison, has now been performed");
            this.oneWayANOVA();
        }
        this.outputFilename = "OneWayANOVAoutput";
        switch(this.fileOption){
            case 1: this.outputFilename += ".txt";
                    break;
            case 2: this.outputFilename += ".xls";
                    break;
        }
   
        String message1 = "Output file name for the one way ANOVA:";
        String message2 = "\nEnter the required name (as a single word) and click OK ";
        String message3 = "\nor simply click OK for default value";
        String message = message1 + message2 + message3;
        String defaultName = this.outputFilename;
        this.outputFilename = Db.readLine(message, defaultName);
        this.outputAnalysis(this.outputFilename);
    }


    // Output one way analysis
    // input file name via method argument list
    public void outputAnalysis(String filename){
        if(!this.oneWayDone){
            System.out.println("Method outputAnalysis: As no ANOVA peforming method had been called a ANOVA of all groups, withour pair comparison, has now been performed");
            this.oneWayANOVA();
        }
        
        // Open output file
        this.outputFilename = filename;
        String outputFilenameWithoutExtension = null;
        String extension = null;
        int pos = filename.indexOf('.');
        if(pos==-1){
            outputFilenameWithoutExtension = filename;
            switch(this.fileOption){
                case 1: this.outputFilename += ".txt";
                        break;
                case 2: this.outputFilename += ".xls";
                        break;
            }
        }
        else{    
            extension = (filename.substring(pos)).trim();
            outputFilenameWithoutExtension = (filename.substring(0, pos)).trim();
            if(extension.equalsIgnoreCase(".xlsx")){
                extension = ".xls";
                this.outputFilename = outputFilenameWithoutExtension + extension;
            }
            if(extension.equalsIgnoreCase(".xls")){
                if(this.fileOption==1){
                    if(this.fileOptionSet){
                        String message1 = "Your entered output file type is " + extension;
                        String message2 = "\nbut you have chosen a .txt output";
                        String message = message1 + message2;
                        String headerComment = "Your output file name extension";
                        String[] comments = {message, "replace it with .txt [text file]"};
                        String[] boxTitles = {"Retain", ".txt"};
                        int defaultBox = 1;
                        int opt =  Db.optionBox(headerComment, comments, boxTitles, defaultBox);
                        if(opt==2)this.outputFilename = outputFilenameWithoutExtension + ".txt";
                    }
                    else{
                        this.fileOption=2;
                    }
                }
            }
            if(extension.equalsIgnoreCase(".txt")){
                if(this.fileOption==2){
                    if(this.fileOptionSet){
                        String message1 = "Your entered output file type is .txt";
                        String message2 = "\nbut you have chosen a .xls output";
                        String message = message1 + message2;
                        String headerComment = "Your output file name extension";
                        String[] comments = {message, "replace it with .xls [Excel file]"};
                        String[] boxTitles = {"Retain", ".xls"};
                        int defaultBox = 1;
                        int opt =  Db.optionBox(headerComment, comments, boxTitles, defaultBox);
                        if(opt==2)this.outputFilename = outputFilenameWithoutExtension + ".xls";
                    }
                    else{
                        this.fileOption=1;
                    }
                }
             }
             if(!extension.equalsIgnoreCase(".txt") && !extension.equalsIgnoreCase(".xls")){
                    String message1 = "Your extension is " + extension;
                    String message2 = "\n    Do you wish to retain it:";
                    String message = message1 + message2;
                    String headerComment = "Your output file name extension";
                    String[] comments = {message, "replace it with .txt [text file]", "replace it with .xls [MS Excel file]"};
                    String[] boxTitles = {"Retain", ".txt", ".xls"};
                    int defaultBox = 1;
                    int opt =  Db.optionBox(headerComment, comments, boxTitles, defaultBox);
                    switch(opt){
                        case 1: this.fileOption=1;
                                break;
                        case 2: this.outputFilename = outputFilenameWithoutExtension + ".txt";
                                this.fileOption=1;
                                break;
                        case 3: this.outputFilename = outputFilenameWithoutExtension + ".xls";
                                this.fileOption=2;
                                break;
                    }
            }
        }
        
        // Set output precision
        if(!this.truncAll)this.setOutputPrecision();
        
        // Write to output file
        if(this.fileOption==1){
            this.outputText();
        }
        else{
            this.outputExcel();
        }
        
        System.out.println("The ANOVA summary has been written to the file " + this.outputFilename);
    }

            
    // Set precision of the output data
    private void setOutputPrecision(){
        int maxPrec = Fmath.checkPrecision(this.responses0[0][0]);
        int prech = maxPrec;
        for(int i=0; imaxPrec)maxPrec = prech;
            }
        }
        if(!this.truncAll && maxPrec>this.trunc)this.trunc = maxPrec;
    }

    // Output processed data as a text file
    private void outputText(){

        FileOutput fout = new FileOutput(this.outputFilename);
        fout.println("PROGRAM ANOVA");
        for(int i=0; i2 && this.comparison){
            fout.println(" ");
            fout.println(" ");
            fout.println("COMPARISON OF PAIRS OF GROUPS: One-Way Analysis of Variance");
            int counter = 0;
            for(int i=0; i2 && this.comparison){
            fout.println(" ");
            fout.println(" ");
            fout.println("COMPARISON OF PAIRS OF GROUPS: One-Way Analysis of Variance");
            int counter = 0;
            for(int i=0; i 

Ads help maintain this website.