我正在尝试在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如何将penalty.factor提供给coef.exact?
已尝试的方案:-
> 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
> 发布于 2018-04-12 21:58:35
下面是一个使用mtcars作为示例数据的示例。注意,在发布时最好提供包含示例数据的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您明确需要再次提供x和y的原因是在?coef.glmnet中给出的(也请参阅@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)一些评论
可能是由于模型的总体正则性参数(s或lambda)与可以应用于每个系数的penalty.factors之间的差异造成的。后者允许对单个参数进行差分调节,而s则控制整个L1/L2正则化的效果。
发布于 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和在原始模型中输入的任何其他参数。
https://stackoverflow.com/questions/49804793
复制相似问题