gpt4 book ai didi

R:使用cv.glm计算弹性网预测误差

转载 作者:行者123 更新时间:2023-11-30 09:20:29 25 4
gpt4 key购买 nike

library(glmnet)
library(boot)
data(iris)
x <- model.matrix(Sepal.Length~., iris)[,-1]
y <- iris$Sepal.Length
m <- cv.glmnet(x, y)
> cv.glm(x, m, K = 10)
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('matrix', 'double', 'numeric')"

bestLambda = m$lambda.min
m2 <- glmnet(x, y, family = "gaussian", lambda = bestLambda)
>cv.glm(x, m2, K = 10)


Error in glmnet(x = x, y = y, family = "gaussian", lambda = bestLambda, :
unused argument (data = c(3.5, 3, 3.2, 3.1, 3.6,

引用this问题,我正在尝试使用 cv.glm 获取我的弹性网络模型的 K 倍交叉验证预测误差,但是,由于该错误,我似乎无法这样做。我不太确定 cv.glm 函数是否可用于计算类 cv.glmglmnet 对象的预测误差。

最佳答案

我认为您将 glmglmnet (弹性网,带有套索和山脊惩罚)混淆了。 cv.glm 需要 glm 模型,而不是 glmnet 模型。

尝试以下任一方法:

  1. 使用 glmnetcv.glmnet 计算 k 倍交叉验证错误,如下所示:

    library(glmnet)
    library(boot)
    data(iris)
    x <- model.matrix(Sepal.Length~., iris)[,-1]
    y <- iris$Sepal.Length
    m <- cv.glmnet(x, y, nfolds=10)
    m$lambda.min
    #[1] 0.0003839539
    m$lambda.1se
    #[1] 0.009078549
    plot(m$lambda, m$cvm,type='l', xlab=expression(lambda), ylab='CV errors', main=expression(paste('CV error for different ', lambda)))
    lines(m$lambda, m$cvup, col='red')
    lines(m$lambda, m$cvlo, col='red')

    enter image description here

[已编辑]

训练数据集的预测误差:

mean((y-predict(m, newx=x))^2)
# [1] 0.1037433
  • 拟合 glm 模型并使用 cv.glm 计算交叉验证误差增量(无需正则化)。根据cv.glm的文档:
  • delta: A vector of length two. The first component is the raw cross-validation estimate of prediction error. The second component is the adjusted cross-validation estimate. The adjustment is designed to compensate for the bias introduced by not using leave-one-out cross-validation.

    df <- cbind.data.frame(x, y)
    m <- glm(y~., df, family='gaussian')
    cv.glm(df, m, K = 10)$delta
    # [1] 0.09992177 0.09940190

    关于R:使用cv.glm计算弹性网预测误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41583300/

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