R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot. # Load packages install.packages(“forecast”) library(forecast) library(ggplot2)

Question 1

actual <- c(17, 13, 15, 11, 17, 14) forecast <- actual[length(actual)]

a. mean absolute error (MAE)

mae <- mean(abs(actual - forecast))

b. MSE

mse <- mean((actual - forecast)^2)

c. Calculate MAPE

mape <- mean(abs(actual - forecast) / actual) * 100

Question 2

building_contracts <- c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230) # Time series object ts_building_contracts <- ts(building_contracts, start = 1, end = 12, frequency = 1) # Plotting the time series plot(ts_building_contracts, type = “o”, col = “blue”, xlab = “Month”, ylab = “Building Contracts ($ millions)”, main = “Alabama Building Contracts”) # Calculate the three month moving average

building_contracts <- c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230) moving_avg_forecast <- mean(building_contracts[10:12])

Calculate the smoothing average with alpha 0.2

exp_smooth <- HoltWinters(building_contracts, alpha = 0.2, beta = FALSE, gamma = FALSE) exp_forecast <- predict(exp_smooth, n.ahead = 1) exp_forecast_value <- exp_forecast[length(exp_forecast)] exp_forecast_value <- exp_forecast[length(exp_forecast)] print(paste(“Forecast using exponential smoothing (alpha = 0.2):”, round(exp_forecast_value, 2))) print(paste(“Forecast using three-month moving average:”, round(moving_avg_forecast, 2)))

Question 3

Construct a time series plot

data <- data.frame( Year_Period = as.Date(c(“2000-01-01”, “2001-01-01”, “2002-01-01”, “2003-01-01”, “2004-01-01”, “2005-01-01”, “2006-01-01”, “2007-01-01”, “2008-01-01”, “2009-01-01”, “2010-01-01”, “2011-01-01”, “2012-01-01”, “2013-01-01”, “2014-01-01”, “2015-01-01”, “2016-01-01”, “2017-01-01”, “2018-01-01”, “2019-01-01”, “2020-01-01”, “2021-01-01”, “2022-01-01”, “2023-01-01”)), Interest_Rate = c(8.05, 6.97, 6.54, 5.83, 5.84, 5.87, 6.41, 6.34, 6.03, 5.04, 4.69, 4.45, 3.66, 3.98, 4.17, 3.85, 3.65, 3.99, 4.54, 3.94, 3.11, 2.96, 5.34, 6.79) ) ggplot(data, aes(x = Year_Period, y = Interest_Rate)) + geom_line(color = “blue”) + labs(title = “Interest Rate Over Time”, x = “Year Period”, y = “Interest Rate (%)”) + theme_minimal()

data <- data.frame( Year_Period = 1:24, # Time period Interest_Rate = c(8.05, 6.97, 6.54, 5.83, 5.84, 5.87, 6.41, 6.34, 6.03, 5.04, 4.69, 4.45, 3.66, 3.98, 4.17, 3.85, 3.65, 3.99, 4.54, 3.94, 3.11, 2.96, 5.34, 6.79) )

Question 4

develop the linear trend equation for the time series

lm_model <- lm(Interest_Rate ~ Year_Period, data = data) intercept <- coef(lm_model)[1] slope <- coef(lm_model)[2]
cat(“Linear trend equation: Y =”, round(intercept, 2), “+”, round(slope, 2), ” * t”)

period_25 <- intercept + slope * 25

Question 5

Use the linear trend equation

cat(“Forecasted average interest rate for period 25 (2024):”, round(period_25, 2))