Question 1: Naive Method

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

errors <- values[-1] - forecasts[-1]  # Remove the initial NA

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

Question 2A: Time Series Plot

library(ggplot2)
library(zoo)

contracts <- c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230)

time <- 1:length(contracts)

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

Question 2B: Moving Average vs. Exponential Smoothing

# Load Metrics package for error calculations
library(Metrics)

# 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]
}

# Clean data for Moving Average (removing NA values)
actual_clean <- contracts[3:length(contracts)]
predicted_ma <- moving_avg[!is.na(moving_avg)]  # Use all available values of moving_avg without NA

# Calculate MSE for Moving Average and Exponential Smoothing
mse_ma <- mse(actual_clean, predicted_ma)
mse_es <- mse(contracts, exp_smooth)

# Display results in the format shown in your screenshot
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")
## 2377.615
# Determine which method has the lower MSE
better_method <- ifelse(mse_ma < mse_es, "Three-Month Moving Average", "Exponential Smoothing")
cat("Better_method\n")
## Better_method
cat(better_method, "\n")
## Three-Month Moving Average

Question 3: Mortgage Data Time Series Plot

library(readxl)

# Prompt to choose Mortgage.xlsx file
file_path <- file.choose()  
mortgage_data <- read_excel(file_path)

# Convert the Year column to Date type if applicable
mortgage_data$Year <- as.Date(as.character(mortgage_data$Year), format="%Y")

# Constructing the time series plot
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()

Question 4: Linear Trend Equation

# Adding Time as a sequence for linear regression
mortgage_data$Time <- 1:nrow(mortgage_data)

# Linear regression model for Interest Rate over Time
linear_model <- lm(Interest_Rate ~ Time, data = mortgage_data)
intercept <- round(coef(linear_model)[1], 2)
slope <- round(coef(linear_model)[2], 5)

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

Question 5: Forecast for Period 25

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

Summary of Changes:

  • Corrected data alignment for the Moving Average calculation to ensure actual and predicted values match properly.
  • Removed incorrect na.rm argument from mse() calculation.
  • Added clear formatting to the output to match the expected display from your provided screenshots.