week <- c(1,2,3,4,5,6)
value <- c(17,13,15,11,17,14)
# by hand calculation, I got MAE = 3.8
# by hand calculation, I got MSE = 16.2
# by hand calculation, I got MAPE = 27.44%
# forecast for week 7 = actual value of week 6 = 14
df1 <- data.frame(month=c("Jan", "Feb","Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov","Dec"),
period=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))
summary(df1)
## month period values
## Length:12 Min. : 1.00 Min. :220.0
## Class :character 1st Qu.: 3.75 1st Qu.:237.5
## Mode :character 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
# install. packages("ggplot2")
library(ggplot2)
ggplot(df1, aes(x = period, y = values)) +
geom_line() +
geom_point() +
xlab("Month") +
ylab("Values") +
ggtitle("Time Series Plot of Alabama building contracts")
# B. Compare the three-month moving average approach with the
exponential smoothing forecast using alpha = 0.2. # Input the data:
alpha = 0.2
#{r error} df1$av <- c(NA, NA, NA, (df$values[1] + df$values[2] + df$values[3]) / 3, (df$values[2] + df$values[3] + df$values[4]) / 3, (df$values[3] + df$values[4] + df$values[5]) / 3, (df$values[4] + df$values[5] + df$values[6]) / 3, (df$values[5] + df$values[6] + df$values[7]) / 3, (df$values[6] + df$values[7] + df$values[8]) / 3, (df$values[7] + df$values[8] + df$values[9]) / 3 )
# Calculate the square errors (only for weeks were moving average is
available)
#{r} df1 <- df1 %>% mutate( square_error = ifelse(is.na(av), NA, (values - av)^2) )
# Compute MSE (excluding the initial weeks with NA)
#{r} mse <- mean(df1$square_error, na.rm = TRUE) mse
#```{r} exp_smooth <- rep(NA, length(df1\(values)) exp_smooth[1] <- df1\)values[1] #Starting point for(i in 2: length(df\(values)) { exp_smooth[i] <- alpha * df1\)values[i-1] + (1 - alpha) * exp_smooth[i-1] } mse_exp_smooth <- mean((df1$values[1:12] - exp_smooth[1:12])^2) mse_exp_smooth #Output the MSE
better_method <- ifelse(mse < mse_exp_smooth, “Three-Month Moving Average”, “Exponential Smoothing”)
list( MSE_Moving_Average = mse, MSE_Exponential_Smoothing = mse_exp_smooth, Better_Method = better_method
### Question 3, 4, 5:
``` r
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.2
data <- read_excel(file.choose())
ggplot(data, aes(x = Period, y = Interest_Rate)) +
geom_line() +
geom_point() +
xlab("Period") +
ylab("Interest_Rate") +
ggtitle("Time Series Plot of FreddieMac website")
# B. Develop the linear trend equation
# Develop a 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