# Define weeks and values from the problem
weeks <- 1:6
values <- c(17, 13, 15, 11, 17, 14)

# Part A: Forecasting Using the Most Recent Value

forecasts_a <- values[-length(values)]  # Excludes the last value
actual_a <- values[-1]  # Excludes the first value
mse_a <- mean((actual_a - forecasts_a)^2)  #Result was 16.2
forecast_week_7_a <- tail(values, 1)  # Predict the value for week 7
# Part B: Forecasting Using the Cumulative Average
cumulative_averages <- cumsum(values[-length(values)]) / (1:(length(values) - 1))
forecasts_b <- cumulative_averages
actual_b <- values[-1]  # Excludes the first value
mse_b <- mean((actual_b - forecasts_b)^2)  # 8.272
forecast_week_7_b <- mean(values)  # Forecast for week 7 using average
# Part C: Which Method is Better
better_method <- ifelse(mse_a < mse_b, "Most Recent Value", "Average of All Data")
results <- list(
  MSE_most_recent_value = mse_a,
  forecast_week_7_most_recent = forecast_week_7_a,
  MSE_average_of_all_data = mse_b,
  forecast_week_7_average = forecast_week_7_b,
  Better_Method = better_method
)

# Output the results
print(results)

Questions 2 ///////////////////////////////////////////////////////////////////////

library(dplyr)
library(zoo)
df <- data.frame(
  month=1:12,
  contracts=c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230)
)
# Part A: Time series plot
plot(df$month, df$contracts, type="o", col="blue", xlab="Month", ylab="Contracts ($ millions)", main="Monthly Alabama Building Contracts")
# Interpretation: Horizontal Patterns
# Part B: Three-month moving average

df$avg_contracts <- c(NA, NA, NA,
                  (df$data[1] + df$data[2] + df$data[3]) / 3, 
                  (df$data[2] + df$data[3] + df$data[4]) / 3, 
                  (df$data[3] + df$data[4] + df$data[5]) / 3, 
                  (df$data[4] + df$data[5] + df$data[6]) / 3, 
                  (df$data[5] + df$data[6] + df$data[7]) / 3,
                  (df$data[6] + df$data[7] + df$data[8]) / 3, 
                  (df$data[7] + df$data[8] + df$data[9]) / 3, 
                  (df$data[8] + df$data[9] + df$data[10]) / 3, 
                  (df$data[9] + df$data[10] + df$data[11]) / 3)


# Calculate the squared errors (only for the months where moving average is available)
df <- df %>%
  mutate(
    squared_error = ifelse(is.na(avg_contracts), NA, (contracts - avg_contracts)^2)
  )

#  MSE for the three-month moving average
mse_moving_average <- mean(df$squared_error, na.rm = TRUE)

# Part C: Exponential smoothing with alpha = 0.2
alpha = 0.2
exp_smooth <- rep(NA, length(df$contracts))
exp_smooth[1] <- df$contracts[1]  # starting point

for(i in 2:length(df$contracts)) {
  exp_smooth[i] <- alpha * df$contracts[i] + (1 - alpha) * exp_smooth[i-1]
}

# Calculate MSE for exponential smoothing
mse_exp_smooth <- mean((df$contracts[2:12] - exp_smooth[2:12])^2)

# Compare methods
better_method <- ifelse(mse_moving_average < mse_exp_smooth, "Three-month moving average", "Exponential smoothing")

# Results list
results <- list(
  MSE_moving_average = mse_moving_average,
  MSE_exponential_smoothing = mse_exp_smooth,
  Better_Method = better_method
)

# Output the results
print(results)
# QUESTION 3 /////////////////////////////////////////////////////////////////////// 


# Load required libraries
library(readxl)
library(ggplot2)

# Load the data from the Excel file
df <- read_excel(file.choose())

print(colnames(df))


ggplot(df, aes(x = Year, y = Interest_Rate)) +
  geom_line(color = "blue") +
  geom_point(color = "red") +
  xlab("Year") +
  ylab("Average Interest Rate (%)") +
  ggtitle("Time Series Plot of 30-Year Fixed-Rate Mortgage Interest Rates")


model <- lm(Interest_Rate ~ Period, data = df)
summary(model) 
#6.7 + (-0.13)*Period

forecast_period_25 <- predict(model, newdata = data.frame(Period = 25))
print(forecast_period_25)  # Output the forecasted interest rate for Period 25