This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boruta_signif <-names(boruta_output_Boruta$finalDecision[boruta_output_Boruta$finalDecision %in% c("Confirmed", "Tentative")]) | |
print(boruta_signif) | |
#Plot: | |
plot(boruta_output_Boruta, cex.axis=.7, las=2, xlab="", main="Variable Importance") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boruta_output_Boruta <- Boruta(Percent.Leave ~ ., data=na.omit(Brexit), doTrace=2) | |
boruta_output_Boruta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We run this with the updated model | |
Brexit_variable_importance <- data.frame(imp = Brexit_model_final$variable.importance) | |
Brexit_variable_importance2 <- Brexit_variable_importance %>% | |
tibble::rownames_to_column() %>% | |
dplyr::rename("variable" = rowname) %>% | |
dplyr::arrange(imp) %>% | |
dplyr::mutate(variable = forcats::fct_inorder(variable)) | |
ggplot2::ggplot(Brexit_variable_importance2) + | |
geom_col(aes(x = variable, y = imp), | |
col = "white", fill = "navy blue", show.legend = F) + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Brexit_test$pred <- predict(Brexit_model_final, Brexit_test, type = "class") | |
base_accuracy <- mean(Brexit_test$pred == Brexit_test$Percent.Leave) | |
base_accuracy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Brexit_model_final <- rpart(Percent.Leave ~ . , | |
data = Brexit, method = "class", | |
control = rpart.control(cp = TYPE_YOUR_CP_VALUE)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
printcp(Brexit_model) | |
plotcp(Brexit_model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#We need to quickly produce a model again - a conditional inference tree. Documentation here (we will switch back to rpart shortly after this) | |
install.packages("ggparty") | |
install.packages("partykit") | |
library(ggparty) | |
library(partykit) | |
Brexit_model_ggparty <- partykit::ctree(Percent.Leave ~ ., data = Brexit_train, control = ctree_control(testtype = "Teststatistic")) | |
ggparty(Brexit_model_ggparty) + | |
geom_edge() + | |
geom_edge_label() + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#decision tree plot: | |
rpart.plot(Brexit_model, type = 4, extra = 0, branch.lty = 3, box.palette = "RdYlGn") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#load packages: | |
install.packages('rpart') | |
install.packages('rpart.plot') | |
library(rpart) | |
library(rpart.plot) | |
#This is our base model: | |
Brexit_model <- rpart(Percent.Leave ~ . , data = Brexit_train, method = "class", control = rpart.control(cp = 0)) | |
Brexit_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Test and training dataset | |
sample_ind <- sample(nrow(Brexit), (nrow(Brexit) * 0.70)) | |
Brexit_train <- Brexit[sample_ind,] | |
Brexit_test <- Brexit[-sample_ind,] |