Q1

values <- c(17, 13, 15, 11, 17, 14)
forecasts <- c(NA, values[-length(values)])

errors <- values[-1] - forecasts[-1]

mae <- mean(abs(errors), na.rm = TRUE)
mse <- mean(errors^2, na.rm = TRUE)
mape <- mean(abs(errors) / values[-1] * 100, na.rm = TRUE)
forecast_week_7 <- tail(values, 1)

cat("Mean Absolute Error (MAE):", mae, "\n")
## Mean Absolute Error (MAE): 3.8
cat("Mean Squared Error (MSE):", mse, "\n")
## Mean Squared Error (MSE): 16.2
cat("Mean Absolute Percentage Error (MAPE):", mape, "%\n")
## Mean Absolute Percentage Error (MAPE): 27.43778 %
cat("Forecast for Week 7:", forecast_week_7, "\n")
## Forecast for Week 7: 14

Q2

library(ggplot2)
library(zoo)
library(Metrics)

# Values of Alabama building contracts for a 12-month period
contracts <- c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230)

# Create a time vector representing each month
time <- 1:length(contracts)

# Constructing the time series plot
ggplot(data.frame(Time = time, Contracts = contracts), aes(x = Time, y = Contracts)) +
  geom_line(color = "blue") +
  geom_point(color = "blue") +
  labs(title = "Alabama Building Contracts", x = "Month", y = "Contracts (in millions)") +
  theme_minimal()

# Moving Average Calculation - 3-month Moving Average
moving_avg <- rollmean(contracts, k = 3, fill = NA, align = "right")

# Exponential Smoothing Calculation with alpha = 0.2
alpha <- 0.2
exp_smooth <- rep(NA, length(contracts))  # Initialize with NA
exp_smooth[1] <- contracts[1]  # Set the first value for exponential smoothing

# Compute Exponential Smoothing for remaining values
for (i in 2:length(contracts)) {
  exp_smooth[i] <- alpha * contracts[i-1] + (1 - alpha) * exp_smooth[i-1]
}

# Align actual and predicted values properly for Moving Average (remove NAs)
actual_clean <- contracts[3:length(contracts)]
predicted_ma <- moving_avg[!is.na(moving_avg)]

# Calculate MSE for Moving Average and Exponential Smoothing
mse_ma <- mse(actual_clean, predicted_ma)
mse_es <- mse(contracts[-1], exp_smooth[-1])  # Exclude first element to align properly

# Display results in the correct format
cat("MSE Moving Average\n")
## MSE Moving Average
cat(mse_ma, "\n\n")
## 996.8
cat("MSE Exponential Smoothing\n")
## MSE Exponential Smoothing
cat(mse_es, "\n\n")
## 2593.762

Q3

library(readxl)

file_path <- file.choose()  
mortgage_data <- read_excel(file_path)

mortgage_data$Year <- as.Date(as.character(mortgage_data$Year), format="%Y")

ggplot(mortgage_data, aes(x = Year, y = Interest_Rate)) +
  geom_line(color = "blue") +
  geom_point(color = "blue") +
  labs(title = "30-Year Fixed-Rate Mortgage Interest Rates", x = "Year", y = "Interest Rate (%)") +
  theme_minimal()

Q4

mortgage_data$Time <- 1:nrow(mortgage_data)

linear_model <- lm(Interest_Rate ~ Time, data = mortgage_data)
intercept <- round(coef(linear_model)[1], 2)
slope <- round(coef(linear_model)[2], 5)

cat("Linear Trend Equation: Interest_Rate =", intercept, slope, "* Period\n")
## Linear Trend Equation: Interest_Rate = 6.7 -0.1289 * Period

Q5

forecast_period_25 <- intercept + slope * 25
cat("Forecast for Period 25 (2024):", forecast_period_25, "\n")
## Forecast for Period 25 (2024): 3.4775