library(ISLR)
## Warning: package 'ISLR' was built under R version 4.4.2
library(tree)
## Warning: package 'tree' was built under R version 4.4.3
library(rpart)
library(caret)
## Warning: package 'caret' was built under R version 4.4.2
## Loading required package: ggplot2
## Loading required package: lattice
library(randomForest)
## Warning: package 'randomForest' was built under R version 4.4.3
## randomForest 4.7-1.2
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
#PROBLEM 3
p = seq(0, 1, 0.001)
gini.index = 2 * p * (1 - p)
class.error = 1 - pmax(p, 1 - p)
cross.entropy = - (p * log(p) + (1 - p) * log(1 - p))
matplot(p, cbind(gini.index, class.error, cross.entropy), ylab = "gini.index, class.error, cross.entropy", col = c("red", "blue", "green"))

#PROBLEM 8 PART A)
set.seed(1)
train = sample(1:nrow(Carseats), nrow(Carseats)/2)
strain = Carseats[train, ]
stest = Carseats[-train, ]
#PROBLEM 8 PART B)
tree.seats = tree(Sales ~ ., data = strain)
summary(tree.seats)
## 
## Regression tree:
## tree(formula = Sales ~ ., data = strain)
## Variables actually used in tree construction:
## [1] "ShelveLoc"   "Price"       "Age"         "Advertising" "CompPrice"  
## [6] "US"         
## Number of terminal nodes:  18 
## Residual mean deviance:  2.167 = 394.3 / 182 
## Distribution of residuals:
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -3.88200 -0.88200 -0.08712  0.00000  0.89590  4.09900
plot(tree.seats)
text(tree.seats, pretty = 0)

treeseat.pred = predict(tree.seats, newdata = stest)
mean((treeseat.pred - stest$Sales)^2)
## [1] 4.922039
#The MSE obtained from the regression tree is 4.15.
#PROBLEM 8 PART c)
set.seed(1)
cv.seats = cv.tree(tree.seats)
plot(cv.seats$size, cv.seats$dev, type = "b")

prune.car = prune.tree(tree.seats, best = 10)
plot(prune.car)
text(prune.car,pretty=0)

treeseat.pred = predict(prune.car, newdata = stest)
mean((treeseat.pred - stest$Sales)^2)
## [1] 4.918134
#The new MSE is 4.82. This means that pruning the tree does not improve the test MSE because it increased compared to the prior value of 4.15. Even if pruning minimizes cross-validated error, it can still have higher actual test MSE because there might be variance between cross-validation and test set. 
#PROBLEM 8 PART D)
set.seed(1)
bag.seats = randomForest(Sales~., data = strain, mtry = 10, ntree = 551, importance = TRUE)
bagseat.pred = predict(bag.seats, newdata = stest)
mean((bagseat.pred - stest$Sales)^2)
## [1] 2.599099
importance(bag.seats)
##                 %IncMSE IncNodePurity
## CompPrice   26.18616309    170.781666
## Income       5.25063979     90.717958
## Advertising 13.25673204     97.498810
## Population  -2.14346969     58.289311
## Price       60.58241525    503.478806
## ShelveLoc   50.77308639    380.258594
## Age         19.03720001    158.282846
## Education    1.24264920     44.834257
## Urban       -0.08461165      9.883299
## US           4.71515903     17.907727
varImpPlot(bag.seats)

