# NOT RUN {
## Use rpart to build a decision tree.
# }
# NOT RUN {
library(rpart)
## Set up the data for modelling.
set.seed(42)
ds <- weather
target <- "RainTomorrow"
risk <- "RISK_MM"
ignore <- c("Date", "Location", risk)
vars <- setdiff(names(ds), ignore)
nobs <- nrow(ds)
form <- formula(paste(target, "~ ."))
train <- sample(nobs, 0.7*nobs)
test <- setdiff(seq_len(nobs), train)
actual <- ds[test, target]
risks <- ds[test, risk]
# Fit the model.
fit <- rpart(form, data=ds[train, vars])
## Plot the model.
fancyRpartPlot(fit)
## Choose different colours.
fancyRpartPlot(fit, palettes=c("Greys", "Oranges"))
## Add a main title to the plot.
fancyRpartPlot(fit, main=target)
# }
Run the code above in your browser using DataLab