1. Learn
  2. /
  3. Courses
  4. /
  5. Time Series Analysis in R

Exercise

Creating a time series object with ts()

The function ts() can be applied to create time series objects. A time series object is a vector (univariate) or matrix (multivariate) with additional attributes, including time indices for each observation, the sampling frequency and time increment between observations, and the cycle length for periodic data. Such objects are of the ts class, and represent data that has been observed at (approximately) equally spaced time points. Now you will create time series objects yourself.

The advantage of creating and working with time series objects of the ts class is that many methods are available for utilizing time series attributes, such as time index information. For example, as you've seen in earlier exercises, calling plot() on a ts object will automatically generate a plot over time.

In this exercise, you'll familiarize yourself with the ts class by encoding some time series data (saved as data_vector) into ts and exploring the result. Your time series data_vector starts in the year 2004 and has 4 observations per year (i.e. it is quarterly data).

Instructions

100 XP
  • Apply print() and plot() to data_vector. Note that, by default, your plot does not contain time information.
  • Use ts() with data_vector to convert your data to a ts object. Set the start argument equal to 2004 and the frequency argument equal to 4. Assign the result to time_series.
  • Use print() and plot() to view your time_series object.