【发布时间】:2017-02-17 21:03:56
【问题描述】:
我一直致力于在梯度增强机器学习算法中生成 AUC 的自举置信区间,并获得连续的结果。 以下是需要重复至少 200 次的一次迭代的代码:
- 使用替换对主数据集 (n=1000) 重新采样
- 分位数归一化结果变量
- 在重采样数据集上运行 GBM 并提取 AUC
我知道在 SAS 中我可以使用一个宏来运行此代码 200 次,创建 200 个数据集并提取 200 个我可以合并的 AUC,但我不知道如何在 R 中循环。
#calling required packages
library("WVPlots")
source('functions.R')
require(gbm)
#generating some data
main<-matrix(
replicate(52,rnorm(1132)),
ncol=52,
nrow=1132,
dimnames = list(
1:1132,
1:52)
)
colnames(main)[1] <- "PctControl"
#creating resampled dataset BS[1] (so my aim is to repeat this for BS[1:200]
BS1=matrix(
rep(as.numeric(NA)),
nrow=1000,
ncol= ncol(main),
dimnames= list(1:1000, colnames(main))
)
BS1[1:1000,]<-as.matrix (main[sample(nrow(main),size=1000,replace=TRUE),])
BS1<-BS1[order(BS1[,1]),]
Survival <- as.numeric (BS1[,1])
NormedSurvival<- as.numeric( ppoints(Survival) )
BS1<-cbind (NormedSurvival, BS1)
BS1<-as.data.frame(BS1[,-2])
#fitting the GBM model
scwrpxy.fit.gbm = gbm(NormedSurvival~.,
data=BS1,
n.trees = 50, verbose = T, shrinkage = 0.005,
bag.fraction = 0.25, # subsampling fraction, 0.5 is probably best
train.fraction = 0.3,
interaction.depth = 3, n.minobsinnode = 10, distribution = "gaussian",
cv.folds = 3)
summary(scwrpxy.fit.gbm)
#Calculating AUC
predictedGBM<-as.numeric(predict.gbm (scwrpxy.fit.gbm,
n.trees = 500,
shrinkage = 0.005,
interaction.depth=3,
bag.fraction=0.5,
train.fraction=0.3,
cv.folds = 5,
distribution="gaussian"))
observedGBM<-as.numeric(BS1$NormedSurvival)
TestTox<-as.data.frame(cbind(observedGBM,predictedGBM))
ROCPlot(TestTox,'predictedGBM','observedGBM','gbm model')
gbmAUC = as.numeric(pROC::auc(TestTox$observedGBM,TestTox$predictedGBM))
print(gbmAUC)
【问题讨论】:
标签: r automation resampling statistics-bootstrap