TemporalData frequency option as in R

9

2

I'm studying R, and am very impressed by the simplicity of some commands and in the way R handles temporal data. For example, in R, if you have some one-dimensional temporal data, you can easily transform it into a data series using:

url<-"http://www.massey.ac.nz/~pscowper/ts/cbe.dat"
data<-read.table(url,header=T)
timeSeriesCol1=ts(data[,1],start=1958,frequency=12)

In Mathematica, we have the new TemporalData function, but to do the same as in R, the code is a little clumsy due to the lack of frequency option or an equivalent. See the code:

url="http://www.massey.ac.nz/~pscowper/ts/cbe.dat";
data=Import[url];
timeSeriesCol1=TemporalData[Rest@data[[All,1]],{{1958,1},DatePlus[{1958,1},{Length[data],"Month"}]}]
DateListPlot[N@timeSeriesCol1["Path"]]

The part {{1958,1},DatePlus[{1958,1},{Length[CBE],"Month"}]} is very inelegant and DateListPlot does not directly recognize the object (and you need to convert it to a number, which look likes a bug).

Does anyone have a clue for a more elegant way to handle this using TemporalData?

Murta

Posted 2012-12-20T00:27:17.917

Reputation: 23 859

Answers

13

The following seems a little more elegant.

data = Import["http://www.massey.ac.nz/~pscowper/ts/cbe.dat"];

ts = TemporalData[data[[2 ;; -1, 1]], {"1958", Automatic, "Month"}];

DateListPlot[ts["Path"]]

enter image description here

TemporalData can also store multiple paths.

ts2= TemporalData[Transpose[data[[2 ;; -1]]], {"1958", Automatic, "Month"}];

DateListPlot[ts2["Paths"]]

enter image description here

Andy Ross

Posted 2012-12-20T00:27:17.917

Reputation: 18 640

much better. Tks! – Murta – 2012-12-20T10:28:41.130