d <- read.csv('https://stats.dip.jp/01_ds/data/bike_rental.csv')

library(DT)
datatable(d,options = list(pageLength = 5))
COL <- c(rgb(255,   0,   0,  105, max = 255), # 赤
         rgb(  0,   0, 255,  105, max = 255), # 青
         rgb(  0, 155,   0,  105, max = 255), # 緑
         rgb(100, 100, 100,   55, max = 255)) # 灰
library(rpart)
library(rpart.plot)
tree <- rpart(レンタル数~ 季節+月+ 祝日+ 曜日+ 休日+天気 + 気温+ 湿度+風速,
              data = d, method = 'anova', cp = 0.02)
rpart.plot(tree, type = 5)

tree <- rpart(レンタル数~ 季節+月+ 祝日+ 曜日+ 休日+天気 + 気温+ 湿度+風速,
              data = d, method = 'anova',cp= 0.01)
rpart.rules(tree, type = 5)
## Warning: rpart.rules: ignoring argument 'type'
##  レンタル数                                                                                          
##        1663 when 気温 <   4.1                            & 季節 is  春 or   冬                       
##        2261 when 気温 is  4.1 to 12.3 & 湿度 >=       68 & 季節 is  春 or   冬                       
##        2723 when 気温 is  4.1 to  7.8 & 湿度 <  68       & 季節 is  春 or   冬                       
##        3382 when 気温 >=         12.3 & 湿度 >=       85                                             
##        3941 when 気温 is  7.8 to 12.3 & 湿度 <  68       & 季節 is  春 or   冬                       
##        4176 when 気温 <  12.3                            & 季節 is          秋                       
##        4626 when 気温 >=         12.3 & 湿度 is 69 to 85                                 & 風速 >= 12
##        4966 when 気温 is 12.3 to 17.1 & 湿度 <  69                             & 月 <  9             
##        5644 when 気温 >=         12.3 & 湿度 is 69 to 85                                 & 風速 <  12
##        5885 when 気温 >=         17.1 & 湿度 <  69                             & 月 <  9             
##        6615 when 気温 >=         12.3 & 湿度 <  69                             & 月 >= 9
rpart.plot(tree, branch.type = 5)

plotcp(tree)

tree2 <- prune(tree, cp = 0.1)

rpart.plot(tree2, branch.type = 5)

tree3 <- prune(tree, cp = 0.01)

rpart.plot(tree3, branch.type = 5)

kableExtra::kable(rpart.rules(tree3,cover = T))
レンタル数 cover
8 1663 when 気温 < 4.1 & 季節 is 春 or 冬 9%
18 2261 when 気温 is 4.1 to 12.3 & 湿度 >= 68 & 季節 is 春 or 冬 6%
38 2723 when 気温 is 4.1 to 7.8 & 湿度 < 68 & 季節 is 春 or 冬 7%
6 3382 when 気温 >= 12.3 & 湿度 >= 85 4%
39 3941 when 気温 is 7.8 to 12.3 & 湿度 < 68 & 季節 is 春 or 冬 4%
5 4176 when 気温 < 12.3 & 季節 is 14%
28 4626 when 気温 >= 12.3 & 湿度 is 69 to 85 & 風速 >= 12 8%
60 4966 when 気温 is 12.3 to 17.1 & 湿度 < 69 & < 9 6%
29 5644 when 気温 >= 12.3 & 湿度 is 69 to 85 & 風速 < 12 10%
61 5885 when 気温 >= 17.1 & 湿度 < 69 & < 9 24%
31 6615 when 気温 >= 12.3 & 湿度 < 69 & >= 9 7%
d2 <- read.csv('https://stats.dip.jp/01_ds/data/iris.csv')

DT::datatable(d2,options = list(pageLength = 5))
library(rpart)
library(rpart.plot)
tree4 <- rpart(Species~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
              data = d2, method = 'class',cp= 0.005)
rpart.plot(tree4, type = 5)

rpart.rules(tree4, cover = T)
##     Species  seto vers virg                                                  cover
##      setosa [1.00  .00  .00] when Petal.Length <  2.5                          33%
##  versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8     36%
##   virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8     31%
rpart.plot(tree4, branch.type = 5)

plotcp(tree4)

tree5 <- prune(tree4, cp = 0.005)

rpart.plot(tree5, branch.type = 5)

kableExtra::kable(rpart.rules(tree5,cover = T))
Species seto vers virg cover
2 setosa [1.00 .00 .00] when Petal.Length < 2.5 33%
6 versicolor [ .00 .91 .09] when Petal.Length >= 2.5 & Petal.Width < 1.8 36%
7 virginica [ .00 .02 .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8 31%
d.new <- data.frame(Sepal.Length = 4.0,
                    Sepal.Width = 3.0,
                    Petal.Length = 3.0,
                    Petal.Width = 1.0
                    )

rpart.predict(tree4, newdata = d.new)
##   setosa versicolor  virginica
## 1      0  0.9074074 0.09259259