I want to make a prediction with Holt Winters
, but I make some kind of mistake.
From my dataset, which contains some 10k values, I only want those that are outside the given range 15-26. All values per week that fall outside the range I put into the data frame "out". Starting from 2nd January week 2020.
So far so good.
I have one value per week and would like to make a prediction with Holt Winters about the increase in the following weeks. But my plot looks very incomprehensible. There is no forecast to see. What am I doing wrong?
df_B2 = fread("C:/Users/B2.csv")
df_B2$Date = as.Date(df_B2$Date, "%d.%m.%y")
df_B2$Week = strftime(df_B2$Date, format = "%V")
#Limit
limit_a = 15
limit_b = 26`
out = (df_B2 %>% filter(ExtractionTimes < limit_a | ExtractionTimes > limit_b) %>% count(Week))
str(out)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables:
$ Week: chr "02" "03" "04" "05"
$ n : int 99 106 156 237
- attr(*, ".internal.selfref")=<externalptr>
out
# A tibble: 4 x 2
Week n
<chr> <int>
1 02 99
2 03 106
3 04 156
4 05 237
data = ts(out$n,start=c(2020,02), frequency = 52)
hw = HoltWinters(data, alpha=NULL, beta=FALSE, gamma=FALSE)
> p = predict (hw, n.ahead=1, level=0.95)
> plot(hw,p)
Appreciate your help.
The stats::plot.HoltWinters()
function is showing you the data (in black) and fitted values and forecasts (in red).
Since you are already using tidyverse packages, you will probably find it easier to use the tsibble
and fable
packages which fit the same model in a tidy framework.
library(dplyr, warn.conflicts=FALSE)
library(tsibble, warn.conflicts=FALSE)
library(fable, warn.conflicts=FALSE)
#> Loading required package: fabletools
out <- tibble(
Week = c("02","03","04","05"),
n = c(99,106,156,237)
) %>%
mutate(Week = yearweek(paste0("2020 W",Week))) %>%
as_tsibble(index=Week)
out
#> # A tsibble: 4 x 2 [1W]
#> Week n
#> <week> <dbl>
#> 1 2020 W02 99
#> 2 2020 W03 106
#> 3 2020 W04 156
#> 4 2020 W05 237
out %>%
model(ses = ETS(n ~ season("N"))) %>%
forecast(h = "10 weeks") %>%
autoplot(out)
Created on 2020-03-25 by the reprex package (v0.3.0)
The model is equivalent to the one you are fitting with HoltWinters()
, although the parameter estimation in ETS()
uses MLE rather than the mix of LS with ad hoc heuristic estimates that is used by HoltWinters()
.
See OTexts.com/fpp3 for a textbook on how to use these packages.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.