+ All Categories
Home > Documents > Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to...

Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to...

Date post: 20-Apr-2020
Category:
Upload: others
View: 11 times
Download: 0 times
Share this document with a friend
64
Gov 2000: 10. Multiple Regression in Matrix Form Matthew Blackwell Fall 2016 1 / 64
Transcript
Page 1: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Gov 2000: 10. MultipleRegression in Matrix Form

Matthew BlackwellFall 2016

1 / 64

Page 2: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

1. Matrix algebra review

2. Matrix Operations

3. Linear model in matrix form

4. OLS in matrix form

5. OLS inference in matrix form

2 / 64

Page 3: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Where are we? Where are wegoing?

โ€ข Last few weeks: regression estimation and inference with oneand two independent variables, varying effects

โ€ข This week: the general regression model with arbitrarycovariates

โ€ข Next week: what happens when assumptions are wrong

3 / 64

Page 4: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Nunn & Wantchekon

โ€ข Are there long-term, persistent effects of slave trade onAfricans today?

โ€ข Basic idea: compare levels of interpersonal trust (๐‘Œ๐‘–) acrossdifferent levels of historical slave exports for a respondentโ€™sethnic group

โ€ข Problem: ethnic groups and respondents might differ in theirinterpersonal trust in ways that correlate with the severity ofslave exports

โ€ข One solution: try to control for relevant differences betweengroups via multiple regression

4 / 64

Page 5: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Nunn & Wantchekon

โ€ข Whaaaaa? Bold letter, quotation marks, what is this?โ€ข Todayโ€™s goal is to decipher this type of writing

5 / 64

Page 6: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Multiple Regression in Rnunn <- foreign::read.dta("../data/Nunn_Wantchekon_AER_2011.dta")mod <- lm(trust_neighbors ~ exports + age + male + urban_dum

+ malaria_ecology, data = nunn)summary(mod)

#### Coefficients:## Estimate Std. Error t value Pr(>|t|)## (Intercept) 1.5030370 0.0218325 68.84 <2e-16 ***## exports -0.0010208 0.0000409 -24.94 <2e-16 ***## age 0.0050447 0.0004724 10.68 <2e-16 ***## male 0.0278369 0.0138163 2.01 0.044 *## urban_dum -0.2738719 0.0143549 -19.08 <2e-16 ***## malaria_ecology 0.0194106 0.0008712 22.28 <2e-16 ***## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1#### Residual standard error: 0.978 on 20319 degrees of freedom## (1497 observations deleted due to missingness)## Multiple R-squared: 0.0604, Adjusted R-squared: 0.0602## F-statistic: 261 on 5 and 20319 DF, p-value: <2e-16

6 / 64

Page 7: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Why matrices and vectors?

7 / 64

Page 8: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

8 / 64

Page 9: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Why matrices and vectors?

โ€ข Hereโ€™s one way to write the full multiple regression model:

๐‘ฆ๐‘– = ๐›ฝ0 + ๐‘ฅ๐‘–1๐›ฝ1 + ๐‘ฅ๐‘–2๐›ฝ2 + โ‹ฏ + ๐‘ฅ๐‘–๐‘˜๐›ฝ๐‘˜ + ๐‘ข๐‘–

โ€ข Notation is going to get needlessly messy as we add variables.โ€ข Matrices are clean, but they are like a foreign language.โ€ข You need to build intuitions over a long period of time.

9 / 64

Page 10: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Quick note about interpretation

๐‘ฆ๐‘– = ๐›ฝ0 + ๐‘ฅ๐‘–1๐›ฝ1 + ๐‘ฅ๐‘–2๐›ฝ2 + โ‹ฏ + ๐‘ฅ๐‘–๐‘˜๐›ฝ๐‘˜ + ๐‘ข๐‘–

โ€ข In this model, ๐›ฝ1 is the effect of a one-unit change in ๐‘ฅ๐‘–1conditional on all other ๐‘ฅ๐‘–๐‘—.

โ€ข Jargon โ€œpartial effect,โ€ โ€œceteris paribus,โ€ โ€œall else equal,โ€โ€œconditional on the covariates,โ€ etc

โ€ข Notation change: lower-case letters here are random variables.

10 / 64

Page 11: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

1/ Matrix algebrareview

11 / 64

Page 12: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Vectors

โ€ข A vector is just list of numbers (or random variables).โ€ข A 1 ร— ๐‘˜ row vector has these numbers arranged in a row:

๐› = [ ๐‘1 ๐‘2 ๐‘3 โ‹ฏ ๐‘๐‘˜ ]

โ€ข A ๐‘˜ ร— 1 column vector arranges the numbers in a column:

๐š =โŽกโŽขโŽขโŽขโŽฃ

๐‘Ž1๐‘Ž2โ‹ฎ

๐‘Ž๐‘˜

โŽคโŽฅโŽฅโŽฅโŽฆ

โ€ข Convention weโ€™ll assume that a vector is column vector andvectors will be written with lowercase bold lettering (๐›)

12 / 64

Page 13: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Vector examples

โ€ข Vector of all covariates for a particular unit ๐‘–:

๐ฑ๐‘– =โŽกโŽขโŽขโŽขโŽขโŽขโŽฃ

1๐‘ฅ๐‘–1๐‘ฅ๐‘–2โ‹ฎ

๐‘ฅ๐‘–๐‘˜

โŽคโŽฅโŽฅโŽฅโŽฅโŽฅโŽฆ

โ€ข For the Nunn-Wantchekon data, we might have:

๐ฑ๐‘– =โŽกโŽขโŽขโŽขโŽฃ

1exports๐‘–

age๐‘–male๐‘–

