# Create the training data frame
train <- data.frame(ClaimID = c(1:7),
RearEnd = c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE),
Fraud = c(TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE),
Whiplash = c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE))
# Display the training data
train
## ClaimID RearEnd Fraud Whiplash
## 1 1 TRUE TRUE TRUE
## 2 2 TRUE TRUE TRUE
## 3 3 FALSE TRUE TRUE
## 4 4 FALSE FALSE TRUE
## 5 5 FALSE FALSE TRUE
## 6 6 FALSE FALSE FALSE
## 7 7 FALSE FALSE FALSE
# Load the rpart package
library(rpart)
## Warning: package 'rpart' was built under R version 4.3.3
library(rattle)
## Warning: package 'rattle' was built under R version 4.3.3
## Loading required package: tibble
## Loading required package: bitops
## Rattle: A free graphical interface for data science with R.
## Version 5.5.1 Copyright (c) 2006-2021 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.3.3
library(RColorBrewer)
# Train the decision tree model
lossmatrix<-matrix(c(0,1,3,0), byrow = TRUE, nrow = 2 )
lossmatrix
## [,1] [,2]
## [1,] 0 1
## [2,] 3 0
mytree <- rpart(Fraud ~RearEnd+Whiplash, data = train, method = "class",minsplit=2,minbucket=1,parms=list(loss=lossmatrix))
# Display the decision tree
mytree
## n= 7
##
## node), split, n, loss, yval, (yprob)
## * denotes terminal node
##
## 1) root 7 4 TRUE (0.5714286 0.4285714)
## 2) Whiplash< 0.5 2 0 FALSE (1.0000000 0.0000000) *
## 3) Whiplash>=0.5 5 2 TRUE (0.4000000 0.6000000) *
fancyRpartPlot(mytree, caption = NULL)
