【问题标题】:How to extract the CV errors for optimal lambda using glmnet package?如何使用 glmnet 包提取最佳 lambda 的 CV 错误?
【发布时间】:2014-07-23 23:46:31
【问题描述】:

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

【问题讨论】:

    标签: r cross-validation glmnet mse


    【解决方案1】:

    如果您使用损失函数“mse”运行 glmnet,则最小 lambda 表示最小 MSE。 因此,您可以通过以下方式找到它:

    mse.min <- min(cv.fit$cvm)

    【讨论】:

      【解决方案2】:

      来自?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]
      

      【讨论】:

        猜你喜欢
        • 2015-08-14
        • 2021-11-21
        • 1970-01-01
        • 2014-10-05
        • 2017-12-05
        • 2012-08-14
        • 2018-03-22
        • 2020-01-27
        • 2014-10-13
        相关资源
        最近更新 更多