Lecture 5: Inference in OLS Regression Ordinary Least Squares: Matrix Form
Total Page:16
File Type:pdf, Size:1020Kb
Explained Variance in Multiple Regression Lecture 5: Inference in OLS Regression Ordinary Least Squares: Matrix Form Dave Armstrong Properties of the OLS Estimator University of Wisconsin – Milwaukee Department of Political Science Statistical Inference for OLS e: [email protected] w: www.quantoid.net/ICPSR.php Example: Duncan Data Model (Mis)Specification 1/54 2/54 Partial Relationshps Table : OLS Regressions of Occupational Prestige Model 1 Model 2 Model 3 Model 4 Intercept 27.141⇤ 10.732⇤ 48.693⇤ 6.794⇤ (2.268)− (3.677) (2.308)− (3.239) Partial relationships are those that “control” for the e↵ects of other Mean income of incumbents (in $1000) 2.897⇤ 1.314⇤ variables. How does this “controlling” happen? (0.283) (0.278) Mean incumbent years of education 5.361⇤ 4.187⇤ • Controlling happens by removing the parts of X1 and Y that are (0.332) (0.389) Percentage of women incumbents 0.064 0.009 explained by X2,...,Xk when calculating the slope coefficient. −(0.054)− (0.030) • Note that if X1 can be perfectly predicted by X2,...,Xk,thenthere N 102 102 102 102 R2 0.511 0.723 0.014 0.798 will be nothing left to be related to Y. adj. R2 0.506 0.720 0.004 0.792 • Because of the way that controlling happens, it is impossible to say Resid. sd 12.090 9.103 17.169 7.846 Main entries are OLS coefficients how much variance each variable uniquely explains, because there Standard errors in parentheses are overlaps in the variance of the X variables. ⇤ indicates singificance at p < 0.05 (two-tailed) 3/54 4/54 Partial Relationships (2) Partial Relationships in R To find the partial relationship, we can just use multiple regression, or we >EP<-lm(prestige~education+women, could do the following: +data=Prestige)$residuals • First, partial out the e↵ects of education and women on prestige >EI<-lm(income~education+women, +data=Prestige)$residuals P Prestigei = A1 + B1Educationi + D1Womeni + Ei >partial.mod<-lm(EP~EI) >coef(mod4) • Then, partial out the e↵ect of education and women on income. (Intercept) income education women I -6.794334203 1.313560428 4.186637275 -0.008905157 Incomei = A2 + B2Educationi + D2Womeni + Ei • Finally, calculate the simple regression: >coef(partial.mod) (Intercept) EI P I Ei = A + BEi + Ei 8.190603e-16 1.313560e+00 5/54 6/54 Vectors and Matrices Explained Variance in Multiple Regression Ordinary Least Squares: Matrix Form • A vector is a listing of numbers in a particular order. We can have Properties of the OLS Estimator 1 2 row-vectors = [1, 2, 3, 4] or column vectors = v v 2 3 3 Statistical Inference for OLS 6 7 6 4 7 6 7 • The ordering of the numbers matters, thus if v =6 [4,73, 2, 1],then ⇤ 46 57 Example: Duncan Data v , v⇤. Model (Mis)Specification 7/54 8/54 Vectors Vectors in R Consider the following two fectors u = [3, 3, 3, 3] (or u = [u , u , u , u ]) We already know how to make vectors in R, it’s just with the c() 1 2 3 4 command. and v = [1, 2, 3, 4] (or v = [v1, v2, v3, v4]). >u<-c(3,3,3,3) • u + v = [u1 + v1, u2 + v2, u3 + v3, u4, v4] = [3 + 1, 3 + 2, 3 + 3, 3 + 4] = [4, 5, 6, 7] >v<-c(1,2,3,4) >u+v • u v = [u1 v1, u2 v2, u3 v3, u4, v4] = [3− 1, 3 2−, 3 3, 3− 4] =−[2, 1, 0, 1] [1] 4 5 6 7 − − − − − • Scalar multiplication and division simply result in the elements of >u-v the vector being multiplied or divided by the scalar. [1] 2 1 0 -1 9/54 10 / 54 Conformability Inner Product The inner product (or dot product) of vectors is an important type of calculation. Vectors can only be added, subtracted, etc... if they are “conformable”. • The vectors have to be of the same size. • The inner product of two vectors results in a scalar (i.e., a single • In the examples above (addition and subtraction) this amounts to number) the vectors having the same length. If vectors do not have the same • For example: u v = [u v + u v + u v + u v ] with the data above: length, they cannot be added together. · 1 1 2 2 3 3 4 4 • This will amount to something di↵erent when we talk about u v = [3 1 + 3 2 + 3 3 + 3 4] = [3 + 6 + 9 + 12] = 30 multiplication of matrices. · ⇥ ⇥ ⇥ ⇥ >u%*%v [,1] [1,] 30 11 / 54 12 / 54 Outer Product I Outer Product II Where the inner-product of vectors generates a scalar (a single-value), the outer-product generates a matrix. • When u is 1 k and v0 is k 1.Theresultis1 1 because the result • A row vector has dimensions 1 k and a column-vector has is the sum of⇥ the product of⇥ pairwise elements⇥ of each row (of which dimensions k 1. ⇥ ⇥ there is 1) and each column (of which there is 1). • In matrix multiplication, as we’ll see shortly, matrices are conformable if they have the same inner-dimension. So, we could The outer product switches these two: u0v.Sinceu0 is k 1 and v is , the result is a matrix with dimensions . ⇥ multiply something 1 k by something that was k n where n is 1 k k k ⇥ ⇥ ⇥ ⇥ some positive integer. When should do the same with vectors. 3 3 6 9 12 • Both u and v are row-vectors above. To make one of them a 3 3 6 9 12 0 = [1, 2, 3, 4] = (1) column-vector, we will have to transpose it. Transposing u v 2 3 3 2 3 6 9 12 3 6 7 6 7 interchanges the rows and columns of a matrix/vector. We can 6 3 7 6 3 6 9 12 7 6 7 6 7 indicate transposition by v0. 6 7 6 7 6 7 6 7 • To be perfectly correct above, we should have multiplied u v . 4 5 4 5 · 0 13 / 54 14 / 54 Outer Product in R Matrices >u%o%v [,1] [,2] [,3] [,4] A matrix is a rectangular arrangement of numbers. Matrices have two [1,] 3 6 9 12 dimensions - the number of rows and the number of columns. The [2,] 3 6 9 12 ordering in both the rows and columns of the matrix matters. For [3,] 3 6 9 12 example: [4,] 3 6 9 12 12 = (2) >outer(u,v,"*") X 34 [,1] [,2] [,3] [,4] " # is a 2 2 matrix. We could say x[2, 1] = x = 3, that is the second [1,] 3 6 9 12 X 21 row, first⇥ column of = 3. [2,] 3 6 9 12 X [3,] 3 6 9 12 [4,] 3 6 9 12 15 / 54 16 / 54 Special matrices Matrix Addition/Subtraction We can also do math on matrices. For addition and subtraction, the • A square matrix is one with the same number of rows and columns. matrices must be of the same order (that is, they must have the same The rows and columns needn’t be the same, but the number of dimensions). elements does need to be the same. x11 x12 y11 y12 x11 + y11 x21 + y21 • A symmetric matrix is one where xij = x ji. X = , Y = , X + Y = x21 x22 y21 y22 x21 + y21 x22 + y22 " # " # " # • A diagonal matrix is a square matrix where xij is non-zero when 12 56 i = j and is zero when i , j. To test, add = and = . X 34 Y 78 • An identity matrix is a diagonal matrix where all diagonal elements " # " # 68 equal 1. Answer: . 10 12 " # 17 / 54 18 / 54 Matrices in R Matrix Multiplication I Multiplication is where things get a bit tricky, but you might be able to see where we’re going. When you enter data into a matrix in R, you have to specify how many x x y y X = 11 12 , Y = 11 12 rows and columns it has. By default, R fills in the matrix by columns. x21 x22 y21 y22 " # " # >x<-matrix(c(1,3,2,4),ncol=2,nrow=2) To figure out what XY is, let’s think about breaking down X into >y<-matrix(c(5,7,6,8),ncol=2,nrow=2) row-vectors and Y into column vectors. >x+y • The first row-vector of X is x1 = x11 x12 [,1] [,2] · [1,] 6 8 • The second row-vector of X is x2 h= x21 x22i [2,] 10 12 · h y11 i • The first column vector of Y is y 1 = · y21 " # y12 • The second column vector of Y is y 2 = · y22 " # 19 / 54 20 / 54 Matrix Multiplication II Properties of Matrix Multiplication We can find the product of X and Y by taking inner-products of the • When we see XY,thewecansaythatY is pre-multiplied by X or appropriate row- and column-vectors. the X is post-multiplied by Y. • The order of the multiplication matters here, so XY , YX x1 y 1 x1 y 2 XY = · · · · · · Much like vectors, matrices can be transposed as well. Here, we x2 y 1 x2 y 2 · · · · · · " # interchange the rows and columns, so that xij = x0ji. Sometimes, >x%*%y matrices need to be transposed to make them conformable for [,1] [,2] multiplication or addition. The properties of the transpose are as follows: [1,] 19 22 • (X0)0 = X [2,] 43 50 • (X + Y)0 = Y0 + X0 th th The ij element of XY is the inner product of the i row of X and the • (XY)0 = Y0 X0 jth column of Y.