โŽคโŽฅโŽฅโŽฅโŽฆ

13 / 64

Page 14: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Matrices

โ€ข A matrix is just a rectangular array of numbers.โ€ข We say that a matrix is ๐‘› ร— ๐‘˜ (โ€œ๐‘› by ๐‘˜โ€) if it has ๐‘› rows and ๐‘˜

columns.โ€ข Uppercase bold denotes a matrix:

๐€ =โŽกโŽขโŽขโŽขโŽฃ

๐‘Ž11 ๐‘Ž12 โ‹ฏ ๐‘Ž1๐‘˜๐‘Ž21 ๐‘Ž22 โ‹ฏ ๐‘Ž2๐‘˜โ‹ฎ โ‹ฎ โ‹ฑ โ‹ฎ

๐‘Ž๐‘›1 ๐‘Ž๐‘›2 โ‹ฏ ๐‘Ž๐‘›๐‘˜

โŽคโŽฅโŽฅโŽฅโŽฆ

โ€ข Generic entry: ๐‘Ž๐‘–๐‘— where this is the entry in row ๐‘– and column ๐‘—

14 / 64

Page 15: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Examples of matrices

โ€ข One example of a matrix that weโ€™ll use a lot is the designmatrix, which has a column of ones, and then each of thesubsequent columns is each independent variable in theregression.

๐— =โŽกโŽขโŽขโŽขโŽฃ

1 exports1 age1 male11 exports2 age2 male2โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ1 exports๐‘› age๐‘› male๐‘›

โŽคโŽฅโŽฅโŽฅโŽฆ

15 / 64

Page 16: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Design matrix in R

head(model.matrix(mod), 8)

## (Intercept) exports age male urban_dum malaria_ecology## 1 1 855 40 0 0 28.15## 2 1 855 25 1 0 28.15## 3 1 855 38 1 1 28.15## 4 1 855 37 0 1 28.15## 5 1 855 31 1 0 28.15## 6 1 855 45 0 0 28.15## 7 1 855 20 1 0 28.15## 8 1 855 31 0 0 28.15

dim(model.matrix(mod))

## [1] 20325 6

16 / 64

Page 17: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

2/ MatrixOperations

17 / 64

Page 18: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Transpose

โ€ข The transpose of a matrix ๐€ is the matrix created byswitching the rows and columns of the data and is denoted ๐€โ€ฒ.

โ€ข ๐‘˜th column of ๐€ becomes the ๐‘˜th row of ๐€โ€ฒ:

๐€ = โŽกโŽขโŽขโŽฃ

๐‘Ž11 ๐‘Ž12๐‘Ž21 ๐‘Ž22๐‘Ž31 ๐‘Ž32

โŽคโŽฅโŽฅโŽฆ

๐€โ€ฒ = [ ๐‘Ž11 ๐‘Ž21 ๐‘Ž31๐‘Ž12 ๐‘Ž22 ๐‘Ž32

]

โ€ข If ๐€ is ๐‘› ร— ๐‘˜, then ๐€โ€ฒ will be ๐‘˜ ร— ๐‘›.โ€ข Also written ๐€๐“

18 / 64

Page 19: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Transposing vectors

โ€ข Transposing will turn a ๐‘˜ ร— 1 column vector into a 1 ร— ๐‘˜ rowvector and vice versa:

๐ฑ๐‘– =โŽกโŽขโŽขโŽขโŽขโŽขโŽฃ

1๐‘ฅ๐‘–1๐‘ฅ๐‘–2โ‹ฎ

๐‘ฅ๐‘–๐‘˜

โŽคโŽฅโŽฅโŽฅโŽฅโŽฅโŽฆ

๐ฑโ€ฒ๐‘– = [ 1 ๐‘ฅ๐‘–1 ๐‘ฅ๐‘–2 โ‹ฏ ๐‘ฅ๐‘–๐‘˜ ]

19 / 64

Page 20: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Transposing in R

a <- matrix(1:6, ncol = 3, nrow = 2)a

## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6

t(a)

## [,1] [,2]## [1,] 1 2## [2,] 3 4## [3,] 5 6

20 / 64

Page 21: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Write matrices as vectorsโ€ข A matrix is just a collection of vectors (row or column)โ€ข As a row vector:

๐€ = [ ๐‘Ž11 ๐‘Ž12 ๐‘Ž13๐‘Ž21 ๐‘Ž22 ๐‘Ž23

] = [ ๐šโ€ฒ1

๐šโ€ฒ2

]

with row vectors๐šโ€ฒ

1 = [ ๐‘Ž11 ๐‘Ž12 ๐‘Ž13 ] ๐šโ€ฒ2 = [ ๐‘Ž21 ๐‘Ž22 ๐‘Ž23 ]

โ€ข Or we can define it in terms of column vectors:

๐ = โŽกโŽขโŽขโŽฃ

๐‘11 ๐‘12๐‘21 ๐‘22๐‘31 ๐‘32

โŽคโŽฅโŽฅโŽฆ

= [ ๐›๐Ÿ ๐›๐Ÿ ]

where ๐›๐Ÿ and ๐›๐Ÿ represent the columns of ๐.โ€ข ๐‘— subscripts columns of a matrix: ๐ฑ๐‘—โ€ข ๐‘– and ๐‘ก will be used for rows ๐ฑโ€ฒ

๐‘– .21 / 64

Page 22: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Design matrix

โ€ข Design matrix as a series of row vectors:

๐— =โŽกโŽขโŽขโŽขโŽฃ

1 exports1 age1 male11 exports2 age2 male2โ‹ฎ โ‹ฎ โ‹ฎ โ‹ฎ1 exports๐‘› age๐‘› male๐‘›

