-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.R
More file actions
69 lines (51 loc) · 2.23 KB
/
Copy pathdemo.R
File metadata and controls
69 lines (51 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
########################################################################
# Introduce the concept of decision tree model through MLHub
#
# Copyright 2018 Graham.Williams@togaware.com
library(mlhub)
mlcat("Predict Iris Plant Species",
"Below we predict the species of a plant using a pre-built model.
")
#-----------------------------------------------------------------------
# Load required packages from local library into the R session.
#-----------------------------------------------------------------------
suppressMessages(
{
library(rpart) # Model: decision tree rpart().
library(magrittr) # Data pipelines: %>% %<>% %T>% equals().
library(dplyr) # Wrangling: tbl_df(), group_by(), print().
library(rattle) # Support: normVarNames(), riskchart(), errorMatrix().
})
#-----------------------------------------------------------------------
# Load the pre-built model.
#-----------------------------------------------------------------------
load("iris_rpart_caret_model.RData")
model <- m$finalModel
set.seed(1427)
# Load a sample dataset, predict, and display a sample of predictions.
read.csv("data.csv") %T>%
assign('ds', ., envir=.GlobalEnv) %>%
predict(model, newdata=., type="class") %>%
as.data.frame() %>%
cbind(Actual=ds$Species) %>%
set_names(c("Predicted", "Actual")) %>%
select(Actual, Predicted) %>%
mutate(Error=ifelse(Predicted==Actual, "", "<----")) %T>%
{sample_n(., 15) %>% print()} ->
ev
#-----------------------------------------------------------------------
# Produce confusion matrix using Rattle.
#-----------------------------------------------------------------------
mlask()
mlcat("Confusion Matrix",
"A confusion matrix summarises the performance of the model on this
dataset. The figures here are percentages, aggregating the actual versus
predicted outcomes. The Error column represents the class error.
")
per <- errorMatrix(ev$Actual, ev$Predicted) %T>% print()
# Calculate the overall error percentage.
cat(sprintf("\nOverall error: %.0f%%\n", 100-sum(diag(per), na.rm=TRUE)))
# Calculate the averaged class error percentage.
cat(sprintf("Average class error: %.0f%%\n",
mean(per[,"Error"], na.rm=TRUE)))
# No risk chart as we have a multiclass outcome.