




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





data=read.table("Sta135jiming/Wichern_data/T1-5.dat") names(data)=c("z1","z2","x3","x4","y1","y2","x7") pairs(data[,c( 5 , 6 , 1 , 2 )]) y1=data[, 5 ] Y=as.matrix(data[, 5 : 6 ]) Z=cbind( 1 ,data[, 1 ],data[, 2 ]) #(a) B=solve(t(Z)%%Z)%%t(Z)%%y B Yhat=Z%%B residuals=y1-Yhat par(mfrow=c( 1 , 2 )) plot(Yhat,residuals) abline(h= 0 ) qqnorm(residuals) qqline(residuals) par(mfrow=c( 1 , 1 ))
z0=c( 1 , 10 , 80 ) n=nrow(Z) r=ncol(Z)- 1 n-r- 1 s2=t(y1-Yhat)%%(y1-Yhat)/(n-r- 1 ) c(t(z0)%%B - qt( 1 - 0.05/ 2 ,n-r- 1 )sqrt(s2( 1 +t(z0)%%solve(t(Z)%%Z)%%z0)), t(z0)%%B + qt( 1 - 0.05/ 2 ,n-r- 1 )sqrt(s2( 1 +t(z0)%%solve(t(Z)%%Z)%%z0))) #(b) B=solve(t(Z)%%Z)%%t(Z)%%Y B Yhat=Z%%B residuals=Y-Yhat par(mfrow=c( 2 , 2 )) plot(Yhat[, 1 ],residuals[, 1 ],xlab="Yhat",ylab="Residulas", main="Residual vs Predicted values (of Response 1)") abline(h= 0 ) plot(Yhat[, 2 ],residuals[, 2 ],xlab="Yhat",ylab="Residulas", main="Residual vs Predicted values (of Response 2)") abline(h= 0 ) qqnorm(residuals[, 1 ], main="Q-Q plot of residuals (of Response 1)") qqline(residuals[, 1 ]) qqnorm(residuals[, 2 ], main="Q-Q plot of residuals (of Response 2)") qqline(residuals[, 2 ]) par(mfrow=c( 1 , 1 )) z0=c( 1 , 10 , 80 ) n=nrow(Z) r=ncol(Z)- 1 m= 2 n-r- 1 S=t(Y-Yhat)%%(Y-Yhat)/(n-r- 1 ) predMean=t(z0)%%B predMean solve(S) c2=( 1 +t(z0)%%solve(t(Z)%%Z)%%z0)m(n-r- 1 )/(n-r-m)qf(0.95,m,n-r-m) c eig=eigen(solve(S)) V1=eig$vec[, 1 ] V2=eig$vec[, 2 ] e1=sqrt( 1 /eig$val[ 1 ]c2) e2=sqrt( 1 /eig$val[ 2 ]c2) angle=( 0 : 100 ) 2 pi/ 100 mu1=e1V1[ 1 ]cos(angle) + e2V2[ 1 ]sin(angle) + predMean[ 1 ] mu2=e1V1[ 2 ]cos(angle) + e2V2[ 2 ]sin(angle) + predMean[ 2 ] plot(mu1,mu2,type="l",col= 4 , xlab="Y_01", ylab="Y_02", main="95% prediction ellipse for the response") points(predMean,pch= 19 ) c(t(z0)%%B - qt( 1 - 0.05/ 2 ,n-r- 1 )sqrt(s2( 1 +t(z0)%%solve(t(Z)%%Z)%%z0)), t(z0)%%B + qt( 1 - 0.05/ 2 ,n-r- 1 )sqrt(s2( 1 +t(z0)%%solve(t(Z)%%Z)%*%z0)))