Econometric Examples in R

Patrick Wynne

April 16, 2016

What is R?

  • From wikipedia:

  • A free software programming language

  • A software environment for statistical computing and graphics

  • An implementation of the S programming language combined with lexical scoping semantics inspired by Scheme

  • A GNU project

Why use R?

  • Free and open source

  • Runs on all major operating systems, including Windows, Mac, Unix, and Linux, etc.

  • Huge and vibrant user community

  • Platform of choice for cutting-edge statistical methodology

  • Widely used tool for the new field of data science

Who uses R

  • Revolution Analytics (owned by Microsoft) has an informal list of companies that use R.

  • Bank of America:“[R is] also catching on on Wall Street. Traditionally, banking analysts would pore over Excel files late into the night, but now R is increasingly being used for financial modeling, particularly as a visualization tool, says Niall O’Connor, vice president at Bank of America. ‘R makes our mundane tables stand out,’ he says.”

Who uses R Continued

  • Facebook

  • Ford Motor Company

  • Google

  • Microsoft

  • Renaissance Technologies, Google, and Microsoft are all sponsors of this years UseR! conference in San Francisco

Who uses R Continued

Spreadsheets vs Statistical Programming Languages

  • Cells in spreadsheets combine both data presentationand programming/computation logic

  • Spreadsheets allow in-place mutation of data

  • These features have strengths in reactivity - changes are visulized immediately

  • These features also make spreadsheets error prone

Famous spreadsheet errors

  • There are several famous spreadsheet blunders

  • Thomas Piketty’s “Capital in the Twenty-First Century”

  • The London Whale

  • Goldman Sachs purchasing of S+ Language

Piketty

  • The analysis for Thomas Piketty’s best selling book “Capital in the Twenty-First Century” was done in excel spreadsheets

  • A Financial Times investigation found various issues with Piketty’s analysis

  • Neil Irwin of New York Times The Upshot outlines some of the mistakes

  • Simple data errors: Selecting the wrong cell ranges

  • Arbitrary or unexplained changes: random data manipulations

London Whale

  • “…what has been generally under-reported about the London Whale debacle is how badly Excel failed as a financial modeling program… the report states that the model suffered from some pretty standard Excel flaws”

  • “the model operated through a series of Excel spreadsheets, completed manually, by a process of copying and pasting data from one spreadsheet to another”

  • “After subtracting the old rate from the new rate, the spreadsheet divided by their sum instead of their average, as the modeler had intended. This error likely had the effect of muting volatility by a factor of two and of lowering the VaR…”

Goldman Sachs

  • Vista Equity Partners in 2014 agreed to acquire Tibco Software (ownders of S-PLUS, a commercial implementation of the S langauge which R is based on)

  • Goldman Sachs, missing counting the number of existing shares of Tibco, valued Tibco at $4.3 billion at $24 a share rather than the actual $4.2 billion

  • Vista Equity Partners were charged an additional $100 million while purchasing analytic software that could have prevented the mistake entirely

  • The correction was made Tibco shareholders made 61 cents less per share than initially announced

Download R

  • To download and install R and RStudio, we are going to loosely follow the Andrew Heiss’ guide

  • First we have to download R for Windows or Mac

  • Once downloaded, Install R and leave all default settings in the installation options.

Download R Studio

  • R Studio is an integrated development environment (IDE) for R

  • It provides graphical user interface to your R environment

  • RStudio

Mac

  • Go to your Applications folder and find a folder named Utilities. Verify that you have a program named “X11” there. If not, go to http://xquartz.macosforge.org/ and download and install the latest version of XQuartz.

Loading packages and importing data

library(dplyr)
library(tidyr)
library(repmis)
library(ggplot2)

#import files from dropbox

Data <- source_DropboxData(file = "FinanceExample.csv", 
                              key = "x61awpr4vqvi97y", header = TRUE)

Summary Statistics

library(stargazer)

stargazer(Data, type = "html")

Summary Statistics

Statistic N Mean St. Dev. Min Max
date 60 20,130,680.000 14,265.690 20,110,131 20,151,231
JPM 60 0.012 0.075 -0.229 0.172
MS 60 0.009 0.100 -0.228 0.309
GS 60 0.005 0.077 -0.186 0.233
S&P 60 0.009 0.034 -0.072 0.108
mktrf 60 0.010 0.035 -0.076 0.114
smb 60 -0.001 0.022 -0.042 0.043
hml 60 -0.002 0.017 -0.045 0.046
rf 60 0.00002 0.00004 0.000 0.0001
JPM-rf 60 0.012 0.075 -0.229 0.172
MS-rf 60 0.008 0.100 -0.228 0.309
GS-rf 60 0.005 0.077 -0.186 0.233
S&P-rf 60 0.009 0.034 -0.072 0.108

Correlation Matrix

CorrelationMatrix <- cor(Data[2:5])

stargazer(CorrelationMatrix, type = "text")

Correlation Matrix

JPM MS GS S&P
JPM 1 0.864 0.858 0.750
MS 0.864 1 0.903 0.769
GS 0.858 0.903 1 0.740
S&P 0.750 0.769 0.740 1

Simple Linear Regression JP Morgan

JPM_SimpleRegression <- lm(`JPM-rf` ~ `S&P-rf`, data = Data)

stargazer(JPM_SimpleRegression, type = "text", title = "JPM Versus S&P-rf",
          single.row = TRUE)

JPM Versus S&P-rf
Dependent variable:
JPM-rf
S&P-rf 1.667*** (0.193)
Constant -0.002 (0.007)
Observations 60
R2 0.563
Adjusted R2 0.555
Residual Std. Error 0.050 (df = 58)
F Statistic 74.669*** (df = 1; 58)
Note: p<0.1; p<0.05; p<0.01

Multiple Regression JPM

JPM_MultipleRegression <- lm(`JPM-rf`  ~ mktrf + smb + hml, data = Data)

stargazer(JPM_MultipleRegression, type = "text", title = "Multiple Regression on JPM",
          single.row = TRUE)

Multiple Regression on JPM
Dependent variable:
JPM-rf
mktrf 1.585*** (0.190)
smb 0.158 (0.305)
hml 0.853** (0.358)
Constant -0.001 (0.007)
Observations 60
R2 0.614
Adjusted R2 0.594
Residual Std. Error 0.048 (df = 56)
F Statistic 29.749*** (df = 3; 56)
Note: p<0.1; p<0.05; p<0.01

Quantitative Visualisation - Importing Data

TidyData <- source_DropboxData(file = "TidyData.csv", 
                              key = "696eznkt27987v5", header = TRUE)

TidyData$date <- as.Date(TidyData$date, "%Y-%m-%d")

head(TidyData)

date Index Performance
1 2011-01-31 JPM 0.061
2 2011-02-28 JPM 0.039
3 2011-03-31 JPM -0.013
4 2011-04-29 JPM -0.005
5 2011-05-31 JPM -0.052
6 2011-06-30 JPM -0.053

SCatter plot

library(ggplot2)

plot <- ggplot(data=TidyData, aes(x=date, y=Performance))

plot + geom_point()