โŽคโŽฅโŽฅโŽฅโŽฆ

=โŽกโŽขโŽขโŽขโŽฃ

๐ฑโ€ฒ1

๐ฑโ€ฒ2โ‹ฎ

๐ฑโ€ฒ๐‘›

โŽคโŽฅโŽฅโŽฅโŽฆ

โ€ข Design matrix as a series of column vectors:

๐— = [ ๐Ÿ ๐ฑ1 ๐ฑ2 โ‹ฏ ๐ฑ๐‘˜ ]

22 / 64

Page 23: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Addition and subtraction

โ€ข How do we add or subtract matrices and vectors?โ€ข First, the matrices/vectors need to be comformable, meaning

that the dimensions have to be the same.โ€ข Let ๐€ and ๐ both be 2 ร— 2 matrices. Then, let ๐‚ = ๐€ + ๐,

where we add each cell together:

๐€ + ๐ = [ ๐‘Ž11 ๐‘Ž12๐‘Ž21 ๐‘Ž22

] + [ ๐‘11 ๐‘12๐‘21 ๐‘22

]

= [ ๐‘Ž11 + ๐‘11 ๐‘Ž12 + ๐‘12๐‘Ž21 + ๐‘21 ๐‘Ž22 + ๐‘22

]

= [ ๐‘11 ๐‘12๐‘21 ๐‘22

]

= ๐‚

23 / 64

Page 24: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Scalar multiplication

โ€ข A scalar is just a single number: you can think of it sort oflike a 1 by 1 matrix.

โ€ข When we multiply a scalar by a matrix, we just multiply eachelement/cell by that scalar:

๐‘๐€ = ๐‘ [ ๐‘Ž11 ๐‘Ž12๐‘Ž21 ๐‘Ž22

] = [ ๐‘ ร— ๐‘Ž11 ๐‘ ร— ๐‘Ž12๐‘ ร— ๐‘Ž21 ๐‘ ร— ๐‘Ž22

]

24 / 64

Page 25: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

3/ Linear model inmatrix form

25 / 64

Page 26: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

The linear model with newnotation

โ€ข Remember that we wrote the linear model as the following forall ๐‘– โˆˆ {1, โ€ฆ , ๐‘›}:

๐‘ฆ๐‘– = ๐›ฝ0 + ๐‘ฅ๐‘–๐›ฝ1 + ๐‘ง๐‘–๐›ฝ2 + ๐‘ข๐‘–

โ€ข Imagine we had an ๐‘› of 4. We could write out each formula:

๐‘ฆ1 = ๐›ฝ0 + ๐‘ฅ1๐›ฝ1 + ๐‘ง1๐›ฝ2 + ๐‘ข1 (unit 1)๐‘ฆ2 = ๐›ฝ0 + ๐‘ฅ2๐›ฝ1 + ๐‘ง2๐›ฝ2 + ๐‘ข2 (unit 2)๐‘ฆ3 = ๐›ฝ0 + ๐‘ฅ3๐›ฝ1 + ๐‘ง3๐›ฝ2 + ๐‘ข3 (unit 3)๐‘ฆ4 = ๐›ฝ0 + ๐‘ฅ4๐›ฝ1 + ๐‘ง4๐›ฝ2 + ๐‘ข4 (unit 4)

26 / 64

Page 27: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

The linear model with newnotation

๐‘ฆ1 = ๐›ฝ0 + ๐‘ฅ1๐›ฝ1 + ๐‘ง1๐›ฝ2 + ๐‘ข1 (unit 1)๐‘ฆ2 = ๐›ฝ0 + ๐‘ฅ2๐›ฝ1 + ๐‘ง2๐›ฝ2 + ๐‘ข2 (unit 2)๐‘ฆ3 = ๐›ฝ0 + ๐‘ฅ3๐›ฝ1 + ๐‘ง3๐›ฝ2 + ๐‘ข3 (unit 3)๐‘ฆ4 = ๐›ฝ0 + ๐‘ฅ4๐›ฝ1 + ๐‘ง4๐›ฝ2 + ๐‘ข4 (unit 4)

โ€ข We can write this as:

โŽกโŽขโŽขโŽขโŽฃ

๐‘ฆ1๐‘ฆ2๐‘ฆ3๐‘ฆ4

โŽคโŽฅโŽฅโŽฅโŽฆ

=โŽกโŽขโŽขโŽขโŽฃ

1111

โŽคโŽฅโŽฅโŽฅโŽฆ

๐›ฝ0 +โŽกโŽขโŽขโŽขโŽฃ

๐‘ฅ1๐‘ฅ2๐‘ฅ3๐‘ฅ4

โŽคโŽฅโŽฅโŽฅโŽฆ

๐›ฝ1 +โŽกโŽขโŽขโŽขโŽฃ

๐‘ง1๐‘ง2๐‘ง3๐‘ง4

โŽคโŽฅโŽฅโŽฅโŽฆ

๐›ฝ2 +โŽกโŽขโŽขโŽขโŽฃ

๐‘ข1๐‘ข2๐‘ข3๐‘ข4

โŽคโŽฅโŽฅโŽฅโŽฆ

โ€ข Outcome is a linear combination of the the ๐ฑ, ๐ณ, and ๐ฎ vectors

27 / 64

Page 28: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Grouping things into matrices

โ€ข Can we write this in a more compact form? Yes! Let ๐— and ๐œทbe the following:

๐—(4ร—3)

=โŽกโŽขโŽขโŽขโŽฃ

1 ๐‘ฅ1 ๐‘ง11 ๐‘ฅ2 ๐‘ง21 ๐‘ฅ3 ๐‘ง31 ๐‘ฅ4 ๐‘ง4

