[R] glmnet: obtain predictions using predict and also by extracting coefficients
Juliet Hannah
juliet.hannah at gmail.com
Wed Mar 21 19:35:36 CET 2012
All,
For my understanding, I wanted to see if I can get glmnet predictions
using both the predict function and also by multiplying coefficients
by the variable matrix. This is not worked out. Could anyone suggest
where I am going wrong?
I understand that I may not have the mean/intercept correct, but the
scaling is also off, which suggests a bigger mistake.
Thanks for your help.
Juliet Hannah
library(ElemStatLearn)
library(glmnet)
data(prostate)
# training data
data.train <- prostate[prostate$train,]
y <- data.train$lpsa
# isolate predictors
data.train <- as.matrix(data.train[,-c(9,10)])
# test data
data.test <- prostate[!prostate$train,]
data.test <- as.matrix(data.test[,-c(9,10)])
# scale test data by using means and sd from training data
trainMeans <- apply(data.train,2,mean)
trainSDs <- apply(data.train,2,sd)
# create standardized test data
data.test.std <- sweep(data.test, 2, trainMeans)
data.test.std <- sweep(data.test.std, 2, trainSDs, "/")
# fit training model
myglmnet =cv.glmnet(data.train,y)
# predictions by using predict function
yhat_enet <- predict(myglmnet,newx=data.test, s="lambda.min")
# attempting to get predictions by using coefficients
beta <- as.vector( t(coef(myglmnet,s="lambda.min")))
testX <- cbind(1,data.test.std)
yhat2 <- testX %*% beta
# does not match
plot(yhat2,yhat_enet)
More information about the R-help
mailing list