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); }
private void createUI() { final XYPlot plot = getPlot(); plot.setAxisOffset(new RectangleInsets(5, 5, 5, 5)); plot.setNoDataMessage(NO_DATA_MESSAGE); int confidenceDSIndex = 0; int regressionDSIndex = 1; int scatterpointsDSIndex = 2; plot.setDataset(confidenceDSIndex, acceptableDeviationDataset); plot.setDataset(regressionDSIndex, regressionDataset); plot.setDataset(scatterpointsDSIndex, scatterpointsDataset); plot.addAnnotation(r2Annotation); final DeviationRenderer identityRenderer = new DeviationRenderer(true, false); identityRenderer.setSeriesPaint(0, StatisticChartStyling.SAMPLE_DATA_PAINT); identityRenderer.setSeriesFillPaint(0, StatisticChartStyling.SAMPLE_DATA_FILL_PAINT); plot.setRenderer(confidenceDSIndex, identityRenderer); final DeviationRenderer regressionRenderer = new DeviationRenderer(true, false); regressionRenderer.setSeriesPaint(0, StatisticChartStyling.REGRESSION_DATA_PAINT); regressionRenderer.setSeriesFillPaint(0, StatisticChartStyling.REGRESSION_DATA_FILL_PAINT); plot.setRenderer(regressionDSIndex, regressionRenderer); final XYErrorRenderer scatterPointsRenderer = new XYErrorRenderer(); scatterPointsRenderer.setDrawXError(true); scatterPointsRenderer.setErrorStroke(new BasicStroke(1)); scatterPointsRenderer.setErrorPaint(StatisticChartStyling.CORRELATIVE_POINT_OUTLINE_PAINT); scatterPointsRenderer.setSeriesShape(0, StatisticChartStyling.CORRELATIVE_POINT_SHAPE); scatterPointsRenderer.setSeriesOutlinePaint( 0, StatisticChartStyling.CORRELATIVE_POINT_OUTLINE_PAINT); scatterPointsRenderer.setSeriesFillPaint(0, StatisticChartStyling.CORRELATIVE_POINT_FILL_PAINT); scatterPointsRenderer.setSeriesLinesVisible(0, false); scatterPointsRenderer.setSeriesShapesVisible(0, true); scatterPointsRenderer.setSeriesOutlineStroke(0, new BasicStroke(1.0f)); scatterPointsRenderer.setSeriesToolTipGenerator( 0, (dataset, series, item) -> { final XYIntervalSeriesCollection collection = (XYIntervalSeriesCollection) dataset; final Comparable key = collection.getSeriesKey(series); final double xValue = collection.getXValue(series, item); final double endYValue = collection.getEndYValue(series, item); final double yValue = collection.getYValue(series, item); return String.format( "%s: mean = %6.2f, sigma = %6.2f | %s: value = %6.2f", getRasterName(), yValue, endYValue - yValue, key, xValue); }); plot.setRenderer(scatterpointsDSIndex, scatterPointsRenderer); final boolean autoRangeIncludesZero = false; final boolean xLog = scatterPlotModel.xAxisLogScaled; final boolean yLog = scatterPlotModel.yAxisLogScaled; plot.setDomainAxis( StatisticChartStyling.updateScalingOfAxis( xLog, plot.getDomainAxis(), autoRangeIncludesZero)); plot.setRangeAxis( StatisticChartStyling.updateScalingOfAxis( yLog, plot.getRangeAxis(), autoRangeIncludesZero)); createUI(createChartPanel(chart), createInputParameterPanel(), bindingContext); plot.getDomainAxis().addChangeListener(domainAxisChangeListener); scatterPlotDisplay.setMouseWheelEnabled(true); scatterPlotDisplay.setMouseZoomable(true); }