โŽคโŽฅโŽฅโŽฅโŽฆ

๐œท(3ร—1)

= โŽกโŽขโŽขโŽฃ

๐›ฝ0๐›ฝ1๐›ฝ2

โŽคโŽฅโŽฅโŽฆ

28 / 64

Page 29: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Matrix multiplication by a vector

โ€ข We can write this more compactly as a matrix(post-)multiplied by a vector:

โŽกโŽขโŽขโŽขโŽฃ

1111

โŽคโŽฅโŽฅโŽฅโŽฆ

๐›ฝ0 +โŽกโŽขโŽขโŽขโŽฃ

๐‘ฅ1๐‘ฅ2๐‘ฅ3๐‘ฅ4

โŽคโŽฅโŽฅโŽฅโŽฆ

๐›ฝ1 +โŽกโŽขโŽขโŽขโŽฃ

๐‘ง1๐‘ง2๐‘ง3๐‘ง4

โŽคโŽฅโŽฅโŽฅโŽฆ

๐›ฝ2 = ๐—๐œท

โ€ข Multiplication of a matrix by a vector is just the linearcombination of the columns of the matrix with the vectorelements as weights/coefficients.

โ€ข And the left-hand side here only uses scalars times vectors,which is easy!

29 / 64

Page 30: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

General matrix by vectormultiplication

โ€ข ๐€ is a ๐‘› ร— ๐‘˜ matrixโ€ข ๐› is a ๐‘˜ ร— 1 column vectorโ€ข Columns of ๐€ have to match rows of ๐›โ€ข Let ๐š๐‘— be the ๐‘—th column of ๐ด. Then we can write:

๐œ(๐‘›ร—1)

= ๐€๐› = ๐‘1๐š1 + ๐‘2๐š2 + โ‹ฏ + ๐‘๐‘˜๐š๐‘˜

โ€ข ๐œ is linear combination of the columns of ๐€

30 / 64

Page 31: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Back to regression

โ€ข ๐— is the ๐‘› ร— (๐‘˜ + 1) design matrix of independent variablesโ€ข ๐œท be the (๐‘˜ + 1) ร— 1 column vector of coefficients.โ€ข ๐—๐œท will be ๐‘› ร— 1:

๐—๐œท = ๐›ฝ0 + ๐›ฝ1๐ฑ1 + ๐›ฝ2๐ฑ2 + โ‹ฏ + ๐›ฝ๐‘˜๐ฑ๐‘˜

โ€ข Thus, we can compactly write the linear model as thefollowing:

๐ฒ(๐‘›ร—1)

= ๐—๐œท(๐‘›ร—1)

+ ๐ฎ(๐‘›ร—1)

31 / 64

Page 32: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Inner product

โ€ข The inner (or dot) product of a two column vectors ๐š and ๐›(of equal dimension, ๐‘˜ ร— 1):

โŸจ๐š, ๐›โŸฉ = ๐šโ€ฒ๐› = ๐‘Ž1๐‘1 + ๐‘Ž2๐‘2 + โ‹ฏ + ๐‘Ž๐‘˜๐‘๐‘˜

โ€ข If ๐šโ€ฒ๐› = 0 we say that the two vectors are orthogonal.โ€ข With ๐œ = ๐€๐›, we can write the entries of ๐œ as inner products:

๐‘๐‘– = ๐šโ€ฒ๐‘–๐›

โ€ข If ๐ฑโ€ฒ๐‘– is the ๐‘–th row of ๐—, then we write the linear model as:

๐‘ฆ๐‘– = ๐ฑโ€ฒ๐‘–๐œท + ๐‘ข๐‘–

= ๐›ฝ0 + ๐‘ฅ๐‘–1๐›ฝ1 + ๐‘ฅ๐‘–2๐›ฝ2 + โ‹ฏ + ๐‘ฅ๐‘–๐‘˜๐›ฝ๐‘˜ + ๐‘ข๐‘–

32 / 64

Page 33: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

4/ OLS in matrixform

33 / 64

Page 34: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Matrix multiplication

โ€ข What if, instead of a column vector ๐‘, we have a matrix ๐with dimensions ๐‘˜ ร— ๐‘š.

โ€ข How do we do multiplication like so ๐‚ = ๐€๐?โ€ข Each column of the new matrix is just matrix by vector

multiplication:

๐‚ = [๐œ1 ๐œ2 โ‹ฏ ๐œ๐‘š] ๐œ๐‘— = ๐€๐›๐‘—

โ€ข Thus, each column of ๐‚ is a linear combination of thecolumns of ๐€.

34 / 64

Page 35: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Properties of matrix multiplication

โ€ข Matrix multiplication is not commutative: ๐€๐ โ‰  ๐๐€โ€ข It is associative and distributive:

๐€(๐๐‚) = (๐€๐)๐‚๐€(๐ + ๐‚) = ๐€๐ + ๐€๐‚

โ€ข The transpose: (๐€๐)โ€ฒ = ๐โ€ฒ๐€โ€ฒ

35 / 64

Page 36: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Square matrices and the diagonal

โ€ข A square matrix has equal numbers of rows and columns.โ€ข The identity matrix, ๐ˆ๐‘˜ is a ๐‘˜ ร— ๐‘˜ square matrix, with 1s along

the diagonal and 0s everywhere else.

๐ˆ3 = โŽกโŽขโŽขโŽฃ

1 0 00 1 00 0 1

โŽคโŽฅโŽฅโŽฆ

โ€ข The ๐‘˜ ร— ๐‘˜ identity matrix multiplied by any ๐‘š ร— ๐‘˜ matrixreturns the matrix:

