Question 1: Using Naive Method(Most recent value)
Step 1: Enter Data
# Time Series Data
week <- 1:6 # Independent variable - time
value <- c(17, 13, 15, 11, 17, 14) # Dependent Variable
Step 2: Set up actual and forecast data
forecast <- value[-length(value)] # Excludes the last value
actual <- value[-1] # Exclude the first value
Step 3: Mean absolute error
error <- actual - forecast
mae <- mean(abs(error))
mae #Mean absolute error is 3.8
## [1] 3.8
Step 4: Mean squared error
mse <- mean((actual - forecast)^2)
mse #Mean squared error is 16.2
## [1] 16.2
Step 5: Mean absolute percentage error
mape <- mean(abs((actual - forecast)/ actual)) * 100
mape #Mean absolute percentage error is 27.44%
## [1] 27.43778
Step 6: Forecast for week 7
forecast_week11_a <- tail(value, 1)
forecast_week11_a
## [1] 14
# Interpretation: The projected number for week 7 is 14
Question 2: Smoothing Approach
Step 1: Install and load packages
# install.packages("dplyr")
# install.packages("zoo")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(zoo)
## Warning: package 'zoo' was built under R version 4.4.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
Step 2: Enter Data
df <- data.frame(week=c(1,2,3,4,5,6,7,8,9,10,11,12),
contract=c(240,352,230,260,280,322,220,310,240,310,240,230))
Step 3: Time Series Plot
plot(df$week, df$contract, type = "o", col = "blue", xlab = "Week", ylab = "Contracts in millions", main = "Alabama Building Contract Plot")

# Interpretation: The plot exhibits a fluctuating pattern as it jumps above and below the mean.
Step 4: Three Month Average
df$avg <- c(NA, NA, NA,
(df$contract[1] + df$contract[2] + df$contract[3])/ 3,
(df$contract[2] + df$contract[3] + df$contract[4])/ 3,
(df$contract[3] + df$contract[4] + df$contract[5])/ 3,
(df$contract[4] + df$contract[5] + df$contract[6])/ 3,
(df$contract[5] + df$contract[6] + df$contract[7])/ 3,
(df$contract[6] + df$contract[7] + df$contract[8])/ 3,
(df$contract[7] + df$contract[8] + df$contract[9])/ 3,
(df$contract[8] + df$contract[9] + df$contract[10])/ 3,
(df$contract[9] + df$contract[10] + df$contract[11])/ 3
)
Step 5: Calculate the squared errors (only for weeks were moving
average is available)
df <- df %>%
mutate(squared_error = ifelse(is.na(avg), NA, (contract - avg)^2))
Step 6: Compute MSE (excluding the initial weeks with NA)
mse <- mean(df$squared_error, na.rm = TRUE)
mse #Output the MSE - 2040.44
## [1] 2040.444
Step 7: Exponential Smoothing using alpha = 0.2
# Exponential Smoothing
alpha <- 0.2
exp_smooth <- rep(NA, length(df$contract))
exp_smooth[1] <- df$contract[1] # Starting point
for(i in 2: length(df$contract)) {
exp_smooth[i] <- alpha * df$contract[i-1] + (1-alpha) * exp_smooth[i-1]}
Step 8: Compute MSE (excluding the initial weeks with NA)
mse_exp_smooth <- mean((df$contract[2:12] - exp_smooth[2:12])^2)
mse_exp_smooth #Output the MSE - 2593.76
## [1] 2593.762
Step 9: Compare
# MSE for three month moving average is 2040.44
# MSE for exponential smoothing using alpha 0.2 = 2593.76
# Exponential smoothing provides more accurate forecasts based on MSE
Question 3: Linear Trend Approach
Step 1: Install and load packages
# install.packages("ggplot2")
# install.packages("readxl")
# install.packages("dplyr")
library(ggplot2)
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.2
library(dplyr)
Step 2: Import and Load data
mortgage_data <- read_excel(file.choose())
Step 3: Construct Time Series Plot
data <- subset(mortgage_data, select = -c(Year)) # eliminates Year column
ggplot(data, aes(x = Period, y = Interest_Rate)) +
geom_line() +
geom_point() +
xlab("Period") +
ylab("Interest Rate") +
ggtitle("Time Series Plot of Fixed-Rate Mortgage")

# Interpretation: Decreasing pattern or trend then started increasing when it entered period 23
Step 4: Develop Linear trend equation
model <- lm(Interest_Rate ~ Period, data = data)
summary(model)
##
## Call:
## lm(formula = Interest_Rate ~ Period, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.3622 -0.7212 -0.2823 0.5015 3.1847
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.69541 0.43776 15.295 3.32e-13 ***
## Period -0.12890 0.03064 -4.207 0.000364 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.039 on 22 degrees of freedom
## Multiple R-squared: 0.4459, Adjusted R-squared: 0.4207
## F-statistic: 17.7 on 1 and 22 DF, p-value: 0.0003637
# Result = Interest Rate = 6.70 - 0.13*Period or
# T_hat = 6.70 - 0.13*t
# R-squared is 0.45 (Moderate fit)
# Overall model is significant as p-value < 0.05
Step 5: Forecast period 25
forecast_period25 <- predict(model, newdata = data.frame(Period = 25))
forecast_period25
## 1
## 3.472942
# Interpretation: The forecasted number for interest rate in 2024 is ~3.47%