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

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.01)
rpart.plot(tree, branch.type = 5)

rpart.rules(tree, branch.type = 5, cover = T)
## Warning: rpart.rules: ignoring argument 'branch.type'
##  レンタル数                                                                                             cover
##        1663 when 気温 <   4.1                            & 季節 is  春 or   冬                             9%
##        2261 when 気温 is  4.1 to 12.3 & 湿度 >=       68 & 季節 is  春 or   冬                             6%
##        2723 when 気温 is  4.1 to  7.8 & 湿度 <  68       & 季節 is  春 or   冬                             7%
##        3382 when 気温 >=         12.3 & 湿度 >=       85                                                   4%
##        3941 when 気温 is  7.8 to 12.3 & 湿度 <  68       & 季節 is  春 or   冬                             4%
##        4176 when 気温 <  12.3                            & 季節 is          秋                            14%
##        4626 when 気温 >=         12.3 & 湿度 is 69 to 85                                 & 風速 >= 12      8%
##        4966 when 気温 is 12.3 to 17.1 & 湿度 <  69                             & 月 <  9                   6%
##        5644 when 気温 >=         12.3 & 湿度 is 69 to 85                                 & 風速 <  12     10%
##        5885 when 気温 >=         17.1 & 湿度 <  69                             & 月 <  9                  24%
##        6615 when 気温 >=         12.3 & 湿度 <  69                             & 月 >= 9                   7%
printcp(tree)
## 
## Regression tree:
## rpart(formula = レンタル数 ~ 季節 + 月 + 祝日 + 曜日 + 
##     休日 + 天気 + 気温 + 湿度 + 風速, data = d, method = "anova", 
##     cp = 0.01)
## 
## Variables actually used in tree construction:
## [1] 気温 季節 月   湿度 風速
## 
## Root node error: 2739535392/731 = 3747654
## 
## n= 731 
## 
##          CP nsplit rel error  xerror     xstd
## 1  0.390509      0   1.00000 1.00429 0.040458
## 2  0.070302      1   0.60949 0.61224 0.026949
## 3  0.052880      2   0.53919 0.55043 0.026563
## 4  0.022313      3   0.48631 0.51124 0.022903
## 5  0.015319      4   0.46400 0.49762 0.021906
## 6  0.012892      5   0.44868 0.50131 0.021577
## 7  0.012226      6   0.43579 0.49564 0.021419
## 8  0.010671      7   0.42356 0.49192 0.021177
## 9  0.010087      8   0.41289 0.48564 0.021319
## 10 0.010000     10   0.39271 0.48592 0.021426
plotcp(tree)

tree2 <- prune(tree, cp = 0.01)
rpart.plot(tree2, branch.type = 5)

rpart.rules(tree2)
##  レンタル数                                                                                          
##        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
printcp(tree2)
## 
## Regression tree:
## rpart(formula = レンタル数 ~ 季節 + 月 + 祝日 + 曜日 + 
##     休日 + 天気 + 気温 + 湿度 + 風速, data = d, method = "anova", 
##     cp = 0.01)
## 
## Variables actually used in tree construction:
## [1] 気温 季節 月   湿度 風速
## 
## Root node error: 2739535392/731 = 3747654
## 
## n= 731 
## 
##          CP nsplit rel error  xerror     xstd
## 1  0.390509      0   1.00000 1.00429 0.040458
## 2  0.070302      1   0.60949 0.61224 0.026949
## 3  0.052880      2   0.53919 0.55043 0.026563
## 4  0.022313      3   0.48631 0.51124 0.022903
## 5  0.015319      4   0.46400 0.49762 0.021906
## 6  0.012892      5   0.44868 0.50131 0.021577
## 7  0.012226      6   0.43579 0.49564 0.021419
## 8  0.010671      7   0.42356 0.49192 0.021177
## 9  0.010087      8   0.41289 0.48564 0.021319
## 10 0.010000     10   0.39271 0.48592 0.021426
plotcp(tree2)

###回帰木でレンタル自転車数を推定するルールは、気温が12.3度以上、湿度が69%以下、9月より前ということが分かる。 ###最も平均値の高いルールはレンタル数が増える。

d2 <- read.csv("https://stats.dip.jp/01_ds/data/iris.csv")

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

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

rpart.rules(tree3, 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%
printcp(tree3)
## 
## Classification tree:
## rpart(formula = Species ~ Sepal.Length + Sepal.Width + Petal.Length + 
##     Petal.Width, data = d2, method = "class", cp = 0.005)
## 
## Variables actually used in tree construction:
## [1] Petal.Length Petal.Width 
## 
## Root node error: 100/150 = 0.66667
## 
## n= 150 
## 
##      CP nsplit rel error xerror     xstd
## 1 0.500      0      1.00   1.15 0.051801
## 2 0.440      1      0.50   0.62 0.060310
## 3 0.005      2      0.06   0.10 0.030551
plotcp(tree3)

d.new <- data.frame(Sepal.Length = 4.0,
                    Sepal.Width = 3.0,
                    Petal.Length = 3.0,
                    Petal.Width = 1.0
                    )

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

###分類木を使ってアイリスの種類を判定するルールは、versicolorが90%である。 ###このデータのアイリスはversicolorである。