Code
<- c("tidyverse", "broom")
packages ::pkg_attach(packages, message = F) xfun
January 30, 2019
In psychology, we typically learn how to calculate OLS regression by calculating each coefficient separately. However, I recently learned how to calculate this using matrix algebra. Here is a brief tutorial on how to perform this using R.
salary yrs.since.phd.V1
Min. : 57800 Min. :-21.31486146100000
1st Qu.: 91000 1st Qu.:-10.31486146100000
Median :107300 Median : -1.31486146096000
Mean :113706 Mean : -0.00000000000001
3rd Qu.:134185 3rd Qu.: 9.68513853904000
Max. :231545 Max. : 33.68513853900000
The Salaries
dataset is from the carData
package, which shows the salary of professors in the US during the academic year of 2008 and 2009. Let’s say we are interested in determining if professors who have had their Ph.D. degree for longer are more likely to also have higher salaries.
The design matrix is just a dataset of the all the predictors, which includes the intercept
set at 1 and yrs.since.phd
.
intercept yrs.since.phd
[1,] 1 -3.314861
[2,] 1 -2.314861
[3,] 1 -18.314861
[4,] 1 22.685139
[5,] 1 17.685139
[6,] 1 -16.314861
[,1]
[1,] 139750
[2,] 173200
[3,] 79750
[4,] 115000
[5,] 141500
[6,] 97000
First, we need to solve for \(X'X\), which is the transposed design matrix (\(X'\)) multiplied by the design matrix (\(X\)).
Let’s take a look at what \(X'\) looks like.
[,1] [,2] [,3] [,4] [,5] [,6]
intercept 1.000000 1.000000 1.00000 1.00000 1.00000 1.00000
yrs.since.phd -3.314861 -2.314861 -18.31486 22.68514 17.68514 -16.31486
After multiplication, the matrix provides the total number of participants (\(n\) = 397; really, the sum of the intercept), sum of yrs.since.phd
(\(\Sigma(yrs.since.phd)\) = 0), and sum of squared yrs.since.phd
(\(\Sigma (yrs.since.phd^2)\) = 65765.64). Respectively, \(\Sigma (years.since.phd)\) and \(\Sigma (yrs.since.phd^2)\) are sum of error (\(\Sigma(yrs.since.phd-M_{yrs.since.phd})\)) and sum of squared error (\(\Sigma(yrs.since.phd-M_{yrs.since.phd})^2\)) because we first centered the yrs.since.phd
variable.
intercept yrs.since.phd
intercept 397 0.00
yrs.since.phd 0 65765.64
Let’s verify this.
intercept yrs.since.phd
397 0
intercept yrs.since.phd
397.00 65765.64
\((X'X)^{-1}\) is the inverse matrix of \(X'X\).
intercept yrs.since.phd
intercept 2.518892e-03 9.280150e-20
yrs.since.phd 9.280150e-20 1.520551e-05
\(X'Y\) contains the sum of Y (\(\Sigma Y\) = 45141464) and sum of \(XY\) (\(\Sigma XY\) = 64801658).
Let’s verify this.
To obtain the coefficients, we can multiply these last two matrices (\(B = (X'X)^{-1}X'Y\)).
To calculate the standard error, we multiply the inverse matrix of \(X'X\) by the mean squared error (MSE) of the model and take the square root of its diagonal matrix (\(\sqrt{diag((X'X)^{-1} * MSE)}\)).
First, we need to calculate the \(MSE\) of the model. Calculating \(MSE\) of the model is still the same, \(MSE = \frac{\Sigma(Y-\hat{Y})^{2}}{n-p} = \frac{\Sigma(e^2)}{df}\) where \(Y\) is the DV, \(\hat{Y}\) is the predicted DV, \(n\) is the total number of participants (or data points), and \(p\) is the total number of variables in the design matrix (or predictors, which includes the intercept).
To obtain the predicted values (\(\hat{Y}\)), we can also use matrix algebra by multiplying the design matrix with the coefficients (\(\hat{Y} = XB\)).
[,1]
[1,] 110440.19
[2,] 111425.53
[3,] 95660.05
[4,] 136059.08
[5,] 131132.37
[6,] 97630.74
Now that we have \(\hat{Y}\), we can then calculate the \(MSE\).
[1] 758098328
Then, we multiply \((X'X)^{-1}\) by MSE.
intercept yrs.since.phd
intercept 1909568 0.00
yrs.since.phd 0 11527.27
Then, we take the square root of the diagonal matrix to obtain the standard error of the coefficients.
intercept yrs.since.phd
1381.87 107.37
The t-statistic is just the coefficient divided by the standard error of the coefficient.
We want the probability of obtaining that score or more extreme and not the other way around. Thus, we need to set lower to FALSE. Also, we need to multiply it by 2 to obtain a two-tailed test.
# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 intercept 113706. 1382. 82.3 1.07e-250
2 yrs.since.phd 985. 107. 9.18 2.50e- 18
lm
Function# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 113706. 1382. 82.3 1.07e-250
2 yrs.since.phd 985. 107. 9.18 2.50e- 18