[1] 3 1 2
[1] "integer"
Applied Statistics for Social Science Research
Inspiration for a lot of the examples came from the Essense of Linear Algebra by 3blue1brown
\[ \DeclareMathOperator{\E}{\mathbb{E}} \DeclareMathOperator{\P}{\mathbb{P}} \DeclareMathOperator{\V}{\mathbb{V}} \DeclareMathOperator{\L}{\mathscr{L}} \DeclareMathOperator{\I}{\text{I}} \]
c()
function or generated random numbers with, say runif()
functionR reports the \(v\) is an integer vector
You can add two vectors in a usual way, elementwise:
%*%
, not *
, which will produce a component-wise multiplication, not a dot product\[ v^T w = v_1w_1 + v_2w_2 + \cdots + v_nw_n \]
\[ ||v|| = \sqrt{v \cdot v} = \sqrt{v_1^2 + v_2^2 + \cdots v_n^2} \]
\[ v = \left[ \begin{matrix}1\\2\end{matrix} \right] w = \left[ \begin{matrix}3\\1\end{matrix} \right] \]
\[ R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} \]
\[ Rv = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \end{bmatrix} = 1 \begin{bmatrix} 0 \\ 1 \end{bmatrix} + 2 \begin{bmatrix} -1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \end{bmatrix} + \begin{bmatrix} -2 \\ 0 \end{bmatrix} = \begin{bmatrix} -2 \\ 1 \end{bmatrix} \]
The resulting matrix \(K\), has the correctly rotated \(v\) in the first column and rotated \(w\) in the second column.
Another way to think about this operation is to encode two transformations in \(K'\) — the \(K\) transformation followed by the \(R\) transformation. We can now use the resulting \(K'\) matrix and apply these two transformations in one swoop to any vector in \(R^2\).
Think about why matrix multiplication, in general, does not commute — \(RK \neq KR\)
Your Turn: Come up with a 90-degree clockwise rotation matrix and show that it sends \((2, 2)\) to \((2, -2)\)
Now pick three vectors in \(R^2\) and rotate all three at the same time
\[ 2x + y = 1 \\ 4x + 2y = 1 \]
\[ \begin{eqnarray} Ax & = & b \\ A^{-1}Ax & = & A^{-1}b \\ x & = & A^{-1}b \end{eqnarray} \]
solve(A)
inverts the matrix, and solve(A, b)
solves \(Ax = b\).\[ \begin{eqnarray} 3x + 2y + 1.5z & = & 4 \\ 7x + y & = & 2 \\ 3y + 2z & = & 1 \end{eqnarray} \]
solve()
function, and validate that the results are correctIf you put on socks and then shoes, the first to be taken off are the ____
— Gilbert Strang (discussing the order of undoing the inverse)
\[ (ABC \dots)^{-1} = \dots C^{-1}B^{-1}A^{-1} \\ (A^T)^{-1} = (A^{-1})^T \\ (A + B)^T = A^T + B^T \\ (ABC \dots)^T = \dots C^T B^T A^T \]
For a complete list, see the Matrix Cookbook
If we can not reach \(y\), we need to find a vector in the column space of \(X\) that is closest (in a certain sense) to \(y\)
The projection of \(y\) onto this plane gives us the answer
Image from Introduction to Linear Algebra, Boyd and Vandenberghe
\[ X^T(y - X\hat{\beta}) = 0 \]
\[ \begin{eqnarray} X^T(y - X\hat{\beta}) & = & 0 \\ X^TX\hat{\beta} & = & X^Ty \\ (X^TX)^{-1}(X^TX)\hat{\beta} & = & (X^TX)^{-1}X^Ty \\ \hat{\beta} & = & (X^TX)^{-1}X^Ty \end{eqnarray} \]
[,1] [,2]
[1,] 2 1
[2,] 5 4
[3,] 3 6
[,1] [,2]
[1,] 38 40
[2,] 40 53
[,1] [,2]
[1,] 0.13 -0.10
[2,] -0.10 0.09
[,1] [,2]
[1,] 1 2
[2,] 2 4
[3,] 3 6
[,1] [,2]
[1,] 14 28
[2,] 28 56
This is a quadratic function. How can we solve it using linear regression (the least squares method)?
We assumed the equation of motion was \(x(t) = a + bt^2\), which we will write as \(y(t) = \beta_0 + \beta_1 t^2\)
The unknowns are \(\beta_0\) and \(\beta_1\)
Let’s express it in the matrix notation
What is our design matrix \(X\)? We have 51 observations, so \(X\) will have two columns and 51 rows: a column of 1s for the intercept and a column of times squared \(t^2\)
Our unknown vector \(\hat{\beta}\) has two elements \((\hat{\beta_0},\, \hat{\beta_1)}\)
\(y\) is our outcome vector of length 51, capturing the car’s position at each time point \(t\)
\(y = X\hat{\beta}\) captures our system in matrix form
[1] 51
[1] 0.0 0.1 0.2 0.3 0.4 0.5
[,1] [,2]
[1,] 1 0.00
[2,] 1 0.01
[3,] 1 0.04
[4,] 1 0.09
[5,] 1 0.16
[6,] 1 0.25
[1] 51
[1] -2.4417028 6.7615084 -0.7502334 -0.4900157 -3.5129263 1.9331119
[,1]
[1,] 1.46
[2,] 3.01
data <- data.frame(y = y, t_squared = t^2)
# compare to R's lm()
fit <- lm(y ~ t_squared, data = data)
arm::display(fit)
lm(formula = y ~ t_squared, data = data)
coef.est coef.se
(Intercept) 1.46 0.54
t_squared 3.01 0.05
---
n = 51, k = 2
residual sd = 2.60, R-Squared = 0.99
mtcars
mpg
against wt
, treating mpg
as the output variable \(y\). (We generally don’t like to use the words dependent vs. independent in this context)lm