How to generate and decompose a Time Series in R?

Asked

Viewed 896 times

1

I’m having trouble generating and decomposing a Time Series. In this case, I was able to create a time series ts, but when I went to decompose the series decompose, indicates that there is an error in the Time Series, which says that the Time Series has no period, or is less than 2.

I’m using the package collection from tidyverse. The "dados_base" have the 3 columns (USUARIO;DATA;VL_PED_PG), where they represent Users, Purchase Date and Value respectively.

I used summarise to summarize the data, to find the amount of sales per day.

dados_dia = dados_base %>%
      group_by(DATA) %>%
      summarise(QTDE_COMPRAS = n(), VALOR_TOTAL = sum(VL_PED_PG))

Then I created the Time Series.

compras = ts(dados_dia$QTDE_COMPRAS, start = c(2018,7), end = c(2019,1), frequency = 180)

But when I’m gonna decompose(decompose) Time Series signals error.

dec = decompose(compras)

Error in decompose(purchases) : time series has no period, or has less than 2

dput to assist the response:

structure(list(USUARIO = c(931053L, 276977L, 354508L, 909717L, 69758L, 
104827L, 6600051L, 5035952L, 335505L, 340387L, 103130L, 317058L, 
424447L, 6862455L, 5040771L), DATA = structure(c(17731, 17731, 17731, 
17731, 17731, 17731, 17731, 17731, 17731, 17731, 17731, 17731, 17731, 
17731, 17731), class = "Date"), VL_PED_PG = c(20, 20, 50, 20.32, 20, 
30, 50, 50, 50, 50, 20, 20, 30, 30, 30)), row.names = c(NA, 15L), 
class = "data.frame")
  • Hello, Isac. Wouldn’t it be better to edit the old issue instead of asking a new question?

  • 1

    Hello! Well, actually I don’t know, I’m new in the community and I’m learning how to use it yet. But I accept suggestions on how to ask the questions. Did you understand my problem? Should I clarify something else?

  • 2

    Use this command on r: dput(dados_base) or head(dput(dados_base)). Then copy and paste the result into the question.

  • Hello, I tried here, but I couldn’t. I tried on R: teste_overflow = dput(dados_base)

compras = ts(teste_overflow, start = c(2018,7), end = c(2019,1), frequency = 60)

dec = decompose(compras)

Error in filter(x, filter) : 
 'filter' está mais comprido do que série temporal I didn’t understand how it works.

  • Suppose your database name is meusdados. What you need to do is just that: dput(meusdados) Just put meusdados within the function dput.

  • I tried: teste_overflow = dput(dados_dia)

compras = ts(dados_dia$QTDE_COMPRAS, start = c(2018,7), end = c(2019,1), frequency = 60) , and also: ;compras = ts(dput(dados_dia$QTDE_COMPRAS), start = c(2018,7), end = c(2019,1), frequency = 60) but unsuccessfully... Continues with the same error. I don’t think I understand how it works. You have an example of how to use dput ?

  • Read here. This can help.

  • Hello, I think I started to understand... would that be it? dput(head(dados_base, 15))
structure(list(USUARIO = c(931053L, 276977L, 354508L, 909717L, 
69758L, 104827L, 6600051L, 5035952L, 335505L, 340387L, 103130L, 
317058L, 424447L, 6862455L, 5040771L), DATA = structure(c(17731, 
17731, 17731, 17731, 17731, 17731, 17731, 17731, 17731, 17731, 
17731, 17731, 17731, 17731, 17731), class = "Date"), VL_PED_PG = c(20, 
20, 50, 20.32, 20, 30, 50, 50, 50, 50, 20, 20, 30, 30, 30)), row.names = c(NA, 
15L), class = "data.frame")

Show 3 more comments

1 answer

4


I’m going to use a data set that I have (data), just to illustrate:

data=structure(list(t = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 
27, 28), format.spss = "F8.0"), sales = structure(c(500, 350, 
250, 400, 450, 350, 200, 300, 350, 200, 150, 400, 550, 350, 250, 
550, 550, 400, 350, 600, 750, 500, 400, 650, 850, 600, 450, 700
), format.spss = "F8.2"), YEAR_ = structure(c(1990, 1990, 1990, 
1990, 1991, 1991, 1991, 1991, 1992, 1992, 1992, 1992, 1993, 1993, 
1993, 1993, 1994, 1994, 1994, 1994, 1995, 1995, 1995, 1995, 1996, 
1996, 1996, 1996), label = "YEAR, not periodic", format.spss = "F8.0", 
display_width = 10L), 
QUARTER_ = structure(c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 
1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4), label = "QUARTER, period 4", 
format.spss = "F1.0", display_width = 10L), 
DATE_ = structure(c("Q1 1990", "Q2 1990", "Q3 1990", "Q4 1990", 
"Q1 1991", "Q2 1991", "Q3 1991", "Q4 1991", "Q1 1992", "Q2 1992", 
"Q3 1992", "Q4 1992", "Q1 1993", "Q2 1993", "Q3 1993", "Q4 1993", 
"Q1 1994", "Q2 1994", "Q3 1994", "Q4 1994", "Q1 1995", "Q2 1995", 
"Q3 1995", "Q4 1995", "Q1 1996", "Q2 1996", "Q3 1996", "Q4 1996"
), label = "Date.  Format:  \"QQ YYYY\"", format.spss = "A7", display_width = 
9L)), .Names = c("t", 
"sales", "YEAR_", "QUARTER_", "DATE_"), row.names = c(NA, -28L
), class = c("tbl_df", "tbl", "data.frame"))

I will create a time series with the variable sales, which I will call x:

x<-ts(data$sales,start=c(2000,1),freq=12)
x
#     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2000 500 350 250 400 450 350 200 300 350 200 150 400
2001 550 350 250 550 550 400 350 600 750 500 400 650
2002 850 600 450 700        

Now, I apply the decomposition function:

decompose(x)

And the result is issued.

So, what’s the problem with your case? Next: you are introducing a frequency (frequency) inconsistent with the argument start and the argument end. Let’s go back to my example, but with a modification. I’ll put freq=15 instead of freq=12:

x<-ts(data$sales,start=c(2000,1),freq=15)

decompose(x)

Error in decompose(x) : time series has no or Less than 2 periods

Why does this error occur? Why the variable sales has 28 lines. And I applied a frequency of 15. So, 15*2=30. So, my series has less than two periods. If I put, for example, 14, 13, 12 ... decomposition would be calculated.

This way you should adjust your period (freq), because it is inconsistent with the dates specified in start and end. In his example, his series starts in July 2018 and runs through January 2019 with a frequency of 180.

  • Got it. Now it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.