R – An Introduction
R is a programming language package aimed at data scientist as a tool for computational statistics and visualisation. It has been developed into a popular language and data science programme for finance and data analytical companies. R is part of the open source revolution and has been created and supported entirely by developers and experts worldwide. R has a number of advantages including; every data analysis technique downloadable and free, cutting edge community reviewed methods, stunning data visualisation infographics, faster results with a manageable programme language and expert resources.
R Code School
The best way to get to grips with R is to take the online tutorial through ‘Try R Code School’. Though basic, it runs through the primary sections and gets you acquainted with the R programming language. The tutorial is pirate themed and this made the sections enjoyable and the pirate in-jokes kept me entertained throughout. The seven sections in the tutorial were:
1. Using R
4. Summary Statistics
6. Data Frames
7. Real World Data
After completing each section, I was rewarded with a badge and each topic covered the basics to get me started with real world data sets.
Analysing the Data
Having previously worked in finance, I have an inherent interest (and experience) in financial analysis and reporting. I decided to use R programming to take financial data from the Irish Stock Exchange (ISEQ). I decided to focus on Aer Lingus shares over a ten year timeframe.
The first part of my research consisted of analysing the most powerful R packages to analyse my data. I found the most trended of the packages best at extracting financial time series data from internet sources were – Quantmod and Quandl. These packages work in a similar vein to a Bloomberg terminal but at no cost. As I was focusing on historic data, I used Quantmod to extract the data. Quandl would be the preferred package when looking at futures.
I installed the Quantmod package from the ‘Packages’ dropdown in R and then tested searching for data using ticker symbols related to Aer Lingus shares – AERL.L.
This command essentially searched google to pull the ticker number ‘AERL.L’ and retrieve any data since 01/Jan/2004. This data is presented as daily log returns as the price; Open price, High price, Low price, Close price and Volume traded.
Now we have the data set, it is time to analyse the data to form some interesting information. The first chart I created was to run a time series showing the share price and the volume traded. This provides an illustration of the shares following an almost U-curve between 2006 and 2015.
Time Series of AERL.L data
We have a large data set giving daily prices of Aer Lingus shares over approximately a ten year period. A majority of modelling systems use the data in a XTS command object to extract subsets from the data range. This is widely used when extracting say monthly or quarterly data for additional analysis or reporting. This functionality is an example of how R can be used effectively over older analytical tools.
R commands to create XTS file and view data sets
Using the capabilities of the data set, I want to plot a graph showing the closing price of the shares. This graph is exactly what would be used to present to management and is an excellent representation of the data set.
R Graph to visualise closing prices of Aer Lingus Shares
An interesting analysis is to plot the daily log return of the closing prices. The resulting time series graph shows the visual impact of volatility in the share price. We can see that during the financial crisis (2008-2009) the share price was in flux and this would be evident of many traded shares at the time. Since 2010, the share price is still fluctuating (though at a lesser rate) and this would indicate instability in the company. Based on remedial research the likely effect has been the recovery in the business since 2010 and the recent speculation of a takeover from International Airlines Group (IAG).
Closing daily prices (daily log return)
Summary statistics of Closing Prices
Concepts – If I had more time
It would be extremely difficult to propose a new effective financial model in such a short timeframe. In the above example we are not using indicators just taking data to determine market direction or trends. This example has given the power of R at modelling data and presenting the data in an excellent visual format. The data is current and can be easily updated through internet searches.
My analysis is limited in the sense that I have taken past data and from only one company. An excellent way to enhance the analysis would be to take competitor data and plot these against each other. This analysis over time would give an insight into market factors.
Quandl is another programme which looks at futures based on financial data. This would be an excellent programme to create models and predict future prices based on this information. Another way to analyse the data trends would be to analyse internet trends and keywords to see if there is a correlation with market movement. R would be able to analyse large data over time and this could be plotted against the share price chart.
- Cookbook for R, http://www.cookbook-r.com/ (Accessed: 01 August 2015)
- Irish Stock Exchange (2015) ‘Market Data’ http://www.ise.ie/Market-Data-Announcements/Companies/Company-data/ (Accessed: 01 August 2015)
- Playing Financial Data Series(1), Chenangen, (2014) http://www.r-bloggers.com/playing-financial-data-series1/ (Accessed: 03 August 2015)
- Quantitative Finance Applications in R (Internet Sources), Joseph Rickert (2013) http://www.r-bloggers.com/quantitative-finance-applications-in-r/ (Accessed: 03 August 2015)
- Quantitative Finance Applications in R (XTS), Joseph Rickert (2014) http://www.r-bloggers.com/quantitative-finance-applications-in-r-2/ (Accessed: 03 August 2015)
- Revolution Analytics (2015) ‘R is Hot’ Available at: http://www.revolutionanalytics.com/whitepaper/r-hot (Accessed: 03 August 2015)
Revolution Analytics (2015) ‘What R’ http://www.revolutionanalytics.com/what-r (Accessed: 03 August 2015)
- Try R Code School (2015) http://tryr.codeschool.com/ (Accessed: 28 July 2015)
R Editor Commands
# Open Quantmod and, xts and moments
library(moments) # to get skew & kurtosis
# Searches from Google and pulls data since 01/08/2005
getSymbols(“AERL.L”, src=”google”,from=”2005-08-01″,to= “2015-08-14”);
# Plot a time series chart
# Create an xts file of the ISEQ data and return TRUE
# View the dataset
(AERL.L.Close) # returns TRUE
AERL.L.Close is.xts(AERL.L.Close) # returns TRUE
#Plot a graphic profile of the data
plot(AERL.L.Close, main = “Closing Daily Prices for Aer Lingus Shares(AERL.L)”,
col = “red”,xlab = “Date”, ylab = “Price”, major.ticks=’years’,
# Set Closing price and Plot data
plot(AERL.L.ret, main = “Closing Daily Prices for Aer Lingus (AERL.L)”,
col = “red”, xlab = “Date”, ylab = “Return”, major.ticks=’years’,
# Set and plot data to find Mean, Std Dev (volatility), Skewness & Kurtosis
statNames AERL.L.stats names(AERL.L.stats) AERL.L.stats