Hi Matt,
I have an alpha,beta and gamma calculation in my code which calculates alpha,beta and gamma over the whole time series.
if i want to have an moving alpha , beta and gamma (ABG) calculated over every former 20 observations and have that i my matrix ,how can i rewrite that piece of code ?
I hope i have provided you enough information.
I know i am a beginner, so i have lots to learn 
as you now it's connected to MonthlySales1.xls
the lay out of MonthlySales1xls looks like this:
Month sales alpha beta gamma
2019-01-02 13
2019-01-05 4
2019-01-09 1
2019-01-12 10
2019-01-16 4
2019-01-19 2
2019-01-23 3
2019-01-26 3
2019-01-30 3
2019-02-02 14
2019-02-06 1
2019-02-09 1
2019-02-13 5
2019-02-16 3
2019-02-20 21
2019-02-23 5
2019-02-27 4
2019-03-02 4
2019-03-06 1
2019-03-09 22 ? / ? / ? (abg of former 20 obervations)
2019-03-13 4 ? / ? / ? (abg of former 20 obervations)
2019-03-16 9 ? / ? / ? (abg of former 20 obervations)
2019-03-20 2 ? / ? / ? (abg of former 20 obervations)
2019-03-23 3 etc.etc.
2019-03-27 16
2019-03-30 7
2019-04-03 12
2019-04-06 58
2019-04-10 10
2019-04-13 4
2019-04-17 10
2019-04-20 11
2019-04-24 6
2019-04-27 5
2019-05-01 23
2019-05-04 3
2019-05-08 58
2019-05-11 3
2019-05-15 5
2019-05-18 7
2019-05-22 13
2019-05-25 12
2019-05-29 4
2019-06-01 16
2019-06-05 6
2019-06-08 9
2019-06-12 22
2019-06-15 10
2019-06-19 21
2019-06-22 69
2019-06-26 4
2019-06-29 2
2019-07-03 13
2019-07-06 10
2019-07-10 7
2019-07-13 5
2019-07-17 21
2019-07-20 4
2019-07-24 2
2019-07-27 1
2019-07-31 2
2019-08-03 4
2019-08-07 5
2019-08-10 2
2019-08-14 69
2019-08-17 4
2019-08-21 2
2019-08-24 3
2019-08-28 1
2019-08-31 2
2019-09-04 1
2019-09-07 78
2019-09-11 2
2019-09-14 6
2019-09-18 3
2019-09-21 1
2019-09-25 16
2019-09-28 1
2019-10-02 20
2019-10-05 14
2019-10-09 5
2019-10-12 78
2019-10-16 2
2019-10-19 2
2019-10-23 11
2019-10-26 4
2019-10-30 23
2019-11-02 8
2019-11-06 6
my code looks like this:
load needed packages
library(readr)
library(dplyr)
library(ggplot2)
library(forecast)
library(readxl)
library(zoo)
import data
MonthlySales1 <- read_excel("C:/Users/jenny/Desktop/R Program/Forecast calcs/MonthlySales1.xls")
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': 96 obs. of 9 variables:
month: Date, format: "2019-06-12" "2019-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(2019,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)
sales <- sqrt(salesLogHW$dispersion)
dnorm(7, mean = coef(m1), sd = sdest)
salesLogHW <- HoltWinters(salesLog)
salesLogHW
HoltWinters(x = salesLog)
MonthlySales1 <- read_excel("C:/Users/jenny/Desktop/R Program/Forecast calcs/MonthlySales1.xls")
MonthlySales1$sales.mean<- rollmean(MonthlySales1$sales, k = 2, fill = 1, align = "right")
MonthlySales1
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
output:
library(dplyr)
load needed packages
library(readr)
library(dplyr)
library(ggplot2)
library(forecast)
library(readxl)
library(zoo)
import data
MonthlySales1 <- read_excel("C:/Users/jenny/Desktop/R Program/Forecast calcs/MonthlySales1.xls")
explore data
str(sales)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 40 obs. of 3 variables:
sales : num 6 5 18 8 43 61 95 139 45 88 ...
return : num 2 3 4 5 8 1 16 19 6 34 ...
$ AlphaHW: num 1 2 6 3 3 7 2 49 49 73 ...
- attr(*, "spec")=
.. cols(
.. sales = col_double(),
.. sales = col_double(),
.. return = col_double()
.. )
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)
A tibble: 5 x 3
sales return AlphaHW
1 6 2 1
2 5 3 2
3 18 4 6
4 8 5 3
5 43 8 3
Classes'tbl_df','tbl' and 'data.frame': 96 obs. of 9 variables:
Error: unexpected string constant in "Classes'tbl_df'"
month: Date, format: "2019-06-12" "2019-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
salesTS <- ts(sales$sales, frequency = 4, start = c(2019,1))
class(salesTS)
[1] "ts"
'ts'
[1] "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(M,Ad,N)
Call:
ets(y = salesLog)
Smoothing parameters:
alpha = 0.1434
beta = 1e-04
phi = 0.8008
Initial states:
l = 1.4663
b = 0.7245
sigma: 0.3113
AIC AICc BIC
166.4996 169.0450 176.6328
ets(x = salesLog)
Error in class(y) %in% c("data.frame", "list", "matrix", "mts") :
argument "y" is missing, with no default
sales <- 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
salesLogHW <- HoltWinters(salesLog)
salesLogHW
Holt-Winters exponential smoothing with trend and additive seasonal component.
Call:
HoltWinters(x = salesLog)
Smoothing parameters:
alpha: 0.3636345
beta : 0.2096302
gamma: 0.4300818
Coefficients:
[,1]
a 3.8519386
b -0.1149221
s1 -0.6216912
s2 -0.3063579
s3 -1.0720958
s4 -0.3704739
HoltWinters(x = salesLog)
Holt-Winters exponential smoothing with trend and additive seasonal component.
Call:
HoltWinters(x = salesLog)
Smoothing parameters:
alpha: 0.3636345
beta : 0.2096302
gamma: 0.4300818
Coefficients:
[,1]
a 3.8519386
b -0.1149221
s1 -0.6216912
s2 -0.3063579
s3 -1.0720958
s4 -0.3704739
MonthlySales1 <- read_excel("C:/Users/jenny/Desktop/R Program/Forecast calcs/MonthlySales1.xls")
MonthlySales1$sales.mean<- rollmean(MonthlySales1$sales, k = 2, fill = 1, align = "right")
MonthlySales1
A tibble: 95 x 9
Month sales Forecast fc alpha
fc beta
1 2019-01-02 00:00:00 13 NA NA NA
2 2019-01-05 00:00:00 4 NA NA NA
3 2019-01-09 00:00:00 1 NA NA NA
4 2019-01-12 00:00:00 10 NA NA NA
5 2019-01-16 00:00:00 4 NA NA NA
6 2019-01-19 00:00:00 2 NA NA NA
7 2019-01-23 00:00:00 3 NA NA NA
8 2019-01-26 00:00:00 3 NA NA NA
9 2019-01-30 00:00:00 3 NA NA NA
10 2019-02-02 00:00:00 14 NA NA NA
... with 85 more rows, and 4 more variables: `fc
gamma` , Low , High , sales.mean
options(repr.plot.width = 6, repr.plot.height = 4)
autolayer(salesLogHW)
Error: Objects of type HoltWinters not supported by autolayer.
Run rlang::last_error()
to see where the error occurred.
forecast next year's sales
nextYearSales <- forecast(salesLogHW, h=4)
plot
autolayer(nextYearSales)
mapping: x = ~x, y = ~y, level = ~level, ymin = ~ymin, ymax = ~ymax
geom_forecast: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
nextYearSales
Point Forecast Lo 80 Hi 80 Lo 95
2029 Q1 3.115325 1.2080498 5.022601 0.1983992
2029 Q2 3.315737 1.2321049 5.399368 0.1290970
2029 Q3 2.435076 0.1306404 4.739513 -1.0892543
2029 Q4 3.021776 0.4553214 5.588231 -0.9032777
Hi 95
2029 Q1 6.032251
2029 Q2 6.502376
2029 Q3 5.959407
2029 Q4 6.946830