Load necessary libraries

library(tidyverse) library(zoo)

Load the data

data <- read.csv(“C:/Users/asus/Downloads/ApplianceShipments.csv”)

Convert the data to a time series object

ts_data <- ts(data$Shipments, start = c(1985, 1), frequency = 4)

Split the data into training and validation sets

train_data <- window(ts_data, end = c(1988, 4)) validation_data <- window(ts_data, start = c(1989, 1))

Apply moving average with window span w = 4

ma4 <- rollmean(train_data, k = 4, align = “right”)

Create a time plot of the moving average series

plot(train_data, main = “Moving Average (w=4) of Appliance Shipments”, ylab = “Shipments (in 000s)”, xlab = “Time”) lines(ma4, col = “blue”)

Forecast appliance sales in Q1-1990

forecast_q1_1990 <- tail(ma4, 1) forecast_q1_1990

Forecast appliance sales in Q1-1991

forecast_q1_1991 <- mean(tail(ma4, 4)) forecast_q1_1991

Apply Holt-Winter’s exponential smoothing

hw_model <- HoltWinters(train_data)

Compute MAPE for the validation data using Holt–Winter’s exponential smoothing

pred_hw <- predict(hw_model, n.ahead = length(validation_data)) MAPE_hw <- mean(abs((validation_data - pred_hw) / validation_data)) * 100 MAPE_hw

Fit a regression model with linear trend and quarterly seasonality

time_index <- as.numeric(time(train_data)) seasonal_factor <- factor(cycle(train_data))

reg_model <- lm(train_data ~ time_index + seasonal_factor)

Create a new data frame for prediction with consistent factor levels

new_data <- data.frame( time_index = as.numeric(time(validation_data)), seasonal_factor = factor(cycle(validation_data), levels = levels(seasonal_factor)) )

Predict using the regression model

pred_reg <- predict(reg_model, newdata = new_data)

Compute MAPE for the validation data using the regression model

MAPE_reg <- mean(abs((validation_data - pred_reg) / validation_data)) * 100 MAPE_reg