21.1 Decision Trees Setup

20180603 Packages used in this chapter include C50 (Kuhn and Quinlan 2020), RWeka (Hornik 2020), party (Hothorn et al. 2021), partykit (Hothorn and Zeileis 2021), rpart (Therneau and Atkinson 2019), rpart.plot (Milborrow 2020), and rattle (G. Williams 2021).

Packages are loaded into the currently running R session from your local library directories on disk. Missing packages can be installed using utils::install.packages() within R. On Ubuntu, for example, R packages can also be installed using $ wajig install r-cran-<pkgname>.

# Load required packages from local library into the R session.

library(C50)          # Original C5.0 implementation.
library(RWeka)        # Weka decision tree J48.
library(party)        # Conditional decision trees ctree().
library(partykit)     # Convert rpart object to BinaryTree
library(rattle)       # GUI for building trees and fancy tree plot.
library(rpart)        # Popular decision tree algorithm.
library(rpart.plot)   # Enhanced tree plots.

The rattle::weatherAUS dataset is loaded into the template variable ds and further template variables are setup as introduced by Graham J. Williams (2017). See Chapter 8 for details.

dsname <- "weatherAUS"
ds     <- get(dsname)
    
nobs   <- nrow(ds)

vnames <- names(ds)
ds    %<>% clean_names(numerals="right")
names(vnames) <- names(ds)

vars   <- names(ds)
target <- "rain_tomorrow"
vars   <- c(target, vars) %>% unique() %>% rev()

It is always useful to remind ourselves of the dataset with a random sample:

ds  %>% sample_frac() %>% select(date, location, sample(3:length(vars), 5))
## # A tibble: 191,431 x 7
##    date       location     cloud_3pm wind_gust_dir cloud_9am min_temp rainfall
##    <date>     <chr>            <int> <ord>             <int>    <dbl>    <dbl>
##  1 2009-05-04 Albany               0 <NA>                  1     11.7        0
##  2 2012-06-05 AliceSprings         1 SSE                   3      2.9        0
##  3 2019-10-18 PerthAirport         8 ESE                   2      9.3        0
##  4 2014-09-03 Darwin               7 E                     6     18.5        0
##  5 2019-02-26 Witchcliffe         NA SSE                  NA     16.5        0
##  6 2020-03-23 Portland             8 SE                    8     10.9        0
##  7 2009-10-29 Melbourne            5 SSW                   4     11.1        0
##  8 2008-07-23 Canberra             5 ESE                   1     -3.5        0
##  9 2009-07-16 NorahHead           NA SSW                  NA      8.4        0
## 10 2012-04-18 Mildura              1 ESE                   0      8.5        0
## # … with 191,421 more rows


Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.