gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:56:36 25 4
gpt4 key购买 nike

我在 R 中使用 glment 包进行回归。我使用 cv.fit<-cv.glmnet(x,y,...) 进行交叉验证,并使用 cvfit$lambda.min 获得最佳 lambda。但我还想获得该 lambda 对应的 MSE(均方误差)。
有人可以帮我拿吗?

最佳答案

来自 ?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)’.
# ...

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

要找到最小 MSE,您可以使用 which 如下:
i <- which(cv.fit$lambda == cv.fit$lambda.min)
mse.min <- cv.fit$cvm[i]

或更短
mse.min <- cv.fit$cvm[cv.fit$lambda == cv.fit$lambda.min]

关于r - 如何使用 glmnet 包提取最佳 lambda 的 CV 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24018585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com