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

Paul Paul.Domaskis at gmail.com
Thu Apr 23 04:32:26 CEST 2015

William Dunlap <wdunlap <at> tibco.com> writes:
> I think we can call this a bug in stl().

I used what I learned from the responses to this thread, I looked at
the code for stl.  As they say in Microsoft, "this is expected
behaviour" according to the code.  And it doesn't look like an
inadvertent coding oversight.
Martin Maechler <maechler <at> lynne.stat.math.ethz.ch> writes:
>> Paul  <Paul.Domaskis <at> gmail.com> 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.
> 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.

The distinction in math is new me, with academic training in
engineering, even at the post grad level.  I haven't seen the
distinction in the math for Comp. Sci., either, and that's in the meat
grinder of Canada.  Admittedly, it's not quite as geeky as some meat
grinders in other countries.  And admittedly, I only took C.S. courses
that were geared to applications.  So I had always considered such a
distinction to a practicality in coding implementation of
vector/matrix classes, e.g., in C, a vector being a single pointer to
a number, while in a 2D array is a pointer to a vector and hence a
different type.

>> 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.

No need to apologize.  To clarify, being new to R, I was referring to
the general use of the term "list".  Specifically, I was referring to
an ordered collection without orientation, so it is consistent with
what you say above about distinguishing between length N vectors vs.
2D matrices of size Nx1 or 1xN.

> 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.

Matlab pretty well only deals with 2D arrays, some of which have size
Nx1 or 1xN.  I haven't seen an example of a 1-D data structure that
doesn't have an orientation, implied or otherwise.  Though of course,
if someone proves me wrong, then I stand corrected (and smarter
because of it).

>  {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}

Big fan of python's readability, though I've only dabbled.  And
I won't start bitchering about R & S cuz I'm a newcomer and it's all
an eye popping wonderland.
David R Forrest <drf <at> vims.edu> writes:
> The details of how str() represents your x and y variables is within
> the utils::stl.default() function.  You can hunt this down and see

I'm assuming that you meant utils.str.default() above.  I can follow
the rest of your post makes sense if I make that assumption.

I snipped the majority of your response because I'm not responding to
anything specific.  However, it was an extremely educational post.
Thank you for that.

> Also, Matlab sometimes needs a squeeze() to drop degenerate
> dimensions, and R's drop() is similar, and is less-black-magic
> looking than the [[1]] code:
> > str(drop(x))
>  Time-Series [1:36] from 1 to 3.92: 464 675 703 887 1139 1077 1318
>  1260 1120 963 ...
> > str(drop(y))
>  Time-Series [1:36] from 1 to 3.92: 464 675 703 887 1139 1077 1318
>  1260 1120 963 ...
> stl(drop(x),s.window='per')
> stl(drop(y),s.window='per')
> Maybe str.default() should do Time-Series interpretation of is.ts()
> objects for matrices as well as vectors.

I'm assuming that you mean stl(), since str() already works on both?
Maybe it's the version I have, however, but I find that the R code for
stl() doesn't have have a section for is.ts().  Instead, it seems to
run through a series of checks for pathological input, with the check
for matrix data consisting of is.matrix(na.action(as.ts(x))), where x
is the time series.  Somehow, the fact that the na.action(time series
argument) returns a matrix implies that the time series data is a
matrix rather than a vector.  In attempting to get insight, I found
that the ts class has no na.action method, and that the default method
for the generic na.action is not visible using getAnywhere (nor is it
visible by entering it at the command line without brackets).

Anyway, pretty educational.  Thanks again.

More information about the R-help mailing list