user113156
user113156

Reputation: 7107

Holt-Winters forecast in R

I am trying to perform a Holt-Winters forecast in R and obtain predictions on the test data but the final forecast plot looks very wrong.

Where am I going wrong, why are the predictions so wild?

Data:

data("sunspots")

data <- as.data.frame(sunspots)



smp_size <- 0.80
train_ind <- nrow(data) * smp_size

train <- data[1:train_ind, ]
test <- data[(train_ind + 1):nrow(data), ]

fit <- HoltWinters(train, gamma=FALSE)

plot(forecast(fit, h = length(test)))

Upvotes: 0

Views: 252

Answers (1)

Sada93
Sada93

Reputation: 2835

The sunspots data is actually a time series data which means that it has a period associated with it. If we use as.data.frame this converts it into a vector and information is lost. Hence, we keep this time series data, subset it and forecast.

Also, HoltWinters() requires a timeseries dataset as an input.

data("sunspots")

data <- sunspots


smp_size <- 0.80
train_ind <- length(data)/12 * smp_size
train = window(data,start = 1749, end = c(1749+train_ind,12))
test = window(data,start = 1749+train_ind+1,end = c(1749+length(data)/12,12))

fit <- HoltWinters(train)

plot(forecast(fit,h = length(test)))
lines(test)

Upvotes: 2

Related Questions