0

I'm trying to get a decision tree from the code:

library(party)
tree <- data.frame(mi[,-2])
levels(tree$OS_Gatunek) <- c("ta", "th", "tl", "to", "tp") # shortening species names
model <- ctree(OS_Gatunek~., data=tree)
plot(model)

My data look like this:

> str(mi)
'data.frame':   4393 obs. of  18 variables:
 $ OS_Gatunek       : Factor w/ 5 levels "Taraxacum ancistrolobum",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ PH_CreateDate    : Factor w/ 15 levels "2016-04-06","2016-04-19",..: 2 2 2 2 2 2 2 2 2 2 ...
 $ L_Dl             : num  7.91 8.96 10.18 10.09 9.4 ...
 $ L_SzerMaksOs     : num  1.93 3.98 3.12 4.04 2.75 2.69 3.69 3.23 2.3 2.49 ...
 $ L_DlMax          : num  3.51 4.08 5.58 5.04 3.99 3.6 5.65 4.62 3.33 4.18 ...
 $ KS_DlSk_Sr       : num  1.78 3.28 2.88 4.19 1.88 2.47 3.11 4.04 1.61 2.09 ...
 $ KS_Dl_Sr         : num  1.68 2.83 2.62 3.84 1.68 2.12 2.87 3.8 1.44 1.86 ...
 $ KS_Sz            : num  1.35 3.41 2.38 3.31 1.66 2.35 2.45 2.96 1.57 1.9 ...
 $ KB_DlSkos_Sr     : num  1.07 1.94 1.84 1.69 1.25 1.49 1.96 1.77 1.43 1.55 ...
 $ KB_Dl_Sr         : num  0.62 1.49 1.12 1.34 0.86 0.77 1.22 1.07 0.82 1.05 ...
 $ KB_Szer_Sr       : num  0.85 1.23 1.46 0.94 0.89 1.32 1.53 1.41 1.17 1.14 ...
 $ KB_SzerPierwKlapy: num  1.75 3.99 2.9 4.1 2.34 2.75 3.11 3.39 1.96 2.46 ...
 $ I_Dl_Sr          : num  0.25 0.86 0.48 0.61 0.44 0.41 0.7 0.86 0.14 0.59 ...
 $ I_SzOs           : num  0.37 0.83 0.47 0.87 0.39 0.73 0.53 0.96 0.4 0.33 ...
 $ I_DlSz_Sr        : num  2.3 4.08 3.35 5.23 2.34 3.39 3.22 4.43 1.96 2.55 ...
 $ O_Dl_Sr          : num  0.67 0.75 2.02 0.85 0.74 1.4 1.07 0.26 0.6 0.96 ...
 $ O_SzerOs         : num  1.35 1.59 1.31 0.91 1.08 0.94 1.18 0.84 1.71 0.93 ...
 $ O_SzerOskrz_Sr   : num  0.55 0.65 0.48 0.34 0.39 0.31 0.49 0.29 0.74 0.27 ...

But the tree looks like this:

My tree

And I would really like it to look like this (taken from: https://ademos.people.uic.edu/Chapter24.html):

The correct tree

What should my code look like? I'll be grateful for all suggestions.

Olga Świder
  • 63
  • 1
  • 11
  • Yes. Thank you for fixing. – G5W Jul 19 '18 at 16:25
  • The code looks ok but for some reason the tree becomes very large (more than 200 nodes). I'm not sure why exactly this happens. Can you share the data so that this is fully reproducible? – Achim Zeileis Jul 20 '18 at 14:27
  • Unfortunately, the boss did not let me share the data :-(. But I changed the code a bit: `model <- ctree (OS_Gatunek ~., Data = tree, controls = ctree_control (mincriterion = 0.95))` (from: [https://stackoverflow.com/questions/18399510/how-to-implement-the-output-of-decision-tree-built-using-the-ctree-party-packag]) and already there are "only" 19 nodes. I will try to combine something in this direction – Olga Świder Jul 22 '18 at 11:33
  • Eventually it succeeded with the parameter `controls = ctree_control(maxdepth=3)`. I saved the plot as `pdf(file="C:/Users/moje/Desktop/mi.pdf", width=15, height=5)` and I succeeded :-). It looks decent. Nevertheless, thank you very much for the comments :-). – Olga Świder Jul 22 '18 at 14:58

0 Answers0