#-0 Preliminaries inv = solve #Define inv() as inverse for a matrix W = matrix(c(1,2,3,4),ncol=2) V = matrix(c(5,6,7,8,9,10),ncol=2) W inv(W) V t(V) # transpose of matrix W V %*% W # matrix multiplication of V & W W %*% V W %*% inv(W) # 2x2 identity matrix #-1 Create data in terms of matrix X and vector Y Y = c(5,7,8,9) X1 = c(1,2,2,3) n = length(Y) #-2 Fit a linear model mod = lm(Y ~ X1) mod attributes(mod) #objects available via mod$ #-3 Create the "design matrix" X X0 = c(1,1,1,1) X = cbind(X0,X1) # column bind 2 vectors together X #-4 Create the X'X matrix and its inverse #** FOR 5-13 IDENTIFY WHAT PORTIONS OF THE R OUTPUT FROM BUILT-IN FUNCTIONS #** CORRESPOND TO (OR DIFFER FROM) THE MATRIX COMPUTATION. #-5 Compute Beta.hat by matrix multiplication # Compare to mod$coeff #-6 Compute Y.hat by matrix multiplication # Compare to mod$fitted and predict(mod) #-7 Compute the residuals using matrix (vector) arithmetic (i.e., subtraction) # Compare to mod$resid #-8 Compute the SSE & MSE from (7) using using matrix (vector) arithmetic # Compute this again using the sum() function # Compare to anova(mod) #-9 Compute the correlation between Y and X1 and square it # Compute the correlation between Y and Y.hat and square it # Compare to values from summary(mod) #10 Compute the leverage values by matrix multiplication # Compare to hatvalues(mod) #11 Compute the (internally) standardized residuals using vector arithmetic Compare to rstandard(mod) #12 For #4, compute the X'X matrix and its inverse by hand #13 Use the 3rd standardized residual, r[3], and 3rd hatvalue, h[3], to compute # the 3rd Cook's Distance # Compare to cooks.distance(mod)