• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Creating a Time Series Object in R

Data Science

This lesson is part 5 of 27 in the course Financial Time Series Analysis in R

In R, we can use the ts() function to create a time series object.

Usage

Below is a simplified format of the ts function. For complete details use ?ts in your R console.

ts(data = NA, start = 1, end = numeric(), frequency = 1,  deltat = 1, names = )
  • data: a vector or matrix of the observed time-series values.
  • start: the time of the first observation.
  • end: the time of the last observation, specified in the same way as start.
  • frequency: the number of observations per unit of time.
  • deltat: the fraction of the sampling period between successive observations; e.g., 1/12 for monthly data. Only one of frequency or deltat should be provided.
  • names: a character vector of names for the series in a multiple series: defaults to the colnames of data, or Series 1, Series 2, ….

While you can have data containing dates and corresponding values in an R object of any other class such as a dataframe, creating objects of ts class offers many benefits such as the time index information. Also, when you plot a ts object, it automatically creates a plot over time.

Let’s look at how we can create a time series object using the ts class.

Dataset

For this example, we will use a dataset that consists of monthly prices of the S&P Composite Index for the last three years starting from July 2014 to June 2017. We load the original data in a data vector called sp_vector.

sp_vector <- c(1973.1,1961.53,1993.23,1937.27,2044.57,2054.27,2028.18,2082.2,2079.99,2094.86,2111.94,2099.29,2094.14,2039.87,1944.41,2024.81,2080.62,2054.08,1918.6,1904.42,2021.95,2075.54,2065.55,2083.89,2148.9,2170.95,2157.69,2143.02,2164.99,2246.63,2275.12,2329.91,2366.82,2359.31,2395.35,2433.99)

Print and Plot the Vector

We can first try to directly print and plot the vector to observe the dataset.

> print(sp_vector)
 [1] 1973.10 1961.53 1993.23 1937.27 2044.57 2054.27 2028.18 2082.20 2079.99 2094.86 2111.94
[12] 2099.29 2094.14 2039.87 1944.41 2024.81 2080.62 2054.08 1918.60 1904.42 2021.95 2075.54
[23] 2065.55 2083.89 2148.90 2170.95 2157.69 2143.02 2164.99 2246.63 2275.12 2329.91 2366.82
[34] 2359.31 2395.35 2433.99
>
> plot(sp_vector)
>

As you can see, the plot does not contain any time index information.

Creating Time Series Object

We can use the ts() function to convert this vector into a time series object. Our data set contains monthly stock prices from July 2017 to June 2017. There are 12 observations per year starting from July 2014. So, we will set the frequency to 12 and the set start argument to c(2014,7) where the first value is the year and the second value is the month index (July).

> sp_ts <- ts(sp_vector,start=c(2014,7),frequency=12)

We can print to check that we have the correct time series.

> print(sp_ts)
         Jan     Feb     Mar     Apr     May     Jun     Jul     Aug     Sep     Oct     Nov     Dec
2014                                                 1973.10 1961.53 1993.23 1937.27 2044.57 2054.27
2015 2028.18 2082.20 2079.99 2094.86 2111.94 2099.29 2094.14 2039.87 1944.41 2024.81 2080.62 2054.08
2016 1918.60 1904.42 2021.95 2075.54 2065.55 2083.89 2148.90 2170.95 2157.69 2143.02 2164.99 2246.63
2017 2275.12 2329.91 2366.82 2359.31 2395.35 2433.99
>

Plot the Time Series

We can now use the plot.ts() function to plot the time series.

> plot.ts(sp_ts)

Note that it automatically recognized that it is a time series and plots time on the x-axis.

Previous Lesson

‹ Handling Missing Values in Time Series

Next Lesson

Check if an object is a time series object in R ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • Financial Time Series Data
  • Exploring Time Series Data in R
  • Plotting Time Series in R
  • Handling Missing Values in Time Series
  • Creating a Time Series Object in R
  • Check if an object is a time series object in R
  • Plotting Financial Time Series Data (Multiple Columns) in R
  • Characteristics of Time Series
  • Stationary Process in Time Series
  • Transforming a Series to Stationary
  • Time Series Transformation in R
  • Differencing and Log Transformation
  • Autocorrelation in R
  • Time Series Models
  • ARIMA Modeling
  • Simulate White Noise (WN) in R
  • Simulate Random Walk (RW) in R
  • AutoRegressive (AR) Model in R
  • Estimating AutoRegressive (AR) Model in R
  • Forecasting with AutoRegressive (AR) Model in R
  • Moving Average (MA) Model in R
  • Estimating Moving Average (MA) Model in R
  • ARIMA Modelling in R
  • ARIMA Modelling – Identify Model for a Time Series
  • Forecasting with ARIMA Modeling in R – Case Study
  • Automatic Identification of Model Using auto.arima() Function in R
  • Financial Time Series in R – Course Conclusion

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us