Hello
I have the code showing me the Alpha, Beta and Gamma (ABG) as output.
It calculates the ABG for the whole time series in table sales (excel file)
Smoothing parameters:
alpha: 0.3636345
beta : 0.2096302
gamma: 0.4300818
Problem: I can't seem to get the right code to show a moving alpha ,beta and gamma in a table. I need to see a moving alpha ,beta and gamma calculated over every 24 past observations.
I know that the Holtwinters or ETS show a fitted model with one ABG for the whole times series.But I need to see table like the below. Anyone who knows the code for that? Ans advise me on the errors i get. I am kinda stuck until i have that table...
Example how I envision that table:
Day Sales α accurate δ accurate γ accurate expected α expected δ expected γ Forecast sales MSE
Febr 3
March 8 0.142208382 0.094449719 0.235930509 4 4.999062106
April 1 0.142208382 0.094449719 0.235930509 0.069173091 0.10798745 0.209918475 2 0.193212972
May 3 0.069173091 0.10798745 0.209918475 0.069173091 0.10798745 0.209918475 3 1.473636406
Jun 5 0.069173091 0.10798745 0.209918475 0.145309691 0.125029156 0.15381477 4 4.509703765
July 8 0.145309691 0.125029156 0.15381477 0.145309691 0.125029156 0.15381477 5 7.978798308
Aug 3 0.145309691 0.125029156 0.15381477 0.116138478 0.113240008 0.130951664 6 13.46953186
Sept 7 0.116138478 0.113240008 0.130951664 0.120159054 0.107930279 0.132951374 4 6.430041868
Oct 5 0.120159054 0.107930279 0.132951374
Next to this , when I run my code , I get the following errors:
#error 1
Error: Objects of type HoltWinters not supported by autolayer.
#error 2
sales1 <- sqrt(salesLogHW$dispersion)
Error in sqrt(salesLogHW$dispersion) :
non-numeric argument to mathematical function
dnorm(7, mean = coef(m1), sd = sdest)
Error in coef(m1) : object 'm1' not found
#Error 3
ets(x = salesLog)
Error in class(y) %in% c("data.frame", "list", "matrix", "mts") :
argument "y" is missing, with no default
Error 4
Classes‘tbl_df’,‘tbl’ and 'data.frame': 341 obs. of 2 variables:
Error: unexpected input in "Classes‘"
$ month: Date, format: "1982-06-12" "1982-06-19" ...
Error: unexpected '' in " "
$ sales: num 14237 4520 55691 28295 23648 ...
Error: unexpected '' in ""
- attr(*, "spec")=
Error: unexpected '' in "- attr("
.. cols(
Error: unexpected symbol in " .. cols"
.. month = col_date(format = ""),
Error: unexpected symbol in " .. month"
.. sales = col_double()
Error: unexpected symbol in " .. sales"
.. )
Error: unexpected ')' in " .. )"
options(repr.plot.width = 6, repr.plot.height = 3)
ggplot(sales, aes(x = month, y = sales)) + geom_line() + geom_smooth(method = 'lm') +labs(x = "Time", y = "Monthly Sales")
Error in FUN(X[[i]], ...) : object 'month' not found
Current Code looks like this:
load needed packages
library(readr)
library(dplyr)
library(ggplot2)
library(forecast)
library(ggplot2)
import data
sales <- read_csv("E:\Forecast calcs\R Studio\Library R\Sales")
explore data
str(sales)
cx <- c(0, cumsum(ifelse(is.na(x), 0, x)))
cn <- c(0, cumsum(ifelse(is.na(x), 0, 1)))
rx <- cx[(2+1):length(cx)] - cx[1:(length(cx) - 2)]
rn <- cn[(2+1):length(cx)] - cn[1:(length(cx) - 2)]
rsum <- rx / rn
head(sales, n = 5)
Classes‘tbl_df’,‘tbl’ and 'data.frame': 341 obs. of 2 variables:
$ month: Date, format: "1982-06-12" "1982-06-19" ...
$ sales: num 14237 4520 55691 28295 23648 ...
- attr(*, "spec")=
.. cols(
.. month = col_date(format = ""),
.. sales = col_double()
.. )
options(repr.plot.width = 6, repr.plot.height = 3)
ggplot(sales, aes(x = month, y = sales)) + geom_line() + geom_smooth(method = 'lm') +labs(x = "Time", y = "Monthly Sales")
salesTS <- ts(sales$sales, frequency = 4, start = c(1982,1))
class(salesTS)
'ts'
options(repr.plot.width = 6, repr.plot.height = 5)
salesDecomp <- decompose(salesTS)
autoplot(salesDecomp)
logging transform time series data
salesLog <- log(salesTS)
salesLogHW <- ets(salesLog)
salesLogHW
ets(x = salesLog)
sales1 <- sqrt(salesLogHW$dispersion)
dnorm(7, mean = coef(m1), sd = sdest)
salesLogHW <- HoltWinters(salesLog)
salesLogHW
HoltWinters(x = salesLog)
options(repr.plot.width = 6, repr.plot.height = 4)
autolayer(salesLogHW)
forecast next year's sales
nextYearSales <- forecast(salesLogHW, h=4)
plot
autolayer(nextYearSales)
nextYearSales
An extract from my excel file :
Month | sales |
---|---|
january | 30 |
february | 64 |
march | 98 |
april | 60 |
may | 50 |
june | 30 |
july | 20 |
august | 25 |
september | 24 |
october | 49 |
november | 52 |
december | 21 |
january | 51 |
february | 51 |
march | 21 |
april | 30 |
may | 64 |
june | 98 |
july | 60 |
august | 50 |
september | 30 |
october | 20 |
november | 25 |
december | 24 |
january | 49 |
february | 52 |
march | 21 |
april | 51 |
may | 51 |
june | 21 |
july | 30 |
august | 64 |
september | 98 |
october | 60 |
november | 50 |
december | 30 |
january | 20 |
february | 25 |
march | 24 |
april | 49 |
may | 52 |
june | 21 |
july | 51 |
august | 51 |
september | 21 |
october | 30 |
november | 64 |
december | 98 |
january | 60 |
february | 50 |
march | 30 |
april | 20 |
may | 25 |
june | 24 |
july | 49 |
august | 52 |
september | 21 |
october | 51 |
november | 51 |
december | 21 |
january | 30 |
february | 64 |
march | 98 |
april | 60 |
may | 50 |
june | 30 |
july | 20 |
august | 25 |
september | 24 |
october | 49 |
november | 52 |
december | 21 |
january | 51 |
february | 51 |
march | 21 |
april | 30 |
may | 64 |
june | 98 |
july | 60 |
august | 50 |
september | 30 |
october | 20 |
november | 25 |
december | 24 |
january | 49 |
february | 52 |
march | 21 |
april | 51 |
may | 51 |