Lesson Downloads
Free Preview
You're previewing a lesson from Financial Time Series Analysis in R
Here's everything waiting for you in the full course:
Test Your Knowledge
Check your understanding of this lesson with a short quiz.
Free Preview
Here's everything waiting for you in the full course:
Check your understanding of this lesson with a short quiz.
Ask questions about this lesson and get instant answers.
Welcome to this course on financial time series analysis using R. In this course, we will learn about financial time series data analysis in R. You will learn about how to explore and build time series data, calculate its key statistics, and plot time series charts. You will also learn about how to use the important time series models such as White Noise, Random Walk, Autoregression and Moving Average, learn how to simulate these models, fit these models into financial time series data and use the models to predict the future. So, let's get started...
Time series refers to a series of data in a chronological order. A lot of data in this world is recorded sequentially, over time, in the form of time series. Some common examples include the weather in a city over time, the prices of a listed stock, the commodity prices and so on. While studying financial assets, the asset prices as well as asset returns are represented as time series. Investors generally prefer to use asset returns, over asset prices, in their analysis. This is primarily for two reasons: 1) the asset returns provide a complete and scale-free summary of asset returns and 2) the asset returns are easier to analyze compared to asset prices because of their statistical properties. In our lessons, we will use examples of both asset prices and asset returns.
There are plenty of financial time series data sources on the Internet. One popular source is Quandl, which contains thousands of datasets including financial and economic datasets.
For the purpose of this course, I would suggest you to signup for a free account on Quandl.com. Once you signup, you will get an API key that you can use to fetch data directly in R from Quandl.
In order to work with Quandl datasets in R, you need to install and load the Quandl package.
1install.packages("Quandl")
2library(Quandl)
3Now that you have your Quandl API key and have loaded Quandl package in your R session, you can download some financial data. In the following example, I am loading the daily closing prices of the Microsoft stock (Symbol: MSFT), for the year 2016 from the WIKI/PRICES dataset in Quandl.
In the following code, add your own api key and then run it in your R console. The code will create an R time series object containing Microsoft stock prices for the year 2016. If you don't understand what's happening in this code, don't worry about it. We will be studying in detail about how to create time series objects in the coming lessons.
1Quandl.api_key("YOUR API KEY")
2msft_data <- Quandl.datatable("WIKI/PRICES" ,
3 qopts.columns=c("date", The easiest way to explore a time series is to view or print it as shown below:
1< print(msft_ts)
2Time Series:
3Start = 1
4End = 252
5Frequency = As you can see, the result explicitly tells us that it is a time series, which starts with 1 and ends at 252, i.e., there are a total of 252 observations with a frequency of 1.
In the next lesson, we will learn more about exploring the time series.
1install.packages("readr")
2library(readr)
3msft_data <- read_csv("msft_data.csv")
4View(msft_data)