Instructions for this demo are down below the graph.


 4.5-minute lecture 

Click on one of these links to open the sound recording in a new window: leastsquares.au, leastsquares.mp3.

Once the sound starts, switch back to this window.

To pause or rewind the lecture, switch to the sound window.

Back to the syllabus.

The current version of this is at sambaker.com.


This applet demonstrates least squares regression.

How To Use It

Least Squares

When the applet starts, you see a set of data points and a horizontal line through the mean of the data, representing a first try at fitting. A poor try, but a try.

Click the Show Squares button to show the squares of the residuals. The residuals are the vertical distances between the points and the line.

Use your mouse to drag the left or right end of the line up or down. Make the total area of the squares as small as you can. The line that makes the squares the smallest is the least squares line!

The R-Squared

Click the Mean line button. Click the Squares' Sum button. If there were no linear relationship between X and Y, this horizontal line through the mean would be the least squares line. Make a note of the Sum of Squares. Click the LS line button, to show the actual least squares line. The R-Squared is based on how much smaller the squares got.
R-Squared = 1 - (Sum of Squares for LS line)/(Sum of Squares for Mean line)

Least Sum of Residuals

An alternative regression method is to choose a line that minimizes the sum of the absolute values of the residuals. The Residuals' Sum button displays this sum, so you can find one of these lines. (There's usually more than one, so this criterion alone isn't enough to define a best fit.)
The data points are generated using the same method as HSPM J716's assignment 1.

Click on the graph to move the near end of the line up or down slightly. You can also drag the ends of the line up or down.

The Show Residuals button shows or hides the residuals, the vertical distances between the points and the line.

The Show Squares button shows or hides squares of the residuals.

The Squares' Sum button shows or hides the sum of the areas of the squares of the residuals.

The Residuals' Sum button shows or hides the sum of the residuals' absolute values.

The LS line button makes your line the least squares regression line. Clicking this is like jumping to the end of the story.

The Mean line button resets the line to the horizontal line through the mean of the points. Clicking this is like starting over.

The New Points button gives a new set of points and sets the line to horizontal through the mean of the new points.



© 2000-2002 Samuel L. Baker
The author is solely responsible for this page. Its contents have not been reviewed or approved by the University of South Carolina.
http://hspm.sph.sc.edu/COURSES/J716/demos/LeastSquaresDemo.html
July 21, 2002