Académique Documents
Professionnel Documents
Culture Documents
R Programming
for Quantitative Finance
Guy Yollin
Applied Mathematics
University of Washington
Guy Yollin (Copyright 2014)
R Basics
1 / 53
Outline
R language references
Short R Tutorial
R Basics
2 / 53
Lecture references
J. Adler.
R in a Nutshell: A Desktop Quick Reference.
OReilly Media, 2010.
Chapters 1-3
W. N. Venables and D. M. Smith.
An Introduction to R.
2013.
Sections 1-3
R Basics
3 / 53
Outline
R language references
Short R Tutorial
R Basics
4 / 53
R Basics
5 / 53
4.0
HAM1 Performance
HAM1
3.5
EDHEC LS EQ
2.5
2.0
1.0
1.5
Data manipulation
Cumulative Return
3.0
SP500 TR
0.00
Monthly Return
0.05
0.0 0.10
0.2
0.3
0.4
Data visualization
Drawdown
0.1
Statistical modeling
0.05
Data analysis
Jan 96
Jan 97
Jan 98
Jan 99
Jan 00
Jan 01
Jan 02
Jan 03
Jan 04
Jan 05
Jan 06
Dec 06
Date
R Basics
6 / 53
S language implementations
R is the most recent and full-featured
implementation of the S language
Original S - AT & T Bell Labs
S-PLUS (S plus a GUI)
Statistical Sciences, Inc.
Mathsoft, Inc.
Insightful, Inc.
Tibco, Inc.
R Basics
7 / 53
R timeline
1991
Statistical Models in S
(white book)
S3 methods
1999
John Chambers
1998 ACM Software Award
1984
S: An Interactive Envirnoment for
Data Analysis and Graphics
1988
(Brown Book)
The New S Language
Written in C
(Blue Book)
Work on S
Version 1
1993
R on Statlib
2000
R 1.0.0
(S3)
2001
R 1.4.0
(S4)
1997
R on CRAN
GNU Project
2002
Modern Applied Statistics
with S
4th Edition
(S+ 6.x, R 1.5.0)
1976
2010
R&R
Given ASA
Statistical Computing
and Graphics Award
2004
R 2.0.0
2011
S-PLULS
developed by
Statistical Sciences, Inc.
1988
StatSci Licenses S
1993
S era
Exponential
growth of
R Users and
R packages
S-PLUS era
R era
R Basics
8 / 53
American Statistical
Association
R Basics
9 / 53
Outline
R language references
Short R Tutorial
R Basics
10 / 53
An Introduction to R
R Reference Card
by Tom Short, EPRI Solutions, Inc., tshort@eprisolutions.com 2005-07-12
Granted to the public domain. See www.Rpad.org for the source and latest
version. Includes material from R for Beginners by Emmanuel Paradis (with
permission).
Data creation
Indexing lists
x[n]
list with elements n
x[[n]]
nth element of the list
x[["name"]] element of the list named "name"
x$name
id.
Indexing vectors
x[n]
nth element
x[-n]
all but the nth element
x[1:n]
first n elements
x[-(1:n)]
elements from n+1 to the end
x[c(1,4,2)]
specific elements
x["name"]
element named "name"
x[x > 3]
all elements greater than 3
x[x > 3 & x < 5]
all elements between 3 and 5
x[x %in% c("a","and","the")] elements in the given set
Indexing matrices
x[i,j]
element at row i, column j
x[i,]
row i
x[,j]
column j
x[,c(1,3)] columns 1 and 3
x["name",] row named "name"
Indexing data frames (matrix indexing plus the following)
x[["name"]] column named "name"
x$name
id.
Variable conversion
as.array(x), as.data.frame(x), as.numeric(x),
as.logical(x), as.complex(x), as.character(x),
... convert type; for a complete list, use methods(as)
Variable information
is.na(x), is.null(x), is.array(x), is.data.frame(x),
is.numeric(x), is.complex(x), is.character(x),
... test for type; for a complete list, use methods(is)
length(x) number of elements in x
dim(x) Retrieve or set the dimension of an object; dim(x) <- c(3,2)
dimnames(x) Retrieve or set the dimension names of an object
nrow(x) number of rows; NROW(x) is the same but treats a vector as a onerow matrix
ncol(x) and NCOL(x) id. for columns
class(x) get or set the class of x; class(x) <- "myclass"
unclass(x) remove the class attribute of x
attr(x,which) get or set the attribute which of x
attributes(obj) get or set the list of attributes of obj
Definitely obtain these PDF files from the R homepage or a CRAN mirror
Guy Yollin (Copyright 2014)
R Basics
11 / 53
Introductory texts
R in a Nutshell: A Desktop
Quick Reference
A Beginners Guide to R
Joseph Adler
OReilly Media, 2009
R Basics
12 / 53
Introductory texts
R in Action
Robert Kabacoff
Manning Publications, 2011
Norman Matloff
No Starch Press, 2011
THE
ART OF R
U iv>Vi,
>`*vVi>i`
U `i>V>}ivi>>]>}i>>
U ->>}L}>`>Vi``iL}}}
7iii`i}}>V>v]viV>}i
PROGR A MMING
A
TOUR O F S TAT I S T I C A L S O F T W A R E D E S I G N
NORMAN MATLOFF
v
>v>]>ii>ViiV`i
U
i>i>v}>>iVi`>>i
v>i`iiii>i>Viv
U 7iiivvViV`i}>>i,>`
->V*i
R Basics
13 / 53
Statistics with R
R Basics
14 / 53
R Basics
15 / 53
Outline
R language references
Short R Tutorial
R Basics
16 / 53
Interacting with R
R is an interpreted language
An R interpreter must be running in order to evaluate R commands or
execute R scripts
RGui which includes an R Console window
RStudio which includes an R Console window
http://en.wikipedia.org/wiki/Interpreted_language
R Basics
17 / 53
R expression evaluation
R expressions are processed via Rs Read-eval-print loop :
http://en.wikipedia.org/wiki/Read-eval-print_loop
R Basics
18 / 53
R Basics
19 / 53
R debugger
R Basics
20 / 53
Calling functions
sin(pi/2)
## [1] 1
print("Hello, world")
## [1] "Hello, world"
abs(-8)
## [1] 8
cos(2*sqrt(2))
## [1] -0.95136313
date()
## [1] "Sun Aug 31 17:08:42 2014"
http://en.wikipedia.org/wiki/Functional_programming
R Basics
21 / 53
## [1] 5
assign("e",2.7183)
e
## [1] 2.7183
s = sqrt(2)
s
## [1] 1.4142136
r <- rnorm(n=2)
r
## [1] -1.0067110533 -0.0020828847
s*e+y
## [1] 8.8442567
R Basics
22 / 53
Object orientation in R
Everything in R is an Object
Use functions ls and objects to list all objects in the current
workspace
x <- c(3.1416,2.7183)
m <- matrix(rnorm(9),nrow=3)
tab <- data.frame(store=c("downtown","eastside","airport"),sales=c(32,17,24))
cities <- c("Seattle","Portland","San Francisco")
ls()
## [1] "cities"
## [7] "tab"
"e"
"x"
"filename" "m"
"y"
"r"
"s"
http://en.wikipedia.org/wiki/Object-oriented_programming
R Basics
23 / 53
Object classes
m
##
[,1]
[,2]
[,3]
## [1,] 0.374352397 0.586864810 -0.73778598
## [2,] -0.071532765 -0.262264339 -0.19904931
## [3,] 0.790144078 0.012603635 1.96472235
class(m)
## [1] "matrix"
tab
##
store sales
## 1 downtown
32
## 2 eastside
17
## 3 airport
24
class(tab)
## [1] "data.frame"
R Basics
24 / 53
Vectors
R is a vector/matrix programming language (also know as an array
programming language )
vectors can easily be created with c, the combine function
most places where single value can be supplied, a vector can be
supplied and R will perform a vectorized operation
my.vector <- c(2, 4, 3, 7, 10)
my.vector
## [1]
7 10
my.vector^2
## [1]
16
49 100
sqrt(my.vector)
## [1] 1.4142136 2.0000000 1.7320508 2.6457513 3.1622777
http://en.wikipedia.org/wiki/Array_programming
R Basics
25 / 53
"euler"
"sqrt2"
"golden"
The [1] in the above output is labeling the first element of the vector
The c function can be used to create character vectors, numeric
vectors, as well as other types of vectors
Guy Yollin (Copyright 2014)
R Basics
26 / 53
Indexing vectors
constants[c(1,3,4)]
##
pi sqrt2 golden
## 3.1416 1.4142 1.6180
constants[c(-1,-2)]
## sqrt2 golden
## 1.4142 1.6180
constants[c("pi","golden")]
##
pi golden
## 3.1416 1.6180
constants > 2
logical vector
##
##
pi
TRUE
euler
TRUE
sqrt2 golden
FALSE FALSE
constants[constants > 2]
##
pi euler
## 3.1416 2.7183
Guy Yollin (Copyright 2014)
R Basics
27 / 53
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
letters[16:26]
##
[1] "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
letters[-(1:15)]
##
[1] "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
Guy Yollin (Copyright 2014)
R Basics
28 / 53
# non-vectorized computation
# taking the log of each element in a vector
n <- length(x)
y <- rep(0,n)
for( i in 1:n )
y[i] <- log(x[i])
y
##
##
R Basics
29 / 53
R Basics
30 / 53
Outline
R language references
Short R Tutorial
R Basics
31 / 53
help.start()
## If nothing happens, you should open
## 'http://127.0.0.1:28913/doc/html/index.html' yourself
R Basics
32 / 53
R Basics
33 / 53
R Basics
34 / 53
R Basics
35 / 53
Outline
R language references
Short R Tutorial
R Basics
36 / 53
R Homepage
http://www.r-project.org
List of CRAN mirror sites
Manuals
FAQs
Site seach
Mailing lists
Links
R Basics
37 / 53
http://cran.fhcrc.org
CRAN Mirrors
About 45 countries
About 100 sites worldwide
About 15 sites in US
R Binaries
R Packages
5800+ packages
R Sources
Task Views
R Basics
38 / 53
R Basics
39 / 53
Stackoverflow
Stackoverflow has become the primary resource for help with R
http://stackoverflow.com/
Guy Yollin (Copyright 2014)
R Basics
40 / 53
R-SIG-FINANCE
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
R Basics
41 / 53
Naming convention
Coding Syntax
Program Organization
http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html
R Basics
42 / 53
Quick R
http://www.statmethods.net
Introductory R Lessons
R Interface
Data Input
Data Management
Basic Statistics
Advanced Statistics
Basic Graphs
Advanced Graphs
R Basics
43 / 53
http://research.stowers-institute.org/efg/R/index.htm
R Basics
44 / 53
Programming in R
Online R programming manual from UC Riverside:
R Basics
Finding Help
Code Editors for R
Control Structures
Functions
Object Oriented Programming
Building R Packages
http://manuals.bioinformatics.ucr.edu/home/programming-in-r
R Basics
45 / 53
R Site Search
R Seek
Revolution Blog
Inside-R
R Basics
46 / 53
Outline
R language references
Short R Tutorial
R Basics
47 / 53
RStudio
RStudio is a fully-featured open-source IDE
for R
R language highlighting
Paste/Source to R console
object explorer
tabbed graphics window
integrated version control
1-click kintr/Sweave compilation
R Basics
48 / 53
R Basics
49 / 53
http://www.winedt.com
http://www.winedt.org/Config/modes/R-Sweave.php
Guy Yollin (Copyright 2014)
R Basics
50 / 53
R Basics
51 / 53
http://notepad-plus-plus.org
http://sourceforge.net/projects/npptor
Guy Yollin (Copyright 2014)
R Basics
52 / 53
http://depts.washington.edu/compfin
R Basics
53 / 53