classes <- treino %>% count(situacao_final)
classes
## # A tibble: 2 x 2
## situacao_final n
## <fct> <int>
## 1 eleito 416
## 2 nao_eleito 3719
Há um desbalanceamento já que o número de observações não é o mesmo para todas as unidades de análise, quando a classe de interesse é mais rara que a classe majoritária. Quando as classe são desbalanceadas a acurácia não é boa métrica e o classificar tende a prever a classe majoritária.
Para balancear o modelo usamos under sampling, onde selecionamos aleatoriamente um subconjunto de amostras da classe com mais instâncias para corresponder ao número de amostras provenientes de cada classe.
treino2 <- treino %>%
select(-nome)
dataPartition <- createDataPartition(y = treino2$situacao_final, p=0.75, list=FALSE)
treino3 <- treino2[ dataPartition, ]
ctrl <- trainControl(method = "repeatedcv",
repeats = 5,
verboseIter = FALSE,
sampling = "down")
arvore1 <- train(situacao_final ~ .,
data = treino3,
method = "rpart",
cp=0.001,
maxdepth=20,
trControl = ctrl)
arvore1
## CART
##
## 3102 samples
## 26 predictor
## 2 classes: 'eleito', 'nao_eleito'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 5 times)
## Summary of sample sizes: 2792, 2792, 2792, 2792, 2792, 2791, ...
## Addtional sampling using down-sampling
##
## Resampling results across tuning parameters:
##
## cp Accuracy Kappa
## 0.02243590 0.8864603 0.5727641
## 0.07211538 0.8864603 0.5727641
## 0.31089744 0.8864603 0.5727641
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.3108974.
Observação: Não consegui tratar o erro (glm.fit: fitted probabilities numerically 0 or 1 occurredprediction from a rank-deficient fit may be misleadingprediction from a rank-deficient fit may be misleadingprediction from a rank-deficient fit may be misleading), mas o modelo foi gerado.
formula = as.formula(situacao_final ~ .)
modeloreglog <- train(formula,
data = treino3,
method="glm",
family="binomial",
trControl = ctrl,
na.action = na.omit)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
modeloreglog
## Generalized Linear Model
##
## 3102 samples
## 26 predictor
## 2 classes: 'eleito', 'nao_eleito'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 5 times)
## Summary of sample sizes: 2792, 2792, 2791, 2792, 2792, 2792, ...
## Addtional sampling using down-sampling
##
## Resampling results:
##
## Accuracy Kappa
## 0.7658264 0.3147278
#modeloada <- train(formula,
# data=treino3,
# trControl = ctrl,
# method = "adaboost")
#modeloada
A acurácia é a medida de desempenho mais intuitiva e é representa a proporção de observação corretamente previstas para as observações totais. Já a precisão é a proporção de observações positivas corretamente previstas para as observações positivas previstas. Alta precisão está relacionada à baixa taxa de falso positivo. Recall é a proporção de observações positivas corretamente previstas para todas as observações na classe real. Por fim, o f1 é a média ponderada de precisão e recall. Portanto, essa pontuação leva em conta falsos positivos e falsos negativos. O f1 é mais útil do que acurácia caso a se tenha uma distribuição de classe desigual. A acurácia funciona melhor se falsos positivos e falsos negativos têm custo semelhante. Se o custo de falsos positivos e falsos negativos for muito diferente, é melhor olhar precisão e recall.
Como os dados foram balanceados, consideramos que a acurácia é uma boa métrica, bem como o f1. Sendo assim, os resultados obtidos com a árvore de decisão foram levemente melhores que os demais.
treino3$predictions_arvore <- predict(arvore1, newdata = treino3)
TP <- treino3 %>% filter(situacao_final == "eleito", predictions_arvore == "eleito") %>% nrow()
TN <- treino3 %>% filter(situacao_final == "nao_eleito" , predictions_arvore == "nao_eleito" ) %>% nrow()
FP <- treino3 %>% filter(situacao_final == "nao_eleito" , predictions_arvore == "eleito") %>% nrow()
FN <- treino3 %>% filter(situacao_final == "eleito", predictions_arvore == "nao_eleito" ) %>% nrow()
accuracy <- (TP + TN)/(TP + TN + FP + FN)
precision <- TP / (TP + FP)
recall <- TP / (TP + FN)
f1 <- 2*(recall * precision) / (recall + precision)
accuracy
## [1] 0.8668601
precision
## [1] 0.4289733
recall
## [1] 0.9775641
f1
## [1] 0.5962854
confusionMatrix(treino3$predictions_arvore, treino3$situacao_final)
## Confusion Matrix and Statistics
##
## Reference
## Prediction eleito nao_eleito
## eleito 305 406
## nao_eleito 7 2384
##
## Accuracy : 0.8669
## 95% CI : (0.8544, 0.8786)
## No Information Rate : 0.8994
## P-Value [Acc > NIR] : 1
##
## Kappa : 0.5307
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.97756
## Specificity : 0.85448
## Pos Pred Value : 0.42897
## Neg Pred Value : 0.99707
## Prevalence : 0.10058
## Detection Rate : 0.09832
## Detection Prevalence : 0.22921
## Balanced Accuracy : 0.91602
##
## 'Positive' Class : eleito
##
treino3$predictions_modeloreglog <- predict(modeloreglog, newdata = treino3)
## Warning in predict.lm(object, newdata, se.fit, scale = 1, type =
## ifelse(type == : prediction from a rank-deficient fit may be misleading
TP <- treino3 %>% filter(situacao_final == "eleito", predictions_modeloreglog == "eleito") %>% nrow()
TN <- treino3 %>% filter(situacao_final == "nao_eleito" , predictions_modeloreglog == "nao_eleito" ) %>% nrow()
FP <- treino3 %>% filter(situacao_final == "nao_eleito" , predictions_modeloreglog == "eleito") %>% nrow()
FN <- treino3 %>% filter(situacao_final == "eleito", predictions_modeloreglog == "nao_eleito" ) %>% nrow()
accuracy <- (TP + TN)/(TP + TN + FP + FN)
precision <- TP / (TP + FP)
recall <- TP / (TP + FN)
f1 <- 2*(recall * precision) / (recall + precision)
accuracy
## [1] 0.7814313
precision
## [1] 0.3151515
recall
## [1] 1
f1
## [1] 0.4792627
confusionMatrix(treino3$predictions_modeloreglog, treino3$situacao_final)
## Confusion Matrix and Statistics
##
## Reference
## Prediction eleito nao_eleito
## eleito 312 678
## nao_eleito 0 2112
##
## Accuracy : 0.7814
## 95% CI : (0.7665, 0.7959)
## No Information Rate : 0.8994
## P-Value [Acc > NIR] : 1
##
## Kappa : 0.3852
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 1.0000
## Specificity : 0.7570
## Pos Pred Value : 0.3152
## Neg Pred Value : 1.0000
## Prevalence : 0.1006
## Detection Rate : 0.1006
## Detection Prevalence : 0.3191
## Balanced Accuracy : 0.8785
##
## 'Positive' Class : eleito
##
#treino3$predictions_modeloada <- predict(modeloada, newdata = treino3)
#TP <- treino3 %>% filter(situacao_final == "eleito", predictions_modeloada == "eleito") %>% nrow()
#TN <- treino3 %>% filter(situacao_final == "nao_eleito" , predictions_modeloada == "nao_eleito" ) %>% nrow()
#FP <- treino3 %>% filter(situacao_final == "nao_eleito" , predictions_modeloada == "eleito") %>% nrow()
#FN <- treino3 %>% filter(situacao_final == "eleito", predictions_modeloada == "nao_eleito" ) %>% nrow()
#accuracy <- (TP + TN)/(TP + TN + FP + FN)
#precision <- TP / (TP + FP)
#recall <- TP / (TP + FN)
#f1 <- 2*(recall * precision) / (recall + precision)
#accuracy
#precision
#recall
#f1
#confusionMatrix(treino3$predictions_modeloada, treino3$situacao_final)
Segundo a árvore de decisão os atributos mais importantes são total_despesa e total_receita.
varImp(arvore1)
## rpart variable importance
##
## only 20 most important variables shown (out of 384)
##
## Overall
## total_despesa 100.00
## total_receita 100.00
## quantidade_despesas 86.78
## quantidade_fornecedores 82.18
## recursos_de_pessoas_juridicas 78.22
## `setor_economico_receitaPreparação de documentos e serviços especializados de apoio administrativo não especificados anteriormente` 0.00
## `setor_economico_despesaComércio varejista especializado de eletrodomésticos e equipamentos de áudio e vÃdeo` 0.00
## descricao_ocupacaoFAXINEIRO 0.00
## descricao_ocupacaoPEDAGOGO 0.00
## partidoPP 0.00
## quantidade_doacoes 0.00
## descricao_ocupacaoMASSAGISTA 0.00
## `descricao_ocupacaoPROFESSOR DE ENSINO MÃDIO` 0.00
## `setor_economico_despesaPromoção de vendas` 0.00
## `setor_economico_receitaFabricação de esquadrias de madeira e de peças de madeira para instalações industriais e comerciais` 0.00
## descricao_ocupacaoDESENHISTA 0.00
## `setor_economico_receitaHotéis` 0.00
## partidoPCO 0.00
## `setor_economico_despesaServiços de pré-impressão` 0.00
## `grauLÃ E ESCREVE` 0.00
varImp(modeloreglog)
## glm variable importance
##
## only 20 most important variables shown (out of 198)
##
## Overall
## `setor_economico_despesaEdição integrada à impressão de cadastros, listas e outros produtos gráficos` 100.00
## `setor_economico_receitaAtividades de organizações polÃticas` 87.32
## quantidade_doadores 78.84
## sexoMASCULINO 78.57
## UFAL 64.60
## UFAP 64.60
## UFCE 64.60
## UFBA 64.60
## UFMA 64.60
## UFPB 64.60
## UFES 64.60
## UFMT 64.60
## UFMG 64.60
## UFRR 64.60
## UFRO 64.60
## UFPE 64.60
## UFPA 64.60
## UFRN 64.60
## UFDF 64.60
## UFSC 64.60
#varImp(modeloada)
Para o modelo de submissão foram utilizadas as variáveis de maior importância.
selecionadados <- treino3 %>% select(total_receita, total_despesa, situacao_final, quantidade_despesas, quantidade_doadores, recursos_de_pessoas_juridicas)
arvore <- train(situacao_final ~ .,
data = selecionadados,
method = "rpart",
cp=0.001,
maxdepth=20,
trControl = ctrl)
predicao <- predict(arvore, teste)
teste$prediction <- predicao
predicaofinal <- teste %>% select(ID, prediction)
write.csv(predicaofinal, file = "lab4_finalpredicao.csv", row.names = FALSE)