/*
 * GoodnessOfFitDialog.java
 *
 * Created on July 1, 2008, 11:24 AM
 */

package org.statcato.dialogs.stat.multinomial;

import org.statcato.*;
import org.statcato.spreadsheet.*;
import org.statcato.utils.HelperFunctions;
import org.statcato.statistics.BasicStatistics;
import javax.swing.*;
import java.util.*;
import org.statcato.statistics.inferential.GoodnessOfFitTest;

/**
 * A dialog for a chi-square goodness-of-fit test given a significance level.  
 * Allows the user to specify the observed frequencies in the Datasheet,
 * as well as the expected frequencies if they are unequal.  
 * Computes degrees of freedom, critical value, test 
 * statistic and 
 * p-Value and displays the results in the log window.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @see org.statcato.statistics.inferential.GoodnessOfFitTest
 * @since 1.0
 */
public class GoodnessOfFitDialog extends StatcatoDialog {

    /** Creates new form GoodnessOfFitDialog */
    public GoodnessOfFitDialog(java.awt.Frame parent, boolean modal,
            Statcato app) {
        super(parent, modal);
        this.app = app;
        ParentSpreadsheet = app.getSpreadsheet(); 
      
        initComponents();
        customInitComponents();
        setHelpFile("stat-multi-goodness");
        name = "Chi-Square Goodness of Fit Test";
        description = "For determining whether an observed frequency " +
                "distribution agrees with some claimed distribution.";
        helpStrings.add("Specify the observed and expected frequencies.");
        helpStrings.add("Specify the significance level.");
    }

    /**
     * Updates elements on the dialog so that they have the most
     * current values.  Called by {@link #setVisible} to make
     * sure the dialog displays current values when made visible again.
     * 
     * @see #setVisible(boolean)
     */
    @Override
    public void updateElements() {
        updateComboBox(OFreqComboBox);
        updateComboBox(ODataComboBox);
        updateComboBox(UnEqFreqComboBox);
        updateComboBox(ProbComboBox);
        updateComboBox(CatComboBox);
        updateComboBox(PastComboBox);
    }
    
