Guest User

Untitled

a guest
Aug 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. library(rpart);
  2.  
  3. library(rpart.plot)
  4.  
  5. data(ptitanic);
  6.  
  7. attr(ptitanic$age,"class") <- NULL;
  8.  
  9. class(ptitanic$age);
  10.  
  11. #This is a function which draws the tree
  12.  
  13. arbre=function(data){
  14.  
  15. set.seed(415)
  16.  
  17. ptitanicTree = rpart(survived~., data = data,
  18. control = rpart.control(minsplit = 5, cp = 0),
  19. method = "class")
  20.  
  21.  
  22. ptitanicOptimal = prune(ptitanicTree,
  23. cp = ptitanicTree$cptable[which.min(ptitanicTree$cptable[,4]),1] )
  24.  
  25.  
  26. prp(ptitanicOptimal,extra=1)
  27. }
  28.  
  29. data1=ptitanic[,c(1,2,3,4,5,6)];
  30.  
  31. arbre(data1)
  32.  
  33. # I change the position switching column 5 and 6
  34. data2=ptitanic[,c(1,2,3,4,6,5)];
  35.  
  36. # Here i get an another tree : why ?
  37. arbre(data2)
Add Comment
Please, Sign In to add comment