library(dplyr)
library(ggplot2)
library(tidyr)
library(readr)
library(lubridate)
library(readxl)
setwd("C:/Users/shubh/OneDrive/Desktop/new dissertation")
shubhi_budget_india <- read_excel("Shubhi Budget-India.xlsx")
shubhi_budget_uk <- read_excel("Shubhi Budget - UK.xlsx")
ovi_budget_india <- read_excel("Ovi India Budget.xlsx")
ovi_budget_UK <- read_excel("Ovi UK Budget.xlsx")
uday_budget_india <- read_excel("Uday india budget.xlsx")
uday_budget_uk <- read_excel("Budget uday UK.xlsx")
tallu_budget_india <- read_excel("Tallu budget - India.xlsx")
tallu_budget_uk <- read_excel("Tallu UK Budget.xlsx")
head(shubhi_budget_uk)
head(ovi_budget_india)
head(ovi_budget_UK)
head(uday_budget_india)
head(uday_budget_uk)
head(tallu_budget_india)
head(tallu_budget_uk)



# Combine India budgets and add a Person and Country column
india_budgets <- bind_rows(
  ovi_budget_india %>% mutate(Person = "Ovi"),
  tallu_budget_india %>% mutate(Person = "Tallu"),
  uday_budget_india %>% mutate(Person = "Uday"),
  shubhi_budget_india %>% mutate(Person = "Shubhi")
) %>% 
  mutate(Country = "India")

# Combine UK budgets and add a Person and Country column
uk_budgets <- bind_rows(
  ovi_budget_UK %>% mutate(Person = "Ovi"),
  tallu_budget_uk %>% mutate(Person = "Tallu"),
  uday_budget_uk %>% mutate(Person = "Uday"),
  shubhi_budget_uk %>% mutate(Person = "Shubhi")
) %>% 
  mutate(Country = "UK")

# View the combined datasets
head(india_budgets)
head(uk_budgets)

# Combine India and UK datasets
all_budgets <- bind_rows(india_budgets, uk_budgets)

##Step 2: Data Preprocessing
# Convert Month to date format
all_budgets <- all_budgets %>%
  mutate(Month = parse_date_time(Month, orders = "my")) %>% 
#Convert Month to Date format
  arrange(Person, Country, Month)

# Inspect the dataset
str(all_budgets)
summary(all_budgets)

# Step 3: Descriptive Analysis
# Summarize expenses by country and person
summary_stats <- all_budgets %>%
  group_by(Country, Person) %>%
  summarise(across(where(is.numeric), \(x) mean(x, na.rm = TRUE)), .groups = 'drop')

# View the summary statistics
print(summary_stats)

# Visualize the summary statistics
# Convert the summary statistics to long format for ggplot
summary_stats_long <- summary_stats %>%
  pivot_longer(cols = -c(Country, Person), names_to = "Category", values_to = "MeanValue")

