I was able to reproduce table 3.1 from ESL. However, when I tried to reproduce table 3.2, my estimated coefficients were way off (shown below):
[,1] [1,] 0.4292 [2,] 0.5765 [3,] 0.6140 [4,] -0.0190 [5,] 0.1448 [6,] 0.7372 [7,] -0.2063 [8,] -0.0295 [9,] 0.0095
The results from table 3.2 in ESL are as follow:
I have attached my code here:
res <- read.table("prostate.data", sep = "") fix(res) XTraining = subset(res, train) XTesting = subset(res, train == FALSE) nrow = dim( XTraining ) p = dim( XTraining ) - 1 # the last column is the response D = XTraining[,1:p-1] # get the predictor data # This gives the raw data for Table 3.1 from the book: # print(cor(D),digts=3) library(xtable) xtable( cor(D), caption="Duplication of the values from Table 3.1 from the book", digits=3 ) # # Duplicate Table 3.2 from the book # # Append a column of ones: # Dp = cbind( matrix(1,nrow,1), as.matrix( D ) ) lpsa = XTraining[,p] library(MASS) betaHat = ginv( t(Dp) %*% Dp ) %*% t(Dp) %*% as.matrix(lpsa) # this is basically the first column in Table 3.2: # print('first column: beta estimates') print(betaHat,digits=2)
Does anyone have similar issues ? I would like to know what went wrong with my output.