3

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 (0.79012346 0.20987654)  
   2) Start>=8.5 62  6 absent (0.90322581 0.09677419)  
     4) Start>=14.5 29  0 absent (1.00000000 0.00000000) *
     5) Start< 14.5 33  6 absent (0.81818182 0.18181818)  
      10) Age< 55 12  0 absent (1.00000000 0.00000000) *
      11) Age>=55 21  6 absent (0.71428571 0.28571429)  
        22) Age>=111 14  2 absent (0.85714286 0.14285714) *
        23) Age< 111 7  3 present (0.42857143 0.57142857) *
   3) Start< 8.5 19  8 present (0.42105263 0.57894737) *

I tried save, dump and dput but they do not work and change tree's format. Is there any method to save the tree into a text file keeping its? sink does not work for me.

MTT
  • 5,113
  • 7
  • 35
  • 61
  • How exactly did you try to use `save`? That should have worked. And it worked in the test I just did. `save(fit, "fit.rdata")` and then when you want it again, `load("fit.rdata")` – MrFlick Jun 25 '15 at 23:57
  • Does it work to save the tree into text file? I tried and it looks does not work! – MTT Jun 26 '15 at 00:10
  • No. `save()` does not write to plain text files, it writes to a binary file. Note that what prints out is not the value of `fit`, it's the results of `print(fit)` which makes a "pretty" text version of the object, it is not the object itself. This object has functions which have environments that cannot be preserved in a plain-text format. – MrFlick Jun 26 '15 at 00:13

2 Answers2

2

I used sink and it worked for me.

sink("clipboard")  # works in Windows, substitute "clipboard" for file name
print(fit)
sink()

Pasting from clipboard, I get

n= 81 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 81 17 absent (0.79012346 0.20987654)  
   2) Start>=8.5 62  6 absent (0.90322581 0.09677419)  
     4) Start>=14.5 29  0 absent (1.00000000 0.00000000) *
     5) Start< 14.5 33  6 absent (0.81818182 0.18181818)  
      10) Age< 55 12  0 absent (1.00000000 0.00000000) *
      11) Age>=55 21  6 absent (0.71428571 0.28571429)  
        22) Age>=111 14  2 absent (0.85714286 0.14285714) *
        23) Age< 111 7  3 present (0.42857143 0.57142857) *
   3) Start< 8.5 19  8 present (0.42105263 0.57894737) *

Tested changing "clipboard" to a text file name, and had the same content above.

Am wondering about your comment that sink didn't work for you, what was the problem / output?

Ricky
  • 4,616
  • 6
  • 42
  • 72
1

save() and load() should work if you want to preserve fit for future use. Experiment with something like

is.list(fit)                       # check it is there 
save(fit,file="thisexample.txt", ascii=TRUE)
rm(fit)                            # to remove fit object
is.list(fit)                       # check it is not there
load(file="thisexample.txt")
is.list(fit)                       # check it is there

You should see [1] TRUE then Error: object 'fit' not found then [1] TRUE and you will be good to go on using fit

I may have misunderstood what you are trying to save, in which case use Ricky's answer instead, with a file name

sink("exampletree.txt")
print(fit)
sink()
Henry
  • 6,704
  • 2
  • 23
  • 39
  • Thanks for the answer but as I said I want to export it into a text file not R file. When you save into text file, everything is mixed up together. – MTT Jun 26 '15 at 00:08