-
Notifications
You must be signed in to change notification settings - Fork 0
/
model-boosting-random-forest.r
213 lines (175 loc) · 9.16 KB
/
model-boosting-random-forest.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
################################################################################################
#
# MODELAGEM PREDITIVA - MBA Business Analytics e Big Data
# Por: RICARDO REIS
#
# CASE - FRAMINGHAM HEART STUDY
#
# male: 0 = Female; 1 = Male
# age: Age at exam time.
# education: 1 = Some High School; 2 = High School or GED; 3 = Some College or Vocational School; 4 = college
# currentSmoker: 0 = nonsmoker; 1 = smoker
# cigsPerDay: number of cigarettes smoked per day (estimated average)
# BPMeds: 0 = Not on Blood Pressure medications; 1 = Is on Blood Pressure medications
# prevalentStroke: AVC
# prevalentHyp: Hipertensão
# diabetes: 0 = No; 1 = Yes
# totChol: Colesterol total mg/dL
# sysBP: Pressão sistólica mmHg
# diaBP: Pressão diastólica mmHg
# BMI: Body Mass Index calculated as: Weight (kg) / Height(meter-squared)
# heartRate: Beats/Min (Ventricular)
# glucose: Glicemia mg/dL
# TenYearCHD: Prever se o paciente vai ter doenças coronarianas em 10 anos
#
################################################################################################
# LENDO OS DADOS
path <- "C:/Users/Ricardo/Documents/R-Projetos/FraminghamHeartStudy/"
baseBRF <- read.csv(paste(path,"framingham.csv",sep=""),
sep=",",header = T,stringsAsFactors = T)
################################################################################################
# ANALISANDO AS VARiÁVEIS DA BASE DE DADOS
# ANÁLISE UNIVARIADA
summary(baseBRF)
# Checando Missing Values
library("VIM")
matrixplot(baseBRF)
aggr(baseBRF)
#Estratégia Adotada:
#Excluindo linhas com Missing Values
index_glucose <- which(is.na(baseBRF$glucose))
index_heartRate <- which(is.na(baseBRF$heartRate))
index_BMI <- which(is.na(baseBRF$BMI))
index_totChol <- which(is.na(baseBRF$totChol))
index_BPMeds <- which(is.na(baseBRF$BPMeds))
index_cigsPerDay <- which(is.na(baseBRF$cigsPerDay))
index_education <- which(is.na(baseBRF$education))
baseBRF_sem_mv <- baseBRF[-c(index_glucose,index_heartRate,index_BMI,index_totChol,index_BPMeds,index_cigsPerDay,index_education),]
matrixplot(baseBRF_sem_mv)
aggr(baseBRF_sem_mv)
# ANÁLISE BIVARIADA
# Variáveis quantitativas
boxplot(baseBRF_sem_mv$male ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$age ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$education ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$currentSmoker ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$cigsPerDay ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$BPMeds ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$prevalentStroke ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$prevalentHyp ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$diabetes ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$totChol ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$sysBP ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$diaBP ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$BMI ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$heartRate ~ baseBRF_sem_mv$TenYearCHD)
boxplot(baseBRF_sem_mv$glucose ~ baseBRF_sem_mv$TenYearCHD)
#Variáveis quantitativas e quali
prop.table(table(baseBRF_sem_mv$TenYearCHD))
prop.table(table(baseBRF_sem_mv$male, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$age, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$education, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$currentSmoker, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$cigsPerDay, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$BPMeds, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$prevalentStroke, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$prevalentHyp, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$diabetes, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$totChol, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$sysBP, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$diaBP, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$BMI, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$heartRate, baseBRF_sem_mv$TenYearCHD),1)
prop.table(table(baseBRF_sem_mv$glucose, baseBRF_sem_mv$TenYearCHD),1)
################################################################################################
# AMOSTRAGEM DO DADOS
library(caret)
set.seed(12345)
index <- createDataPartition(baseBRF_sem_mv$TenYearCHD, p= 0.7,list = F)
data.train <- baseBRF_sem_mv[index, ] # base de desenvolvimento: 70%
data.test <- baseBRF_sem_mv[-index,] # base de teste: 30%
# Checando se as proporções das amostras são próximas à base original
prop.table(table(baseBRF_sem_mv$TenYearCHD))
prop.table(table(data.train$TenYearCHD))
prop.table(table(data.test$TenYearCHD))
# Algoritmos de árvore necessitam que a variável resposta num problema de classificação seja
# um factor; convertendo aqui nas amostras de desenvolvimento e teste
data.train$TenYearCHD <- as.factor(data.train$TenYearCHD)
data.test$TenYearCHD <- as.factor(data.test$TenYearCHD)
################################################################################################
# MODELAGEM DOS DADOS - MÉTODOS DE ENSEMBLE
names <- names(data.train) # salva o nome de todas as variáveis e escreve a fórmula
f_full <- as.formula(paste("TenYearCHD ~",
paste(names[!names %in% "TenYearCHD"], collapse = " + ")))
# a) Random Forest
library(randomForest)
# Aqui começamos a construir um modelo de random forest usando sqrt(n var) | mtry = default
# Construimos 500 árvores, e permitimos nós finais com no mínimo 50 elementos
rndfor <- randomForest(f_full,data= data.train,importance = T, nodesize =5, ntree = 500)
rndfor
# Avaliando a evolução do erro com o aumento do número de árvores no ensemble
plot(rndfor, main= "Mensuração do erro")
legend("topright", c('Out-of-bag',"1","0"), lty=1, col=c("black","green","red"))
# Uma avaliação objetiva indica que a partir de ~30 árvores não há mais ganhos expressivos
rndfor2 <- randomForest(f_full,data= data.train,importance = T, nodesize =5, ntree = 50)
rndfor2
plot(rndfor2, main= "Mensuração do erro")
legend("topright", c('Out-of-bag',"1","0"), lty=1, col=c("black","green","red"))
# Importância das variáveis
varImpPlot(rndfor2, sort= T, main = "Importância das Variáveis")
# Aplicando o modelo nas amostras e determinando as probabilidades
rndfor2.prob.train <- predict(rndfor2, type = "prob")[,2]
rndfor2.prob.test <- predict(rndfor2,newdata = data.test, type = "prob")[,2]
# Comportamento da saida do modelo
hist(rndfor2.prob.test, breaks = 25, col = "lightblue",xlab= "Probabilidades",
ylab= "Frequência",main= "Random Forest")
boxplot(rndfor2.prob.test ~ data.test$TenYearCHD,col= c("green", "red"), horizontal= T)
#-------------------------------------------------------------------------------------------
# b) Boosted trees
library(adabag)
# Aqui construimos inicialmente um modelo boosting com 1000 iterações, profundidade 1
# e minbucket 50, os pesos das árvores serão dados pelo algoritimo de Freund
boost <- boosting(f_full, data= data.train, mfinal= 110,
coeflearn = "Freund",
control = rpart.control(minbucket= 5,maxdepth = 12))
# Avaliando a evolução do erro conforme o número de iterações aumenta
plot(errorevol(boost, data.train))
# podemos manter em 200 iterações
# Importância das variáveis
var_importance <- boost$importance[order(boost$importance,decreasing = T)]
var_importance
importanceplot(boost)
# Aplicando o modelo na amostra de teste e determinando as probabilidades
boost.prob.train <- predict.boosting(boost, data.train)$prob[,2]
boost.prob.test <- predict.boosting(boost, data.test)$prob[,2]
# Comportamento da saída do modelo
hist(boost.prob.test, breaks = 25, col = "lightblue",xlab= "Probabilidades",
ylab= "Frequ?ncia",main= "Boosting")
boxplot(boost.prob.test ~ data.test$TenYearCHD,col= c("green", "red"), horizontal= T)
################################################################################################
# AVALIANDO A PERFORMANCE
# Métricas de discriminação para ambos modelos
library(hmeasure)
rndfor.train <- HMeasure(data.train$TenYearCHD,rndfor2.prob.train)
rndfor.test <- HMeasure(data.test$TenYearCHD,rndfor2.prob.test)
rndfor.train$metrics
rndfor.test$metrics
boost.train <- HMeasure(data.train$TenYearCHD,boost.prob.train)
boost.test <- HMeasure(data.test$TenYearCHD,boost.prob.test)
boost.train$metrics
boost.test$metrics
library(pROC)
roc3 <- roc(data.test$TenYearCHD,rndfor2.prob.test)
y3 <- roc3$sensitivities
x3 <- 1-roc3$specificities
roc4 <- roc(data.test$TenYearCHD,boost.prob.test)
y4 <- roc4$sensitivities
x4 <- 1-roc4$specificities
plot(x3,y3, type="n",
xlab = "1 - Especificidade",
ylab= "Sensitividade")
lines(x3, y3,lwd=3,lty=1, col="pink")
lines(x4, y4,lwd=3,lty=1, col="blue")
legend("topright", c('Random Forest',"Boosting"), lty=1, col=c("pink","blue"))
################################################################################################
################################################################################################