'rpart'函数如何交叉验证(xstd的计算)?

我试图用RStudio理解和解释rpart :: rpart的一些值。我正在研究“ spambase”(二进制分类器)。

我对交叉验证和“ xstd”的特殊性有疑问。

我制作了返回如下信息矩阵的代码:

         xerror    xstd       cp
nsplit.0 0.5183012 0.02345902 0.48651079
nsplit.1 0.3893216 0.02933012 0.14298561

我想解释默认树“ arbre0”的输出cptable的结果。

我想模拟“ cptable”,我的意思是对于每个cp值,我都会对cp大小的树进行交叉验证。

1:我使用“ bloc”(K = 10)进行交叉验证
2:我创建了一个rpart树
3:我使用函数predict()计算错误错过类
4:xerror =每个K = i块的均值(错误)(看起来不错) 5:xstd = sd(错误向量)/ sqrt(长度(错误)-1)

我没有真正找到xstd的值,我也不知道rpart如何计算它。

我的交叉验证为具有可变大小和最坏情况(另一个主要分割)的子样本树生成。

互联网上没有人解释用于计算xstd的简单公式...

我的代码:

arbre0 = rpart(DATASET.train$spam~.,data=DATASET.train)
arbre0$cptable

#   CP         nsplit rel error xerror    xstd
# 1 0.49551020      0 1.0000000 1.0000000 0.02214215
# 2 0.13795918      1 0.5044898 0.5257143 0.01841309
# 3 0.05142857      2 0.3665306 0.3877551 0.01635578
# 4 0.03265306      3 0.3151020 0.3216327 0.01512706
# 5 0.03102041      4 0.2824490 0.3069388 0.01482715
# 6 0.01000000      5 0.2514286 0.2718367 0.01406463

#init 
n=dim(DATASET.train)[1]
K=10 # nombres de blocs de la c.v
size=n%/%K  # taille des blocs en nombre entier
Ind=rep(1:K,size)
set.seed(50)
block=sample(Ind,length(Ind),rep=F)
block=as.factor(block) # permet de nommer facilement les blocs

Y.pos = table(DATASET.train$spam)["1"]/length(DATASET.train$spam)
res=c()
#summary(block)
for(j in 1:(length(arbre0$cptable[,1])-1) ) #la ligne du dernier cp n'est pas executée
{

 # Début de la c.v
 err=c();cp.x=matrix(nrow = 10,byrow = T) # vecteur des erreurs

 for(i in 1:K) { 
  #pour obtenir le cp de chaque split
  set.seed(50)
  treecp= rpart(DATASET.train[block!=i,]$spam~ .,data=DATASET.train[block!=i,],method="class") 

   #if(length(treecp$cptable[,1])==6){
    # apprentissage sur le complémentaire du bloc 
    set.seed(50)
    tree= rpart(DATASET.train[block!=i,method="class",control=rpart.control(cp=treecp$cptable[j,1] -0.001   )) #-0.001 pour ne pas avoir le root 

    pred= predict(tree,newdata=DATASET.train[block==i,type="class") # prediction sur le bloc

    mc<-table(DATASET.train$spam[block==i],pred)
    err[i]=((mc[2,1]+mc[1,2])/sum(mc))
    #cp.x = cbind(cp.x,treecp$cptable[,1])
   #}


 }
 err=err  /Y.pos #toujours en % par rapport au % des mal classés du root,d'où la division par Y.pos
 xerror = mean(err,na.rm = T)  # xerror 
 xstd = sd(err,na.rm = T) / sqrt((length(is.na(err)==F)-1))   # xstd  
 print(err)#;print(cp.x)
 res=rbind(res,xerror,xstd,treecp$cptable[j,1])

}
res=matrix(res,ncol=3,byrow=T)
data.frame(xerror=res[,1],xstd=res[,2],cp=res[,3],row.names = paste0("nsplit.",treecp$cptable[1:j,2]))

第一个问题,我想输出一个cp,但是我有可变大小的树用于子样本...(不是最重要的)
xerror看起来不错
xstd与我的树“ arbre0”

的输出不相似

如果有人已经计算了rpart树的xstd(分类情况),我需要一些帮助。

谢谢。

PS:

我已经看过一些文档:
https://cran.r-project.org/web/packages/rpart/rpart.pdf
https://cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
https://github.com/cran/rpart/blob/master/src/xval.c

windy1986 回答:'rpart'函数如何交叉验证(xstd的计算)?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3167105.html

大家都在问