Difference between revisions of "Simple Linear Regression"

From SOCR
Jump to: navigation, search
m
 
Line 103: Line 103:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
 +
{{translate|pageName=http://wiki.stat.ucla.edu/socr/index.php?title=Simple_Linear_Regression}}

Latest revision as of 00:46, 19 January 2007

/*

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);
		}
	}
}




Translate this page:

(default)
Uk flag.gif

Deutsch
De flag.gif

Español
Es flag.gif

Français
Fr flag.gif

Italiano
It flag.gif

Português
Pt flag.gif

日本語
Jp flag.gif

България
Bg flag.gif

الامارات العربية المتحدة
Ae flag.gif

Suomi
Fi flag.gif

इस भाषा में
In flag.gif

Norge
No flag.png

한국어
Kr flag.gif

中文
Cn flag.gif

繁体中文
Cn flag.gif

Русский
Ru flag.gif

Nederlands
Nl flag.gif

Ελληνικά
Gr flag.gif

Hrvatska
Hr flag.gif

Česká republika
Cz flag.gif

Danmark
Dk flag.gif

Polska
Pl flag.png

România
Ro flag.png

Sverige
Se flag.gif