1> head(msft_ts,n=10)
2 [1] 54.80 55.05 54.05 52.17 52.33 52.30 52.78 51.64 53.11 50.99
3>
4
tail()
The tail()
function displays the last n elements of the dataset. This is useful while exploring large datasets.
1> tail(msft_ts,n=10)
2 [1] 62.30 63.62 63.54 63.54 63.55 63.24 63.28 62.99 62.90 62.14
3>
4
Example 2 - Quarterly GDP Data
Let's take one more example, this time quarterly GDP data. We will load this data from Quandl. The following command will load the quarterly GDP data from Quandl for the years 2014 to 2016
1> GDP_data = Quandl("FRED/GDP", start_date="2014-01-01", end_date="2016-12-31",type="ts")
2
Let's explore this data.
print()
We earlier saw that we can also use the print()
function to display the time series.
1> print(GDP_data)
2 Qtr1 Qtr2 Qtr3 Qtr4
32014 17025.2 17285.6 17569.4 17692.2
42015 17783.6 17998.3 18141.9 18222.8
52016 18281.6 18450.1 18675.3 18869.4
6>
7
As we can see, this data is presented as yearly data with 4 observations in each year.
start() and end()
1> start(GDP_data)
2[1] 2014 1
3> end(GDP_data)
4[1] 2016 4
5>
6
frequency()
This tells us number of observations per unit of time.
1> frequency(GDP_data)
2[1] 4
3>
4
The data is quarterly, so the lag between successive observations is 1 quarter. The dataset GDP_data
has been set up so that the unit of time is 1 year (frequency=4).
deltat()
This is the fraction of the sampling period between successive observations; e.g., 1/12 for monthly data, and 1/4 for quarterly data. The function deltat
uses this time unit to compute the lag by the formula ∆t= 1/frequency). Only one of frequency
or deltat
should be provided.
1> deltat(GDP_data)
2[1] 0.25
3>
4
time()
The functions time and cycle create time series of the times at which the observations in a time series are taken and their "seasons".
1> time(GDP_data)
2 Qtr1 Qtr2 Qtr3 Qtr4
32014 2014.00 2014.25 2014.50 2014.75
42015 2015.00 2015.25 2015.50 2015.75
52016 2016.00 2016.25 2016.50 2016.75
6>
7
1> cycle(GDP_data)
2 Qtr1 Qtr2 Qtr3 Qtr4
32014 1 2 3 4
42015 1 2 3 4
52016 1 2 3 4
6>
7