0

I have a two variable dataframe (df) in R of daily sales for a ten year period from 2004-07-09 through 2014-12-31. Not every single date is represented in the ten year period, but pretty much most days Monday through Friday.

My objective is to aggregate sales by quarter, convert to a time series object, and run a seasonal decomposition and other time series forecasting.

I am having trouble with the conversion, as ulitmately I receive a error:

time series has no or less than 2 periods

Here's the structure of my code.

# create a time series object
library(xts)
x <- xts(df$amount, df$date)
# create a time series object aggregated by quarter
q.x <- apply.quarterly(x, sum)

When I try to run

fit <- stl(q.x, s.window = "periodic")  

I get the error message

series is not periodic or has less than two periods  

When I try to run

q.x.components <- decompose(q.x)  
# or  
decompose(x)  

I get the error message

time series has no or less than 2 periods  

So, how do I take my original dataframe, with a date variable and an amount variable (sales), aggregate that quarterly as a time series object, and then run a time series analysis?

sasquatch
  • 1
  • 3

1 Answers1

0

I think I was able to answer my own question. I did this. Can anyone confirm if this structure makes sense?

library(lubridate)
# add a new variable indicating the calendar year.quarter (i.e. 2004.3) of each observation
df$year.quarter <- quarter(df$date, with_year = TRUE)

library(plyr)
# summarize gift amount by year.quarter
new.data <- ddply(df, .(year.quarter), summarize,
              sum = round(sum(amount), 2))

# convert the new data to a quarterly time series object beginning
# in July 2004 (2004, Q3) and ending in December 2014 (2014, Q4)
nd.ts <- ts(new.data$sum, start = c(2004,3), end = c(2014,4), frequency = 4)
sasquatch
  • 1
  • 3