-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.Performance comparison of Regression Model, Regression Tree, Bagging Tree, Random Forest and Gradient Boosted Model.Rmd
160 lines (116 loc) · 3.48 KB
/
8.Performance comparison of Regression Model, Regression Tree, Bagging Tree, Random Forest and Gradient Boosted Model.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
output:
word_document: default
html_document: default
---
```{r}
data <- palmerpenguins::penguins
anyNA(data)
dim(data)
data <- na.omit(data)
anyNA(data)
dim(data)
```
DATA STRUCTURE
```{r}
head(data)
str(data)
summary(data)
```
SPLITTING DATA
```{r}
#Splitting Data
library(caret)
set.seed(2380)
index <- createDataPartition(data$body_mass_g, p = 0.75,
list = FALSE, times = 1)
train <- data[index,]
test <- data[-index,]
nrow(train)
nrow(test)
```
LINEAR REGRESSİON MODEL
```{r}
#Traing the Model
model_LM <- lm(body_mass_g~. ,data=train)
#Performance for Train
predicted_train_LM <- predict(model_LM, train)
rmse_train_LM <-sqrt(mean((predicted_train_LM - train$body_mass_g) ^ 2))
#Performance for Test
predicted_test_LM <- predict(model_LM, test)
rmse_test_LM <- sqrt(mean((predicted_test_LM - test$body_mass_g) ^ 2))
cat("test_rmse:", rmse_test_LM,"\n")
cat("train_rmse:", rmse_train_LM)
```
REGRESSION TREE
```{r}
#Training the Model
library(rpart)
library(rpart.plot)
model_DT <- rpart(body_mass_g ~. , method = "anova", data = train)
#Performance for test
predicted_test_DT <- predict(model_DT, test)
rmse_test_DT <- sqrt(mean((predicted_test_DT - test$body_mass_g) ^ 2))
#Performance for train
predicted_train_DT <- predict(model_DT, train)
rmse_train_DT <- sqrt(mean((predicted_train_DT - train$body_mass_g) ^ 2))
cat("test_rmse:", rmse_test_DT,"\n")
cat("train_rmse:", rmse_train_DT)
```
BEGGING TREE
```{r}
#Training the Model
set.seed(2380)
library(randomForest)
model_BT <- randomForest(body_mass_g~. , data=train, mtry= 7)
plot(model_BT,col="red")
which.min(model_BT$mse)
#Performance for test
predicted_test_BT <- predict(model_BT, test)
rmse_test_BT <- sqrt(mean((predicted_test_BT - test$body_mass_g) ^ 2))
#Performance for train
predicted_train_BT <- predict(model_BT, train)
rmse_train_BT <- sqrt(mean((predicted_train_BT - train$body_mass_g) ^ 2))
cat("test_rmse:", rmse_test_BT,"\n")
cat("train_rmse:", rmse_train_BT)
```
RANDOM FOREST
```{r}
#Training Model
set.seed(2380)
model_RF <- randomForest(body_mass_g ~ ., data = train)
plot(model_RF,col = "blue")
which.min(model_RF$mse)
#Performance for test
predicted_test_RF <- predict(model_RF, test)
rmse_test_RF <- sqrt(mean((predicted_test_RF - test$body_mass_g) ^ 2))
#Performance for train
predicted_train_RF <- predict(model_RF, train)
rmse_train_RF <- sqrt(mean((predicted_train_RF - train$body_mass_g) ^ 2))
cat("test_rmse:", rmse_test_RF,"\n")
cat("train_rmse:", rmse_train_RF)
```
GRADIENT BOOSTED MODEL
```{r}
library(gbm)
#Training Model
set.seed(2380)
model_GBM <- gbm(body_mass_g ~ ., data = train, distribution = "gaussian", n.trees = 100, cv.folds = 5)
gbm.perf(model_GBM)
#Performance for test
predicted_test_GBM <- predict(model_GBM, test)
rmse_test_GBM <- sqrt(mean((predicted_test_GBM - test$body_mass_g) ^ 2))
#Performance for train
predicted_train_GBM <- predict(model_GBM, train)
rmse_train_GBM <- sqrt(mean((predicted_train_GBM - train$body_mass_g) ^ 2))
cat("test_rmse:", rmse_test_GBM,"\n")
cat("train_rmse:", rmse_train_GBM)
```
```{r}
#Comparing models performances
data.frame(
"TEST" = c(rmse_test_BT,rmse_test_DT,rmse_test_LM,rmse_test_RF,rmse_test_GBM),
"TRAIN" = c(rmse_train_BT,rmse_train_DT,rmse_train_LM,rmse_train_RF,rmse_train_GBM),
row.names = c("BEGGING TREE", "DECISION TREE","LINEAR MODEL","RANDOM FOREST","GRADIENT BOOSTED")
)
```