Question 1: Naive Approach
Use naive approach to forecast data and calculate errors
Import the data
week <- 1:6
value <- c(17, 13, 15, 11, 17, 14)
Calculate forecasted data
forecast_a <- value[-length(value)]
actual_a <- value[-1]
Calculate Errors
mae_a <- mean(abs(actual_a-forecast_a))
mae_a
## [1] 3.8
mse_a <- mean((actual_a-forecast_a)^2)
mse_a
## [1] 16.2
mape_a <- mean((abs(actual_a-forecast_a))/actual_a)*100
mape_a
## [1] 27.43778
Forecast for Week 7
fc_wk7_a <- tail(value,1)
fc_wk7_a
## [1] 14
Question 2: Time Series Plot and Compare forecast
methods
Create a time series plot and compare a three month moving average to exponential smoothing
Load packages
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.2
##
## 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
Import the data
AL_building_data <- data.frame(month=c(1,2,3,4,5,6,7,8,9,10,11,12),values=c(240,352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230))
Descriptive statistics
summary(AL_building_data)
## month values
## Min. : 1.00 Min. :220.0
## 1st Qu.: 3.75 1st Qu.:237.5
## Median : 6.50 Median :250.0
## Mean : 6.50 Mean :269.5
## 3rd Qu.: 9.25 3rd Qu.:310.0
## Max. :12.00 Max. :352.0
Interpretation: Average value over the 12 months is $269.5 million
Make the Time Series Plot
plot(AL_building_data$month, AL_building_data$values, type = "o", col = "blue", xlab = "Month", ylab = "Value",
main = "Alabama Building Contracts Values")

Interpretation: The time series plot exhibits a horizontal pattern as it is steady around the mean.
Calculate three month moving average
AL_building_data$avg_value3 <- c(NA, NA, NA,
(AL_building_data$values[1]+AL_building_data$values[2]+AL_building_data$values[3])/3,
(AL_building_data$values[2]+AL_building_data$values[3]+AL_building_data$values[4])/3,
(AL_building_data$values[3]+AL_building_data$values[4]+AL_building_data$values[5])/3,
(AL_building_data$values[4]+AL_building_data$values[5]+AL_building_data$values[6])/3,
(AL_building_data$values[5]+AL_building_data$values[6]+AL_building_data$values[7])/3,
(AL_building_data$values[6]+AL_building_data$values[7]+AL_building_data$values[8])/3,
(AL_building_data$values[7]+AL_building_data$values[8]+AL_building_data$values[9])/3,
(AL_building_data$values[8]+AL_building_data$values[9]+AL_building_data$values[10])/3,
(AL_building_data$values[9]+AL_building_data$values[10]+AL_building_data$values[11])/3)
Calculate the squared errors and MSE
AL_building_data <- AL_building_data %>%
mutate(
squared_error = ifelse(is.na(avg_value3), NA, (values-avg_value3)^2))
mse_3ma <- mean(AL_building_data$squared_error, na.rm = TRUE)
mse_3ma
## [1] 2040.444
Calculate Exponential Smoothing and its MSE
alpha <- 0.2
exp_smooth <- rep(NA, length(AL_building_data$values))
exp_smooth[1] <- AL_building_data$values[1]
for(i in 2: length(AL_building_data$values)) {
exp_smooth[i] <- alpha * AL_building_data$values[i-1] + (1 - alpha) * exp_smooth[i-1]
}
mse_expsm <- mean((AL_building_data$values[2:12] - exp_smooth[2:12])^2)
mse_expsm
## [1] 2593.762
Question 3: Time series plot and linear trend
Create a time series plot and find a linear trend equation
Load the packages
library(ggplot2)
library(readxl)
Import the data
mortgage_df <- read_excel(file.choose())
Do Descriptive Statistics
summary(mortgage_df)
## Year Period Interest_Rate
## Min. :2000-01-01 00:00:00 Min. : 1.00 Min. :2.958
## 1st Qu.:2005-10-01 18:00:00 1st Qu.: 6.75 1st Qu.:3.966
## Median :2011-07-02 12:00:00 Median :12.50 Median :4.863
## Mean :2011-07-02 18:00:00 Mean :12.50 Mean :5.084
## 3rd Qu.:2017-04-02 06:00:00 3rd Qu.:18.25 3rd Qu.:6.105
## Max. :2023-01-01 00:00:00 Max. :24.00 Max. :8.053
InterpretationThe average interest rate for a mortgage from 2000 to 2023 is 5.08%
Construct a time series plot
ggplot(mortgage_df, aes(x = Period, y = Interest_Rate)) +
geom_line() +
geom_point() +
xlab("Period") +
ylab("Interest Rate") +
ggtitle("Time Series Plot of Mortgage Interest Rate")

Interpretation: There is a decreasing pattern/trend in the plot
Develop a linear trend equation
model <- lm(Interest_Rate ~ Period, data = mortgage_df)
summary(model)
##
## Call:
## lm(formula = Interest_Rate ~ Period, data = mortgage_df)
##
## 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 - estimated linear trend equation: interest rate = 6.70 - 0.13*period
Forecast interest rate for period 25
forecast_period25 <- predict(model, newdata = data.frame(Period = 25))
forecast_period25
## 1
## 3.472942