๐€๐ˆ๐‘˜ = ๐€

36 / 64

Page 37: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Identity matrixโ€ข To get the diagonal of a matrix in R, use the diag() function:

b <- matrix(1:4, nrow = 2, ncol = 2)b

## [,1] [,2]## [1,] 1 3## [2,] 2 4

diag(b)

## [1] 1 4

โ€ข diag() also creates identity matrices in R:diag(3)

## [,1] [,2] [,3]## [1,] 1 0 0## [2,] 0 1 0## [3,] 0 0 1

37 / 64

Page 38: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Multiple linear regression in matrixform

โ€ข Let ๐œท be the matrix of estimated regression coefficients and ๏ฟฝ๏ฟฝbe the vector of fitted values:

๐œท =โŽกโŽขโŽขโŽขโŽฃ

๐›ฝ0๐›ฝ1โ‹ฎ

๐›ฝ๐‘˜

โŽคโŽฅโŽฅโŽฅโŽฆ

๏ฟฝ๏ฟฝ = ๐—๐œท

โ€ข It might be helpful to see this again more written out:

๏ฟฝ๏ฟฝ =โŽกโŽขโŽขโŽขโŽฃ

๐‘ฆ1๐‘ฆ2โ‹ฎ๐‘ฆ๐‘›

โŽคโŽฅโŽฅโŽฅโŽฆ

= ๐—๐œท =โŽกโŽขโŽขโŽขโŽฃ

1๐›ฝ0 + ๐‘ฅ11๐›ฝ1 + ๐‘ฅ12๐›ฝ2 + โ‹ฏ + ๐‘ฅ1๐‘˜๐›ฝ๐‘˜1๐›ฝ0 + ๐‘ฅ21๐›ฝ1 + ๐‘ฅ22๐›ฝ2 + โ‹ฏ + ๐‘ฅ2๐‘˜๐›ฝ๐‘˜

โ‹ฎ1๐›ฝ0 + ๐‘ฅ๐‘›1๐›ฝ1 + ๐‘ฅ๐‘›2๐›ฝ2 + โ‹ฏ + ๐‘ฅ๐‘›๐‘˜๐›ฝ๐‘˜

โŽคโŽฅโŽฅโŽฅโŽฆ

38 / 64

Page 39: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Residuals

โ€ข We can easily write the residuals in matrix form:

๏ฟฝ๏ฟฝ = ๐ฒ โˆ’ ๐—๐œท

โ€ข The norm or length of a vector generalizes Euclidean distanceand is just the square root of the squared entries,

โ€–๐šโ€– = โˆš๐‘Ž21 + ๐‘Ž2

2 + โ‹ฏ + ๐‘Ž2๐‘˜

โ€ข We can write the norm in terms of inner product: โ€–๐šโ€–2 = ๐šโ€ฒ๐šโ€ข Thus we can compactly write the sum of the squared residuals

as:โ€–๏ฟฝ๏ฟฝโ€–2 = ๏ฟฝ๏ฟฝโ€ฒ๏ฟฝ๏ฟฝ

=๐‘›

โˆ‘๐‘–=1

๐‘ข2๐‘–

39 / 64

Page 40: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

OLS estimator in matrix form

โ€ข OLS still minimizes sum of the squared residuals

arg min๐›โˆˆโ„๐‘˜+1

โ€–๏ฟฝ๏ฟฝโ€–2 = arg min๐›โˆˆโ„๐‘˜+1

โ€–๐ฒ โˆ’ ๐—๐›โ€–2

โ€ข Take (matrix) derivatives, set equal to 0โ€ข Resulting first order conditions:

๐—โ€ฒ(๐ฒ โˆ’ ๐—๐œท) = 0

โ€ข Rearranging:๐—โ€ฒ๐—๐œท = ๐—โ€ฒ๐ฒ

โ€ข In order to isolate ๐œท, we need to move the ๐—โ€ฒ๐— term to theother side of the equals sign.

โ€ข Weโ€™ve learned about matrix multiplication, but what aboutmatrix โ€œdivisionโ€?

40 / 64

Page 41: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Scalar inverses

โ€ข What is division in its simplest form? 1๐‘Ž is the value such that๐‘Ž1๐‘Ž = 1:

โ€ข For some algebraic expression: ๐‘Ž๐‘ข = ๐‘, letโ€™s solve for ๐‘ข:

1๐‘Ž๐‘Ž๐‘ข = 1

๐‘Ž๐‘

๐‘ข = ๐‘๐‘Ž

โ€ข Need a matrix version of this: 1๐‘Ž .

41 / 64

Page 42: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Matrix inverses

โ€ข Definition If it exists, the inverse of square matrix ๐€, denoted๐€โˆ’1, is the matrix such that ๐€โˆ’1๐€ = ๐ˆ.

โ€ข We can use the inverse to solve (systems of) equations:

๐€๐ฎ = ๐›๐€โˆ’๐Ÿ๐€๐ฎ = ๐€โˆ’๐Ÿ๐›

๐ˆ๐ฎ = ๐€โˆ’๐Ÿ๐›๐ฎ = ๐€โˆ’๐Ÿ๐›

โ€ข If the inverse exists, we say that ๐€ is invertible or nonsingular.

42 / 64

Page 43: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Back to OLS

โ€ข Letโ€™s assume, for now, that the inverse of ๐—โ€ฒ๐— exists (weโ€™llcome back to this)

โ€ข Then we can write the OLS estimator as the following:

๐œท = (๐—โ€ฒ๐—)โˆ’1๐—โ€ฒ๐ฒ

