-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree_and_Ensemble_Methods.R
59 lines (37 loc) · 1.93 KB
/
Tree_and_Ensemble_Methods.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
load(file = "Data_Preprocessings.R")
library(rpart)
set.seed(1)
tree_model <- rpart(y_train ~ ., data = data.frame(x_train), method = "class")
rpart.plot::rpart.plot(tree_model)
test_predictions <- predict(tree_model, newdata = test_data, type = "class")
test_predictions
test_predictions_recode <- ifelse(test_predictions == 1, 1, 0)
classification_error_rate <- mean(test_predictions != test_data$outcome)
sum(test_predictions == 1)
cat("Classification Error Rate:", round(classification_error_rate, 3), "\n")
term_deposit_count <- sum(test_predictions_recode == 1)
cat("Number of individuals predicted to open a term deposit:", term_deposit_count, "\n")
library(SuperLearner)
library(randomForest)
library(glmnet)
library(e1071)
base_learners <- c("SL.randomForest", "SL.glmnet", "SL.ksvm")
cvControl<-SuperLearner.CV.control(V=5)
control <- SuperLearner.control()
set.seed(1)
superlearner_model <- SuperLearner(Y = train_data$outcome,
X = train_data[, -which(names(train_data) == "outcome")],
SL.library = base_learners,
method = "method.NNLS", family=binomial,
control = control, verbose = TRUE, cvControl = cvControl)
superlearner_model
test_predictions <- predict(superlearner_model, newdata = test_data[, -which(names(test_data) == "outcome")])
test_predictions$pred[1]
predicted_prob_first_obs <- test_predictions$pred[1]
test_predictions_recode <- ifelse(test_predictions$pred > 0.5, 1, 0)
classification_error_rate <- mean(test_predictions_recode != test_data$outcome)
test_predictions
term_deposit_count <- sum(test_predictions$pred > 0.5)
cat("Predicted outcome for the first observation:", round(predicted_prob_first_obs, 3), "\n")
cat("Number of individuals predicted to open a term deposit:", term_deposit_count, "\n")
cat("Classification error rate:", round(classification_error_rate, 3), "\n")