Formatting ordinary least squares equations into matrix form can be a very efficient way of solving for a least squares regression line. This is because, instead of taking every value individually, the values are taken as a whole column. Try to apply what you’ve learned from our other guides to the problem below. If you struggle to come up with a solution or are learning about this concept for the first time, read through this guide.

 

The best Maths tutors available
Paolo
5
5 (63 reviews)
Paolo
£30
/h
Gift icon
1st lesson free!
Hiren
5
5 (23 reviews)
Hiren
£150
/h
Gift icon
1st lesson free!
Akash
5
5 (58 reviews)
Akash
£45
/h
Gift icon
1st lesson free!
Shane
5
5 (33 reviews)
Shane
£30
/h
Gift icon
1st lesson free!
Johann
5
5 (35 reviews)
Johann
£35
/h
Gift icon
1st lesson free!
Intasar
5
5 (48 reviews)
Intasar
£79
/h
Gift icon
1st lesson free!
Luke
5
5 (76 reviews)
Luke
£125
/h
Gift icon
1st lesson free!
Harinder
5
5 (37 reviews)
Harinder
£20
/h
Gift icon
1st lesson free!
Paolo
5
5 (63 reviews)
Paolo
£30
/h
Gift icon
1st lesson free!
Hiren
5
5 (23 reviews)
Hiren
£150
/h
Gift icon
1st lesson free!
Akash
5
5 (58 reviews)
Akash
£45
/h
Gift icon
1st lesson free!
Shane
5
5 (33 reviews)
Shane
£30
/h
Gift icon
1st lesson free!
Johann
5
5 (35 reviews)
Johann
£35
/h
Gift icon
1st lesson free!
Intasar
5
5 (48 reviews)
Intasar
£79
/h
Gift icon
1st lesson free!
Luke
5
5 (76 reviews)
Luke
£125
/h
Gift icon
1st lesson free!
Harinder
5
5 (37 reviews)
Harinder
£20
/h
Gift icon
1st lesson free!
Let's go

Problem 8

A shop owner is interested in understanding the demand of certain goods in her store based off of the price. In order to help the store owner, you’re tasked with conducting a simple regression analysis. Given the following data set, your first task is to explain how to format these observations into matrices along with the benefits of using matrices in statistics.

 

Price Demand
120 110
125 100
130 90
135 45
140 20

 

OLS Linear Equation

Recall that ordinary least squares, or OLS, is a type of regression analysis that strives to reduce the sum of squared residuals. OLS can take on two forms, summarized in the table below.

 

Simple Linear Regression Multiple Linear Regression
SLR MLR
One dependent variable One dependent variable
One independent variable Two or more independent variables

 

mlr_equation

 

slr_equation

 

To understand OLS regression, you have to understand the formulas involved. There are two major formulas, which are the least squares linear equation and the residual formula.

 

The least squares linear equation
The y-intercept, the value of y when all x’s are zero
The regression coefficients, which tell us how one independent variable relates to y
The estimation of the error term, otherwise known as the residual

 

To understand the reason why OLS tries to reduce the sum of the squared error, look at the image below.

 

residual_estimation

 

The residual is the difference between the actual observed value given by an and the predicted value , also known as y-hat, given by the same . The best line, naturally, would be the regression line with the least distance between the actual observed y values and the predicted y values. However, how can you compare residuals when they have different signs?

 

Example Sign Interpretation
positive Overestimated
negative Underestimated

 

As you can see, while the magnitudes are both 15, if we didn’t square the residuals the negative sign would deflate the total sum. This is why residuals are squared.

 

OLS Matrix Equation

If we were to solve this equation using OLS, we would have to utilize the steps and equations summarized below.

 

Step Description Formula
1 Write down the OLS equation
2 Find the slope
3 Find all
4 Find all
5 Find all parameters
...

 

This can be time consuming, especially if we had more than just two independent variables. This is where matrices come into play. Think about the points in your data as a system of equations.

 

Points Equation
... ...

 

Taking this system of equations, you can reformulate this data into matrix form.

 

matrix_regression

 

In this form, we only have two steps, summarized below.

 

Step Description Formula
1 Write down the OLS equation
2 Solve for the matrix A

 

Solving OLS Matrix Equation

By multiplying the matrices out, you can see the resulting system of equations from earlier. Take the first two entries, as an example.

 

matrix_regression_example

 

This would multiply out to be the following.

 

 

Here, we would like to solve for as it contains the y-intercept and slope information. Because matrices can’t just be moved around on either side of the equals sign, we have to perform special operations known as taking the transpose and inverse of a matrix, resulting in the following formula.

 

matrix_regression_solve

 

The transpose of a matrix is simply flipping a matrix from, for example, being 2x3 to 3x2, like below.

 

transpose_matrix

 

The inverse of a matrix, on the other hand, involves elementary row operations to get from ot .

 

inverse_3x3_matrix

 

OLS Matrix Equation Example

In order to get used to matrix notation, let’s go through an example. You have data on height and heart beats per minute, which are illustrated below.

 

Height Heart Beat
160 55
169 50
174 70
180 80

 

You know this could be written in the following equations:

 

(160, 55)
(169, 50)
(174, 70)
(180, 80)

 

Instead, we can now write them in matrix notation.

 

regression_matrix_formula

OLS Matrix Equation Problem

In this problem, you were asked to format the data set into matrix multiplication. First, let’s start by separating the data into a system of equations. Doing this will help us visualize which numbers we have to place in each matrix.

 

Y X Formula
120 110
125 100
130 90
135 45
140 20

 

Now that we have the data formatted this way, it’s easy to see what to put in which matrix. Now, we just follow the formula from above, where each matrix contains the different variables and/or parameters necessary. Below, you'll see what this should look like.

 

regression_matrix_formula

 

The benefit to this is that it will be a lot easier to solve by hand, and computationally, than without matrices. As you can see, instead of having to find the solutions to the formulas of about 5 or more metrics, we can now just solve for A through less complicated matrix multiplication.

Did you like this article? Rate it!

1 Star2 Stars3 Stars4 Stars5 Stars 4.00 (2 rating(s))
Loading...
Emma

Emma

I am passionate about travelling and currently live and work in Paris. I like to spend my time reading, gardening, running, learning languages and exploring new places.