library(car) save_figs = TRUE # Get the data: # N = 6 X = matrix( data=c( 1, 999, 60, 150, 851, 80, 92, 910, 110, 903, 100, 130, 450, 554, 150, 75, 930, 175 ), nrow=N, ncol=3, byrow=TRUE ) DF = data.frame(X) Y = 0:(N-1) DF$Y = Y # Drop X3: # DF$X3 = NULL m = 2 C = cor(DF) XTX = C[1:m, 1:m] XTy = C[, m+1] print(XTX) m_full = lm( Y ~ ., data=DF ) print(summary(m_full)) print('Variance inflation factors:') print(vif(m_full)) ES = eigen(XTX) print('Eigenvalues') print(ES$values) # # Compute the principal components (and a scatter plot of them): # DFs = scale(DF) Z = as.matrix(DFs[, 1:m]) %*% ES$vectors colnames(Z) = c('Z1', 'Z2') print(summary(Z)) if( save_figs ){ postscript("../../WriteUp/Graphics/Chapter5/ex_5_14_scatter_plot_of_Z.eps", onefile=FALSE, horizontal=FALSE) } pairs(Z, xlim=c(-3, +3), ylim=c(-3, +3)) if( save_figs ){ dev.off() } # PC regression: # source('../../../Hastie/Code/Chapter3/pcr_wwx.R') #source('http://www.waxworksmath.com/Authors/G_M/Hastie/Code/Chapter3/pcr_wwx.R') res = pcr_wwx(DF, DF) print(sprintf('Linear model with just one principle component:')) R = regression_coefficients_given_pcr(DF$Y, res, M=1) print(R[1, ]) print(sprintf('Linear model with just all principle components (duplicates LS results):')) R = regression_coefficients_given_pcr(DF$Y, res, M=m) print(R[1, ])