From 46b8130bd2a46bcb5292ea258331cf7e7c0f8e67 Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Wed, 11 Feb 2026 16:20:00 +0100 Subject: [PATCH] Add Random Forest analysis notebook for Machine Learning 2 --- M2/Machine Learning 2/Random_forest.Rmd | 79 +++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 M2/Machine Learning 2/Random_forest.Rmd diff --git a/M2/Machine Learning 2/Random_forest.Rmd b/M2/Machine Learning 2/Random_forest.Rmd new file mode 100644 index 0000000..9d7764c --- /dev/null +++ b/M2/Machine Learning 2/Random_forest.Rmd @@ -0,0 +1,79 @@ +```{r} +library(randomForest) +library(MASS) +library(tree) +data(Boston) +``` + +## Sampling + +```{r} +set.seed(123) +appr = sample(1:nrow(Boston), nrow(Boston) / 2) +``` + +## Regression Tree + +```{r} +arbre.boston = tree(medv ~ ., Boston, subset = appr) +summary(arbre.boston) +plot(arbre.boston) +text(arbre.boston, pretty = 0) +``` + + +```{r} +cv.boston = cv.tree(arbre.boston) +plot(cv.boston$size, cv.boston$dev, type = 'b') +``` + + +```{r} +yprev = predict(arbre.boston, newdata = Boston[-appr, ]) +boston.test = Boston[-appr, "medv"] +plot(yprev, boston.test) +abline(0, 1) +mean((yprev - boston.test)^2) +``` + +## Bagging + +```{r} +set.seed(123) + +bag.boston = randomForest( + medv ~ ., + data = Boston, + subset = appr, + mtry = 13, + importance = TRUE +) +bag.boston +yprev.bag = predict(bag.boston, newdata = Boston[-appr, ]) +plot(yprev.bag, boston.test) +abline(0, 1) +mean((yprev.bag - boston.test)^2) +``` + +## Random Forest + +```{r} +set.seed(123) +rf.boston = randomForest( + medv ~ ., + data = Boston, + subset = appr, + mtry = 6, + importance = TRUE +) +yprev.rf = predict(rf.boston, newdata = Boston[-appr, ]) +mean((yprev.rf - boston.test)^2) +``` + +## Variable importance + + +```{r} +importance(rf.boston) +varImpPlot(rf.boston) +``` \ No newline at end of file