โ€ข Memorize this: โ€œex prime ex inverse ex prime yโ€ sear it intoyour soul.

43 / 64

Page 44: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Understanding check

โ€ข Suppose ๐ฒ is ๐‘› ร— 1 and ๐— is ๐‘› ร— (๐‘˜ + 1).โ€ข What are the dimensions of ๐—โ€ฒ๐—?โ€ข True/False: ๐—โ€ฒ๐— is symmetric.

โ–ถ Note: A square matrix is symmetric if ๐€ = ๐€โ€ฒ.

โ€ข What are the dimensions of (๐—โ€ฒ๐—)โˆ’1?โ€ข What are the dimensions of ๐—โ€ฒ๐ฒ?โ€ข What are the dimensions of ๐œท?

44 / 64

Page 45: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Implications of OLS

โ€ข We can generalize some mechanical results about OLS.โ€ข The independent variables are orthogonal to the residuals:

๐—โ€ฒ๏ฟฝ๏ฟฝ = ๐—โ€ฒ(๐ฒ โˆ’ ๐—๐œท) = 0

โ€ข The fitted values are orthogonal to the residuals:

๏ฟฝ๏ฟฝโ€ฒ๏ฟฝ๏ฟฝ = (๐—๐œท)โ€ฒ๏ฟฝ๏ฟฝ = ๐œทโ€ฒ๐—โ€ฒ๏ฟฝ๏ฟฝ = 0

45 / 64

Page 46: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

OLS by hand in R

๐œท = (๐—โ€ฒ๐—)โˆ’1๐—โ€ฒ๐ฒ

โ€ข First we need to get the design matrix and the response:

X <- model.matrix(trust_neighbors ~ exports + age + male+ urban_dum + malaria_ecology, data = nunn)

dim(X)

## [1] 20325 6

## model.frame always puts the response in the first columny <- model.frame(trust_neighbors ~ exports + age + male

+ urban_dum + malaria_ecology, data = nunn)[,1]length(y)

## [1] 20325

46 / 64

Page 47: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

OLS by hand in R

๐œท = (๐—โ€ฒ๐—)โˆ’1๐—โ€ฒ๐ฒ

โ€ข Use the solve() for inverses and %*% for matrixmultiplication:

solve(t(X) %*% X) %*% t(X) %*% y

## (Intercept) exports age male urban_dum## [1,] 1.503 -0.001021 0.005045 0.02784 -0.2739## malaria_ecology## [1,] 0.01941

coef(mod)

## (Intercept) exports age male## 1.503037 -0.001021 0.005045 0.027837## urban_dum malaria_ecology## -0.273872 0.019411

47 / 64

Page 48: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Intuition for the OLS in matrix form

๐œท = (๐—โ€ฒ๐—)โˆ’1๐—โ€ฒ๐ฒ

โ€ข Whatโ€™s the intuition here?โ€ข โ€œNumeratorโ€ ๐—โ€ฒ๐ฒ: is roughly composed of the covariances

between the columns of ๐— and ๐ฒโ€ข โ€œDenominatorโ€ ๐—โ€ฒ๐— is roughly composed of the sample

variances and covariances of variables within ๐—โ€ข Thus, we have something like:

๐œท โ‰ˆ (variance of ๐—)โˆ’1(covariance of ๐— & ๐ฒ)

โ€ข This is a rough sketch and isnโ€™t strictly true, but it canprovide intuition.

48 / 64

Page 49: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

5/ OLS inferencein matrix form

49 / 64

Page 50: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Random vectors

โ€ข A random vector is a vector of random variables:

๐ฑ๐‘– = [ ๐‘ฅ๐‘–1๐‘ฅ๐‘–2

]

โ€ข Here, ๐ฑ๐‘– is a random vector and ๐‘ฅ๐‘–1 and ๐‘ฅ๐‘–2 are randomvariables.

โ€ข When we talk about the distribution of ๐ฑ๐‘–, we are talkingabout the joint distribution of ๐‘ฅ๐‘–1 and ๐‘ฅ๐‘–2.

50 / 64

Page 51: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Distribution of random vectors

โ€ข Expectation of random vectors:

๐”ผ[๐ฑ๐‘–] = [ ๐”ผ[๐‘ฅ๐‘–1]๐”ผ[๐‘ฅ๐‘–2] ]

โ€ข Variance of random vectors:

๐•[๐ฑ๐‘–] = [ ๐•[๐‘ฅ๐‘–1] Cov[๐‘ฅ๐‘–1, ๐‘ฅ๐‘–2]Cov[๐‘ฅ๐‘–1, ๐‘ฅ๐‘–2] ๐•[๐‘ฅ๐‘–2] ]

โ€ข Properties of this variance-covariance matrix:โ–ถ if ๐š is constant, then ๐•[๐šโ€ฒ๐ฑ๐‘–] = ๐šโ€ฒ๐•[๐ฑ๐‘–]๐š.โ–ถ if matrix ๐€ and vector ๐› are constant, then

๐•[๐€๐ฑ๐‘– + ๐›] = ๐€๐•[๐ฑ๐‘–]๐€โ€ฒ

51 / 64

Page 52: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Most general OLS assumptions

1. Linearity: ๐‘ฆ๐‘– = ๐ฑโ€ฒ๐‘–๐œท + ๐‘ข๐‘–

2. Random/iid sample: (๐‘ฆ๐‘–, ๐ฑโ€ฒ๐‘–) are a iid sample from the

population.3. No perfect collinearity: ๐— is an ๐‘› ร— (๐‘˜ + 1) matrix with rank

