【问题标题】:trying to use exact=TRUE feature in R glmnet尝试在 R glmnet 中使用exact=TRUE 功能
【发布时间】:2018-04-12 19:50:35
【问题描述】:

我正在尝试在 glmnet 中使用exact=TRUE 功能。但我收到一条错误消息。

> fit = glmnet(as.matrix(((x_values))), (as.matrix(y_values)),penalty=variable.list$penalty)
> coef.exact = coef(fit, s = 0.03, exact = TRUE)
Error: used coef.glmnet() or predict.glmnet() with `exact=TRUE` so must in addition supply original argument(s)  x and y and penalty.factor  in order to safely rerun glmnet

如何向 coef.exact 提供penalty.factor?

尝试的选项:-

> coef.exact = coef(as.matrix(((x_values))), (as.matrix(y_values)),penalty=variable.list$penalty, s = 0.03, exact = TRUE)
Error: $ operator is invalid for atomic vectors
> 
> coef.exact = coef((as.matrix(((x_values))), (as.matrix(y_values)),penalty=variable.list$penalty), s = 0.03, exact = TRUE)
Error: unexpected ',' in "coef.exact = coef((as.matrix(((x_values))),"
> 
> coef.exact = coef((as.matrix(((x_values))) (as.matrix(y_values)) penalty=variable.list$penalty), s = 0.03, exact = TRUE)
Error: unexpected symbol in "coef.exact = coef((as.matrix(((x_values))) (as.matrix(y_values)) penalty"
> 
> coef.exact = coef(fit(as.matrix(((x_values))), (as.matrix(y_values)),penalty=variable.list$penalty), s = 0.03, exact = TRUE)
Error in fit(as.matrix(((x_values))), (as.matrix(y_values)), penalty = variable.list$penalty) : 
  could not find function "fit"
> 
> coef.exact = coef(glmnet(as.matrix(((x_values))), (as.matrix(y_values)),penalty=variable.list$penalty), s = 0.03, exact = TRUE)
Error: used coef.glmnet() or predict.glmnet() with `exact=TRUE` so must in addition supply original argument(s)  x and y and penalty.factor  in order to safely rerun glmnet
> 

【问题讨论】:

    标签: r glmnet


    【解决方案1】:

    这是一个使用mtcars 作为样本数据的示例。请注意,在 SO 上发布时,始终建议提供包含示例数据的 minimal & reproducible code example

    # Fit mpg ~ wt + disp
    x <- as.matrix(mtcars[c("wt", "disp")]);
    y <- mtcars[, "mpg"];
    fit <- glmnet(x, y, penalty = 0.1); 
    
    # s is our regularisation parameter, and since we want exact results
    # for s=0.035, we need to refit the model using the full data (x,y)
    coef.exact <- coef(fit, s = 0.035, exact = TRUE, x = x, y = y, penalty.factor = 0.1);
    coef.exact;
    #3 x 1 sparse Matrix of class "dgCMatrix"
    #                      1
    #(Intercept) 34.40289989
    #wt          -3.00225110
    #disp        -0.02016836
    

    ?coef.glmnet 中给出了您明确需要再次提供xy 的原因(另请参阅@FelipeAlvarenga 帖子)。


    所以在你的情况下,以下应该有效:

    fit = glmnet(x = as.matrix(x_values), y = y_values, penalty=variable.list$penalty)
    coef.exact = coef(
        fit, 
        s = 0.03, 
        exact = TRUE, 
        x = as.matrix(x_values), 
        y = y_values, 
        penalty.factor = variable.list$penalty)
    

    一些cmets

    也许混淆是由于模型的整体正则化参数(s 或 lambda)与您可以应用于每个系数的penalty.factors 之间的差异造成的。后者允许对单个参数进行差分正则化,而s 控制整体 L1/L2 正则化的效果。

    【讨论】:

      【解决方案2】:

      coef 中的参数s 对应于惩罚参数。在帮助文件中:

      s 预测的惩罚参数 lambda 的值 必需的。默认是用于创建模型的整个序列。

      [...]

      exact=TRUE 时,这些不同的 s 值被合并(并排序) 使用 object$lambda,并且在预测之前对模型进行了改装 制作。在这种情况下,需要提供原始数据 x= 和 y= 作为 predict() 或 coef() 的附加命名参数。主力军 predict.glmnet() 需要更新模型,所以需要用到的数据 创建它。权重、偏移量、惩罚因子也是如此, 如果在原始调用中使用了 lower.limits、upper.limits。 否则将导致错误。

      因此,要使用exact = T,您必须指定原始惩罚、x、y 和您在原始模型中输入的任何其他参数

      【讨论】:

      • 那部分我知道了,但是要提供多少惩罚。我尝试了几种方法,但到目前为止似乎都没有奏效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多