Exemplo n.º 1
0
  private void computeCoefficientOfDetermination() {
    int numberOfItems = scatterpointsDataset.getSeries(0).getItemCount();
    double arithmeticMeanOfX = 0; // arithmetic mean of X
    double arithmeticMeanOfY = 0; // arithmetic mean of Y
    double varX = 0; // variance of X
    double varY = 0; // variance of Y
    double coVarXY = 0; // covariance of X and Y;
    // compute arithmetic means
    for (int i = 0; i < numberOfItems; i++) {
      arithmeticMeanOfX += scatterpointsDataset.getXValue(0, i);
      arithmeticMeanOfY += scatterpointsDataset.getYValue(0, i);
    }
    arithmeticMeanOfX /= numberOfItems;
    arithmeticMeanOfY /= numberOfItems;
    // compute variances and covariance
    for (int i = 0; i < numberOfItems; i++) {
      varX += Math.pow(scatterpointsDataset.getXValue(0, i) - arithmeticMeanOfX, 2);
      varY += Math.pow(scatterpointsDataset.getYValue(0, i) - arithmeticMeanOfY, 2);
      coVarXY +=
          (scatterpointsDataset.getXValue(0, i) - arithmeticMeanOfX)
              * (scatterpointsDataset.getYValue(0, i) - arithmeticMeanOfY);
    }
    // computation of coefficient of determination
    double r2 = Math.pow(coVarXY, 2) / (varX * varY);
    r2 = MathUtils.round(r2, Math.pow(10.0, 5));

    final double[] coefficients = Regression.getOLSRegression(scatterpointsDataset, 0);
    final double intercept = coefficients[0];
    final double slope = coefficients[1];
    final String linearEquation;
    if (intercept >= 0) {
      linearEquation = "y = " + (float) slope + "x + " + (float) intercept;
    } else {
      linearEquation = "y = " + (float) slope + "x - " + Math.abs((float) intercept);
    }

    TextTitle tt = new TextTitle(linearEquation + "\nR² = " + r2);
    tt.setTextAlignment(HorizontalAlignment.RIGHT);
    tt.setFont(chart.getLegend().getItemFont());
    tt.setBackgroundPaint(new Color(200, 200, 255, 100));
    tt.setFrame(new BlockBorder(Color.white));
    tt.setPosition(RectangleEdge.BOTTOM);

    r2Annotation = new XYTitleAnnotation(0.98, 0.02, tt, RectangleAnchor.BOTTOM_RIGHT);
    r2Annotation.setMaxWidth(0.48);
    getPlot().addAnnotation(r2Annotation);
  }
Exemplo n.º 2
0
 private XYIntervalSeries computeRegressionData(double xStart, double xEnd) {
   if (scatterpointsDataset.getItemCount(0) > 1) {
     final double[] coefficients = Regression.getOLSRegression(scatterpointsDataset, 0);
     final Function2D curve = new LineFunction2D(coefficients[0], coefficients[1]);
     final XYSeries regressionData =
         DatasetUtilities.sampleFunction2DToSeries(curve, xStart, xEnd, 100, "regression line");
     final XYIntervalSeries xyIntervalRegression = new XYIntervalSeries(regressionData.getKey());
     for (int i = 0; i < regressionData.getItemCount(); i++) {
       XYDataItem item = regressionData.getDataItem(i);
       final double x = item.getXValue();
       final double y = item.getYValue();
       xyIntervalRegression.add(x, x, x, y, y, y);
     }
     return xyIntervalRegression;
   } else {
     JOptionPane.showMessageDialog(
         this,
         "Unable to compute regression line.\n"
             + "At least 2 values are needed to compute regression coefficients.");
     return null;
   }
 }
Exemplo n.º 3
0
  public static JFreeChartWrapper regressionChart() {

    DefaultTableXYDataset ds = new DefaultTableXYDataset();

    XYSeries series;

    series = new XYSeries("BAR", false, false);
    series.add(1, 1);
    series.add(2, 4);
    series.add(3, 6);
    series.add(4, 9);
    series.add(5, 9);
    series.add(6, 11);
    ds.addSeries(series);

    JFreeChart scatterPlot =
        ChartFactory.createScatterPlot(
            "Regression", "X", "Y", ds, PlotOrientation.HORIZONTAL, true, false, false);

    XYPlot plot = (XYPlot) scatterPlot.getPlot();

    double[] regression = Regression.getOLSRegression(ds, 0);

    // regression line points

    double v1 = regression[0] + regression[1] * 1;
    double v2 = regression[0] + regression[1] * 6;

    DefaultXYDataset ds2 = new DefaultXYDataset();
    ds2.addSeries("regline", new double[][] {new double[] {1, 6}, new double[] {v1, v2}});
    plot.setDataset(1, ds2);
    plot.setRenderer(1, new XYLineAndShapeRenderer(true, false));

    JFreeChart c = new JFreeChart(plot);

    return new JFreeChartWrapper(c);
  }