# Plot the mean value of each budget category for each person in India and the UK
ggplot(summary_stats_long, aes(x = Category, y = MeanValue, fill = Person)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_wrap(~ Country) +
  labs(title = "Average Budget Comparison by Category and Person", 
       x = "Budget Category", y = "Average Value") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

setwd("C:/Users/shubh/OneDrive/Desktop/new dissertation")
shubhi_budget_india <- read_excel("Shubhi Budget-India.xlsx")
shubhi_budget_uk <- read_excel("Shubhi Budget - UK.xlsx")
ovi_budget_india <- read_excel("Ovi India Budget.xlsx")
ovi_budget_UK <- read_excel("Ovi UK Budget.xlsx")
uday_budget_india <- read_excel("Uday india budget.xlsx")
uday_budget_uk <- read_excel("Budget uday UK.xlsx")
tallu_budget_india <- read_excel("Tallu budget - India.xlsx")
tallu_budget_uk <- read_excel("Tallu UK Budget.xlsx")
# Read and merge income data for India
india_budgets <- bind_rows(
  shubhi_budget_india %>% mutate(Person = "Shubhi"),
  uday_budget_india %>% mutate(Person = "Uday"),
  ovi_budget_india %>% mutate(Person = "Ovi"),
  tallu_budget_india %>% mutate(Person = "Tallu"),
  .id = "Country"
) %>%
  mutate(Country = "India")

# Read and merge income data for UK
uk_budgets <- bind_rows(
  shubhi_budget_uk %>% mutate(Person = "Shubhi"),
  uday_budget_uk %>% mutate(Person = "Uday"),
  ovi_budget_UK %>% mutate(Person = "Ovi"),
  tallu_budget_uk %>% mutate(Person = "Tallu"),
  .id = "Country"
) %>%
  mutate(Country = "UK")

# Combine both India and UK datasets
all_budgets <- bind_rows(india_budgets, uk_budgets)

# Check the structure of all_budgets
str(all_budgets)

# View the first few rows of all_budgets
head(all_budgets)

# Monthly Income comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Income, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Income Comparison Between India and UK",
       x = "Month",
       y = "Income") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Rent comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Rent, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Rent Comparison Between India and UK",
       x = "Month",
       y = "Rent") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Groceries comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Groceries, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Groceries Comparison Between India and UK",
       x = "Month",
       y = "Groceries") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly EMIs comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = EMIs, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly EMIs Comparison Between India and UK",
       x = "Month",
       y = "EMIs") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Utilities comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Utilities, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Utilities Comparison Between India and UK",
       x = "Month",
       y = "Utilities") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Transportation comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Transportation, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Transportation Comparison Between India and UK",
       x = "Month",
       y = "Transportation") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Entertainment comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Entertainment, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Entertainment Comparison Between India and UK",
       x = "Month",
       y = "Entertainment") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly SelfCare comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = SelfCare, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly SelfCare Comparison Between India and UK",
       x = "Month",
       y = "SelfCare") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Healthcare comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Healthcare, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Healthcare Comparison Between India and UK",
       x = "Month",
       y = "Healthcare") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Savings comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Savings, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Savings Comparison Between India and UK",
       x = "Month",
       y = "Savings") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Miscellaneous comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Miscellaneous, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Miscellaneous Comparison Between India and UK",
       x = "Month",
       y = "Miscellaneous") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Insurance comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Insurance, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Insurance Comparison Between India and UK",
       x = "Month",
       y = "Insurance") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Dineout comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Dineout, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Dineout Comparison Between India and UK",
       x = "Month",
       y = "Dineout") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Clothing comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Clothing, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Clothing Comparison Between India and UK",
       x = "Month",
       y = "Clothing") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Travel comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Travel, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Travel Comparison Between India and UK",
       x = "Month",
       y = "Travel") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

# Monthly Total_Expenses comparison
plot_object <- ggplot(all_budgets, aes(x = Month, y = Total_Expenses, color = Country, group = Person)) +
  geom_line() +
  labs(title = "Monthly Total Expenses Comparison Between India and UK",
       x = "Month",
       y = "Total_Expenses") +
  facet_wrap(~Person) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot_object)

summary_stats <- all_budgets %>%
  group_by(Country, Person) %>%
  summarise(across(where(is.numeric), list(mean = mean, sd = sd, median = median), na.rm = TRUE))

# Comparative bar chart for Income
ggplot(all_budgets, aes(x = Country, y = Income, fill = Country)) +
  geom_boxplot() +
  labs(title = "Comparison of Income between India and UK",
       x = "Country",
       y = "Income") +
  theme_minimal()


# Comparative bar chart for Total_Expenses
ggplot(all_budgets, aes(x = Country, y = Total_Expenses, fill = Country)) +
  geom_boxplot() +
  labs(title = "Comparison of Total Expenses between India and UK",
       x = "Country",
       y = "Total_Expenses") +
  theme_minimal()

# Correlation plot
ggplot(all_budgets, aes(x = Income, y = Savings, color = Country)) +
  geom_point() +
  labs(title = "Correlation between Income and Savings",
       x = "Income",
       y = "Savings") +
  theme_minimal()

