0
votes

My data is organized quarterly and weekly. I was able to convert quarterly data to the Date format by using Zoo package and TS package. However, I am not quite sure whether weekly data can be analyzed.

For instance, here's how the data looks like:

testdata:

  Fiscal.Year Fiscal.Quarter   Seller Product.Bookings.Net Product.Bookings.Quantity Product.Family Sales.Level.1          Group Fiscal.Week
1        2015        2015.50 ABCD1234                 4000                         4      Paper cup      Americas Paper Division          32
2        2014        2014.00  DDH1234                  300                         5   Paper tissue  Asia Pacific Paper Division          33
3        2015        2015.00  PNS1234                  298                         6         Spoons          EMEA        Cutlery          34
4        2016        2016.75  CCC1234                  289                         7         Knives        Africa        Cutlery          33

As you can see, I was able to convert quarter day to date format. However, I am not quite sure whether we can analyze weekly data.

I took a stab at converting the weekly data (i.e. Fiscal Week) to date of type TS():

library("lubridate")
testdata$Fiscal.Year <- paste("1/1/",testdata$Fiscal.Year,sep = "")
testdata$Fiscal.Date <- dmy(testdata$Fiscal.Year) + lubridate::weeks(testdata$Fiscal.Week)
testdata$date_ts <- as.ts(testdata$Fiscal.Date)

I just checked the output:

class(testdata$date_ts)
[1] "ts"

So, it seems I was able to convert those weeks to TS type, but here's what happens when:

Attempt1

I try to use POSIXlt and POSIXct function from base class (to be able to draw the graphs etc.):

 as.POSIXlt(testdata$date_ts)
Error in as.POSIXlt.default(testdata$date_ts) : 
  do not know how to convert 'testdata$date_ts' to class “POSIXlt”

Attempt2

as.POSIXct.date(testdata$Fiscal.Date)
Error in as.POSIXct.date(testdata$Fiscal.Date) : 
  'testdata$Fiscal.Date' is not a "date" object

even though, my object is of type:

class(testdata$Fiscal.Date)
[1] "Date"

I have two related questions:

Question 1: Can someone please help me with the error? I am not quite sure what the problem is in each of the two methods above.

Question 2: Is there more effective way (in terms of speed and number of lines of code) to do what I have done?

My post is influenced by: Time series weekly data and after reading this thread: Analyzing Daily/Weekly data using ts in R

Please help me. Thanks in advance.


Here's dput of my data: dput(testdata)

structure(list(Fiscal.Year = c(2015, 2014, 2015, 2016), Fiscal.Quarter = c("2015Q3", 
"2014Q1", "2015Q1", "2016Q4"), Seller = c("ABCD1234", "DDH1234", 
"PNS1234", "CCC1234"), Product.Bookings.Net = c(4000, 300, 298, 
289), Product.Bookings.Quantity = c(4, 5, 6, 7), Product.Family = c("Paper cup", 
"Paper tissue", "Spoons", "Knives"), Sales.Level.1 = c("Americas", 
"Asia Pacific", "EMEA", "Africa"), Group = c("Paper Division", 
"Paper Division", "Cutlery", "Cutlery"), Fiscal.Week = c(32, 
33, 34, 33)), .Names = c("Fiscal.Year", "Fiscal.Quarter", "Seller", 
"Product.Bookings.Net", "Product.Bookings.Quantity", "Product.Family", 
"Sales.Level.1", "Group", "Fiscal.Week"), row.names = c(NA, 4L
), class = "data.frame")
1
can you share the dput of your data, I get an error trying to read.table your data.shayaa
@shayaa: I have added the dput...watchtower
Have you even read help("as.ts")? It creates a time series object, which is a completely different beast than a datetime object.Roland
@Roland-I did read ?as.ts before posting. It seems that there are two conceptual issues in my post 1) TS type object is used for regularly spaced events. For irregularly spaced events, I should be depending on Zoo or other packages. 2) I am converting object of type "Date" to "ts" and then to "POSIXct" or "POSIXlt". Do you think this is reasonable? I am a beginner, so I apologize for asking lunatic questions at times.watchtower

1 Answers

0
votes

Ok. So, I figured out the answer to my question:

I shouldn't be converting an object of class Date to TS because it isn't periodic. I should rather use zoo packages' classes to be consistent.

library("lubridate")
#take care of weeks:
testdata$Fiscal.Year <- paste("1/1/",testdata$Fiscal.Year,sep = "")
testdata$Fiscal.Date <- dmy(testdata$Fiscal.Year) + lubridate::weeks(testdata$Fiscal.Week)

#testdata$date_ts <- as.ts(testdata$Fiscal.Date)
ggplot(testdata,aes(x=year(testdata$Fiscal.Date),y=testdata$Product.Bookings.Net))+geom_bar(stat = "identity")

This works well.

The same holds good for the other two issues that were posted by me. I found this pdf extremely useful in my analysis: https://faculty.washington.edu/ezivot/econ424/Working%20with%20Time%20Series%20Data%20in%20R.pdf

This resolved my query. I am still open to any thoughts for reading "weekly" data and then efficiently converting it to of type "Date"