k折交叉验证-如何自动获取预测? [英] k-fold cross validation - how to get the prediction automatically?

查看:125
本文介绍了k折交叉验证-如何自动获取预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我只是找不到一个程序包来做到这一点...我知道我可以编写一些代码来获取我想要的东西,但是拥有一个自动执行功能的函数会很好!

This may be a silly question but I just can't find a package to do that...I know I can write some codes to get what I want but it would be nice to have a function to do it automatically!

所以基本上我想对glm模型进行k折交叉验证。我也想自动获得每个验证集的预测以及实际值。因此,如果我要进行10折CV,则我希望函数返回10个验证集以及实际响应和预测。

So basically I want to do a k-fold cross-validation for a glm model. I want to automatically get the predictions of each validation set and the actual value too. So if I am doing a 10-fold CV, I want a function to return the 10 validation sets with the actual responses and predictions all together.

谢谢!

推荐答案

如评论中所述,插入符使交叉验证非常容易。只需使用 glm方法即可,就像这样:

As stated in the comments, caret makes cross-validation very easy. Just use the "glm" method, like so:

> library(caret)
> set.seed(2)
> dat <- data.frame(label=round(runif(100,0,5)),v1=rnorm(100),v2=rnorm(100))
> tc <- trainControl("cv",10,savePred=T)
> (fit <- train(label~.,data=dat,method="glm",trControl=tc,family=poisson(link = "log")))
100 samples
  2 predictors

No pre-processing
Resampling: Cross-Validation (10 fold) 

Summary of sample sizes: 90, 91, 91, 90, 90, 89, ... 

Resampling results

  RMSE  Rsquared  RMSE SD  Rsquared SD
  1.53  0.146     0.131    0.235      


> fit$finalModel$family

Family: poisson 
Link function: log 

> head(fit$pred)
      pred obs rowIndex .parameter Resample
1 2.684367   1        1       none   Fold01
2 2.165246   1       18       none   Fold01
3 2.716165   3       35       none   Fold01
4 2.514789   3       36       none   Fold01
5 2.249137   5       47       none   Fold01
6 2.328514   2       48       none   Fold01

这篇关于k折交叉验证-如何自动获取预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