rpart没有构建完整的树 - cp的问题?

时间:2014-06-10 19:50:57

标签: r rpart cart-analysis

我正在尝试通过将控件设置为rpart.control(minsplit=2, minbucket = 1,cp=0)来构建完整的树,但它不起作用。我认为原因可能是具有4个拆分的摘要树具有cp = 0,但是该树未满,因此其cp应该是> 0
我还检查了数据,可以进行更多分割。 这是我的代码:

#################
# libraries #####
library(datasets)
library(rpart)
library(rpart.plot)
##################
# preparing data #
titanic_obs=c()
for (cl in c("1st", "2nd", "3rd", "Crew")) {
  for (se in c("Male","Female")) {
    for (ag in c("Child","Adult")) {
      for (sur in c("Yes","No")) {
        titanic_obs = rbind(titanic_obs,matrix(rep(c(cl,se,ag,sur),length.out=4*Titanic[cl,se,ag,sur]),ncol=4,byrow=T))    
      }
    }
  }
}

colnames(titanic_obs)= c("Class", "Sex", "Age","Survived")
titanic_data = data.frame(titanic_obs)
summary(titanic_data) 
#################
# fitting model #
titanic_rpart = rpart(Survived ~ Sex + Age + Class,
                  data = titanic_data,method="class",
                  control=rpart.control(minsplit=2, minbucket = 1,cp=0))
#################
# checking ######
summary(titanic_rpart)
prp(titanic_rpart, extra=1, uniform=F, branch=1, yesno=F, border.col=0, xsep="/")
#################
# data ##########
adult_men = titanic_data[titanic_data$Sex=="Male" & titanic_data$Age=="Adult",]
all_am = table(adult_men$Class)
    survived_am = table(adult_men[adult_men$Survived=="Yes",]$Class)
survived_am/all_am

2 个答案:

答案 0 :(得分:0)

如对此问题的评论所述,设置Manifest.permission.FOREGROUND_SERVICE将构建完整的树。

答案 1 :(得分:-1)

现在无法检查,但我似乎记得设置cp = 0.000001或类似的小 数字在某些时候解决了我的问题。另请注意,minsplit和minbucket等参数可能会阻碍树的增长,因此您可能还需要为这些参数设置适当的值。