Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

r - How to build Exponential Smoothing Model

My data :

library(forecast)
library(Mcomp)

# Time Series
# Subset the M3 data to contain the relevant series 
ts.data<- subset(M3, 12)[[551]]
print(ts.data)

I have selected time series 551 of the monthly data of the M3 competition.

I want to build exponential smoothing model (ETS) and then calculate the in-sample error and out-of-sample error of the model.

How can i get through this ? Any help?

EDITED !

My code :

# Exponential Smoothing Model

library(forecast)
library(Mcomp)

# My data is
# Time Series
# Subset the M3 data to contain the relevant series 
ts.data<- subset(M3, 12)[[551]]
print(ts.data)


library(tidyverse)
library(fpp2) 

# Holt’s Method

# create training and validation 
# of the data 
data.train <- window(???) 
data.test <- window(???)

holt.data <- holt(data.train, 
                  h = 100) 
autoplot(holt.data)

# holt's method 
holt.data$model 

# accuracy of the model 
accuracy(holt.data, data.test)

# try to find the optimal value of beta through a loop ranging from 0.0001 to 0.5 that will minimize the RMSE test
# identify optimal alpha parameter 
beta <- seq(.0001, .5, by = .001) 
RMSE <- NA
for(i in seq_along(beta)) { 
  fit <- holt(data.train, 
              beta = beta[i],  
              h = 100) 
  RMSE[i] <- accuracy(fit,  
                      data.test)[2,2] 
} 

# convert to a data frame and 
# idenitify min alpha value 
beta.fit <- data_frame(beta, RMSE) 
beta.min <- filter(beta.fit,  
                   RMSE == min(RMSE))

# plot RMSE vs. alpha 
ggplot(beta.fit, aes(beta, RMSE)) + 
  geom_line() + 
  geom_point(data = beta.min,  
             aes(beta, RMSE),  
             size = 2, color = "red")


# Refit the model with the obtained optimal value of beta :
# Set the optimal value of beta nad also compare the predictive accuracy with our original model.

# new model with optimal beta 
holt.data.opt <- holt(data.train, 
                      h = 100, 
                      beta = 0.0601) 

# accuracy of first model 
accuracy(holt.data, data.test) 

# accuracy of new optimal model 
accuracy(holt.data.opt, data.test) 

p1 <- autoplot(holt.data) + 
  ggtitle("Original Holt's Model") + 
  coord_cartesian(ylim = c(400, 1000)) 

p2 <- autoplot(holt.data.opt) + 
  ggtitle("Optimal Holt's Model") + 
  coord_cartesian(ylim = c(400, 1000)) 

gridExtra::grid.arrange(p1, p2,  
                        nrow = 1)

My problem is that i cant create my data.train and data.test samples.

data.train <- window(???) 
data.test <- window(???)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From this good stack exchange answer you could do :

library(forecast)
library(Mcomp)

# Time Series
# Subset the M3 data to contain the relevant series 
ts.data<- subset(M3, 12)[[551]]
ts.data

mod1 <- HoltWinters(ts.data$x, alpha=0.1, beta=FALSE, gamma=FALSE)
pred <- predict(mod1, n.ahead=30)

abs_error <- abs(pred - ts.data$xx)
mae <- sum(abs_error)/30
mae

# with forcats
mod2 <- forecast::ses(ts.data$x, h=30, alpha=0.1, initial="simple")
pred2 <- predict(mod2, n.ahead=30)

abs_error2 <- abs(pred2$mean - ts.data$xx)
mae2 <- sum(abs_error2)/30
mae2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...