glmnet组预测的k折交叉验证

时间:2017-10-09 13:10:16

标签: r prediction glmnet

我有一组部分相关的预测变量,我想将它们缩减为功能集,并使用简化模型进行预测。我可以使用

找到一个好的lambda
> require(glmnet); require(glmnetUtils)
> cvfit <- cv.glmnet(
+         SpeakerGroup ~ Age +transient_mean +syllablerate+syllablerate_sd+intensitysfraction_mean + NucleusPercentVoiced_mean +NucleusPercentVoiced_sd +OnsetPercentVoiced_mean + OnsetPercentVoiced_sd +Shim + Jitt +intensityslope+rateslope + APQ3 +APQ5+DDP_A+RAP +PPQ5 +DDP, nfolds = 20
+         ,family="binomial",data=curr.df,type.measure = "class")
> 
> plot(cvfit)

(见图片:https://umu.box.com/s/9rt60v3btfo8qhz870vludv6whxlgfx0 imgur对我不起作用。)

> cbind(coef.cv.glmnet(cvfit, s = "lambda.1se"),coef.cv.glmnet(cvfit, s = "lambda.min"))
20 x 2 sparse Matrix of class "dgCMatrix"
                                  1           1
(Intercept)               -1.229948 -0.84290372
Age                        .         .         
transient_mean             .         .         
syllablerate               .        -0.31636610
syllablerate_sd            .         .         
intensitysfraction_mean    .         .         
NucleusPercentVoiced_mean  .         .         
NucleusPercentVoiced_sd    .         .         
OnsetPercentVoiced_mean    .         0.01119326
OnsetPercentVoiced_sd      .         .         
Shim                       .         .         
Jitt                       .         8.09912574
intensityslope             .        -1.68472631
rateslope                  .         .         
APQ3                       .         .         
APQ5                       .         .         
DDP_A                      .         .         
RAP                        .         .         
PPQ5                       .         .         
DDP                        .         .         
> 

好的,这个模型给我一组预测因子,我想在预测组成员资格(两个可能的组)方面评估准确性,特异性等。

> require(resamplr) # https://github.com/jrnold/resamplr
> # compute 5 folds that have the same balance between groups as the original data set
> curr.df %>% group_by(SpeakerGroup) %>% crossv_kfold(k=5,stratify=TRUE) -> folds
> 

我可以计算模型

> folds <- folds %>% mutate(model = map(train, ~ glmnet(
+         SpeakerGroup ~ Age + transient_mean +syllablerate+syllablerate_sd+intensitysfraction_mean +NucleusPercentVoiced_sd +OnsetPercentVoiced_mean + OnsetPercentVoiced_sd  + Jitt +intensityslope + APQ3 +DDP_A,data=.,family="binomial")))

(您可以在此处找到生成的folds对象https://umu.box.com/s/ktxbba4ptzf3hke8g5ze6qgvt0rv42fp

现在,我想基于每个模型和5倍程序设置的测试数据进行预测。

> 
> predicted <- folds %>% mutate(predicted =map2(model, test, ~ predict(.x, data = .y,type="response",s=cvfit$lambda.min)))

我的错误:

Error in mutate_impl(.data, dots) : 
  Evaluation error: argument "data" is missing, with no default.

我对此感到困惑,因为我提供了一个data参数。

有什么可能出错的想法吗?

是否有更简单的方法来获得GLMNET模型的5倍交叉验证的标准2x2混淆矩阵?

谢谢!

的Fredrik

2 个答案:

答案 0 :(得分:0)

正如上面的评论所指出的,cv.glmnet程序已经是交叉验证。问题是我没有办法从模型中提取拟合值。

以供参考,

cvfit <- cv.glmnet(
        <description of model>... ,type.measure = "auc",keep=TRUE)

主要观点是keep=TRUE,它允许您稍后提取参数。

currInd <- match(cvfit$lambda.min,cvfit$glmnet.fit$lambda)
# There is also a 'cvfit$lambda.1se' to have a look at 
  cutoff <- 0.5
  predicted <- cut(as.numeric(cvfit$fit.preval[,currInd]),c(-1000,cutoff,1000),labels=<your labels> )

使用您的截止值为您提供预测矢量,然后您可以将其与实际值进行比较。

我确实希望有一种标准化的方法可以在不用手工提取参数的情况下实现这一目标,但确实如此。至少这是有效的。

答案 1 :(得分:0)

回答您的问题&#34;是否有更简单的方法来获得GLMNET模型的5倍交叉验证的标准2x2混淆矩阵?&#34;,您可以通过{相当简单地完成此操作{1}}包装如下图所示。该代码还根据要求输出特异性,准确性等。

免责声明:代码已经从&#34; A short introduction to the caret package&#34;

修改
caret
相关问题