2

I'm having trouble creating an xts object for multi-dimensional panel data. The data looks like this:

Object  Factor  Quarter     Units
A       x       01.01.2013  255
A       x       01.04.2013  240
A       x       01.07.2013  310
A       x       01.10.2013  420
A       x       01.01.2014  520
A       y       01.01.2013  170
A       y       01.04.2013  160
A       y       01.07.2013  207
A       y       01.10.2013  280
A       y       01.01.2014  347
B       x       01.01.2013  1199
B       x       01.04.2013  1330
B       x       01.07.2013  1450
B       x       01.10.2013  1214
B       x       01.01.2014  1429
B       y       01.01.2013  827
B       y       01.04.2013  764
B       y       01.07.2013  924
B       y       01.10.2013  844
B       y       01.01.2014  893

Orienting myself at this post here, my R code looks like this:

library(xts)
Data2 <- read.csv('TestData.csv')
Data2List <- split(Data2, Data2$Object)
xtsData2 <- lapply(Data2List, function(x){
  attrCol <- c("Object", "Factor")
  numCol <- c("Units")
  y <- xts(x[,numCol], as.Date(x$Quarter, format = '%d.%m.%Y'))
  xtsAttributes(y) <- as.list(x[1,attrCol]) 
  y
})

Summary of A looks like this, to me this looks fine:

str(xtsData2$A)
An ‘xts’ object on 2013-01-01/2014-01-01 containing:
  Data: int [1:10, 1] 255 170 240 160 310 207 420 280 520 347
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ Object: Factor w/ 2 levels "A","B": 1
 $ Factor: Factor w/ 2 levels "x","y": 1

But somehow, the periodicity is messed up:

 > periodicity(xtsData2$A)
0 seconds periodicity from 2013-01-01 to 2014-01-01 

This is also visible in the axTicksByTime function:

axTicksByTime(xtsData2$A)
Jan 01 00:00:00 Apr 01 00:00:00 Jul 01 00:00:00 Okt 01 00:00:00 Jan 01 00:00:00 Jan 01 00:00:00 
              1               3               5               7               9              10 

When I enter the data without the Factor column, everything works fine and the periodicity is correctly displayed as quarterly.

How do I correctly enter my multi-dimensional data with R still recognizing the data format?

Community
  • 1
  • 1
s-heins
  • 679
  • 1
  • 8
  • 20
  • 1
    xts wasn't designed to handle panel timeseries. `periodicity` returns zero because your index has duplicate entries for every observation, and the median difference in the index values is zero. How you "correctly enter multi-dimensional data" into xts depends on what you want to do with the data, which you haven't specified. So I might be able to help if you can provide more detail about what you ultimately want to do. – Joshua Ulrich Sep 11 '15 at 16:06
  • Hey Joshua, thanks for the reply! At the moment, I have a forecast for each object (A/B/...) in units and want to do a forecast for the factors (x/y/...) in percent of that number of units. So say the forecast for A is 60k, I'd want to have a forecast like 30%/18k for x and 70%/42k for y. My real life example is a bit more complex though, I have 62 Objects, 21 Factors and different types of units for which I want to examine correlations in order to do a good forecast. – s-heins Sep 14 '15 at 08:28
  • It seems like you should just make your data wide. one row per timestamp, one column per forecast or factor. Then your model works on those columns. if your factors exist independently, then you could have an object for the factors, and one for the forecasts, but it's not clear from your example how separable they are/ – Brian G. Peterson Sep 19 '15 at 17:01

0 Answers0