library(rattle)
## Rattle: A free graphical interface for data science with R.
## Version 5.2.0 Copyright (c) 2006-2018 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(rpart.plot)
## Loading required package: rpart
library(rpart)
library(RColorBrewer)
library(MASS)
head(birthwt)
## low age lwt race smoke ptl ht ui ftv bwt
## 85 0 19 182 2 0 0 0 1 0 2523
## 86 0 33 155 3 0 0 0 0 3 2551
## 87 0 20 105 1 1 0 0 0 1 2557
## 88 0 21 108 1 1 0 0 1 2 2594
## 89 0 18 107 1 1 0 0 1 0 2600
## 91 0 21 124 3 0 0 0 0 0 2622
hist(birthwt$bwt)
- This table reads that 130 infants weigh more than 2.5kg and 59 weigh less than 2.5kg.
table(birthwt$low)
##
## 0 1
## 130 59
cols <- c('low', 'race', 'smoke', 'ht', 'ui')
birthwt[cols] <- lapply(birthwt[cols], as.factor)
set.seed(1)
train <- sample(1:nrow(birthwt), 0.75 * nrow(birthwt))
Build the model using rpart() from the rpart package
Since ‘low’ equals the bodyweight that is less than 2.5kg, it is excluded from the model exclude, and since it is a classification task, we specify method
birthwtTree <- rpart(low ~ . - bwt, data = birthwt[train, ], method = 'class')
plot(birthwtTree)
text(birthwtTree, pretty = 0)
fancyRpartPlot(birthwtTree)
birthwtPred <- predict(birthwtTree, birthwt[-train, ], type = 'class')
table(birthwtPred, birthwt[-train, ]$low)
##
## birthwtPred 0 1
## 0 31 10
## 1 2 5