[R] How numerical data is stored inside ts time series objects
David R Forrest
drf at vims.edu
Wed Apr 22 18:06:41 CEST 2015
> On Apr 21, 2015, at 9:39 PM, Paul <Paul.Domaskis at gmail.com> wrote:
...
> I rummaged around the help files for str, summary, dput, args. This
> seems like a more complicated language than Matlab, VBA, or even C++'s
> STL of old (which was pretty thoroughly documented). A function like
> str() returns an object description, and I'm guessing the conventions
> with which the object is described depends a lot on the person who
> wrote the handling code for the class. The description for the
> variable y seems particularly elaborate.
>
> Would I be right in assuming that the notation is ad-hoc and not
> documented? For example, the two invocations str(x) and str(y) show a
> Time-Series and a ts. And there are many lines of output for str(y)
> that is heavy in punctuation.
>
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 the code with:
methods(class=class(x)) # Find the class-specific handlers -- no str()
methods(str) # Find the methods for the generic
getAnywhere(str.default) # or getFromNamespace('str.default','utils')
Within the utils::str.default code, this 'Time-Series' specific code only triggers if the object doesn't match a long list of other items (for example: is.function(), is.list(), is.vector(object) || (is.array(object) && is.atomic(object)) ...)
else if (stats::is.ts(object)) {
tsp.a <- stats::tsp(object)
str1 <- paste0(" Time-Series ", le.str, " from ",
format(tsp.a[1L]), " to ", format(tsp.a[2L]),
":")
std.attr <- c("tsp", "class")
}
This handling is not dependent on who wrote the ts class, but on who wrote the str.default function.
A more explict way to look at the difference without the str() summarization is with dput(x) and dput(y):
> dput(x)
structure(c(464L, 675L, 703L, 887L, 1139L, 1077L, 1318L, 1260L,
1120L, 963L, 996L, 960L, 530L, 883L, 894L, 1045L, 1199L, 1287L,
1565L, 1577L, 1076L, 918L, 1008L, 1063L, 544L, 635L, 804L, 980L,
1018L, 1064L, 1404L, 1286L, 1104L, 999L, 996L, 1015L), .Tsp = c(1,
3.91666666666667, 12), class = "ts")
> dput(y)
structure(c(464L, 675L, 703L, 887L, 1139L, 1077L, 1318L, 1260L,
1120L, 963L, 996L, 960L, 530L, 883L, 894L, 1045L, 1199L, 1287L,
1565L, 1577L, 1076L, 918L, 1008L, 1063L, 544L, 635L, 804L, 980L,
1018L, 1064L, 1404L, 1286L, 1104L, 999L, 996L, 1015L), .Dim = c(36L,
1L), .Dimnames = list(NULL, "V1"), .Tsp = c(1, 3.91666666666667,
12), class = "ts")
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.
Dave
More information about the R-help
mailing list