PDA

View Full Version : Problem with LinearRegression.CaseStatistics (JMSL 5.0)



mrhee
05-25-2009, 03:57 PM
I'm having problems with the LinearRegression.CaseStatistics class. If I try to call getPredictedResponse() or getResidual(), the returned values don't make sense. As far as I can tell, it just seems to return the intercept term and ignore the explanatory variables.

Here's a slightly modified example that demonstates.



import com.imsl.math.PrintMatrix;
import com.imsl.math.PrintMatrixFormat;
import com.imsl.stat.LinearRegression;
import java.util.Arrays;

public class LinearRegressionEx2
{
public static void main(String args[])
{
LinearRegression r = new LinearRegression(2, true);
double y[] = {3, 4, 5, 7, 7, 8, 9};
double x[][] = {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {0, 6}, {1, 7}};
double[] coefs = new double[3];
double[][] results = new double[7][8];
double[] confint = new double[2];
r.update(x, y);
coefs = r.getCoefficients();
for (int k = 0; k < 7; k++) {
LinearRegression.CaseStatistics cs = r.getCaseStatistics(x[k], y[k]);
confint = cs.getConfidenceInterval();
results[k][0] = cs.getObservedResponse();
results[k][1] = cs.getPredictedResponse();
results[k][2] = cs.getResidual();
results[k][3] = cs.getJackknifeResidual();
results[k][4] = cs.getCooksDistance();
results[k][5] = cs.getDFFITS();
results[k][6] = confint[0];
results[k][7] = confint[1];
}
PrintMatrix p = new PrintMatrix("Selected Case Statistics");
PrintMatrixFormat mf = new PrintMatrixFormat();
String labels[] = {
"Observed", "Predicted", "Residual", "Jackknife Residual.", "Cook's D", "DFFITS",
"[Conf. Interval", "on the Mean]"};
mf.setColumnLabels(labels);
p.print(mf, results);
System.out.println("Coefficient estimates: " + Arrays.toString(coefs));
}
}

SebGR
05-26-2009, 01:48 AM
Hello mrhee,

There are some significant fixes for LinearRegression.CaseStatistics in JMSL 5.0.1. I think this should does the trick. So, I suggest you to contact your local sales representative at VNI to get a CD of this 5.0.1 release. :)


SebGR

mrhee
05-26-2009, 11:57 AM
Just got a fresh version and the problem still exists. I think I was using JMSL 5.0.1 anyway.

ed
05-26-2009, 12:37 PM
As I started looking into this, I notice several warning messages are issued:

com.imsl.stat.LinearRegression: A deleted residual mean square (= -0.467) much less than zero has been computed. It is being set to zero.
com.imsl.stat.LinearRegression: A deleted residual mean square (= -1.754) much less than zero has been computed. It is being set to zero.
com.imsl.stat.LinearRegression: A deleted residual mean square (= -3.622) much less than zero has been computed. It is being set to zero.
com.imsl.stat.LinearRegression: A deleted residual mean square (= -10.129) much less than zero has been computed. It is being set to zero.
com.imsl.stat.LinearRegression: A deleted residual mean square (= -11.111) much less than zero has been computed. It is being set to zero.
com.imsl.stat.LinearRegression: A deleted residual mean square (= -46.584) much less than zero has been computed. It is being set to zero.


I'll leave it to one of our stat guys to comment on the importance of these warnings, but are you seeing these too?

mrhee
05-26-2009, 01:43 PM
I see those errors too. I don't think they're consequential. I've set up other regression problems without those errors and get the same predict/residual problems.

ed
05-27-2009, 05:46 AM
I've dropped this code into the IMSL C Library and am seeing the expected results. We'll need to get someone from the development team to dig into the CaseStatistics class. It's not obvious what's going on in this case since other tests look OK.

ed
05-29-2009, 12:25 PM
OK, we've uncovered the issue. There used to be a CaseStatistics.setEffects() method. However, the valid values were constrained, so they basically had to be set to specific values -- which means you really don't need a setter method. The problem is that when we deprecated it, its functionality wasn't completely implemented in the main body.

So, in short, the answer for JMSL 5.0 is to make a call to cs.setEffects() setting the value to negative the number of coefficients (here 2). The code change is:


LinearRegression.CaseStatistics cs = r.getCaseStatistics(x[k], y[k]);
cs.setEffects(-2);
confint = cs.getConfidenceInterval();


This will give you results that you're expecting (and that match our C library version). This will be fixed for JMSL 6.0 without the need to call a deprecated method.