๐‘˜ + 14. Zero conditional mean: ๐”ผ[๐‘ข๐‘–|๐ฑ๐‘–] = 05. Homoskedasticity: ๐•[๐‘ข๐‘–|๐ฑ๐‘–] = ๐œŽ2๐‘ข6. Normality: ๐‘ข๐‘–|๐ฑ๐‘– โˆผ ๐‘(0, ๐œŽ2๐‘ข)

52 / 64

Page 53: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Matrix rankโ€ข Definition The rank of a matrix is the maximum number of

linearly independent columns.โ€ข Definition The columns of a matrix ๐— are linearly

independent if ๐—๐› = 0 if and only if ๐› = 0:

๐‘1๐ฑ1 + ๐‘2๐ฑ๐Ÿ + โ‹ฏ + ๐‘๐‘˜๐ฑ๐‘˜ = 0

โ€ข Example violation: one column is a linear function of theothers.

โ–ถ 3 covariates with ๐ฑ1 = ๐ฑ2 + ๐ฑ3

0 = ๐‘1๐ฑ1 + ๐‘2๐ฑ2 + ๐‘3๐ฑ3= ๐‘1(๐ฑ2 + ๐ฑ3) + ๐‘2๐ฑ2 + ๐‘3๐ฑ3= (๐‘1 + ๐‘2)๐ฑ2 + (๐‘1 + ๐‘3)๐ฑ3

โ€ข โ€ฆequals 0 when ๐‘1 = โˆ’๐‘2 = โˆ’๐‘3 โ‡ not linearly independent!53 / 64

Page 54: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Rank and matrix inversion

โ€ข If ๐— is ๐‘› ร— (๐‘˜ + 1) has rank ๐‘˜ + 1, then all of its columns arelinearly independent

โ–ถ Generalization of no perfect collinearity to arbitrary ๐‘˜.

โ€ข ๐— has rank ๐‘˜ + 1โ‡ (๐—โ€ฒ๐—) has rank ๐‘˜ + 1โ€ข If a square (๐‘˜ + 1) ร— (๐‘˜ + 1) matrix has rank ๐‘˜ + 1, then it is

invertible.โ€ข ๐— has rank ๐‘˜ + 1โ‡ (๐—โ€ฒ๐—)โˆ’1 exists and is unique.

54 / 64

Page 55: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Zero conditional mean error

โ€ข Combining zero mean conditional error and iid we have:

๐”ผ[๐‘ข๐‘–|๐—] = ๐”ผ[๐‘ข๐‘–|๐ฑ๐‘–] = 0

โ€ข Stacking these into the vector of errors:

๐”ผ[๐ฎ|๐—] =โŽกโŽขโŽขโŽขโŽฃ

๐”ผ[๐‘ข1|๐—]๐”ผ[๐‘ข2|๐—]

โ‹ฎ๐”ผ[๐‘ข๐‘›|๐—]

โŽคโŽฅโŽฅโŽฅโŽฆ

=โŽกโŽขโŽขโŽขโŽฃ

00โ‹ฎ0

โŽคโŽฅโŽฅโŽฅโŽฆ

55 / 64

Page 56: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Expectation of OLSโ€ข Useful to write OLS as:

