sales ,forecast and holtwinters values :combine values into one matrix

Hi to all.
I am trying (hard) to have the summarized output from my code ,shown per row and as a grand total only.
I have learned that would be cbind code into a matrix (values only).

Is there anyone who could helpme with that?
I would need to see my sales ,forecast and Holtwinters values on rowbases in a matrix.

my reprex:

# load needed packages
library(readr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(readxl)
library(forecast) 
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library (ggplot2)
library(holtwinters)
#> Error in library(holtwinters): there is no package called 'holtwinters'

sales <-  read_excel("C:/Users/jenny/Desktop/R Studio/MonthlySales1.xls")
#> New names:
#> * `` -> ...7
#> * `` -> ...8

salesLogHW <- HoltWinters(salesLog)
#> Error in as.ts(x): object 'salesLog' not found
HoltWinters(x= salesLog)
#> Error in as.ts(x): object 'salesLog' not found

HoltWinters(nextYearSales)
#> Error in as.ts(x): object 'nextYearSales' not found
autoplot(forecast(fit))
#> Error in forecast(fit): object 'fit' not found

fit <- Arima(sales$sales)
nextYearSales
#> Error in eval(expr, envir, enclos): object 'nextYearSales' not found
sales
#> # A tibble: 95 x 8
#>    Month     sales `ETS Forecast` `Hw -alpha` `Hw-beta` `Hw-gamma`  ...7  ...8
#>    <chr>     <dbl>          <dbl>       <dbl>     <dbl>      <dbl> <dbl> <dbl>
#>  1 january      43             NA          NA        NA         NA    NA    NA
#>  2 february     48             NA          NA        NA         NA    NA    NA
#>  3 march        47             NA          NA        NA         NA    NA    NA
#>  4 april        48             NA          NA        NA         NA    NA    NA
#>  5 may          48             NA          NA        NA         NA    NA    NA
#>  6 june         49             NA          NA        NA         NA    NA    NA
#>  7 july         38             NA          NA        NA         NA    NA    NA
#>  8 august       25             NA          NA        NA         NA    NA    NA
#>  9 september    48             NA          NA        NA         NA    NA    NA
#> 10 october      48             NA          NA        NA         NA    NA    NA
#> # ... with 85 more rows

Created on 2020-05-18 by the reprex package (v0.3.0)

These are all functions for building higher dimensional objects. I have taken the code you posted and modified it a little to show that the first three functions each produces a matrix, which is a two dimensional object and data.frame, obviously, makes a data frame. A data frame is typically two dimensional. The biggest difference between a matrix and a data frame is that a matrix must contain only one type of data but a data frame can mix types such as the numbers and characters in this example.

#cbind(a, b, c) Combine vectors as columns in a matrix 
FIRST <- cbind(1:5, 6:10, 11:15)
FIRST
#>      [,1] [,2] [,3]
#> [1,]    1    6   11
#> [2,]    2    7   12
#> [3,]    3    8   13
#> [4,]    4    9   14
#> [5,]    5   10   15
class(FIRST)
#> [1] "matrix"
#rbind(a, b, c) Combine vectors as rows in a matrix 
SECOND <- rbind(1:5, 6:10, 11:15)
SECOND
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    2    3    4    5
#> [2,]    6    7    8    9   10
#> [3,]   11   12   13   14   15
class(SECOND)
#> [1] "matrix"
#matrix(x, nrow, ncol, byrow) Create a matrix from a vector x 
THIRD <- matrix(data = 1:12, nrow = 3, ncol = 4)
THIRD
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12
class(THIRD)
#> [1] "matrix"
#data.frame() Create a dataframe from named columns 
FOURTH <- data.frame("age" = c(19, 21),mf = c("m", "f"))
FOURTH
#>   age mf
#> 1  19  m
#> 2  21  f
class(FOURTH)
#> [1] "data.frame"

Created on 2020-05-17 by the reprex package (v0.3.0)

Hi , great .I understand what i i need now.: a matrix
If the reprex is run in Rstudio ,it shows you forecast values , but also Holtwinters Alpha,Beta and Gamma values.(ABG values)
**I am trying to get a combined matrix with my sales values and forecast values with the respective ABG in one simple matrix row wise, .Which matrix function do i need to use you think?

I have my reprex for you :Can you help me showing what code i would need??

load needed packages

library(readr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(readxl)
library(forecast) # necessary to use forecasting methods
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
library(tsintermittent) # TSB (Teunter-Syntetos-Babai) method
#> Loading required package: MAPA
#> Loading required package: parallel
#> Loading required package: RColorBrewer
#> Loading required package: smooth
#> Loading required package: greybox
#> Error: package or namespace load failed for 'greybox' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
#> there is no package called 'RcppParallel'
#> Error: package 'greybox' could not be loaded
library (ggplot2)

sales <- read_excel("C:/Users/jenny/Desktop/R Studio/MonthlySales1.xls")
#> New names:
#> * -> ...7 #> * -> ...8

salesLogHW <- HoltWinters(salesLog)
#> Error in as.ts(x): object 'salesLog' not found
HoltWinters(x= salesLog)
#> Error in as.ts(x): object 'salesLog' not found
findfrequency(nextYearSales)
#> Error in findfrequency(nextYearSales): object 'nextYearSales' not found

sales <- sqrt(salesLogHW$dispersion)
#> Error in eval(expr, envir, enclos): object 'salesLogHW' not found
dnorm(7, mean = coef(nextYearSales), sd = sales)
#> Error in coef(nextYearSales): object 'nextYearSales' not found
autoplot(forecast(fit))
#> Error in forecast(fit): object 'fit' not found
fit <- Arima(sales$sales)
salesLogHW
#> Error in eval(expr, envir, enclos): object 'salesLogHW' not found
nextYearSales
#> Error in eval(expr, envir, enclos): object 'nextYearSales' not found

class =
c("spec_tbl_df", "tbl_df", "tbl", "data.frame")
row.names =
c(NA,-40L)
spec = structure(list(
cols = list(
sales = structure(list(salesLogHW),
class =
c("collector_double", "collector")),
sales = structure(list(sales),
class =
c("collector_double", "collector")),
return = structure(list(HoltWinters(fit)),
class =
c("collector_double", "collector"))
),
default = structure(list(),
class =
c("collector_guess", "collector")),
skip = 10
),
class = "col_spec")
#> Error in structure(list(salesLogHW), class = c("collector_double", "collector")): object 'salesLogHW' not found
sales
#> # A tibble: 95 x 8
#> Month sales ETS Forecast Hw -alpha Hw-beta Hw-gamma ...7 ...8
#>
#> 1 january 43 NA NA NA NA NA NA
#> 2 february 48 NA NA NA NA NA NA
#> 3 march 47 NA NA NA NA NA NA
#> 4 april 48 NA NA NA NA NA NA
#> 5 may 48 NA NA NA NA NA NA
#> 6 june 49 NA NA NA NA NA NA
#> 7 july 38 NA NA NA NA NA NA
#> 8 august 25 NA NA NA NA NA NA
#> 9 september 48 NA NA NA NA NA NA
#> 10 october 48 NA NA NA NA NA NA
#> # ... with 85 more rows

My excel file (MonthlySales1.xls) looks like this:

Month sales
january 43
february 48
march 47
april 48
may 48
june 49
july 38
august 25
september 48
october 48
november 39
december 49
january 41
february 35
march 43
april 47
may 39
june 49
july 37
august 46
september 31
october 45
november 26
december 24
january 48
february 32
march 47
april 49
may 48
june 45
july 43
august 47
september 43
october 27
november 43
december 49
january 48
february 46
march 47
april 34
may 35
june 49
july 42
august 49
september 49
october 43
november 48
december 49
january 47
february 44
march 33
april 46
may 39
june 43
july 49
august 45
september 42
october 46
november 48
december 43
january 48
february 46
march 31
april 49
may 41
june 48
july 43
august 24
september 47
october 49
november 29
december 48
january 49
february 43
march 47
april 39
may 47
june 40
july 46
august 46
september 44
october 41
november 45
december 49
january 46
february 44
march 45
april 43
may 40
june 49
july 49
august 47
september 45

Your example returns many errors but rather than try to fix that, let's aim for a simpler goal. Are you successfully calculating the forecast, alpha, beta and gamma? In your reprex they are all NA but that might be due to the reprex process. If you do get the desired values, how are they organized? If you could just show a small example of the structure, that would be very helpful. Please also show the format that you want them in finally.

The examples do not have to be your actual calculations. It is fine to invent a small set of data that just shows the organization of your results. You can use the data.frame function to make the examples.

Hi ,I agree with your approach and I am very happy you want to teach me this.
STEP 1 Before I move on with anything else : Basically I only need a matrix with values calculated based on sales.
The below shows my goal: a matrix based on values.No characters.

with regards t your questions:
1.Are you successfully calculating the forecast, alpha, beta and gamma. Answer:Not sure.My code generates alpha ,beta and gamma for the whole cumulative series of observations.
I need it on a row basis.
2.What format do i need? Answer: a simple matrix (numeric values)

STEP 2 : My code needs adjusting ?
I look forward to your advise.I hope this is not an overflow of information and I gave the correct answers.

Month Sales Multivariate ARIMA Extrapolation Forecast Alpha Beta Gamma Mean Mean error
January 43 n/a n/a n/a n/a n/a n/a
february 48 n/a n/a n/a n/a n/a n/a
March 47 n/a n/a n/a n/a n/a n/a
April 48 n/a n/a n/a n/a n/a n/a
May 48 n/a n/a n/a n/a n/a n/a
June 49 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
July 38 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
August 25 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
September 48 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
October 48 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
November 49 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
December 38 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
January 25 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values
February 38 Multivariate ARIMA Extrapolation Forecast Based on previous 20 values Alpha Based on previous 20 values Beta Based on previous 20 values Gamma Based on previous 20 values Mean Based on previous 20 values Mean error Based on previous 20 values

my current simplified reprex:

# load needed packages
library(readr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(readxl)
library(forecast) 
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library (ggplot2)
library(holtwinters)
#> Error in library(holtwinters): there is no package called 'holtwinters'

sales <-  read_excel("C:/Users/jenny/Desktop/R Studio/MonthlySales1.xls")
#> New names:
#> * `` -> ...7
#> * `` -> ...8

salesLogHW <- HoltWinters(salesLog)
#> Error in as.ts(x): object 'salesLog' not found
HoltWinters(x= salesLog)
#> Error in as.ts(x): object 'salesLog' not found

HoltWinters(nextYearSales)
#> Error in as.ts(x): object 'nextYearSales' not found
autoplot(forecast(fit))
#> Error in forecast(fit): object 'fit' not found

fit <- Arima(sales$sales)
nextYearSales
#> Error in eval(expr, envir, enclos): object 'nextYearSales' not found
sales
#> # A tibble: 95 x 8
#>    Month     sales `ETS Forecast` `Hw -alpha` `Hw-beta` `Hw-gamma`  ...7  ...8
#>    <chr>     <dbl>          <dbl>       <dbl>     <dbl>      <dbl> <dbl> <dbl>
#>  1 january      43             NA          NA        NA         NA    NA    NA
#>  2 february     48             NA          NA        NA         NA    NA    NA
#>  3 march        47             NA          NA        NA         NA    NA    NA
#>  4 april        48             NA          NA        NA         NA    NA    NA
#>  5 may          48             NA          NA        NA         NA    NA    NA
#>  6 june         49             NA          NA        NA         NA    NA    NA
#>  7 july         38             NA          NA        NA         NA    NA    NA
#>  8 august       25             NA          NA        NA         NA    NA    NA
#>  9 september    48             NA          NA        NA         NA    NA    NA
#> 10 october      48             NA          NA        NA         NA    NA    NA
#> # ... with 85 more rows

Created on 2020-05-18 by the reprex package (v0.3.0)

I am sorry I gave you the wrong impression about what I can help you with. If you had the values you need but needed to reshape them into a matrix, I could help you with that. I cannot help you with Holt Winters processing of data. To get help with that, you should start a thread with Holt Winters in the title. This thread only mentions cbind in the title and that will not attract the attention of the right people.

Also, please adjust your reprex in that thread so it does not throw errors.

2 posts were split to a new topic: reshape my table into a matrix

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.