首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试着在R中使用exact=TRUE特性

试着在R中使用exact=TRUE特性
EN

Stack Overflow用户
提问于 2018-04-12 19:50:35
回答 2查看 3.1K关注 0票数 0

我正在尝试在glmnet中使用exact=TRUE特性。但我收到了一条错误信息。

代码语言:javascript
复制
> 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

如何将penalty.factor提供给coef.exact?

已尝试的方案:-

代码语言:javascript
复制
> 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
> 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-12 21:58:35

下面是一个使用mtcars作为示例数据的示例。注意,在发布时最好提供包含示例数据的minimal & reproducible code example

代码语言:javascript
复制
# 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

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

因此,就您的情况而言,以下内容应该有效:

代码语言:javascript
复制
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)

一些评论

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

票数 1
EN

Stack Overflow用户

发布于 2018-04-12 19:54:58

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

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

..。

使用exact=TRUE,s的这些不同值与对象$lambda合并(并排序),并在进行预测之前对模型进行修改。在这种情况下,需要提供原始数据x=和y=作为额外的命名参数来预测()或coef()。工作流predict.glmnet()需要更新模型,因此需要用于创建模型的数据。在原始调用中使用的权重、偏移量、penalty.factor、lower.limits、upper.limits也是如此。如果不这样做,将导致错误。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49804793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档