如何使用glmnet包提取最佳lambda的CV错误?

use*_*363 3 r mse glmnet cross-validation

我在R中使用glment包进行回归.我使用了交叉验证cv.fit<-cv.glmnet(x,y,...),并且我使用了最佳lambda cvfit$lambda.min.但我想得到MSE该lambda 的相应(均方误差).有人会帮我搞定吗?

sie*_*ste 8

来自?cv.glmnet:

# ...
# Value:
#
#     an object of class ‘"cv.glmnet"’ is returned, which is a list with
#     the ingredients of the cross-validation fit. 
#
# lambda: the values of ‘lambda’ used in the fits.
#
#   cvm: The mean cross-validated error - a vector of length
#       ‘length(lambda)’.
# ...
Run Code Online (Sandbox Code Playgroud)

因此,在您的情况下,交叉验证的均方误差在,cv.fit$cvm并且相应的λ值在cv.fit$lambda.

要找到最低MSE,您可以使用which如下:

i <- which(cv.fit$lambda == cv.fit$lambda.min)
mse.min <- cv.fit$cvm[i]
Run Code Online (Sandbox Code Playgroud)

或更短

mse.min <- cv.fit$cvm[cv.fit$lambda == cv.fit$lambda.min]
Run Code Online (Sandbox Code Playgroud)