Skip to content

Instantly share code, notes, and snippets.

View BenHeubl's full-sized avatar

Ben Heubl BenHeubl

  • London
View GitHub Profile
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")
boruta_output_Boruta <- Boruta(Percent.Leave ~ ., data=na.omit(Brexit), doTrace=2)
boruta_output_Boruta
# 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) +
Brexit_test$pred <- predict(Brexit_model_final, Brexit_test, type = "class")
base_accuracy <- mean(Brexit_test$pred == Brexit_test$Percent.Leave)
base_accuracy
Brexit_model_final <- rpart(Percent.Leave ~ . ,
data = Brexit, method = "class",
control = rpart.control(cp = TYPE_YOUR_CP_VALUE))
printcp(Brexit_model)
plotcp(Brexit_model)
#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() +
#decision tree plot:
rpart.plot(Brexit_model, type = 4, extra = 0, branch.lty = 3, box.palette = "RdYlGn")
#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
#Test and training dataset
sample_ind <- sample(nrow(Brexit), (nrow(Brexit) * 0.70))
Brexit_train <- Brexit[sample_ind,]
Brexit_test <- Brexit[-sample_ind,]