wnba <- read.table("http://www.stat.ufl.edu/~winner/data/wnba_ht_wt.dat", header=F,col.names=c("id","height","weight")) attach(wnba) ### Matrix Notes: ### t(A) is the transpose of A ### A %*% B gives matrix multiplication of A and B ### solve(A) gives the inverse of A n <- length(height) Y <- as.matrix(weight) X0 <- rep(1,length(height)) X <- as.matrix(cbind(X0,height)) b <- solve(t(X)%*%X)%*%t(X)%*%Y b J_n <- matrix(rep(1/n,n^2),ncol=n) H <- X %*% solve(t(X)%*%X)%*%t(X) I <- diag(n) (SSR <- t(Y) %*% (H-J_n) %*% Y) (SSE <- t(Y) %*% (I-H) %*% Y) (MSE <- SSE/(n-2)) (s2_b <- MSE[1,1] * solve(t(X)%*%X)) X_72 <- matrix(c(1,72),ncol=1) (yhat_72 <- t(X_72) %*% b) (s2_yhat_72 <- t(X_72) %*% s2_b %*% X_72) (yhat.ci <- yhat_72+qt(c(.025,.975),n-2)*sqrt(s2_yhat_72)) ## 95% CI for Mean@72