9
$\begingroup$

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?

$\endgroup$
13
$\begingroup$

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

$\endgroup$
  • $\begingroup$ much better. Tks! $\endgroup$ – Murta Dec 20 '12 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.