I have a problem understanding the behavior of rpart function of R.
Here is the r code part :
library(rpart);
library(rpart.plot)
data(ptitanic);
attr(ptitanic$age,"class") <- NULL;
class(ptitanic$age);
#This is a function which draws the tree
arbre=function(data){
set.seed(415)
ptitanicTree = rpart(survived~., data = data,
control = rpart.control(minsplit = 5, cp = 0),
method = "class")
ptitanicOptimal = prune(ptitanicTree,
cp = ptitanicTree$cptable[which.min(ptitanicTree$cptable[,4]),1] )
prp(ptitanicOptimal,extra=1)
}
data1=ptitanic[,c(1,2,3,4,5,6)];
arbre(data1)
# I change the position switching column 5 and 6
data2=ptitanic[,c(1,2,3,4,6,5)];
# Here i get an another tree : why ?
arbre(data2)
Is anyone able to explain why we get two different trees we have only changed the position of columns in data input ?
ptitanic
data in base R, nor inrpart
package. As about the results, they are nearly the same -- the only difference is that pruning returned one extra level. $\endgroup$Understanding rpart package
is too vague. A better title would be something likeWhy does rpart generate a different tree when the order of the variables in the data is changed?
. $\endgroup$