Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package correlation.and.regression.analysis; import java.awt.Color; import java.io.IOException; import java.text.NumberFormat; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; /** * * @author Dell */ public class MainWindow extends javax.swing.JFrame { /** * Creates new form MainWindow */ public MainWindow() { initComponents(); } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jFileChooser1 = new javax.swing.JFileChooser(); jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); jSPanelChart = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTblStatistic = new javax.swing.JTable(); jScrollPane2 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jScrollPane3 = new javax.swing.JScrollPane(); jTable2 = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); jLabelDet = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabelFtest = new javax.swing.JLabel(); jLabelRes = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout jSPanelChartLayout = new javax.swing.GroupLayout(jSPanelChart); jSPanelChart.setLayout(jSPanelChartLayout); jSPanelChartLayout.setHorizontalGroup(jSPanelChartLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 607, Short.MAX_VALUE)); jSPanelChartLayout.setVerticalGroup(jSPanelChartLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 428, Short.MAX_VALUE)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSPanelChart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSPanelChart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jTabbedPane1.addTab("", jPanel1); modelStatistic = new DefaultTableModel( new Object[][] { { "Mean", "Mark", null, null }, { "", "Interval", null, null }, { "Mean square", "Mark", null, null }, { "", "Interval", null, null }, { "Coefficient asymmetry", "Mark", null, null }, { "", "Interval", null, null }, { "Coefficient excess", "Mark", null, null }, { "", "Interval", null, null } }, new String[] { "", "", "X", "Y" }); jTblStatistic.setModel(modelStatistic); jScrollPane1.setViewportView(jTblStatistic); modelCoefCorr = new DefaultTableModel(new Object[] { "", "", "?", "", "?", "." }, 4); modelCoefCorr.setValueAt(" ?. .", 0, 0); modelCoefCorr.setValueAt(". ", 1, 0); modelCoefCorr.setValueAt("-", 1, 5); modelCoefCorr.setValueAt("?. ", 2, 0); modelCoefCorr.setValueAt("-", 2, 5); modelCoefCorr.setValueAt("?. ", 3, 0); modelCoefCorr.setValueAt("-", 3, 5); jTable1.setModel(modelCoefCorr); jScrollPane2.setViewportView(jTable1); modelEstem = new DefaultTableModel(new Object[] { "", " ", "???", "?", "", "?", "." }, 2); modelEstem.setValueAt("a", 0, 0); modelEstem.setValueAt("b", 1, 0); jTable2.setModel(modelEstem); jScrollPane3.setViewportView(jTable2); jLabel1.setText("? :"); jLabel2.setText("f:"); 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().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE) .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane3) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabelDet)) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel2) .addGap(18, 18, 18).addComponent(jLabelFtest).addGap(70, 70, 70) .addComponent(jLabelRes))) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabelDet)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jLabelFtest).addComponent(jLabelRes)) .addContainerGap(22, Short.MAX_VALUE))); jTabbedPane1.addTab("", jPanel2); jMenu1.setText(""); jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem1.setText(""); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane1)); pack(); }// </editor-fold>//GEN-END:initComponents private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed NumberFormat f = NumberFormat.getInstance(); f.setGroupingUsed(false); try { if (arr[0] == null) { jFileChooser1.showDialog(null, "Open file"); arr[0] = new OrderedSeries(); arr[0].loadFromFile(jFileChooser1.getSelectedFile().getPath()); } else { if (arr[1] == null) { jFileChooser1.showDialog(null, "Open file"); arr[1] = new OrderedSeries(); arr[1].loadFromFile(jFileChooser1.getSelectedFile().getPath()); showCorrelationField(); jTblStatistic.setModel(arr[0].showStatisticsForLab4(modelStatistic, 1 - 0.05 / 2)); jTblStatistic.setModel(arr[1].showStatisticsForLab4(modelStatistic, 1 - 0.05 / 2)); /*Show coef correlation*/ modelCoefCorr.setValueAt(f.format(StaticFunctions.pairCorrelationCoef(arr[0], arr[1])), 0, 1); modelCoefCorr.setValueAt(f.format(StaticFunctions.significancePairCor(arr[0], arr[1])), 0, 2); modelCoefCorr.setValueAt(f.format(Quantiles.Student(1 - 0.05 / 2, arr[0].size() - 2)), 0, 3); if (Math.abs(StaticFunctions.significancePairCor(arr[0], arr[1])) <= Quantiles .Student(1 - 0.05 / 2, arr[0].size() - 2)) { modelCoefCorr.setValueAt("? ", 0, 4); } else { modelCoefCorr.setValueAt("", 0, 4); } modelCoefCorr.setValueAt(StaticFunctions.getIntervalForPairCorr( StaticFunctions.pairCorrelationCoef(arr[0], arr[1]), arr[0].size()), 0, 5); modelCoefCorr.setValueAt(f.format(StaticFunctions.markCorrelationRatio(arr[0], arr[1])), 1, 1); modelCoefCorr.setValueAt(f.format(StaticFunctions.statisticsCorrelationRatio( StaticFunctions.markCorrelationRatio(arr[0], arr[1]), arr[0])), 1, 2); modelCoefCorr .setValueAt(f.format(Quantiles.Fisher(1 - 0.05, StaticFunctions.getK(arr[0].size()) - 1, arr[0].size() - StaticFunctions.getK(arr[0].size()))), 1, 3); if (StaticFunctions.statisticsCorrelationRatio( StaticFunctions.markCorrelationRatio(arr[0], arr[1]), arr[0]) <= Quantiles.Fisher(1 - 0.05, StaticFunctions.getK(arr[0].size()) - 1, arr[0].size() - StaticFunctions.getK(arr[0].size()))) { modelCoefCorr.setValueAt("? ", 1, 4); } else { modelCoefCorr.setValueAt("", 1, 4); } modelCoefCorr.setValueAt( f.format(StaticFunctions.spirmenCoef(arr[0].toArrayDouble(0), arr[1].toArrayDouble(0))), 2, 1); modelCoefCorr.setValueAt(f.format( StaticFunctions.statisticsSpirmen(arr[0].toArrayDouble(0), arr[1].toArrayDouble(0))), 2, 2); modelCoefCorr.setValueAt(f.format(StaticFunctions.tDistrib(arr[0].size() - 2)), 2, 3); if (Math.abs(StaticFunctions.statisticsSpirmen(arr[0].toArrayDouble(0), arr[1].toArrayDouble(0))) <= StaticFunctions.tDistrib(arr[0].size() - 2)) { modelCoefCorr.setValueAt("? ", 2, 4); } else { modelCoefCorr.setValueAt("", 2, 4); } modelCoefCorr.setValueAt( f.format( StaticFunctions.kendallsCoef(arr[0].toArrayDouble(0), arr[1].toArrayDouble(0))), 3, 1); modelCoefCorr.setValueAt(f.format( StaticFunctions.staticticsKendall(arr[0].toArrayDouble(0), arr[1].toArrayDouble(0))), 3, 2); modelCoefCorr.setValueAt(f.format(Quantiles.Norm(1 - 0.05 / 2)), 3, 3); if (Math.abs(StaticFunctions.staticticsKendall(arr[0].toArrayDouble(0), arr[1].toArrayDouble(0))) <= Quantiles.Norm(1 - 0.05 / 2)) { modelCoefCorr.setValueAt("? ", 3, 4); } else { modelCoefCorr.setValueAt("", 3, 4); } /*Show estemate parametrs*/ modelEstem.setValueAt(StaticFunctions.estimateA(arr[0], arr[1]), 0, 1); modelEstem.setValueAt(f.format(StaticFunctions.dispersiaA(arr[0], arr[1])), 0, 2); modelEstem.setValueAt(f.format(StaticFunctions.statisticA(arr[0], arr[1])), 0, 3); modelEstem.setValueAt(f.format(Quantiles.Student(1 - 0.05 / 2, arr[0].size() - 2)), 0, 4); modelEstem.setValueAt(StaticFunctions.significanceA(arr[0], arr[1]), 0, 5); modelEstem.setValueAt(StaticFunctions.getIntervalForEstemateA(arr[0], arr[1]), 0, 6); modelEstem.setValueAt(StaticFunctions.estimateB(arr[0], arr[1]), 1, 1); modelEstem.setValueAt(f.format(StaticFunctions.dispersiaB(arr[0], arr[1])), 1, 2); modelEstem.setValueAt(f.format(StaticFunctions.statisticB(arr[0], arr[1])), 1, 3); modelEstem.setValueAt(f.format(Quantiles.Student(1 - 0.05 / 2, arr[0].size() - 2)), 1, 4); modelEstem.setValueAt(StaticFunctions.significanceB(arr[0], arr[1]), 1, 5); modelEstem.setValueAt(StaticFunctions.getIntervalForEstemateB(arr[0], arr[1]), 1, 6); double[] tmp = new double[arr[0].size()]; double a = StaticFunctions.estimateA(arr[0], arr[1]); double b = StaticFunctions.estimateB(arr[0], arr[1]); for (int i = 0; i < arr[0].size(); i++) { tmp[i] = StaticFunctions.regression(arr[0].getNumber(i), a, b); } OrderedSeries t = new OrderedSeries(); t.loadFromArray(tmp); jLabelDet.setText(f.format(Math.pow(StaticFunctions.pairCorrelationCoef(arr[0], t), 2) * 100)); jLabelFtest.setText("" + f.format(StaticFunctions.getFtest(arr[0], arr[1]))); if (StaticFunctions.getFtest(arr[0], arr[1]) > Math .pow(Quantiles.Student(1 - 0.05 / 2, arr[0].size() - 2), 2)) { jLabelRes.setText("??? "); } else { jLabelRes.setText("??? "); } } else { arr[0] = null; arr[1] = null; clearAll(); jMenuItem1ActionPerformed(evt); } } } catch (IOException ex) { JOptionPane.showMessageDialog(null, "File has wrong information"); } }//GEN-LAST:event_jMenuItem1ActionPerformed /** * @param args the command line arguments */ public static void start() { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainWindow().setVisible(true); } }); } OrderedSeries[] arr = new OrderedSeries[2]; JFreeChart chart; DefaultTableModel modelStatistic; DefaultTableModel modelCoefCorr; DefaultTableModel modelEstem; ChartPanel ChP; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JFileChooser jFileChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabelDet; private javax.swing.JLabel jLabelFtest; private javax.swing.JLabel jLabelRes; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jSPanelChart; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTable jTable1; private javax.swing.JTable jTable2; private javax.swing.JTable jTblStatistic; // End of variables declaration//GEN-END:variables private void clearAll() { //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } private void showCorrelationField() { XYSeriesCollection dataset = new XYSeriesCollection(); XYSeries correlation = new XYSeries("Correlation Field"); for (int i = 0; i < arr[0].countOfNumbers; i++) { //XYSeries tmp = new XYSeries(""+i); correlation.add(arr[0].getNumber(i), arr[1].getNumber(i)); //tmp.add(arr[0].getNumber(i), arr[1].getNumber(i)); //dataset.addSeries(tmp); } dataset.addSeries(correlation); XYSeries regr = StaticFunctions.drawRegressionLine(arr[0], arr[1]); dataset.addSeries(regr); XYSeries ConfidenceIntervalMax = StaticFunctions.drawConfidenceIntervalMax(arr[0], arr[1]); dataset.addSeries(ConfidenceIntervalMax); XYSeries ConfidenceIntervalMin = StaticFunctions.drawConfidenceIntervalMin(arr[0], arr[1]); dataset.addSeries(ConfidenceIntervalMin); XYSeries Confidence2IntervalMax = StaticFunctions.drawConfidence2IntervalMax(arr[0], arr[1]); dataset.addSeries(Confidence2IntervalMax); XYSeries Confidence2IntervalMin = StaticFunctions.drawConfidence2IntervalMin(arr[0], arr[1]); dataset.addSeries(Confidence2IntervalMin); XYSeries TolerantIntervalMax = StaticFunctions.drawTolerantIntervalMax(arr[0], arr[1]); dataset.addSeries(TolerantIntervalMax); XYSeries TolerantIntervalMin = StaticFunctions.drawTolerantIntervalMin(arr[0], arr[1]); dataset.addSeries(TolerantIntervalMin); JFreeChart chart = ChartFactory.createXYLineChart("Relation", "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... chart.setBackgroundPaint(Color.white); final XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesLinesVisible(0, false); renderer.setSeriesShapesVisible(1, false); renderer.setSeriesShapesVisible(2, false); renderer.setSeriesShapesVisible(3, false); renderer.setSeriesShapesVisible(4, false); renderer.setSeriesShapesVisible(5, false); plot.setRenderer(renderer); // change the auto tick unit selection to integer units only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); ChP = new ChartPanel(chart); ChP.setSize(jSPanelChart.getWidth(), jSPanelChart.getHeight()); jSPanelChart.removeAll(); jSPanelChart.revalidate(); jSPanelChart.add(ChP); jSPanelChart.repaint(); } }