決定木2

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

library(DT)
library(rpart)
library(rpart.plot)

datatable(d,option = 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))

xs <- c(1, 2, 1, 2)
ys <- c(1, 1, 2, 2)

d0 <- data.frame(x = c(xs, xs,   xs+5, xs+5),
                 y = c(ys, ys+5, ys+3, ys+9),
                 z = c(rep(1, 4), rep(2, 4), rep(3, 4), rep(4, 4)))

matplot(x = d0$x, y = d0$y, pch = 1, cex = 2, col = COL[1])
text(d0$x, d0$y, d0$z)

tree <- rpart(z ~ x + y, data = d0, minsplit = 2, minbucket = 1)
rpart.plot(tree)

matplot(x = d0$x, y = d0$y, pch = 1, cex = 2, col = COL[1])
segments(4, 0, 4,12, lty = 3, col = COL[2])
segments(4, 8, 8, 8, lty = 3, col = COL[2])
segments(0, 4, 4, 4, lty = 3, col = COL[2])
text(d0$x, d0$y, d0$z)

plotcp(tree)

tree <- rpart(z ~ x + y, data = d0, minsplit = 2, minbucket = 1)
rpart.plot(tree)

tree <- rpart(レンタル数 ~ 季節 + 月 + 祝日 + 曜日 + 休日 + 天気 + 気温 + 湿度 + 風速, data = d)

rpart.plot(tree, type = 5)

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

気温が12度より高く,湿度が69%以下,10月以降