    public void customInitComponents() {
        ButtonGroup group1 = new ButtonGroup();
        group1.add(OFreqRadioButton);
        group1.add(ODataRadioButton);
        
        ButtonGroup group2 = new ButtonGroup();
        group2.add(EqFreqRadioButton);
        group2.add(UnEqualFreqRadioButton);
        group2.add(ExCatRadioButton);
        
        ButtonGroup group3 = new ButtonGroup();
        group3.add(UneqFreqRadioButton);
        group3.add(UneqProbRadioButton);
        
        ParentSpreadsheet.populateComboBox(OFreqComboBox);
        ParentSpreadsheet.populateComboBox(ODataComboBox);
        ParentSpreadsheet.populateComboBox(UnEqFreqComboBox);
        ParentSpreadsheet.populateComboBox(ProbComboBox);
        ParentSpreadsheet.populateComboBox(CatComboBox);
        ParentSpreadsheet.populateComboBox(PastComboBox);
        
        getRootPane().setDefaultButton(OKButton);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        ODataComboBox = new javax.swing.JComboBox();
        CatComboBox = new javax.swing.JComboBox();
        OFreqRadioButton = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        ODataRadioButton = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        OFreqComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        UnEqualFreqRadioButton = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        EqFreqRadioButton = new javax.swing.JRadioButton();
        UnEqFreqComboBox = new javax.swing.JComboBox();
        UneqLabel = new javax.swing.JLabel();
        UneqFreqRadioButton = new javax.swing.JRadioButton();
        UneqProbRadioButton = new javax.swing.JRadioButton();
        ProbComboBox = new javax.swing.JComboBox();
        ExCatRadioButton = new javax.swing.JRadioButton();
        PastLabel = new javax.swing.JLabel();
        PastComboBox = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        SigTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Chi-Square Goodness of Fit Test");

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Inputs"));

        ODataComboBox.setEnabled(false);

        OFreqRadioButton.setSelected(true);
        OFreqRadioButton.setText("Frequencies in Column:");
        OFreqRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                OFreqRadioButtonStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Observed Frequencies:");

        ODataRadioButton.setText("Categorical Data in Column:");
        ODataRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ODataRadioButtonStateChanged(evt);
            }
        });

        jLabel7.setText("(optional)");

        jLabel5.setText("Category names in Column:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(ODataRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ODataComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CatComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addComponent(OFreqRadioButton)
                                .addGap(18, 18, 18)
                                .addComponent(OFreqComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ODataComboBox, OFreqComboBox});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OFreqRadioButton)
                    .addComponent(OFreqComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(CatComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ODataRadioButton)
                    .addComponent(ODataComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        UnEqualFreqRadioButton.setText("Unequal Frequencies");
        UnEqualFreqRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                UnEqualFreqRadioButtonStateChanged(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Expected Frequencies:");

        EqFreqRadioButton.setSelected(true);
        EqFreqRadioButton.setText("Equal Frequencies");

        UnEqFreqComboBox.setEnabled(false);

        UneqLabel.setText("(assume in the same order as the categories provided)");
        UneqLabel.setEnabled(false);

        UneqFreqRadioButton.setSelected(true);
        UneqFreqRadioButton.setText("Frequencies in Column:");
        UneqFreqRadioButton.setEnabled(false);
        UneqFreqRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                UneqFreqRadioButtonStateChanged(evt);
            }
        });

        UneqProbRadioButton.setText("Probabilities in Column:");
        UneqProbRadioButton.setEnabled(false);
        UneqProbRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                UneqProbRadioButtonStateChanged(evt);
            }
        });

        ProbComboBox.setEnabled(false);

        ExCatRadioButton.setText("Categorical Data");
        ExCatRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ExCatRadioButtonStateChanged(evt);
            }
        });

        PastLabel.setText("Past Sample Data in Column:");
        PastLabel.setEnabled(false);

        PastComboBox.setEnabled(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(UneqProbRadioButton)
                                    .addComponent(UneqFreqRadioButton))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(UnEqFreqComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(ProbComboBox, 0, 84, Short.MAX_VALUE)))
                            .addComponent(UneqLabel)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(EqFreqRadioButton)
                            .addComponent(jLabel2)
                            .addComponent(UnEqualFreqRadioButton)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ExCatRadioButton)
                                    .addGroup(jPanel4Layout.createSequentialGroup()
                                        .addGap(21, 21, 21)
                                        .addComponent(PastLabel)))
                                .addGap(10, 10, 10)
                                .addComponent(PastComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ProbComboBox, UnEqFreqComboBox});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EqFreqRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(UnEqualFreqRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UneqFreqRadioButton)
                    .addComponent(UnEqFreqComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UneqProbRadioButton)
                    .addComponent(ProbComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(UneqLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ExCatRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PastLabel)
                    .addComponent(PastComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(6, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Significance"));

        SigTextField.setText("0.05");

        jLabel4.setText("0 - 1.00 (e.g. 0.05)");

        jLabel6.setText("Significance level:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLabel4)
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(SigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(OKButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CancelButton, OKButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OFreqRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_OFreqRadioButtonStateChanged
        if (OFreqRadioButton.isSelected()) {
            OFreqComboBox.setEnabled(true);
            CatComboBox.setEnabled(true);
        }
        else {
            OFreqComboBox.setEnabled(false);
            CatComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_OFreqRadioButtonStateChanged

    private void ODataRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_ODataRadioButtonStateChanged
        if (ODataRadioButton.isSelected()) {
            ODataComboBox.setEnabled(true);
        }
        else {
            ODataComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_ODataRadioButtonStateChanged

    private void UnEqualFreqRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_UnEqualFreqRadioButtonStateChanged
        if (UnEqualFreqRadioButton.isSelected()) {
            UnEqFreqComboBox.setEnabled(true);
            ProbComboBox.setEnabled(true);
            UneqFreqRadioButton.setEnabled(true);
            UneqProbRadioButton.setEnabled(true);
            UneqLabel.setEnabled(true);
        }
        else {
            UnEqFreqComboBox.setEnabled(false);
            ProbComboBox.setEnabled(false);
            UneqFreqRadioButton.setEnabled(false);
            UneqProbRadioButton.setEnabled(false);
            UneqLabel.setEnabled(false);
        }
        UneqFreqRadioButtonStateChanged(evt);
        UneqProbRadioButtonStateChanged(evt);
}//GEN-LAST:event_UnEqualFreqRadioButtonStateChanged

    @SuppressWarnings("unchecked") 
    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("goodness of fit");
        
        double confidenceLevel = 0;
        Vector<Double> ColumnVector = new Vector<Double>();
        Vector<Double> EFreqVector = new Vector<Double>();
        Vector<Cell> CatColumnVector = null;
        String heading = "Chi-Square Goodness-of-Fit Test:";
        String text = "";
        
        // get significance level from dialog
        try {
            confidenceLevel = 1 - Double.parseDouble(SigTextField.getText());
        } catch (NumberFormatException e) {
            app.showErrorDialog("Enter a number between 0 and 1 for the significance level.");
            return;
        }
        if (confidenceLevel <= 0 || confidenceLevel >= 1) {
            app.showErrorDialog("Enter a number between 0 and 1 for the significance level.");
            return;
        }
        
        if (OFreqRadioButton.isSelected()) {
            // frequencies in column
            String columnLabel = OFreqComboBox.getSelectedItem().toString();
            if (columnLabel.equals("")) { // no input variable
                app.showErrorDialog("Select the column containing observed" +
                        " frequencies.");
                return;
            }
            int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
            Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
            ColumnVector = HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
            if (ColumnVector == null) {
                app.showErrorDialog("Invalid input column " + columnLabel +
                        ": all data must be numbers.");
                return;
            }
            text += "Input: " + columnLabel + "<br>";
            ColumnVector = HelperFunctions.removeNullValues(ColumnVector);
            
            // optional category names            
            if (CatComboBox.getSelectedIndex() != 0) {
                String catColumnLabel = CatComboBox.getSelectedItem().toString();
                CatColumnVector = ParentSpreadsheet.getColumn(
                        ParentSpreadsheet.parseColumnNumber(catColumnLabel));
                CatColumnVector = HelperFunctions.removeNullCells(CatColumnVector);
            }
        }
        else {
            // get frequencies from categorical data
            // frequencies in column
            String columnLabel = ODataComboBox.getSelectedItem().toString();
            if (columnLabel.equals("")) { // no input variable
                app.showErrorDialog("Select the column containing categorical " +
                        "data.");
                return;
            }
            text += "Input: " + columnLabel + "<br>";
            int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
            Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
            StrColumnVector = HelperFunctions.removeNullCells(StrColumnVector);
            Object returnValues[] = HelperFunctions.ComputeCategoryFrequency(
                    StrColumnVector);
            ColumnVector = (Vector<Double>)returnValues[1];
            CatColumnVector = (Vector<Cell>)returnValues[0];
        }
        
        double total = BasicStatistics.sum(ColumnVector);
        // assume equal frequencies
        if (EqFreqRadioButton.isSelected()) {            
            double expected = total / ColumnVector.size();
            for (int i = 0; i < ColumnVector.size(); ++i) 
                EFreqVector.addElement(new Double(expected));
            text += "Expected frequency = " + 
                    HelperFunctions.formatFloat(expected, 4) + "<br>";
        }
        // get expected unequal frequencies from column
        else if (UnEqualFreqRadioButton.isSelected()) {
            // expected frequencies in column
            if (UneqFreqRadioButton.isSelected()) {
                // frequencies
                String columnLabel = UnEqFreqComboBox.getSelectedItem().toString();
                if (columnLabel.equals("")) { // no input variable
                    app.showErrorDialog("Select the column containing " +
                            "expected frequencies.");
                    return;
                }
                int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
                Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
                EFreqVector = HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
                if (EFreqVector == null) {
                    app.showErrorDialog("Invalid input column " + columnLabel +
                            ": all data must be numbers.");
                    return;
                }
                text += "Expected frequencies in " + columnLabel + "<br>";
                EFreqVector = HelperFunctions.removeNullValues(EFreqVector);     
            }
            else  {
                // probabilities
                String columnLabel = ProbComboBox.getSelectedItem().toString();
                if (columnLabel.equals("")) { // no input variable
                    app.showErrorDialog("Select the column containing " +
                            "expected probabilites.");
                    return;
                }
                int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
                Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
                EFreqVector = HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
                if (EFreqVector == null) {
                    app.showErrorDialog("Invalid input column " + columnLabel +
                            ": all data must be numbers.");
                    return;
                }
                
                text += "Expected probabilities in " + columnLabel + "<br>";
                EFreqVector = HelperFunctions.removeNullValues(EFreqVector);  
                double totalProb = BasicStatistics.sum(EFreqVector);
                if (totalProb != 1) {
                    app.showErrorDialog("The expected probabilities must add " +
                            "up to 1.");
                    return;
                }
                // multiply probabilites with total frequency
                for (int i = 0; i < EFreqVector.size(); ++i) {
                    EFreqVector.setElementAt(EFreqVector.elementAt(i).doubleValue() * total, i);                    
                }
            }
            
        }
        else {  // historical sample categorical data
                String columnLabel = PastComboBox.getSelectedItem().toString();
                if (columnLabel.equals("")) { // no input variable
                    app.showErrorDialog("Select the column containing past sample " +
                            "categorical data.");
                    return;
                }
                text += "Past Sample Data: " + columnLabel + "<br>";
                int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
                Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
                StrColumnVector = HelperFunctions.removeNullCells(StrColumnVector);
                Object returnValues[] = HelperFunctions.ComputeCategoryFrequency(
                        StrColumnVector);
                Vector<Double> PastEFreqVector = (Vector<Double>)returnValues[1];
                Vector<Cell> PastCatColumnVector = (Vector<Cell>)returnValues[0];
                if (PastCatColumnVector.size() != CatColumnVector.size()) {
                    app.showErrorDialog("The number of categories in " +
                            "the past sample data must be the same as the " +
                            "number of categories observed.");
                    return;
                }
                double pastTotal = BasicStatistics.sum(PastEFreqVector);
                for (int i = 0; i < CatColumnVector.size(); ++i) {
                    Cell cat = CatColumnVector.elementAt(i);                    
                    int index = -1;
                    for (int j = 0; j < PastCatColumnVector.size(); ++j) {
                        if (PastCatColumnVector.elementAt(j).toString().equals(
                                cat.toString())) {
                            index = j;
                            break;
                        }
                    }
                    if (index == -1) {
                        app.showErrorDialog("The category " + cat + " is not " +
                                "found in the past sample data.");
                        return;
                    }
                    double freq = PastEFreqVector.elementAt(index).doubleValue();
                    EFreqVector.addElement(freq / pastTotal * total);
                }
        }
        
        if (ColumnVector.size() != EFreqVector.size()) {
            app.showErrorDialog("The number of observed frequencies and " +
                    "the number of expected frequencies must be the same.");
            return;
        }
        GoodnessOfFitTest test = new GoodnessOfFitTest(ColumnVector.size(), 
                confidenceLevel);
        Vector<Double> tsCont = test.testStatisticContribution(ColumnVector, 
                EFreqVector);
        double ts = BasicStatistics.sum(tsCont);
        
        text += "<table border=1>";
        text += "<tr><td>Category</td><td>Observed Frequency</td>" +
                "<td>Expected Frequency</td><td>Contribution to &Chi;<sup>2</sup>" +
                "</td></tr>";
        for (int i = 0; i < ColumnVector.size(); ++i) {
            text += "<tr><td>";
            if (CatColumnVector == null)
                text += i;
            else
                text += CatColumnVector.elementAt(i);
            text += "</td><td>" + ColumnVector.elementAt(i) + "</td><td>";
            text += HelperFunctions.formatFloat(EFreqVector.elementAt(i).doubleValue(), 4) 
                    + "</td><td>" +
                    HelperFunctions.formatFloat(tsCont.elementAt(i).doubleValue(), 4) 
                    + "</td></tr>";
        }
        text += "</table><br>";
        
        
        text += "<table border=1>";
        text += "<tr><td>N</td><td>Number of Categories</td><td>DOF</td><td>Significance</td>" +
                "<td>Critical Value</td><td>Test statistics</td>" +
                "<td>p-Value</td></tr>";
        text += "<tr><td>" + total + "</td><td>" + 
                ColumnVector.size() + "</td><td>" +
                test.DOF() + "</td><td>" +
                HelperFunctions.formatFloat(1 - confidenceLevel, 2) + "</td><td>" + 
                HelperFunctions.formatFloat(test.criticalValue(), 4) +
                "</td><td>" + HelperFunctions.formatFloat(ts, 4) + 
                "</tr><td>" + HelperFunctions.formatFloat(test.pValue(ts), 4) 
                + "</td></tr>";
        text += "</table>";
        
        app.addLogParagraph(heading, text);

        app.compoundEdit.end();
        app.addCompoundEdit(app.compoundEdit);
        
        setVisible(false);
    }//GEN-LAST:event_OKButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void UneqFreqRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_UneqFreqRadioButtonStateChanged
        if (UneqFreqRadioButton.isSelected() && UnEqualFreqRadioButton.isSelected()) {
            UnEqFreqComboBox.setEnabled(true);
        }
        else {
            UnEqFreqComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_UneqFreqRadioButtonStateChanged

    private void UneqProbRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_UneqProbRadioButtonStateChanged
        if (UneqProbRadioButton.isSelected() && UnEqualFreqRadioButton.isSelected()) {
            ProbComboBox.setEnabled(true);
        }
        else {
            ProbComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_UneqProbRadioButtonStateChanged

    private void ExCatRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_ExCatRadioButtonStateChanged
        if (ExCatRadioButton.isSelected()) {
            PastLabel.setEnabled(true);
            PastComboBox.setEnabled(true);
        }
        else {
            PastLabel.setEnabled(false);
            PastComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_ExCatRadioButtonStateChanged
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JComboBox CatComboBox;
    private javax.swing.JRadioButton EqFreqRadioButton;
    private javax.swing.JRadioButton ExCatRadioButton;
    private javax.swing.JComboBox ODataComboBox;
    private javax.swing.JRadioButton ODataRadioButton;
    private javax.swing.JComboBox OFreqComboBox;
    private javax.swing.JRadioButton OFreqRadioButton;
    private javax.swing.JButton OKButton;
    private javax.swing.JComboBox PastComboBox;
    private javax.swing.JLabel PastLabel;
    private javax.swing.JComboBox ProbComboBox;
    private javax.swing.JTextField SigTextField;
    private javax.swing.JComboBox UnEqFreqComboBox;
    private javax.swing.JRadioButton UnEqualFreqRadioButton;
    private javax.swing.JRadioButton UneqFreqRadioButton;
    private javax.swing.JLabel UneqLabel;
    private javax.swing.JRadioButton UneqProbRadioButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables
    
}
