• 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

Exploring Time Series Data in R

Data Science

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

Let’s look at a few commands that we will frequently use while exploring time series data.

length()

The length() function tells us the number of elements in out time series dataset.

> length(msft_ts)
[1] 252
>

head()

The head() function displays the top n elements of the dataset. This is useful while exploring large datasets.

> head(msft_ts,n=10)
 [1] 54.80 55.05 54.05 52.17 52.33 52.30 52.78 51.64 53.11 50.99
>

tail()

The tail() function displays the last n elements of the dataset. This is useful while exploring large datasets.

> tail(msft_ts,n=10)
 [1] 62.30 63.62 63.54 63.54 63.55 63.24 63.28 62.99 62.90 62.14
>

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

> GDP_data = Quandl("FRED/GDP", start_date="2014-01-01", end_date="2016-12-31",type="ts")

Let’s explore this data.

print()

We earlier saw that we can also use the print() function to display the time series.

> print(GDP_data)
        Qtr1    Qtr2    Qtr3    Qtr4
2014 17025.2 17285.6 17569.4 17692.2
2015 17783.6 17998.3 18141.9 18222.8
2016 18281.6 18450.1 18675.3 18869.4
>

As we can see, this data is presented as yearly data with 4 observations in each year.

start() and end()

> start(GDP_data)
[1] 2014    1
> end(GDP_data)
[1] 2016    4
>

frequency()

This tells us number of observations per unit of time.

> frequency(GDP_data)
[1] 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.

> deltat(GDP_data)
[1] 0.25
>

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”.

> time(GDP_data)
        Qtr1    Qtr2    Qtr3    Qtr4
2014 2014.00 2014.25 2014.50 2014.75
2015 2015.00 2015.25 2015.50 2015.75
2016 2016.00 2016.25 2016.50 2016.75
>
> cycle(GDP_data)
     Qtr1 Qtr2 Qtr3 Qtr4
2014    1    2    3    4
2015    1    2    3    4
2016    1    2    3    4
>
Previous Lesson

‹ Financial Time Series Data

Next Lesson

Plotting Time Series 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