#The tope three variables that seem to be the most important are: Price, ShelveLoc and Age.
#PROBLEM 8 PART E)
set.seed(1)
rando.seats = randomForest(Sales~., data = strain, mtry = 10, importance = TRUE)
randseat.pred = predict(rando.seats, newdata = stest)
mean((randseat.pred - stest$Sales)^2)
## [1] 2.605253
#The test MSE is 2.61. This is higher value than 2.60, meaning that the random forests do not result in improved resultws over bagging. 
importance(rando.seats)
##                %IncMSE IncNodePurity
## CompPrice   24.8888481    170.182937
## Income       4.7121131     91.264880
## Advertising 12.7692401     97.164338
## Population  -1.8074075     58.244596
## Price       56.3326252    502.903407
## ShelveLoc   48.8886689    380.032715
## Age         17.7275460    157.846774
## Education    0.5962186     44.598731
## Urban        0.1728373      9.822082
## US           4.2172102     18.073863
#PROBLEM 9 PART A)
library(ISLR)
set.seed(1)
train = sample(dim(OJ)[1],800)
OJ_train = OJ[train,]
OJ_test = OJ[-train,]
#PROBLEM 9 PART B)
OJ_tree = tree(Purchase~., data=OJ_train)
summary(OJ_tree)
## 
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ_train)
## Variables actually used in tree construction:
## [1] "LoyalCH"       "PriceDiff"     "SpecialCH"     "ListPriceDiff"
## [5] "PctDiscMM"    
## Number of terminal nodes:  9 
## Residual mean deviance:  0.7432 = 587.8 / 791 
## Misclassification error rate: 0.1588 = 127 / 800
#The training error rate is 0.165, and the tree has 8 termminal nodes.
#PROBLEM 9 PART C)
OJ_tree
## node), split, n, deviance, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 800 1073.00 CH ( 0.60625 0.39375 )  
##    2) LoyalCH < 0.5036 365  441.60 MM ( 0.29315 0.70685 )  
##      4) LoyalCH < 0.280875 177  140.50 MM ( 0.13559 0.86441 )  
##        8) LoyalCH < 0.0356415 59   10.14 MM ( 0.01695 0.98305 ) *
##        9) LoyalCH > 0.0356415 118  116.40 MM ( 0.19492 0.80508 ) *
##      5) LoyalCH > 0.280875 188  258.00 MM ( 0.44149 0.55851 )  
##       10) PriceDiff < 0.05 79   84.79 MM ( 0.22785 0.77215 )  
##         20) SpecialCH < 0.5 64   51.98 MM ( 0.14062 0.85938 ) *
##         21) SpecialCH > 0.5 15   20.19 CH ( 0.60000 0.40000 ) *
##       11) PriceDiff > 0.05 109  147.00 CH ( 0.59633 0.40367 ) *
##    3) LoyalCH > 0.5036 435  337.90 CH ( 0.86897 0.13103 )  
##      6) LoyalCH < 0.764572 174  201.00 CH ( 0.73563 0.26437 )  
##       12) ListPriceDiff < 0.235 72   99.81 MM ( 0.50000 0.50000 )  
##         24) PctDiscMM < 0.196196 55   73.14 CH ( 0.61818 0.38182 ) *
##         25) PctDiscMM > 0.196196 17   12.32 MM ( 0.11765 0.88235 ) *
##       13) ListPriceDiff > 0.235 102   65.43 CH ( 0.90196 0.09804 ) *
##      7) LoyalCH > 0.764572 261   91.20 CH ( 0.95785 0.04215 ) *
#The terminal nodes chosen for the interpretation is 9. The split cliteriro is LoyalCH <0.036, and the number of observations in the branch is 109. It has a deviance of 100.90 and overall prediction of MM. There are less than 2% of the observation sin the branch that tkae the value of CH, which leaves that remaining 98% to take the value of MM.
#PROBLEM 9 PART D)
plot(OJ_tree)
text(OJ_tree,pretty=TRUE)

#The most important variable seems to be LoyalCH for "Purchase" as the first branch shows the strength of customer loyalty to CH. 
#PROBLEM 9 PART E)
tree.pred = predict(OJ_tree, newdata = OJ_test, type = "class")
table(tree.pred,OJ_test$Purchase)
##          
## tree.pred  CH  MM
##        CH 160  38
##        MM   8  64
(147+62)/270
## [1] 0.7740741
#Above shows that 77% of test observations are correctly classified. This means that the error rate is abotu 23%.
#PROBLEM 9 PART F)
cv_OJ = cv.tree(OJ_tree, FUN = prune.misclass)
cv_OJ
## $size
## [1] 9 8 7 4 2 1
## 
## $dev
## [1] 150 150 149 158 172 315
## 
## $k
## [1]       -Inf   0.000000   3.000000   4.333333  10.500000 151.000000
## 
## $method
## [1] "misclass"
## 
## attr(,"class")
## [1] "prune"         "tree.sequence"
#PROBLEM 9 PART G)
plot(cv_OJ$size,cv_OJ$dev,type='b', xlab = "Tree size", ylab = "Deviance")

#PROBLEM 9 PART H)
#From the plot above, we can se that the tree with 5 node correspondes to the lowest cross-validated classification error rate on the y-axis.
#PROBLEM 9 PART I)
prune_OJ = prune.misclass(OJ_tree, best=5)
plot(prune_OJ)
text(prune_OJ,pretty=0)

#PROBLEM 9 PART J)
summary(OJ_tree)
## 
## Classification tree:
## tree(formula = Purchase ~ ., data = OJ_train)
## Variables actually used in tree construction:
## [1] "LoyalCH"       "PriceDiff"     "SpecialCH"     "ListPriceDiff"
## [5] "PctDiscMM"    
## Number of terminal nodes:  9 
## Residual mean deviance:  0.7432 = 587.8 / 791 
## Misclassification error rate: 0.1588 = 127 / 800
summary(prune_OJ)
## 
## Classification tree:
## snip.tree(tree = OJ_tree, nodes = c(4L, 10L))
## Variables actually used in tree construction:
## [1] "LoyalCH"       "PriceDiff"     "ListPriceDiff" "PctDiscMM"    
## Number of terminal nodes:  7 
## Residual mean deviance:  0.7748 = 614.4 / 793 
## Misclassification error rate: 0.1625 = 130 / 800
#The trainig error rates between the pruned and unpruned trees seems to be the same. However, pruning helped create a more interpretable tree. 
#PROBLEM 9 PART K)
tree.pred = predict(OJ_tree, newdata = OJ_test, type = "class")
table(tree.pred,OJ_test$Purchase)
##          
## tree.pred  CH  MM
##        CH 160  38
##        MM   8  64
pruneOJ.pred = predict(prune_OJ, newdata = OJ_test, type = "class")
table(pruneOJ.pred, OJ_test$Purchase)
##             
## pruneOJ.pred  CH  MM
##           CH 160  36
##           MM   8  66
(147+62)/270
## [1] 0.7740741
#The test error rates between the pruned and unpruned trees seem to be the same with aproximately 0.77.