R365: Day 13 – ts()

So we have been dealing with data from the {databases} package that was already amenable to time series. What if we want to define a data series as a time series? There are a few functions within the {stats} package that let us define a data series as a time series. Defining a data series as a time series comes with a sticky assumption: the data were collected at equidistant time points. This is just like anything else in science where you need to be careful that you are observing the requirements of the test before going ahead with the statistics. My guess is that time series analyses are fairly robust to minor deviations from this assumption, but if your time series was collected at odd intervals it might be a problem. A lot of molecular biology work measure their experiments in very small increments at the beginning and larger increments later (eg- 1s, 5s, 15s, 60s, 300s, etc), and this might not work out so well.

There are a couple of functions that can help test or coerce time series. as.ts coerces a dataset that already exists into being a time series. I am still kind of hazy on how it defines the index, I think that I will look at {InternalMethods} sometime soon to see how R works some stuff. is.ts basically asks R whether an object is a time series, and will result in TRUE or FALSE depending on the answer. The examples given for ts() (see ?ts for the function info) are surprisingly interesting considering that the function doesn’t really do much out of the way. The first example creates a new time series, and allows you to simulate an increasing GDP over different quarters.


The second example lets you randomly generate three normal data series, each of length 100, and graph them starting with Jan 1960 and ending with April 1969. You can tweak the code a bit and change the number of columns if that is easier to view (below are only two columns).


The time series graphs one on top of one another or plotted together:


The third example plots what they called a lag-plot, or a phase plot. I don’t really know much about phase plots, and I think it might be interesting to look at phase plots in a later post.



One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s