R Markdown

The R CODE

knitr::opts_chunk$set(echo = TRUE)

Install and load required packages

install.packages(c(“tidyverse”, “lubridate”, “ggplot2”, “caret”, “e1071”, “vcd”)) library(tidyverse) library(lubridate) library(ggplot2) library(caret) library(e1071) library(vcd)

Import data

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”)

Convert traveller_rating_4 column to character

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)

Combine datasets

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” )

Data cleaning

restaurant_data <- restaurant_data %>% mutate(across(where(is.character), str_trim)) %>%
mutate_if(is.character, na_if, ““) %>%
mutate(across(contains(”_date”), ymd))

Exploratory Data Analysis

summary(restaurant_data) # Summary statistics str(restaurant_data) # Data structure

Plot restaurant ratings distribution

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()

Convert ‘status’ column to a factor

restaurant_data <- restaurant_data %>% mutate(status = factor(status, levels = c(“Open”, “Closed”)))

Analyze restaurant survival based on rating

survival_by_rating <- restaurant_data %>% filter(!is.na(status)) %>% group_by(rating, status) %>% summarize(count = n(), .groups = “drop”) %>% mutate(percentage = count / sum(count) * 100)

Stacked bar plot for restaurant survival by rating

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()

Box plot for rating distribution by location

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()

Association plot for restaurant survival and location

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 for different restaurants

user_ratings <- restaurant_data %>% group_by(rest_name) %>% summarize(avg_rating = mean(rating, na.rm = TRUE))

Bar plot for user ratings of different restaurants

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()

Mosaic plot for restaurant survival and location

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)

Locations where users traveled

user_locations <- restaurant_data %>% group_by(location) %>% summarize(count = n())

Dot chart for top locations visited by users

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”)

Build a decision tree model to predict restaurant survival

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”)

Evaluate decision tree model performance

confusion_matrix <- confusionMatrix(data = predictions, reference = test_data$status) confusion_matrix

Plot the decision tree

plot(tree_model) text(tree_model, pretty = 0) title(“Decision Tree for Restaurant Survival”)