1,rpart没有自带剪枝功能,需要你根据准则进行判断,这里是SE准则和cp准则。
2,使用prune进行剪枝,命令:
library(DMwR)
library(rpart)
data(algae)
algae <- algae[-manyNAs(algae), ]
rt.a1 <- rpart(a1 ~ .,data=algae[,1:12])
rt.a1
rt2.a1 <- prune(rt.a1,cp=0.0278)
rt2.a1
(Prune a tree-based model using the SE rule,se is
the value of the SE threshold (defaulting to 1))
根据cp准则选取xerror最小值
或者
set.seed(1234)
rt.a1 <- rpartXse(a1 ~ .,data=algae[,1:12])
rt.a1
根据se准则选取xerror小于xerror+xstd(help(prune))
3,cp is the Complexity parameter to which the rpart object will be trimmed)
printcp(rt.a1)