library(tree)
## Warning: package 'tree' was built under R version 4.3.1
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
wine_clustering <- read.csv("C:/Users/Mauri/Downloads/wine-clustering.csv")
train <- sample(seq(length(wine_clustering$Alcohol)), length(wine_clustering$Alcohol) * 0.7, replace = FALSE)
wine.tree <- tree(wine_clustering$Alcohol ~ Color_Intensity, wine_clustering, subset = train)
plot(wine.tree)
text(wine.tree, pretty = 0)

wine.tree
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##  1) root 124 78.5100 13.03  
##    2) Color_Intensity < 3.46 37  8.0410 12.18 *
##    3) Color_Intensity > 3.46 87 32.0500 13.40  
##      6) Color_Intensity < 4.65 24  5.6820 13.13 *
##      7) Color_Intensity > 4.65 63 24.0300 13.50  
##       14) Color_Intensity < 7 35 13.1700 13.63  
##         28) Color_Intensity < 6.05 25 11.3800 13.52  
##           56) Color_Intensity < 5.465 14  6.5560 13.73 *
##           57) Color_Intensity > 5.465 11  3.3990 13.25 *
##         29) Color_Intensity > 6.05 10  0.7598 13.90 *
##       15) Color_Intensity > 7 28  9.5410 13.34  
##         30) Color_Intensity < 7.675 9  2.0500 13.05 *
##         31) Color_Intensity > 7.675 19  6.4220 13.47 *
test <- setdiff(seq(length(wine_clustering$Alcohol)), train)
tree.pred <- predict(wine.tree, wine_clustering[test, ])
summary(tree.pred)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   12.18   12.18   13.13   13.00   13.47   13.90
result_table <- with(wine_clustering[test, ], table(tree.pred, Alcohol))
print(result_table)
##                   Alcohol
## tree.pred          11.56 11.61 11.62 11.64 11.66 11.76 11.79 11.81 12 12.04
##   12.1789189189189     0     1     1     1     0     0     1     1  1     1
##   13.0511111111111     0     0     0     0     0     0     0     0  0     0
##   13.1295833333333     0     0     0     0     1     1     0     0  0     0
##   13.2481818181818     1     0     0     0     0     0     0     0  0     0
##   13.4694736842105     0     0     0     0     0     0     0     0  0     0
##   13.7285714285714     0     0     0     0     0     0     0     0  0     0
##   13.898               0     0     0     0     0     0     0     0  0     0
##                   Alcohol
## tree.pred          12.08 12.25 12.29 12.37 12.42 12.43 12.45 12.47 12.51 12.52
##   12.1789189189189     1     1     2     0     2     0     0     1     0     1
##   13.0511111111111     0     0     0     0     0     0     1     0     0     0
##   13.1295833333333     0     0     0     1     0     1     0     0     0     0
##   13.2481818181818     0     0     0     0     0     0     0     0     0     0
##   13.4694736842105     0     0     0     0     0     0     0     0     0     0
##   13.7285714285714     0     0     0     1     0     0     0     0     1     0
##   13.898               0     0     0     0     0     0     0     0     0     0
##                   Alcohol
## tree.pred          12.69 12.7 12.77 12.84 12.85 12.88 12.93 13.07 13.27 13.3
##   12.1789189189189     1    0     1     0     0     0     0     0     0    0
##   13.0511111111111     0    0     0     0     0     0     0     0     0    0
##   13.1295833333333     0    0     0     0     1     0     1     1     0    1
##   13.2481818181818     0    0     0     0     0     0     0     0     0    0
##   13.4694736842105     0    0     0     0     0     0     0     0     1    0
##   13.7285714285714     0    1     0     1     0     1     0     0     0    0
##   13.898               0    0     0     0     0     0     0     0     0    0
##                   Alcohol
## tree.pred          13.32 13.39 13.45 13.49 13.62 13.64 13.69 13.72 13.73 13.76
##   12.1789189189189     0     0     0     0     0     0     0     0     0     0
##   13.0511111111111     0     0     0     0     0     0     0     0     0     0
##   13.1295833333333     0     0     0     0     1     0     0     0     0     0
##   13.2481818181818     0     0     0     1     0     0     1     0     1     0
##   13.4694736842105     1     0     1     0     0     0     0     0     0     0
##   13.7285714285714     0     1     0     0     0     1     0     0     0     1
##   13.898               0     0     0     0     0     0     0     1     0     0
##                   Alcohol
## tree.pred          13.77 13.82 13.84 13.86 13.94 14.06 14.12 14.21 14.23 14.38
##   12.1789189189189     0     0     0     1     0     0     0     0     0     0
##   13.0511111111111     0     1     0     0     0     0     0     0     0     1
##   13.1295833333333     0     0     0     0     0     0     0     0     0     0
##   13.2481818181818     0     0     0     0     0     1     0     0     1     0
##   13.4694736842105     0     0     1     0     1     0     0     0     0     0
##   13.7285714285714     0     0     0     0     0     0     1     1     0     0
##   13.898               1     0     0     0     0     0     0     0     0     0
##                   Alcohol
## tree.pred          14.39
##   12.1789189189189     0
##   13.0511111111111     0
##   13.1295833333333     0
##   13.2481818181818     0
##   13.4694736842105     0
##   13.7285714285714     1
##   13.898               0