[R] How numerical data is stored inside ts time series objects

Martin Maechler maechler at lynne.stat.math.ethz.ch
Wed Apr 22 15:15:32 CEST 2015


>>>>> Paul  <Paul.Domaskis at gmail.com>
>>>>>     on Wed, 22 Apr 2015 01:39:16 +0000 writes:

    > William Dunlap <wdunlap <at> tibco.com> writes:
    >> Use the str() function to see the internal structure of most
    >> objects.  In your case it would show something like:
    >> 
    >> > Data <- data.frame(theData=round(sin(1:38),1))
    >> > x <- ts(Data[[1]], frequency=12) # or Data[,1]
    >> > y <- ts(Data, frequency=12)
    >> > str(x)
    >> Time-Series [1:38] from 1 to 4.08: 0.8 0.9 0.1 -0.8 -1 -0.3 0.7 1 0.4 -
    > 0.5
    >> ...
    >> > str(y)
    >> ts [1:38, 1] 0.8 0.9 0.1 -0.8 -1 -0.3 0.7 1 0.4 -0.5 ...
    >> - attr(*, "dimnames")=List of 2
    >> ..$ : NULL
    >> ..$ : chr "theData"
    >> - attr(*, "tsp")= num [1:3] 1 4.08 12
    >> 
    >> 'x' contains a vector of data and 'y' contains a 1-column matrix of
    >> data.  stl(x,"per") and stl(y, "per") give similar results as you
    >> got.
    >> 
    >> Evidently, stl() does not know that 1-column matrices can be treated
    >> much the same as vectors and gives an error message.  Thus you must
    >> extract the one column into a vector: stl(y[,1], "per").

    > Thanks, William.

    > Interesting that a 2D matrix of size Nx1 is treated as a different
    > animal from a length N vector.  It's a departure from math convention,
    > and from what I'm accustomed to in Matlab.  

Ha -- Not at all!
The above is exactly the misconception I have been fighting --
mostly in vane -- for years.

Matlab's convention of treating a vector as an  N x 1 matrix is
a BIG confusion to much of math teaching :

The vector space  |R^n  is not all the same space as the space  |R^{n x 1}
even though of course there's a trivial mapping between the
objects (and the metrics) of the two.
A vector *is NOT* a matrix -- but in some matrix calculus
notations there is a convention to *treat* n-vectors as  (n x 1) matrices.

Good linear algebra teaching does distinguish vectors from
one-column or one-row matrices -- I'm sure still the case in all
good math departments around the globe -- but maybe not in math
teaching to engineers and others who only need applied math.
Yes, linear algebra teaching will also make a point that in
the usual matrix product notations, it is convenient and useful to treat
vectors as if they were 1-column matrices.

    > That R's vector seems
    > more akin to a list, where the notion of orientation doesn't apply.

Sorry, but again:  not at all in the sense 'list's are used in R.

Fortunately, well thought out languages such as S, R, Julia, Python,
all do make a good distinction between vectors and matrices
i.e. 1D and 2D arrays.  If Matlab still does not do that, it's
just another sign that Matlab users should flee and start using julia
or R or python.

  {and well yes, we could start bitchering about S' and hence R's distinction
   between a 1D array and a vector ... which I think has been a
   clear design error... but that's not the topic here}



More information about the R-help mailing list