Questions tagged [rpart]

An R package for fitting classification and regression trees.

rpart is an package for fitting and trees ().

Repositories

Vignettes

Other resources

Related tags

445 questions
3
votes
2 answers

How can I get the depth of the tree in RPART model?

Suppose I build a toy tree model with RPART, how can I get the depth of the tree? library(rpart) library(partykit) fit=rpart(factor(am)~.,mtcars,control=rpart.control(cp=0,minsplit = 1)) plot(as.party(fit)) I know how to count the leaves, for…
hxd1011
  • 885
  • 2
  • 11
  • 23
3
votes
1 answer

rpart formula parameter usage: "NAs are not allowed in subscripted assignments"

in simple linear regression, having a data frame, we can use that to write the formula easier, for example: lm(my_dep_var ~ .-var1, data=my_df) will return the model with the vars except var1 as indipendent variables in our model. However, when…
d_e
  • 435
  • 1
  • 6
  • 15
3
votes
2 answers

Selecting CP value for decision tree pruning using rpart

I understand that the common practice to select CP value is by choosing the lowest level with the minimum xerror value. However, in my following case, using cp <- fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"] will give me 0.17647059 which will…
Ivan
  • 163
  • 1
  • 3
  • 15
3
votes
1 answer

How to climb the tree structure of rpart object using path in order to purge manually some nodes?

I am using rpart for building a classification tree. I want to develop my own pruning function based on some criteria on the leaf nodes. For instance, if one leaf node is not good regardind to some criteria (stability of parameters' estimation in…
Lydie
  • 117
  • 1
  • 10
3
votes
1 answer

Root node error in classification tree model

I'm struggling with understanding output of tree classification in rpart. I don't understand how 'root node error' is calculated(one of the output of printcp function). I couldn't find it definition also in rpart package description. On example I…
michalk
  • 1,487
  • 3
  • 16
  • 21
3
votes
2 answers

format split labels in rpart.plot

I am plotting a tree with rpart.plot::prp(), much like: library("rpart.plot") data("ptitanic") data <- ptitanic data$sibsp <- as.integer(data$sibsp) # just to show that these are integers data$age <- as.integer(data$age) # just to show that these…
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
3
votes
3 answers

Print dates without scientific notation in rpart classification tree

When I create an rpart tree that uses a date cutoff at a node, the print methods I use - both rpart.plot and fancyRpartPlot - print the dates in scientific notation, which makes it hard to interpret the result. Here's the fancyRpartPlot: Is there…
Sam Firke
  • 21,571
  • 9
  • 87
  • 105
3
votes
2 answers

Saving decision tree's output into a text file

I'm looking for a method to save decision tree's output in R. Here is a simple decision tree code in R: library(rpart) data(kyphosis) fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis) and here is the value of fit: 1) root 81 17 absent…
MTT
  • 5,113
  • 7
  • 35
  • 61
3
votes
1 answer

Using ordinal variables in rpart and caret without converting to dummy categorical variables

I am trying to create an ordinal regression tree in R using rpart, with the predictors mostly being ordinal data, stored as factor in R. When I created the tree using rpart, I get something like this: where the values are the factor values (E.g.…
Ricky
  • 4,616
  • 6
  • 42
  • 72
3
votes
1 answer

R ctree strange error

I have some strange problem in for loops with ctree data. If I write this code in a loop then R freezes. data = read.csv("train.csv") #data description https://www.kaggle.com/c/titanic-gettingStarted/data treet = ctree(Survived ~ ., data =…
2xP
  • 47
  • 1
  • 9
3
votes
1 answer

Minbucket and weights in rpart

A couple questions for the rpart and party experts. 1) I am trying to understand the difference of the control parameter "minbucket" in rpart and party. Is it correct that minbucket in rpart is unweighted (even if weights are provided to fit the…
AriesV
  • 31
  • 4
3
votes
1 answer

How to plot levels rather than labels / indices for factor variables rpart using text?

Plotting a tree split on a factor variable results in a plot where the node is labeled with an index of the factor rather than the text of the level. How do I plot a tree where the label is the actual level of the factor variable? For example, here…
goldisfine
  • 4,742
  • 11
  • 59
  • 83
3
votes
1 answer

How to traverse the tree structure of rpart object in R? I need to get all the nodes associated with a subtree, how can I do it?

I am using rpart for building a decision tree classifier. I wish to use my own pruning function based on certain parameters of the leaf nodes corresponding to a subtree. For this, I think I need to traverse the tree. I explored the documentation of…
3
votes
3 answers

Pass arguments to function using ellipsis and set default for some if not existing

I have a main function which passes arguments to another function which uses rpart to generate model. I would like to have the possibility to specify rpart.control from main function using ellipsis. In case when no cp or minbucket is defined, then I…
Tomas Greif
  • 21,685
  • 23
  • 106
  • 155
3
votes
0 answers

How to manually add a rule to a classification tree in R

I have fitted two classification trees using the rpart and tree packages in R. I would like to manually add a rule to one of the fitted trees, whichever is easiest. Is there a way to do this? Here is an example (taken from the article A Brief Tour…
ramin BBA
  • 39
  • 3