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")
help("as.ts")
? It creates a time series object, which is a completely different beast than a datetime object. – Roland