rstatisticscross-validationglmnetmse

Finding training MSE from cv.glmnet R package


I've been trying to find the MSE for the training dataset when using the cv.glmnet function from the glmnet package in R.

Any help would be appreciated.

Thanks

The best I've come up with is below but I'm not sure if it's correct

set.seed(34064064)
library(ISLR2)
Library(glmnet)

x<- matrix.model(Salary~.,data=Hitters)[,-1]
y<- Hitters$Salary

cv<-cv.glmnet(x,y,lambda=exp(seq(-2, 4, length.out = 30)),k=10,alpha=1,standardize = TRUE,type.measure = "mse")

best.lambda <- cv$lambda.min

fit <- glmnet(x, y, lambda=best.lambda, alpha=0, standardize=TRUE)

y.pred <- predict(fit, newx=x)

training.mse <- mean((y - y.pred)^2)

print(training.mse)

Solution

  • You can use the glmnet::assess.glmnet function, which provides summary performance measures for the glmnet model.

    library(ISLR2)
    library(glmnet)
    
    Hitters <- na.omit(Hitters)
    x <- model.matrix(Salary~., data=Hitters)[,-1]
    y <- Hitters$Salary
    
    cv <- cv.glmnet(x,y, lambda=exp(seq(-2, 4, length.out = 30)), k=10, 
                    alpha=1, standardize = TRUE, type.measure = "mse")
    
    best.lambda <- cv$lambda.min
    fit <- glmnet(x, y, lambda=best.lambda, alpha=1, standardize=TRUE)
    
    # The method based on "predict"
    y.pred <- predict(fit, newx=x)
    training.mse <- mean((y - y.pred)^2)
    print(training.mse)
    
    [1] 94310.44
    
    # The method based on "assess.glmnet"    
    perf <- assess.glmnet(fit, newx=x, newy=y)
    print(perf$mse)
    
          s0 
    94310.44
    attr(,"measure")
    [1] "Mean-Squared Error"