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).
This exercise is part of the course
Time Series Analysis in R
Exercise instructions
- Apply
print()
andplot()
todata_vector
. Note that, by default, your plot does not contain time information. - Use
ts()
withdata_vector
to convert your data to ats
object. Set thestart
argument equal to2004
and thefrequency
argument equal to4
. Assign the result totime_series
. - Use
print()
andplot()
to view yourtime_series
object.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use print() and plot() to view data_vector
print(___)
plot(___)
# Convert data_vector to a ts object with start = 2004 and frequency = 4
time_series <-
# Use print() and plot() to view time_series