Single Exponential Smoothing with R

khusnul Solahuddin
3 min readFeb 24, 2021

Throwback to “moving average forecast” , it implies same weight to all of the data. Also we know, on forecasting the newest data gives us the best guide to predict. So here comes Exponential Smoothing, the most recent data will be have greater weight (decrease exponentially). The simplest method of exponential smoothing is Single Exponential Smoothing (SES). SES only handles “level” in our data (no trend and seasonal). The formula of SES(Hyndman and Athanasopoulos, 2018)

Where the lt is the level (smoothed value) of the series at the time t and alpha (α) is a smoothing parameter for level .

For the initialization l1, we can take the oldest observation (l1=y1). If alpha (α) approaches 1, smoothed value will look like the actual value. Otherwise, smaller alpha (α) (closer to) 0 gives smoother value.

here’s the R sytanx for Single Exponential Smoothing.

first, we have to install this package

install.packages(“forecast”)
library(forecast)

enter the data and split to train-test data

ses.data <- c(143, 152, 161, 139, 137, 174, 142,141, 162, 180, 164, 171)
ses.data <- data.frame(ses.data)
train_data <- data[1:9,]
test_data <- ses.data[10:12,]

change train_data to time series object and exploring

train.ts<-ts(train_data, start = 1)
plot.ts(train.ts,
col = “navyblue”,
xlab = “Minggu”,
ylab = “Permintaan”,
main = “Permintaan Something”)
points(train.ts)

for example alpha (α) = 0.3

SES.1 <- HoltWinters(train.ts, alpha = 0.3, beta = FALSE, gamma = FALSE) #beta and gamma are false cause in SES alpha is the only parameter
Xhat.1 <- SES.1$fitted[,1]
pred_1 <- predict(SES.1, n= 3) #forecast

we can make an accuracy function for Exponential smoothing/Holt

akurasi <- function(aktual, forecast, alpha, beta, gamma){
alpha = alpha
beta = beta
gamma = gamma
et = aktual — forecast
sse = sum(et², na.rm = TRUE)
mse = mean(et², na.rm = TRUE)
mad = mean(abs(et), na.rm = TRUE)
mape = 100*mean(abs(et/aktual), na.rm = TRUE)
df = data.frame(Alpha = alpha, Beta = beta, Gamma = gamma, SSE = sse, MSE = mse, MAD = mad, MAPE = mape)
return(df)
}

and the accuracy

acc_1 <- akurasi(test_data, pred_1, alpha = 0.3, beta = FALSE, gamma = FALSE)

To predict the next period (13rd data), we need to take all the data (train+test). here’s the syntax

ses.ts <- ts(ses.data, start=1)
SES.data <- HoltWinters(ses.ts, alpha = 0.3, beta = FALSE, gamma = FALSE)
Xhat.data <- SES.data$fitted[,1]
#forecast
pred_data <- predict(SES.data, n=1)
#akurasi seluruh data
acc_data <- akurasi(ses.ts, Xhat.data, alpha=0.3, beta = FALSE, gamma = FALSE)

can R gives us the optimum alpha(α)? sure!! we dont need to call it.

SES.opt <- HoltWinters(ses.ts, beta = FALSE, gamma = FALSE)
SES.opt #alpha: 0.3039054
Xhat.opt <- SES.opt$fitted[,1]
pred_opt <- predict(SES.opt, n=1)
acc_opt <- akurasi(ses.ts, Xhat.opt, alpha=0.3039054, beta = FALSE, gamma = FALSE)

The final step is plotting and analayz the result

plot.ts(ses.ts, main=”demand”, type=”l”, col=”black”, lwd = 1)
lines(Xhat.opt, type=”l”, col=”red”)
lines(pred_opt,col=”red”, lwd=2, type=”p”)
legend(“topleft”,
c(“Actual Data”,
expression(paste(alpha, “=0.3039054”))),
col=c(“black”,”red”),
cex=0.5,
lty=1,
bty = “n”)

Hope this story helps you guys!!!!

--

--