Legacy Documentation

Time Series (2011)

This is documentation for an obsolete product.
Current products and services

Previous section-----Next section

1.4.1 Plotting the Data

The first thing to do in analyzing time series data is to plot them since visual inspection of the graph can provide the first clues to the nature of the series: we can "spot" trends, seasonality, and nonstationary effects. Often the data are stored in a file and we need to read in the data from the file and put them in the appropriate format for plotting using Mathematica. We provide several examples below.
In[1]:=
In[2]:=
We use the ReadList command to read in these numbers and put them in a list.
We read in the time series data from the file file1.dat. The specification {Number, Number} in ReadList makes each entry in the list a list of two numbers {t, xt}.
In[3]:=
Out[3]=
Now data1 defined above is in the right format for ListLinePlot.
In[4]:=
Out[4]=
We can check if the data were in fact taken at equally spaced intervals by doing the following. First extract the time coordinates.
In[5]:=
Out[5]=
Now take the differences of adjacent time coordinates and see if they give the same number.
In[6]:=
Out[6]=
So we see that in this case the data were indeed taken at constant time intervals. Since we have assumed all along that the data are taken at equally spaced time intervals, it is often convenient to drop the time coordinate and consider just the time series. All time series data to be input to time series functions should be in the form {x1, x2, ... , xn} where xi is a number for a scalar time series and a list, xi={xi1, xi2, ... , xim}, for an m-variate time series.
We can cast the above data in the form appropriate for input to time series functions by taking the second (i.e., the last) entry from each data point.
In[7]:=
Out[7]=
Now the data are in the right form to be used in time series functions.
If we plot the above data using ListLinePlot we will get the same plot as before except (a) the origin of time will have been shifted and (b) it will be in different time units with the first entry in data corresponding to time 1, and the second to time 2, and so on.
In[8]:=
Out[8]=
In[9]:=
It is convenient to use the Mathematica function Short to find out if the data are in the appropriate format without printing out all the numbers.
In[10]:=
Out[10]//Short=
We can plot lynxdata directly using ListLinePlot as we have demonstrated in the previous plot. On the other hand, if we want the time plot to show the real times at which the data were taken, we can reverse the above procedure of transforming data1 to data. Suppose the data were taken from time t1 to tn in intervals of t, Range[t1, tn, deltat] will generate a list of all the times at which data were taken. If t is omitted the default value of 1 is assumed. In our lynx example, the data were taken annually from the year 1821 to the year 1934.
In[11]:=
We use Short to display data1.
In[12]:=
Out[12]//Short=
Now if we plot data1 using ListLinePlot the horizontal axis corresponds to the "real" time.
In[13]:=
Out[13]=
In[14]:=
We have used the option RecordSeparators -> "," because of the presence of commas between the numbers. To convert data1, which is now a long list of numbers, into the correct time series data format we put every pair of numbers into a list using Partition.
We use Partition to put the data in the right form for a multivariate series.
In[15]:=
In[16]:=
Out[16]=
We see that data is a bivariate time series of length 100. Again it is in the correct format of the form {x1, x2, ... , xn} with the ith entry xi being a list of length 2.
To extract the ith time series from multivariate data we can use data[[All,i]]. Here are the plots of the two series of the bivariate series data.
In[17]:=
Out[17]=
In[18]:=
Out[18]=