๐œท = (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐ฒ= (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ(๐—๐œท + ๐ฎ)= (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐—๐œท + (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐ฎ= ๐œท + (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐ฎ

โ€ข Under assumptions 1-4, OLS is conditionally unbiased for ๐œท:

๐”ผ[๐œท|๐—] = ๐œท + (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐”ผ[๐ฎ|๐—]= ๐œท + (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐ŸŽ= ๐œท

โ€ข Implies that OLS is unconditionally unbiased: ๐”ผ[๐œท] = ๐œท56 / 64

Page 57: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Variance of OLS

โ€ข What about ๐•[๐œท|๐—]?โ€ข Using some facts about variances and matrices, can derive:

๐•[๐œท|๐—] = (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐•[๐ฎ|๐—]๐— (๐—โ€ฒ๐—)โˆ’1

โ€ข What the covariance matrix of the errors, ๐•[๐ฎ|๐—]?

๐•[๐ฎ|๐—] =โŽกโŽขโŽขโŽขโŽฃ

๐•[๐‘ข1|๐—] cov[๐‘ข1, ๐‘ข2|๐—] โ€ฆ cov[๐‘ข1, ๐‘ข๐‘›|๐—]cov[๐‘ข2, ๐‘ข1|๐—] ๐•[๐‘ข2|๐—] โ€ฆ cov[๐‘ข2, ๐‘ข๐‘›|๐—]

โ‹ฎ โ‹ฑcov[๐‘ข๐‘›, ๐‘ข1|๐—] cov[๐‘ข๐‘›, ๐‘ข2|๐—] โ€ฆ ๐•[๐‘ข๐‘›|๐—]

โŽคโŽฅโŽฅโŽฅโŽฆ

โ€ข This matrix is symmetric since cov(๐‘ข๐‘–, ๐‘ข๐‘—) = cov(๐‘ข๐‘–, ๐‘ข๐‘—)

57 / 64

Page 58: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Homoskedasicityโ€ข By homoskedasticity and iid, for any units ๐‘–, ๐‘ , ๐‘ก:

โ–ถ ๐•[๐‘ข๐‘– |๐—] = ๐•[๐‘ข๐‘– |๐ฑ๐‘–] = ๐œŽ2๐‘ข (constant variance)โ–ถ cov[๐‘ข๐‘ , ๐‘ข๐‘ก |๐—] = 0 (uncorrelated errors)

โ€ข Then, the covariance matrix of the errors is simply:

๐•[๐ฎ|๐—] = ๐œŽ2๐‘ข๐ˆ๐‘› =โŽกโŽขโŽขโŽขโŽฃ

๐œŽ2๐‘ข 0 0 โ€ฆ 00 ๐œŽ2๐‘ข 0 โ€ฆ 0

โ‹ฎ0 0 0 โ€ฆ ๐œŽ2๐‘ข

โŽคโŽฅโŽฅโŽฅโŽฆ

โ€ข Thus, we have the following:

๐•[๐œท|๐—] = (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐•[๐ฎ|๐—]๐— (๐—โ€ฒ๐—)โˆ’1

= (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ(๐œŽ2๐‘ข๐ˆ๐‘›)๐— (๐—โ€ฒ๐—)โˆ’1

= ๐œŽ2๐‘ข (๐—โ€ฒ๐—)โˆ’1 ๐—โ€ฒ๐— (๐—โ€ฒ๐—)โˆ’1

= ๐œŽ2 (๐—โ€ฒ๐—)โˆ’1

58 / 64

Page 59: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Sampling variance for OLSestimates

โ€ข Under assumptions 1-5, the sampling variance of the OLSestimator can be written in matrix form as the following:

๐•[๐œท|๐—] = ๐œŽ2๐‘ข(๐—โ€ฒ๐—)โˆ’1

โ€ข This symmetric matrix looks like this:

โŽกโŽขโŽขโŽขโŽขโŽฃ

๐• [๐›ฝ0|๐—] Cov [๐›ฝ0, ๐›ฝ1|๐—] โ‹ฏ Cov [๐›ฝ0, ๐›ฝ๐‘˜ |๐—]Cov [๐›ฝ0, ๐›ฝ1|๐—] ๐• [๐›ฝ1|๐—] โ‹ฏ Cov [๐›ฝ1, ๐›ฝ๐‘˜ |๐—]

โ‹ฎ โ‹ฎ โ‹ฑ โ‹ฎCov [๐›ฝ0, ๐›ฝ๐‘˜ |๐—] Cov [๐›ฝ๐‘˜, ๐›ฝ1|๐—] โ‹ฏ ๐• [๐›ฝ๐‘˜ |๐—]

โŽคโŽฅโŽฅโŽฅโŽฅโŽฆ

59 / 64

Page 60: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Inference in the general settingโ€ข Under assumption 1-5 in large samples:

๐›ฝ๐‘— โˆ’ ๐›ฝ๐‘—se[๐›ฝ๐‘—]

โˆผ ๐‘(0, 1)

โ€ข In small samples, under assumptions 1-6,๐›ฝ๐‘— โˆ’ ๐›ฝ๐‘—se[๐›ฝ๐‘—]

โˆผ ๐‘ก๐‘›โˆ’(๐‘˜+1)

โ€ข Thus, under the null of ๐ป0 โˆถ ๐›ฝ๐‘— = 0, we know that๐›ฝ๐‘—

se[๐›ฝ๐‘—]โˆผ ๐‘ก๐‘›โˆ’(๐‘˜+1)

โ€ข Here, the estimated SEs come from:๏ฟฝ๏ฟฝ[๐œท] = ๏ฟฝ๏ฟฝ2๐‘ข(๐—โ€ฒ๐—)โˆ’1

๏ฟฝ๏ฟฝ2๐‘ข = ๏ฟฝ๏ฟฝโ€ฒ๏ฟฝ๏ฟฝ๐‘› โˆ’ (๐‘˜ + 1)

60 / 64

Page 61: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Covariance matrix in Rโ€ข We can access this estimated covariance matrix, ๏ฟฝ๏ฟฝ2๐‘ข(๐—โ€ฒ๐—)โˆ’1,

in R:

vcov(mod)

## (Intercept) exports age male## (Intercept) 0.0004766593 1.164e-07 -7.956e-06 -6.676e-05## exports 0.0000001164 1.676e-09 -3.659e-10 7.283e-09## age -0.0000079562 -3.659e-10 2.231e-07 -7.765e-07## male -0.0000667572 7.283e-09 -7.765e-07 1.909e-04## urban_dum -0.0000965843 -4.861e-08 7.108e-07 -1.711e-06## malaria_ecology -0.0000069094 -2.124e-08 2.324e-10 -1.017e-07## urban_dum malaria_ecology## (Intercept) -9.658e-05 -6.909e-06## exports -4.861e-08 -2.124e-08## age 7.108e-07 2.324e-10## male -1.711e-06 -1.017e-07## urban_dum 2.061e-04 2.724e-09## malaria_ecology 2.724e-09 7.590e-07

61 / 64

Page 62: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Standard errors from thecovariance matrix

โ€ข Note that the diagonal are the variances. So the square rootof the diagonal is are the standard errors:

sqrt(diag(vcov(mod)))

## (Intercept) exports age male## 0.02183253 0.00004094 0.00047237 0.01381627## urban_dum malaria_ecology## 0.01435491 0.00087123

coef(summary(mod))[, "Std. Error"]

## (Intercept) exports age male## 0.02183253 0.00004094 0.00047237 0.01381627## urban_dum malaria_ecology## 0.01435491 0.00087123

62 / 64

Page 63: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Nunn & Wantchekon

63 / 64

Page 64: Gov 2000: 10. Multiple Regression in Matrix Form...Whymatricesandvectors? โ€ข Hereโ€™s one way to write the full multiple regression model: ๐‘–= ๐›ฝ0 + ๐‘–1๐›ฝ1 + ๐‘–2๐›ฝ2 +

Wrapping up

โ€ข You have the full power of matrices.โ€ข Key to writing the OLS estimator and discussing higher level

concepts in regression and beyond.โ€ข Next week: diagnosing and fixing problems with the linear

model.

64 / 64


Recommended