# Load required libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
library(rpart)
library(readxl)
# Load the dataset
hotel_data <- read_excel("C:\\Users\\olajide.shittu\\OneDrive\\Documents\\HOTELS_NEW (ori).xlsx")
#Summary
summary(hotel_data)
## User country Nr. reviews Nr. hotel reviews Helpful votes
## Length:504 Min. : 1.00 Min. : 0.00 Min. : 0.00
## Class :character 1st Qu.: 12.00 1st Qu.: 5.00 1st Qu.: 8.00
## Mode :character Median : 23.50 Median : 9.00 Median : 16.00
## Mean : 48.13 Mean : 16.02 Mean : 31.75
## 3rd Qu.: 54.25 3rd Qu.: 18.00 3rd Qu.: 35.00
## Max. :775.00 Max. :263.00 Max. :365.00
## Score Period of stay Traveler type Pool
## Min. :1.000 Length:504 Length:504 Length:504
## 1st Qu.:4.000 Class :character Class :character Class :character
## Median :4.000 Mode :character Mode :character Mode :character
## Mean :4.123
## 3rd Qu.:5.000
## Max. :5.000
## Gym Tennis court Spa Casino
## Length:504 Length:504 Length:504 Length:504
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## Free internet Hotel name Hotel stars Nr. rooms
## Length:504 Length:504 Min. :3.000 Min. : 0
## Class :character Class :character 1st Qu.:3.000 1st Qu.: 716
## Mode :character Mode :character Median :4.000 Median :2700
## Mean :4.048 Mean :2057
## 3rd Qu.:5.000 3rd Qu.:3025
## Max. :5.000 Max. :4027
## User continent Member years Review month Review weekday
## Length:504 Min. :-1806.0000 Length:504 Length:504
## Class :character 1st Qu.: 0.0000 Class :character Class :character
## Mode :character Median : 3.0000 Mode :character Mode :character
## Mean : -0.0556
## 3rd Qu.: 6.0000
## Max. : 13.0000
# Convert relevant columns to factor (string values)
factor_columns <- c("User country", "Period of stay", "Traveler type", "Pool", "Gym", "Tennis court", "Spa", "Casino", "Free internet", "Hotel name", "User continent", "Review month", "Review weekday")
hotel_data[factor_columns] <- lapply(hotel_data[factor_columns], as.factor)
# Convert Member years to factor (assuming it represents membership duration)
hotel_data$`Member years` <- as.factor(hotel_data$`Member years`)
# Split data into training and testing sets
set.seed(123)
train_index <- createDataPartition(hotel_data$Score, p = 0.8, list = FALSE)
train_data <- hotel_data[train_index, ]
test_data <- hotel_data[-train_index, ]
# Train the Decision Tree model
model_tree <- rpart(Score ~ ., data = train_data)
# Make predictions on test data
predictions <- predict(model_tree, test_data)
# Extract predicted values and convert to factor with levels
predicted_values <- as.factor(ifelse(predictions > 0.5, "Positive", "Negative"))
levels(predicted_values) <- c("Positive", "Negative")
# Convert actual values to factor with levels
actual_values <- as.factor(ifelse(test_data$Score > 0.5, "Positive", "Negative"))
levels(actual_values) <- c("Positive", "Negative")
# Evaluate model performance
performance <- confusionMatrix(predicted_values, actual_values)
print(performance)
## Confusion Matrix and Statistics
##
## Reference
## Prediction Positive Negative
## Positive 100 0
## Negative 0 0
##
## Accuracy : 1
## 95% CI : (0.9638, 1)
## No Information Rate : 1
## P-Value [Acc > NIR] : 1
##
## Kappa : NaN
##
## Mcnemar's Test P-Value : NA
##
## Sensitivity : 1
## Specificity : NA
## Pos Pred Value : NA
## Neg Pred Value : NA
## Prevalence : 1
## Detection Rate : 1
## Detection Prevalence : 1
## Balanced Accuracy : NA
##
## 'Positive' Class : Positive
##
# Load required libraries
library(caret)
# Generate confusion matrix
conf_matrix <- confusionMatrix(predicted_values, actual_values)
# Plot confusion matrix
plot(conf_matrix$table, col = c("darkgreen", "lightgreen"),
main = "Confusion Matrix",
xlab = "Predicted",
ylab = "Actual")
