#method 1 naive approach, mean absolute error, mean absolute error percentage,mean square error

week= 1:6 
values= c(17,13,15,11,17,14)

forecast_a = values [-length(values)]
actual_a = values[-1]
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=mean(abs(actual_a - forecast_a)/actual_a)*100
mape
## [1] 27.43778
forecast_week7_a = tail(values,1)
forecast_week7_a
## [1] 14

#question 2

library(dplyr) library(zoo)

df = data.frame(month = c(1,2,3,4,5,6,7,8,9,10,11,12),
                data=c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230))

#descrptive stats

summary(df)
##      month            data      
##  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

#time series plot

plot(df$month, df$data, type = "o", col="blue", xlab = "week", ylab = "Contract", main= "Alabama Contracts over a 12 month period time plot")

PART B: Exponential Smoothing Approach

df$avg_sales3 = c(NA,NA,NA,
                  (df$data[1]+df$data[2]+df$data[3])/3,
                  (df$data[2]+df$data[3]+df$data[4])/3,
                  (df$data[3]+df$data[4]+df$data[5])/3,
                  (df$data[4]+df$data[5]+df$data[6])/3,
                  (df$data[5]+df$data[6]+df$data[7])/3,
                  (df$data[6]+df$data[7]+df$data[8])/3,
                  (df$data[7]+df$data[8]+df$data[9])/3,
                  (df$data[8]+df$data[9]+df$data[10])/3,
                  (df$data[9]+df$data[10]+df$data[11])/3
)

df1 = df %>%

mutate( squared_error =ifelse(is.na(avg_sales3),NA, (data - avg_sales3)^2) )

#compute MSE

mse1= mean(df$avg_sales3,na.rm = TRUE )
mse1
## [1] 273.7037

Step 1: Construct Function (using the same variables)

alpha <- 0.2
exp_smooth <- rep(NA, length(df$data))
exp_smooth[1] <- df$data[1]

for(i in 2: length(df$data)) {
  exp_smooth[i] <-alpha * df$data[i-1] + (1-alpha) * exp_smooth[i-1]
}
mse_exp_smooth <- mean((df$data[2:12] - exp_smooth[2:12])^2)
mse_exp_smooth
## [1] 2593.762

#Output for MSE is 2593.76

Step 2: Comparison

better_method <- ifelse(mse1 < mse_exp_smooth, "Three Month Moving Average", "Exponential Smoothing")

Results

list(
  MSE_Moving_Average = mse1,
  MSE_Exponential_Smoothing = mse_exp_smooth,
  Better_Method = better_method
)
## $MSE_Moving_Average
## [1] 273.7037
## 
## $MSE_Exponential_Smoothing
## [1] 2593.762
## 
## $Better_Method
## [1] "Three Month Moving Average"

#question 3 #load libraries library(readxl) library(tidyr) library(ggplot2)

“mortgage”= read_excel(file.choose()) mortgage

summary(mortgage) plot(mortgage\(Year, mortgage\)Interest_Rate, type = “o”, col=“blue”, xlab = “Year”, ylab = “interest rate”, main= “30 year fixed-rate mortgage over a 20 year period”) model = lm(Interest_Rate ~ Period, data = mortgage) summary(model)