gpt4 book ai didi

r - cv.glmnet对于使用编码器错误的模拟数据的岭而不是套索失败

转载 作者:行者123 更新时间:2023-12-04 03:56:51 26 4
gpt4 key购买 nike

要旨
错误: Error in predmat[which, seq(nlami)] = preds : replacement has length zero上下文:数据使用二进制y进行模拟,但是ntrue y编码器。数据被堆叠n次,并拟合了一个模型,试图获取true y
接收到错误

  • L2罚款,但不包括L1罚款。
  • ,当Y是编码器Y时,但不是当它是真正的Y时。
  • 错误不是确定性的,而是取决于种子。

  • 更新:该错误适用于1.9-8之后的版本。 1.9-8不会失败。
    再生产
    基本数据:
    library(glmnet)
    rm(list=ls())
    set.seed(123)

    num_obs=4000
    n_coders=2
    precision=.8

    X <- matrix(rnorm(num_obs*20, sd=1), nrow=num_obs)
    prob1 <- plogis(X %*% c(2, -2, 1, -1, rep(0, 16))) # yes many zeros, ignore
    y_true <- rbinom(num_obs, 1, prob1)
    dat <- data.frame(y_true = y_true, X = X)
    创建编码员
    classify <- function(true_y,precision){
    n=length(true_y)
    y_coder <- numeric(n)
    y_coder[which(true_y==1)] <- rbinom(n=length(which(true_y==1)),
    size=1,prob=precision)
    y_coder[which(true_y==0)] <- rbinom(n=length(which(true_y==0)),
    size=1,prob=(1-precision))
    return(y_coder)
    }
    y_codings <- sapply(rep(precision,n_coders),classify,true_y = dat$y_true)
    全部堆叠
    expanded_data <- do.call(rbind,rep(list(dat),n_coders))
    expanded_data$y_codings <- matrix(y_codings, ncol = 1)
    重现错误
    由于错误取决于种子,因此必须进行循环。只有第一个循环会失败,其他两个会结束。
    X <- as.matrix(expanded_data[,grep("X",names(expanded_data))])

    for (i in 1:1000) cv.glmnet(x = X,y = expanded_data$y_codings,
    family="binomial", alpha=0) # will fail
    for (i in 1:1000) cv.glmnet(x = X,y = expanded_data$y_codings,
    family="binomial", alpha=1) # will not fail
    for (i in 1:1000) cv.glmnet(x = X,y = expanded_data$y_true,
    family="binomial", alpha=0) # will not fail
    是否有任何想法来自glmnet,以及如何避免它?根据我对 cv.glmnet的阅读,这是在cv例程之后,并且在 cvstuff = do.call(fun, list(outlist, lambda, x, y, weights, offset, foldid, type.measure, grouped, keep))内部,我不了解它的作用,因此无法理解失败以及如何避免它。
    session (Ubuntu和PC)
    R version 3.3.1 (2016-06-21)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 16.04.1 LTS

    locale:
    [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
    [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
    [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
    [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

    attached base packages:
    [1] stats graphics grDevices utils datasets methods base

    other attached packages:
    [1] glmnet_2.0-2 foreach_1.4.3 Matrix_1.2-7.1 devtools_1.12.0

    loaded via a namespace (and not attached):
    [1] httr_1.2.1 R6_2.2.0 tools_3.3.1 withr_1.0.2 curl_2.1
    [6] memoise_1.0.0 codetools_0.2-15 grid_3.3.1 iterators_1.0.8 knitr_1.14
    [11] digest_0.6.10 lattice_0.20-34
    R version 3.3.1 (2016-06-21)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 7 x64 (build 7601) Service Pack 1

    locale:
    [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
    [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
    [5] LC_TIME=English_United States.1252

    attached base packages:
    [1] stats graphics grDevices utils datasets methods base

    other attached packages:
    [1] glmnet_2.0-2 foreach_1.4.3 Matrix_1.2-7.1 devtools_1.12.0

    loaded via a namespace (and not attached):
    [1] httr_1.2.1 R6_2.2.0 tools_3.3.1 withr_1.0.2 curl_2.1
    [6] memoise_1.0.0 codetools_0.2-15 grid_3.3.1 iterators_1.0.8 digest_0.6.10
    [11] lattice_0.20-34

    最佳答案

    我在glmnet_2.0-5中有相同的错误
    它与在某些情况下如何自动创建lambda有关。解决方案是提供自己的lambda

    例如:

    cv.glmnet(x = X,
    y = expanded_data$y_codings,
    family="binomial",
    alpha=0,
    lambda=exp(seq(log(0.001), log(5), length.out=100)))

    恭喜 https://github.com/lmweber/glmnet-error-example/blob/master/glmnet_error_example.R

    关于r - cv.glmnet对于使用编码器错误的模拟数据的岭而不是套索失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40145209/

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