Simple Linear Regression
/*
July 2006. Annie Che <chea@stat.ucla.edu>. UCLA Statistics.
Source of example data: An Introduction to Computational Statitics by Robert I. Jennrich. Page 5, example of regression on students' midterm and final scores.
- /
package edu.ucla.stat.SOCR.analyses.example;
import java.util.HashMap; import edu.ucla.stat.SOCR.analyses.data.Data; import edu.ucla.stat.SOCR.analyses.data.DataType; import edu.ucla.stat.SOCR.analyses.result.SimpleLinearRegressionResult;
public class SimpleLinearRegressionExample {
public static void main(String args[]) {
double[] midtermGrade =
{68,49,60,68,97,82,59,50,73,39,71,95,61,72,87,40,66,58,58,77};
double[] finalGrade =
{75,63,57,88,88,79,82,73,90,62,70,96,76,75,85,40,74,70,75,72};
// you'll need to instantiate a data instance first. Data data = new Data();
/********************************************************************* then put the data into the Data Object. append the predictor data using method "addPredictor". append the response data using method "addResponse". **********************************************************************/
data.addPredictor(midtermGrade, DataType.QUANTITATIVE); data.addResponse(finalGrade, DataType.QUANTITATIVE);
try {
SimpleLinearRegressionResult result = data.modelSimpleLinearRegression();
if (result != null) {
// Getting the model's parameter estiamtes and statistics.
double alpha = result.getAlpha(); double beta = result.getBeta();
double meanX = result.getMeanX(); double meanY = result.getMeanY();
double seAlpha = result.getAlphaSE(); double seBeta = result.getBetaSE(); double tStatAlpha = result.getAlphaTStat(); double tStatBeta = result.getBetaTStat();
/* to avoid cases such "p-value < 0.0001" sometimes generated
by R, String is used for p-values. */
String pvAlpha = result.getAlphaPValue(); String pvBeta = result.getBetaPValue();
double[] predicted = result.getPredicted(); double[] residuals = result.getResiduals();
// residuals after being sorted ascendantly. double[] sortedResiduals = result.getSortedResiduals();
// sortedResiduals after being standardized. double[] sortedStandardizedResiduals =
result.getSortedStandardizedResiduals();
// the original index of sortedResiduals, stored as integer array. int[] sortedResidualsIndex = result.getSortedResidualsIndex();
// the normal quantiles of sortedResiduals. double[] sortedNormalQuantiles = result.getSortedNormalQuantiles();
// sortedNormalQuantiles after being standardized. double[] sortedStandardizedNormalQuantiles =
result.getSortedStandardizedNormalQuantiles();
System.out.println("intercept = " + alpha); System.out.println("slope = " + beta); System.out.println("meanX = " + meanX); System.out.println("meanY = " + meanY);
System.out.println("seAlpha = " + seAlpha); System.out.println("seBeta = " + seBeta); System.out.println("tStatAlpha = " + tStatAlpha); System.out.println("tStatBeta = " + tStatBeta);
System.out.println("pvAlpha = " + pvAlpha); System.out.println("pvBeta = " + pvBeta); for (int i = 0; i < residuals.length; i++) { System.out.println("residuals["+i+"] = " + residuals[i]); }
} } catch (Exception e) { System.out.println(e); } } }