Regression and Splines

Summary

This demo program illustrates how the JMSL Library can be used to develop Java™ applications that allow visual data interaction and statistical analysis. In this case, the program computes a regression line based on the points entered by the user. Note that summary characteristics of the line and the fit are displayed above the chart. Spline fits can be added by selecting choices under the Spline menu item.

Usage

Data can be entered into this demo in the following ways:

  1. Use the left mouse button to add a point to the chart, and the right mouse button to remove a point from the chart.
  2. Click the "Enter Points" button to enter specific x,y pairs.
  3. Click the "Generate" button to generate the number of random points specified in the "# of points" box.

As points are added to the chart, the regression statistics are updated in the JTextArea above the chart. Add Spline fits to the chart by selecting one or more methods from the "Spline" menu. Note that these classes require three or four points in the chart area before they can be computed.

JMSL Libaray Math/Stat Classes

com.imsl.stat.LinearRegression - this class is used in the getRegCoefficient() method to compute the slope and intercept values of the linear fit. Its getANOVA().getErrorMeanSquare() method is used to compute the mean square error that is displayed in the text area.

com.imsl.math.Spline - each of the spline classes extend this class, so it is this value() method used to retrieve computed values for the splines.

com.imsl.chart.ChartFunction - this interface is used repeatedly in the drawGraph() method to plot the regression line and any of the Spline functions. Given an array of x values, it is worth pointing out how to construct a Data object for a Chart from a slope/intercept:

ChartFunction fcn = new ChartFunction() {
    public
double f(double x) {
        return
x*x1 + intercept;   // x1 is the slope
    }

}
;

line
[0] = new Data(axis, fcn, 0.0, 50.0);

or from a spline fit:

final CsAkima cs0 = new CsAkima(x, y);
ChartFunction
fcn0 = new ChartFunction() {
    public
double f(double x) {
        return
cs0.value(x);
    }

}
;
line[1] = new Data(axis, fcn0, 0.0, 50.0);

JMSL Library Charting Classes

The chart contains the Data object point for the points to fit and line[], an array of Data objects containing the linear regression line and any splines.

Java Code

java.util.Vector is used in this example, although there is no reason why the newer collection class java.util.ArrayList could not be used in its place. In fact, such a change could be made to this source simply by replacing instances of "Vector" with "ArrayList".

Link to Source Code

Regression.java This is the main class, and it extends JFrameChart. Several listeners are implemented for the interface elements and to allow right- and left-clicking in the chart area.