knitr::opts_chunk$set(echo = TRUE)
install.packages(c(“tidyverse”, “lubridate”, “ggplot2”, “caret”, “e1071”, “vcd”)) library(tidyverse) library(lubridate) library(ggplot2) library(caret) library(e1071) library(vcd)
bg <- read.csv(“C:\Users\Denno\Desktop\BG_with_webscan.csv”) fi <- read.csv(“C:\Users\Denno\Desktop\FI_with_webscan.csv”) ni <- read.csv(“C:\Users\Denno\Desktop\NI_with_webscan.csv”) sk <- read.csv(“C:\Users\Denno\Desktop\SK_with_webscan.csv”)
bg\(traveller_rating_4 <- as.character(bg\)traveller_rating_4) fi\(traveller_rating_4 <- as.character(fi\)traveller_rating_4) ni\(traveller_rating_4 <- as.character(ni\)traveller_rating_4) sk\(traveller_rating_4 <- as.character(sk\)traveller_rating_4)
restaurant_data <- bind_rows( mutate(bg, traveller_rating_4 = as.character(traveller_rating_4)), mutate(fi, traveller_rating_4 = as.character(traveller_rating_4)), mutate(ni, traveller_rating_4 = as.character(traveller_rating_4)), mutate(sk, traveller_rating_4 = as.character(traveller_rating_4)), .id = “location” )
restaurant_data <- restaurant_data %>%
mutate(across(where(is.character), str_trim)) %>%
mutate_if(is.character, na_if, ““) %>%
mutate(across(contains(”_date”), ymd))
summary(restaurant_data) # Summary statistics str(restaurant_data) # Data structure
ggplot(restaurant_data, aes(x = rating, na.rm = TRUE)) + geom_histogram(bins = 10, fill = “steelblue”, color = “black”) + xlab(“Rating”) + ylab(“Count”) + ggtitle(“Distribution of Restaurant Ratings”) + theme_minimal()
restaurant_data <- restaurant_data %>% mutate(status = factor(status, levels = c(“Open”, “Closed”)))
survival_by_rating <- restaurant_data %>% filter(!is.na(status)) %>% group_by(rating, status) %>% summarize(count = n(), .groups = “drop”) %>% mutate(percentage = count / sum(count) * 100)
ggplot(survival_by_rating, aes(x = rating, y = percentage, fill = status)) + geom_col(position = “fill”) + xlab(“Rating”) + ylab(“Percentage”) + ggtitle(“Restaurant Survival by Rating”) + scale_fill_manual(values = c(“steelblue”, “firebrick”)) + theme_minimal()
ggplot(restaurant_data, aes(x = location, y = rating, na.rm = TRUE)) + geom_boxplot(fill = “steelblue”, alpha = 0.6) + xlab(“Location”) + ylab(“Rating”) + ggtitle(“Rating Distribution by Location”) + theme_minimal()
table_status_location <- table(restaurant_data\(status, restaurant_data\)location) assocplot(table_status_location, xlab = “Status”, ylab = “Location”, main = “Association between Restaurant Survival and Location”)
user_ratings <- restaurant_data %>% group_by(rest_name) %>% summarize(avg_rating = mean(rating, na.rm = TRUE))
top_restaurants <- user_ratings %>% filter(!is.na(avg_rating)) %>% # Remove NAs arrange(desc(avg_rating)) %>% head(10)
ggplot(top_restaurants, aes(x = reorder(rest_name, -avg_rating), y = avg_rating)) + geom_bar(stat = “identity”, fill = “steelblue”) + xlab(“Restaurant”) + ylab(“Average Rating”) + ggtitle(“Top 10 Restaurants by User Rating”) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + theme_minimal()
table_status_location <- na.omit(table_status_location) # Remove NAs mosaicplot(table_status_location, main = “Mosaic Plot of Restaurant Survival and Location”, xlab = “Status”, ylab = “Location”, shade = TRUE)
user_locations <- restaurant_data %>% group_by(location) %>% summarize(count = n())
top_locations <- user_locations %>% filter(!is.na(count)) %>% # Remove NAs arrange(desc(count)) %>% head(10)
dotchart(top_locations\(count, labels = top_locations\)location, main = “Top Locations Visited by Users”, xlab = “Number of Visits”, color = “steelblue”)
set.seed(123) train_index <- createDataPartition(restaurant_data$status, p = 0.8, list = FALSE) train_data <- restaurant_data[train_index, ] test_data <- restaurant_data[-train_index, ]
tree_model <- rpart(status ~ rating + location, data = train_data, method = “class”) predictions <- predict(tree_model, newdata = test_data, type = “class”)
confusion_matrix <- confusionMatrix(data = predictions, reference = test_data$status) confusion_matrix
plot(tree_model) text(tree_model, pretty = 0) title(“Decision Tree for Restaurant Survival”)