# Fit ARIMA model for Income 
arima_model <- auto.arima(all_budgets$Income)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Income (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Income
autoplot(forecast) +
  labs(title = "Forecasting Income using ARIMA",
       x = "Month",
       y = "Income")

# Fit ARIMA model for Groceries 
arima_model <- auto.arima(all_budgets$Groceries)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Groceries (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Groceries
autoplot(forecast) +
  labs(title = "Forecasting Groceries using ARIMA",
       x = "Month",
       y = "Groceries")

# Fit ARIMA model for Rent 
arima_model <- auto.arima(all_budgets$Rent)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Rent (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Rent
autoplot(forecast) +
  labs(title = "Forecasting Rent using ARIMA",
       x = "Month",
       y = "Rent")

# Fit ARIMA model for Groceries 
arima_model <- auto.arima(all_budgets$Groceries)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Groceries (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Groceries
autoplot(forecast) +
  labs(title = "Forecasting Groceries using ARIMA",
       x = "Month",
       y = "Groceries")

# Fit ARIMA model for EMIs 
arima_model <- auto.arima(all_budgets$EMIs)


# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future EMIs (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted EMIs
autoplot(forecast) +
  labs(title = "Forecasting EMIs using ARIMA",
       x = "Month",
       y = "EMIs")

# Fit ARIMA model for Utilities 
arima_model <- auto.arima(all_budgets$Utilities)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Utilities (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Utilities
autoplot(forecast) +
  labs(title = "Forecasting Utilities using ARIMA",
       x = "Month",
       y = "Utilities")

# Fit ARIMA model for Transportation 
arima_model <- auto.arima(all_budgets$Transportation)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Transportation (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Transportation
autoplot(forecast) +
  labs(title = "Forecasting Transportation using ARIMA",
       x = "Month",
       y = "Transportation")

# Fit ARIMA model for Entertainment 
arima_model <- auto.arima(all_budgets$Entertainment)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Entertainment (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Entertainment
autoplot(forecast) +
  labs(title = "Forecasting Entertainment using ARIMA",
       x = "Month",
       y = "Entertainment")

# Fit ARIMA model for SelfCare 
arima_model <- auto.arima(all_budgets$SelfCare)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future SelfCare (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted SelfCare
autoplot(forecast) +
  labs(title = "Forecasting SelfCare using ARIMA",
       x = "Month",
       y = "SelfCare")

# Fit ARIMA model for Healthcare 
arima_model <- auto.arima(all_budgets$Healthcare)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Healthcare (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Healthcare
autoplot(forecast) +
  labs(title = "Forecasting Healthcare using ARIMA",
       x = "Month",
       y = "Healthcare")

# Fit ARIMA model for Savings 
arima_model <- auto.arima(all_budgets$Savings)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Savings (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Savings
autoplot(forecast) +
  labs(title = "Forecasting Savings using ARIMA",
       x = "Month",
       y = "Savings")

# Fit ARIMA model for Miscellaneous 
arima_model <- auto.arima(all_budgets$Miscellaneous)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Miscellaneous (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Miscellaneous
autoplot(forecast) +
  labs(title = "Forecasting Miscellaneous using ARIMA",
       x = "Month",
       y = "Miscellaneous")

# Fit ARIMA model for Insurance  
arima_model <- auto.arima(all_budgets$Insurance)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Insurance (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Insurance
autoplot(forecast) +
  labs(title = "Forecasting Insurance using ARIMA",
       x = "Month",
       y = "Insurance")

# Fit ARIMA model for Dineout  
arima_model <- auto.arima(all_budgets$Dineout)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Dineout (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Dineout
autoplot(forecast) +
  labs(title = "Forecasting Dineout using ARIMA",
       x = "Month",
       y = "Dineout")

# Fit ARIMA model for Clothing  
arima_model <- auto.arima(all_budgets$Clothing)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Clothing (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Clothing
autoplot(forecast) +
  labs(title = "Forecasting Clothing using ARIMA",
       x = "Month",
       y = "Clothing")

# Fit ARIMA model for Travel  
arima_model <- auto.arima(all_budgets$Travel)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Travel (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Travel
autoplot(forecast) +
  labs(title = "Forecasting Travel using ARIMA",
       x = "Month",
       y = "Travel")

# Fit ARIMA model for Total_Expenses  
arima_model <- auto.arima(all_budgets$Total_Expenses)

# Check the summary of the ARIMA model
summary(arima_model)

# Forecast future Total_Expenses (e.g., 12 months ahead)
forecast <- forecast(arima_model, h = 12)

# Plot actual vs. predicted Total_Expenses
autoplot(forecast) +
  labs(title = "Forecasting Total_Expenses using ARIMA",
       x = "Month",
       y = "Total_Expenses")

