Splus handout: Manipulating matrices Some useful Splus commands: +matrix multiplication in Splus: %*% +matrix inversion (and more): solve() +transpose: t() Recall the boiling point data from Splus handout #3 #X=boiling point (degrees F) #Y=log(barometric pressure) (inches of mercury) #17 locations in the Alps and in Scotland #_________________________________________________________________________ > bp<-read.table("/home/fac/d/jah/DATA/alr/alr.3",header=T) > bp$pressure<-bp$pressure*100 > > X<-cbind(rep(1,17),bp$boil) > Y<-cbind(bp$pressure) > n<-length(Y) #_________________________________________________________________________ #Compute beta.hat > XtX<-t(X)%*%X > beta.hat<- solve(XtX)%*%t(X)%*%Y > beta.hat [,1] [1,] -42.1308708 [2,] 0.8954625 > #compare to coefficients from lm: > bp.lm<-lm(pressure~boil,bp) > coef(bp.lm) (Intercept) boil -42.13087 0.8954625 #_________________________________________________________________________ #Use hat matrix > H<-X%*%solve(XtX)%*%t(X) > y.hat<-H%*%Y > y.hat [,1] [1,] 132.0366 [2,] 131.8575 [3,] 135.0812 [4,] 135.5289 [5,] 136.4244 [6,] 136.8721 [7,] 137.7676 [8,] 137.9466 [9,] 138.2153 [10,] 138.1257 [11,] 140.1853 [12,] 141.0808 [13,] 145.4685 [14,] 144.6626 [15,] 146.5431 [16,] 147.6176 [17,] 147.8863 > #compare to the predicted values from Splus command: >predict(bp.lm) > 1 2 3 4 5 6 7 8 132.0366 131.8575 135.0812 135.5289 136.4244 136.8721 137.7676 137.9466 9 10 11 12 13 14 15 16 138.2153 138.1257 140.1853 141.0808 145.4685 144.6626 146.5431 147.6176 17 147.8863 #_________________________________________________________________________ #Compute SSTO and SSE > SSTO<-t(Y)%*%Y-1/n*t(Y)%*%J%*%Y > SSTO [,1] [1,] 427.7628 > > > SSE<-t(Y)%*%Y-t(beta.hat)%*%t(X)%*%Y > SSE [,1] [1,] 2.153319 > > > #compare to ANOVA output > anova(bp.lm) Analysis of Variance Table Response: pressure Terms added sequentially (first to last) Df Sum of Sq Mean Sq F Value Pr(F) boil 1 425.6095 425.6095 2964.792 0 Residuals 15 2.1533 0.1436