Unable to plot ts, posting sample data

Hi all,
I am trying to plot time series after importing the data from csv to R.
now, the issue is:
the data needs to be read by Rows and not by columns.
when I am putting, Row header which is a product ID, it is taking the column value.
pasting the same code here.

rm(list = ls())

#set directory
setwd("d:\\Users\\AFG")

install.packages("reprex")
library(plyr)
# load required packages
library(readr)
library(dplyr)
library(ggplot2)
library(forecast)
library(reprex)

# import data
sales <- read.csv("SalesHistoryTop50SKU-reprex.csv", header = TRUE, blank.lines.skip = TRUE)

# reading sales 
sales 

# convert our sales data to a time series object
sales <- ts(sales$Article_ID, frequency = 9, start = c(2016,1))
class(sales)

plot(sales)
reprex()

here is the sample data.


tried using reprex, but it is not working, and R studio is restarting everytime.

Goal: I want to read the data row wise. for the 8 weeks, and then forecast next 3 weeks data. for all the Article_ID individually. firstly I want to do it for 1. Then, i will try to use a loop to do it for other articles.

Hi @rdubai,

if your data is weekly, then frequency = 52, not 9. And weekly data is even more tricky because some years have 53 weeks, so you don't have a fixed frequency which makes things even messier.

Hi Valeri, my actual data will be h = 52. but here just to paste in the community, i removed rest of the data nd kept only 9 weeks.
you have valid point of 53 weeks. But for now, I want to run the code error free and see the result. Later we can modify and make it better. please advice.

Regardless of how many weeks you have in the actual dataset, you need to set frequency = 52 because that is the inherent frequency of the data (52 observations per cycle - a year in this case).

To be more clear (I hope), frequency = 9 means that the year is split into 9 equally spaced intervals, which in your case it definitely isn't.

ok, I kept the freq: 9 as i removed the rest and have only 9 observation for this demo. ok, I will change it to 52.

That is not sample data, that is a screenshot and is not very useful. Can you tell us what problems are you having to properly share sample data?, I think we should fix that first, since it would make things much easier.

this is the error i got after doing reprex().

reprex()
Rendering reprex...
Error: callr subprocess failed: Timeout was reached: [api.imgur.com] Connection timed out after 10000 milliseconds

I don't have your dataset, but this should work:

set.seed(22)
sales <- data.frame(Article_ID = letters[1:6])
sales[paste0("Week", 1:9)] <- replicate(9, rpois(6, 100))
sales
#   Article_ID Week1 Week2 Week3 Week4 Week5 Week6 Week7 Week8 Week9
# 1          a    94    98   126    98    84    86   110    89    95
# 2          b   100    98    92   102    95    88   132    85    92
# 3          c   105   103   108    94   106    93    98    92    89
# 4          d   106    92   120   104    94   105   103    99    98
# 5          e    96   108   109    96   104   107    88   116    91
# 6          f    99   114    83   108   108   119   102    91   125

sales_ts <- ts(
  t(sales[, -1]),
  frequency = 52,
  class = c("mts", "ts", "matrix")
)
rownames(sales_ts) <- names(sales)[-1]
colnames(sales_ts) <- sales[["Article_ID"]]
sales_ts
# Time Series:
# Start = c(1, 1) 
# End = c(1, 9) 
# Frequency = 52 
#            a   b   c   d   e   f
# 1.000000  94 100 105 106  96  99
# 1.019231  98  98 103  92 108 114
# 1.038462 126  92 108 120 109  83
# 1.057692  98 102  94 104  96 108
# 1.076923  84  95 106  94 104 108
# 1.096154  86  88  93 105 107 119
# 1.115385 110 132  98 103  88 102
# 1.134615  89  85  92  99 116  91
# 1.153846  95  92  89  98  91 125

The key part is this:

sales_ts <- ts(
  t(sales[, -1]),
  frequency = 52,
  class = c("mts", "ts", "matrix")
)

t(sales[, -1]) takes the sales dataset, omits the first column ("Article_ID"), and transposes it so the rows become columns. The resulting dataset is then turned into a time series with frequency of 52. The class = c("mts", "ts", "matrix") part tells the ts() function to make a "matrix time series," where each column is a separate time series. You can read about with help("ts").


Note for the future: To format a code block on this forum, wrap the code lines between two lines of three accent marks:

```
# Formatted like code
1 * 1 + 1 * 1
```

Also, when just sharing a single data object, you can see the code to recreate it with dput(myobject). Just share that code with others for a quick-and-dirty reprex.

2 Likes

It seems like you are not allowed to connect to imgur (maybe a restriction on the network you are connecting to) but regardless of this issue, you don't necessarily need to use reprex package to put together a proper reproducible example or even share sample data on a proper format, for the latest you can use datapasta package, see this blog post for a nice explanation.

this code worked, I wrote the output in a CSV before the ts(), it is correct. while ts() and forecast function did not work. Keep on getting this:

plot(salesLogHW)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