GLMNET 错误 -- penalty.factor 的长度与变量的数量不匹配 -- 不确定如何修复此错误

GLMNET error -- the length of penalty.factor does not match the number of variables -- not sure how to fix this error

提问人:watsontr 提问时间:11/17/2023 更新时间:11/17/2023 访问量:17

问:

我对 R 中的 GLMNET 包比较陌生。

当我运行此处找到的“多项式示例”的示例代码时,我收到以下错误消息:

Error in glmnet(x = x_multi, y = y_multi, family = "multinomial", alpha = 1,  : 
  the length of penalty.factor does not match the number of variables

作为参考,下面是代码:


 # Multinomial example 

  data(MultinomialExample)
  x <- MultinomialExample$x
  y <- MultinomialExample$y
  
  x_multi <- x
  y_multi <- y
  ## Perform ridge regression
  ridge2 <- glmnet(x = x_multi, y = y_multi,
                   ## Multinomial regression
                   family = "multinomial",
                   ## ‘alpha = 1’ is the lasso penalty, and ‘alpha = 0’ the ridge penalty.
                   alpha = 0)
  plot(ridge2, xvar = "lambda")
  
  ## Perform ridge regression with 10-fold CV
  ridge2_cv <- cv.glmnet(x = x_multi, y = y_multi,
                         ## type.measure: loss to use for cross-validation.
                         type.measure = "deviance",
                         ## K = 10 is the default.
                         nfold = 10,
                         ## Multinomial regression
                         family = "multinomial",
                         ## ‘alpha = 1’ is the lasso penalty, and ‘alpha = 0’ the ridge penalty.
                         alpha = 0)
  ## Penalty vs CV MSE plot
  plot(ridge2_cv)
  
  ## Extract coefficients at the error-minimizing lambda
  ridge2_cv$lambda.min
  
  ## s: Value(s) of the penalty parameter ‘lambda’ at which
  ##    predictions are required. Default is the entire sequence used
  ##    to create the model.
  best_ridge_coef2 <- do.call(cbind, coef(ridge2_cv, s = ridge2_cv$lambda.min))
  best_ridge_coef2  
  
  ## Transform into a matrix
  ## The intercept estimates should be dropped.
  best_ridge_weights2 <- 1 / abs(as.matrix(best_ridge_coef2)[-1,])
  best_ridge_weights2
  
  ## Perform adaptive LASSO
  alasso2 <- glmnet(x = x_multi, y = y_multi,
                    ## Multinomial regression
                    family = "multinomial",
                    ## ‘alpha = 1’ is the lasso penalty, and ‘alpha = 0’ the ridge penalty.
                    alpha = 1,
                    ##
                    ## penalty.factor: Separate penalty factors can be applied to each
                    ##           coefficient. This is a number that multiplies ‘lambda’ to
                    ##           allow differential shrinkage. Can be 0 for some variables,
                    ##           which implies no shrinkage, and that variable is always
                    ##           included in the model. Default is 1 for all variables (and
                    ##           implicitly infinity for variables listed in ‘exclude’). Note:
                    ##           the penalty factors are internally rescaled to sum to nvars,
                    ##           and the lambda sequence will reflect this change.
                    penalty.factor = best_ridge_weights2,
                    intercept = F,
                    type.multinomial = "grouped")
  plot(alasso2, xvar = "lambda")

我正在尝试弄清楚如何解决此错误。任何帮助将不胜感激。

其他上下文信息:

R 版本 4.2.1 (2022-06-23) 运行环境:macOS 14.0

glmnet_4.1-7

我尝试在glmnet中使用intercept = F参数。它没有用。

R glmnet 套索回归多项

评论


答: 暂无答案