-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomForest.R
358 lines (299 loc) · 12.6 KB
/
RandomForest.R
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# random forest
# import the libraries
library(randomForest) # for making the model
library(caret) # for predict function
# to plot graphs
library('ggplot2')
library('cowplot')
#------------------------- extraversion --------------------
dqset.seed(101)
rf_extraversion1 <- randomForest(extraversion_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
proximity = TRUE,
importance =TRUE)
# print the result
print(rf_extraversion1)
# predict on default model
test$e_rf_default <- predict(rf_extraversion1, test)
confusionMatrix(test$e_rf_default, test$extraversion_score)
# error rate of defualt model
plot(rf_extraversion1, main = "Random Forest Extroversion Default model")
# using tuneRf function find best mtry
t <- tuneRF(train[,c(4:23, 32)], train[,33],
stepFactor = 0.4,
plot=TRUE,
ntreeTry = 500,
trace = TRUE,
improve = 0.05)
# got mtry = 5 as most optimal and ntress = 400
# train model with these parameters
rf_extraversion <- randomForest(extraversion_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
ntree = 500,
mtry = 5,
proximity = TRUE,
importance =TRUE)
print(rf_extraversion)
# predict on tuned model
test$e_rf <- predict(rf_extraversion, test)
confusionMatrix(test$e_rf, test$extraversion_score)
# Number of nodes for the trees
hist(treesize(rf_extraversion),
main = "Number of nodes for trees - extraversion RF",
col = "blue")
# to check importance of the variable
varImpPlot(rf_extraversion,
sort = T,
main = "Variable improtance for Extroversion - RF")
varImpPlot(rf_extraversion,
n.var = 8,
sort = T,
main = "Top 5 variable improtance for Extroversion - RF")
importance(rf_extraversion)
varUsed(rf_extraversion)
# partial dependence plot
partialPlot(rf_extraversion, test, AU02, "High",
main = "AU02 dependency on Extroversion - RF" )
partialPlot(rf_extraversion, test, AU17, "High",
main = "AU17 dependency on Extroversion - RF" )
partialPlot(rf_extraversion, test, AU01, "Low",
main = "AU01 dependency on Low class Extroversion - RF" )
#---------------------------------------------------------
#------------------------- openness --------------------
dqset.seed(101)
rf_openness1 <- randomForest(openness_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
proximity = TRUE,
importance =TRUE)
# print the result
print(rf_openness1)
# predict on default model
test$o_rf_default <- predict(rf_openness1, test)
confusionMatrix(test$o_rf_default, test$openness_score)
# error rate of defualt model
plot(rf_openness1, main = "Random Forest Openness Default model")
# trees = 500
# using tuneRf function find best mtry
t <- tuneRF(train[,c(4:23, 32)], train[,33],
stepFactor = 0.2,
plot=TRUE,
ntreeTry = 500,
trace = TRUE,
improve = 0.05)
# got mtry = 8 as most optimal and ntress = 500
# train model with these parameters
rf_openness <- randomForest(openness_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
ntree = 500,
mtry = 5,
proximity = TRUE,
importance =TRUE)
print(rf_openness)
# predict on tuned model
test$o_rf <- predict(rf_openness, test)
confusionMatrix(test$o_rf, test$openness_score)
# Number of nodes for the trees
hist(treesize(rf_openness1),
main = "Number of nodes for trees - Openness RF",
col = "blue")
# to check importance of the variable
varImpPlot(rf_openness,
sort = T,
main = "Variable improtance for Openness - RF")
varImpPlot(rf_openness,
n.var = 5,
sort = T,
main = "Top 5 variable improtance for Openness - RF")
importance(rf_openness)
varUsed(rf_openness)
# partial dependence plot
partialPlot(rf_openness, train, AU01, "High",
main = "AU01 dependency on Openness - RF" )
partialPlot(rf_openness, train, AU28, "High",
main = "AU28 dependency on Openness - RF" )
partialPlot(rf_openness, train, gender, "High",
main = "Gender dependency on Openness - RF" )
#---------------------------------------------------------
#------------------------- conscientious --------------------
dqset.seed(101)
rf_conscientiousness1 <- randomForest(conscientious_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
proximity = TRUE,
importance =TRUE)
# print the result
print(rf_conscientiousness1)
# predict on default model
test$c_rf_default <- predict(rf_conscientiousness1, test)
confusionMatrix(test$c_rf_default, test$conscientious_score)
# error rate of defualt model
plot(rf_conscientiousness1, main = "Random Forest Conscientiousness Default model")
# using tuneRf function find best mtry
t <- tuneRF(train[,c(4:23,32)], train[,33],
plot=TRUE,
ntreeTry = 500,
trace = TRUE,
stepFactor=0.8,
improve=0.05)
# got mtry = 5 as most optimal and ntress = 500
# train model with these parameters
rf_conscientiousness <- randomForest(conscientious_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
ntree = 500,
mtry = 5,
proximity = TRUE,
importance =TRUE)
print(rf_conscientiousness)
# predict on tuned model
test$c_rf <- predict(rf_conscientiousness, test)
confusionMatrix(test$c_rf, test$conscientious_score)
# defualt model mty = 4 and trees = 500 gives the best results
# Number of nodes for the trees
hist(treesize(rf_conscientiousness1),
main = "Number of nodes for trees - conscientiousness RF",
col = "blue")
# to check importance of the variable
varImpPlot(rf_conscientiousness1,
sort = T,
main = "Variable improtance for conscientiousness - RF")
varImpPlot(rf_conscientiousness1,
n.var = 4,
sort = T,
main = "Top 4 variable improtance for conscientiousness - RF")
importance(rf_conscientiousness1)
varUsed(rf_conscientiousness1)
# partial dependence plot
partialPlot(rf_conscientiousness, train, AU02, "High",
main = "AU02 dependency on conscientiousness - RF" )
partialPlot(rf_conscientiousness1, train, AU01, "High",
main = "AU01 dependency on conscientiousness - RF" )
partialPlot(rf_conscientiousness, train, AU28, "High",
main = "AU02 dependency on conscientiousness - RF" )
#---------------------------------------------------------
#------------------------- agreeableness --------------------
dqset.seed(101)
rf_agreeableness1 <- randomForest(agreeableness_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
proximity = TRUE,
importance =TRUE)
# print the result
print(rf_agreeableness1)
# predict on default model
test$a_rf_default <- predict(rf_agreeableness1, test)
confusionMatrix(test$a_rf_default, test$agreeableness_score)
# error rate of defualt model
plot(rf_agreeableness1, main = "Random Forest Agreeableness Default model")
# using tuneRf function find best mtry
t <- tuneRF(train[,c(4:23, 32)], train[,33],
stepFactor = 0.4,
plot=TRUE,
ntreeTry = 500,
trace = TRUE,
improve = 0.05)
# got mtry = 5 as most optimal and ntress = 500
# train model with these parameters
rf_agreeableness <- randomForest(agreeableness_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
ntree = 500,
mtry = 5,
proximity = TRUE,
importance =TRUE)
print(rf_agreeableness)
# predict on tuned model
test$a_rf <- predict(rf_agreeableness, test)
confusionMatrix(test$a_rf, test$agreeableness_score)
# Number of nodes for the trees
hist(treesize(rf_agreeableness),
main = "Number of nodes for trees - Agreeableness RF",
col = "blue")
# to check importance of the variable
varImpPlot(rf_agreeableness,
sort = T,
main = "Variable improtance for Agreeableness - RF")
varImpPlot(rf_agreeableness,
n.var = 5,
sort = T,
main = "Top 5 variable improtance for Agreeableness - RF")
importance(rf_agreeableness)
varUsed(rf_agreeableness)
# partial dependence plot
partialPlot(rf_agreeableness, train, AU28, "High",
main = "AU28 dependency on Agreeableness - RF" )
partialPlot(rf_agreeableness, train, AU01, "High",
main = "AU01 dependency on Agreeableness - RF" )
partialPlot(rf_agreeableness, train, AU28, "High",
main = "AU02 dependency on Agreeableness - RF" )
#---------------------------------------------------------
#------------------------- Neuroticism --------------------
dqset.seed(101)
rf_neuroticism_ <- randomForest(neurotcism_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
ntree = 800,
proximity = TRUE,
importance =TRUE)
# print the result
print(rf_neuroticism_)
# predict on default model
test$n_rf_default <- predict(rf_neuroticism1, test)
confusionMatrix(test$n_rf_default, test$neurotcism_score)
# error rate of default model
plot(rf_neuroticism1, main = "Random Forest Neuroticism Default model")
# using tuneRf function find best mtry
t <- tuneRF(train[,c(4:23, 32)], train[,33],
stepFactor = 0.8,
plot=TRUE,
ntreeTry = 800,
trace = TRUE,
improve = 0.05)
# got mtry = 5 as most optimal and ntress = 500
# train model with these parameters
rf_neuroticism3 <- randomForest(neurotcism_score~AU01+AU02+AU05+
AU06+AU07+AU09+AU10+AU11+AU12+AU14+AU15+AU17+
AU20+AU23+AU24+AU25+AU26+AU28+AU43+gender,
data = train,
ntree = 1000,
mtry = 5,
proximity = TRUE,
importance =TRUE)
print(rf_neuroticism3)
# predict on tuned model
test$n_rf <- predict(rf_neuroticism_, test)
confusionMatrix(test$n_rf, test$neurotcism_score)
# Number of nodes for the trees
hist(treesize(rf_neuroticism1),
main = "Number of nodes for trees - Neuroticism RF",
col = "blue")
# to check importance of the variable
varImpPlot(rf_neuroticism1,
sort = T,
main = "Variable improtance for Neuroticism - RF")
varImpPlot(rf_neuroticism1,
n.var = 7,
sort = T,
main = "Top 7 variable improtance for Neuroticism - RF")
importance(rf_neuroticism1)
varUsed(rf_neuroticism)
# partial dependence plot
partialPlot(rf_neuroticism1, train, AU28, "High",
main = "AU28 dependency on Neuroticism - RF" )
partialPlot(rf_neuroticism1, train, AU14, "High",
main = "AU14 dependency on Neuroticism - RF" )
partialPlot(rf_neuroticism1, train, gender, "High",
main = "Gender dependency on Neuroticism - RF" )