The Ordinary Least Squares estimator, is the first thing one learns in econometrics. It has two forms, one in standard algebra and one in matrix algebra, but it's important to remember the two are equivalent:
I think most students will find it extremely easy to get lost in notation and miss the link to be made with real world data. The following exercise is a helpful way I found to make sure one continues to make the link between traditional 'simple' notation, Matrix Algebra notation, and the underlying data and arithmetic that goes into the ordinary linear regression estimator.
Deriving the Algebraic Notation for the Simple Bivariate Model
The familiar simple bivariate model is expressed as an independent observation as a function of an intercept, a regression coefficient, and an error term (respectively):
Where we wish to minimize the sum of squared errors (SSE):
To do so we isolate the error of the regression to make it a function of the other terms:
For our purposes, we'll ignore the derivation of the intercept and take it as a given that it is and just solve for the slope coefficient. To minimize the errors, we need to take the partial derivative with respect to
Move the summation operator through since the the derivative of a sum is equal to the sum of the derivatives:
Take the derivative (using the chain rule), then setting it equal to 0 for the first order condition to find the min/max:
Then multiply by to simplify:
Substitute the solution for the intercept, , that we took as a given above:
Then rearrange and distribute the summation operator to solve for :
Which is algebraically equivalent to:
Deriving the Matrix Algebra Notation
Despite typically not being taught until the senior undergraduate, or graduate, level: the derivation for the matrix notation is actually a little more straightforward (as long as one remembers the rules for matrix algebra (which I typically do not)).
First, visualize the linear model again, but this time in matrix notation where Y and e are vectors of the observations and X is the matrix of the independent variables and their observations:
Just as before, we want to minimize the sum of squared errors
Rearranging and substituting yields:
Push the transpose operator through:
Multiply the whole equation out:
Simplify the two equivalent terms in the middle:
Then again as before we'll take the partial derivative for the first order condition:
And set to 0 to find the minimum:
Then isolate and simplify:
Getting Real World Data
Pulling data from autotrader.com for Honda CR-V's, I came up with 9 observations across three simple variables: Price, Mileage, and Year.
Using car price as a function of car year in a simple bivariate model, one can find the OLS slope coefficient in R using the simple call:
options("scipen"=100, "digits"=4) price <- c(19998,16995,22491,27571,25998,24000,15495,13290,8444) mileage <- c(16568,68399,23813,15156,17201,28946,87440,83060,153549) year <- c(2012,2011,2013,2014,2014,2012,2010,2007,2006) crv <- data.frame(mileage,price,year) lm(crv$price ~ crv$year)
Which yields the basic result that the asking price of the car goes down by $2103 for each year it gets older.
The Bivariate Example Using Simple Algebra and Arithmetic
This is the important part. It's tedious, but straightforward, and writing it out all by hand will really remind you that computers are remarkable tools.
The derivation results from Part I tell us:
In this case is the sample mean for years, which is 2011, and is the sample mean for price, which is $19365.
Expanding the estimator out to its completely tangible form yields the ridiculously cumbersome equation:
Which simplifies to
Almost exactly what R told us the coefficient would be!
The Bivariate Example Using Matrix Algebra
Things get a little trickier here, but the process is the same, the bivariate estimator is:
Substituting our real world data for the general matrices leaves the form:
Which simplifies from 4 sets of matrices down to two as:
Which multiplies through to:
The first row being the intercept in our regression model; the second being our estimated slope coefficient calculated in both R and using the expanded summation operator by hand!
Derivations, proofs, and advanced topics in econometrics can be really tricky to wrap your ahead around at first. By going through the motions of making real world data stand in for the notation, it can a) help you really understand the proofs and derivations, and b) remind you just how many trillions of human hours computers save in answering complicated technical questions!