Knowing that study hours and marks are related, a teacher would like to predict the marks of a student who studies seven hours. For this a line must be fitted through the points. Regression provides such a line, and the method of least squares provides the best one.

Regression

Regression is the statistical method of estimating the average value of one variable from the known value of another. The variable to be estimated is the dependent variable y, and the variable used for the estimate is the independent variable x. In linear regression the relation is taken as a straight line, y = a + bx. Here a is the intercept, the value of y when x is 0, and b is the slope, the change in y for a unit change in x.

Definition: Regression. A statistical technique for estimating the average value of a dependent variable from the values of one or more independent variables, by means of a fitted equation.

The Principle of Least Squares

Many lines can be drawn through a scatter of points. The method of least squares chooses the line for which the sum of the squares of the vertical distances of the points from the line is smallest. A vertical distance, the difference between the observed y and the value on the line, is called a residual. Squaring the residuals prevents positive and negative residuals from cancelling.

Normal Equations

Minimising the sum of squares leads to two equations, the normal equations. Sum of y = n × a + b × sum of x, and sum of xy = a × sum of x + b × sum of x squared. Solving them gives b = (n × sum of xy - sum of x × sum of y) divided by (n × sum of x squared - (sum of x) squared), and a = mean of y - b × mean of x.

Table 2.5: Sums for fitting y = a + bx

x y xy x squared
2 50 100 4
4 55 220 16
6 65 390 36
8 70 560 64
10 85 850 100
Sum 30 Sum 325 Sum 2120 Sum 220

Example 2.4: Fitting a regression line by least squares

Problem: Fit the line y = a + bx to the data of hours of study and marks, and estimate the marks for 7 hours of study.

Solution:

  1. The normal equations are 325 = 5a + 30b and 2120 = 30a + 220b.
  2. Substituting a = 65 - 6b in the second gives 2120 = 1950 - 180b + 220b, so 40b = 170 and b = 4.25.
  3. Then a = 65 - 6 × 4.25 = 39.5.
  4. The regression line is y = 39.5 + 4.25x. For x = 7, y = 39.5 + 29.75 = 69.25.

Result: y = 39.5 + 4.25x, and a student who studies 7 hours is expected to score about 69 marks.

x = [2, 4, 6, 8, 10]
y = [50, 55, 65, 70, 85]
n, sx, sy = len(x), sum(x), sum(y)
sxy = sum(a * b for a, b in zip(x, y))
sxx = sum(a * a for a in x)
b = (n * sxy - sx * sy) / (n * sxx - sx ** 2)
a = sy / n - b * sx / n
print(a, b, a + b * 7)

Output

39.5 4.25 69.25

Common mistakes

  • Using the line to estimate values far outside the range of the data.
  • Taking the regression of y on x and of x on y to be the same line.
  • Calculating the vertical distances with the wrong sign and forgetting that they must be squared.

Key takeaways

  • The line of regression of y on x is y = a + bx.
  • The least squares line makes the sum of squared residuals smallest.
  • The normal equations determine a and b.
  • The line passes through the point (mean of x, mean of y).

Practice questions

  1. What does the method of least squares minimise?
  2. What is the slope in y = a + bx?
  3. State the normal equations of the line y = a + bx.

Answers to the practice questions

  1. The sum of the squares of the vertical distances (residuals) of the points from the line.
  2. The change in y for a unit change in x.
  3. Sum y = na + b sum x, and sum xy = a sum x + b sum x squared.

Sources

  • Official BCA syllabus Probability and Statistics, course code 26BCA403DS01, Unit II