Decision Tree
Decision Tree
Decision Tree
# Split data into training (80%) and testing (20%) sets using random sampling
set.seed(1234) # Set seed for reproducibility
ind = sample(2, nrow(Cardio), replace = TRUE, prob = c(0.8, 0.2)) # Random sampling
Cardio_train = Cardio[ind == 1, ] # Training data
Cardio_test = Cardio[ind == 2, ] # Testing data
# Build a decision tree using the 'party' package (conditional inference tree)
tree1 = ctree(NSPF ~ LB + AC + FM, data = Cardio_train) # Conditional inference tree
model
plot(tree1) # Plot the tree
# Make predictions with the conditional inference tree model
Cardio_predict = predict(tree1, Cardio_test) # Predict using the conditional inference tree
(Cardio_predict_table = table(Cardio_predict, Cardio_test$NSPF)) # Confusion matrix
(Cardio_performance = sum(diag(Cardio_predict_table)) / sum(Cardio_predict_table) * 100)
# Calculate accuracy