Computational Finance and Risk Management

R Introduction

Guy Yollin
Acting Instructor, Applied Mathematics University of Washington

Guy Yollin (Copyright © 2012)

Computational Finance & Risk Management

R Introduction

1 / 128

Outline
1

Part 1 R overview and history R language references Part 2 R language and environment basics Data structures, data manipulation, working directory, data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R
Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 2 / 128

2

3

Lecture references

W. N. Venables and D. M. Smith. An Introduction to R. 2011. J. Adler. R in a Nutshell: A Desktop Quick Reference. O’Reilly Media, 2010.

Guy Yollin (Copyright © 2012)

Computational Finance & Risk Management

R Introduction

3 / 128

data manipulation. working directory.Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 4 / 128 2 3 .

r-project.What is R? R is a language and environment for statistical computing and graphics R is based on the S language originally developed by John Chambers and colleagues at AT&T Bell Labs in the late 1970s and early 1980s R (sometimes called “GNU S" ) is free open source software licensed under the GNU general public license (GPL 2) R development was initiated by Robert Gentleman and Ross Ihaka at the University of Auckland.org Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 5 / 128 . New Zealand R is formally known as The R Project for Statistical Computing www.

0 1.3 −0.05 1.5 3.00 0.4 −0.What is R great at? HAM1 Performance 4.1 0.2 −0.5 2.5 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 Plot from the PerformanceAnalytics package Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 6 / 128 .10 −0.0 3.0 −0.0 2.05 0.0 HAM1 EDHEC LS EQ SP500 TR Cumulative Return Monthly Return Drawdown Data manipulation Data analysis Statistical modeling Data visualization −0.

† Mathsoft. Tibco. Inc.The R Project for Statistical Computing † Founded by UW Professor Doug Martin. Inc. 2006 Computational Finance & Risk Management R Introduction 7 / 128 . Inc. R . CompFin Program Director Guy Yollin (Copyright © 2012) Figure from The History of S and R. John Chambers.S language implementations R is the most recent and full-featured implementation of the S language Original S . Insightful.AT & T Bell Labs S-PLUS (S plus a GUI) Statistical Sciences. Inc.

4.R timeline 1991 Statistical Models in S (white book) S3 methods 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 1999 John Chambers 1998 ACM Software Award 2001 R 1.0 (S3) 2002 Modern Applied Statistics with S 4th Edition (S+ 6. 5.5. R 1.x) (R complements) 1998 1999 Exponential growth of R Users and R packages S era S-PLUS era R era Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 8 / 128 .x) (S+ 5.0 (S4) 1993 R on Statlib 1997 R on CRAN GNU Project 2000 R 1. 1988 StatSci Licenses S 1993 Modern Applied Statistics with S-PLUS 1994 Modern Applied Statistics with S-PLUS 2nd Edition 1997 Modern Applied Statistics with S-PLUS Programming with Data 3rd Edition (Green Book) (S+ 2000.x. Inc.0 2010 R&R Given ASA Statistical Computing and Graphics Award 1976 2011 S-PLULS developed by Statistical Sciences.0.0) 2004 R 2.0.

visualize.Recognition of software excellence Association for Computing Machinery John Chambers received the 1998 ACM Software System Award Dr. and manipulate data American Statistical Association Robert Gentleman and Ross Ihaka received the 2009 ASA Statistical Computing and Graphics Award In recognition for their work in initiating the R Project for Statistical Computing Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 9 / 128 . Chambers’ work will forever alter the way people analyze.

Austria which is responsible for developing and maintaining R Hold and administer the copyright of R software and documentation Support continued development of R Organize meetings and conferences related to statistical computing Officers Presidents Secretary Treasurer At Large Auditors Robert Gentleman. Martin Maechler Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 10 / 128 .The R Foundation The R Foundation is the non-profit organization located in Vienna. Ross Ihaka Friedrich Leisch Kurt Hornik John Chambers Peter Dalgaard.

The R Core Team Douglas Bates – University of Wisconsin Madison John Chambers – Stanford University Peter Dalgaard – University of Copenhagen Seth Falcon – Fred Hutchinson Cancer Research Center Robert Gentleman – Genetech Kurt Hornik – Vienna University of Economics and Business Stefano Iacus – University of Milan Ross Ihaka – University of Auckland Friedrich Leisch – Ludwig–Maximilians –University Munich Uwe Ligges – TU Dortmund University Thomas Lumley – University of Auckland Martin Maechler – ETH Swiss Federal Institute of Technology Zurich Duncan Murdoch – University of Western Ontario Paul Murrell – University of Auckland Martyn Plummer – International Agency for Research on Cancer Brian Ripley – University of Oxford Deepayan Sarkar – Fred Hutchinson Cancer Research Center Duncan Temple Lang – University of California Davis Luke Tierney – University of Iowa Simon Urbanek – AT & T Research Labs Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 11 / 128 .

data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 12 / 128 2 3 . working directory.Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data manipulation.

EPRI Solutions. 2.numeric(x). use as. Connections can include files.character(x). rep(c(1.) generic function to combine arguments with the default forming a vector.delim("filename".complex(x).. Help and basics Most R functions have online documentation."d"). and others cbind(.null(x). usually a statistical summary but it is generic meaning it has different operations for different classes of a ls() show objects in the search path.frame(v=1:4.. Inc.names=NA) For database interaction. use methods(as) Variable information is.frame(x)."the")] elements in the given set Indexing matrices x[i. attach(x) database x to the R search path.start() start the HTML version of help str(a) display the internal *str*ucture of an R object summary(a) gives a “summary” of a...c(1. see packages RODBC.which) get or set the attribute which of x attributes(obj) get or set the list of attributes of obj Data selection and manipulation which. use col. “:” has operator priority. zipped files.) saves the specified objects (. shorter vectors are recycled to the length of the longest list(.times) replicate x times. On windows.. and ROracle. and R variables.2. generic. for a complete list.."c". library(help=x) lists datasets and functions in package x.array(x).col. as. elements of x recycle if x is not long enough matrix(x. use comment.) set or examine many global options. elements of x recycle factor(x. eol is the end-of-line separator. by columns 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. use header=TRUE to read the first line as a header of column names. use skip=n to skip n lines before reading data. the file connection can also be used with description = "clipboard". Includes material from R for Beginners by Emmanuel Paradis (with permission). use write. is..j] column j x[. See packages XML. is.names=TRUE.sep="\t". and others read. digits.delim("clipboard") To write a table to the clipboard for Excel..sep="". NROW(x) is the same but treats a vector as a onerow matrix ncol(x) and NCOL(x) id.search("topic") search the help system apropos("topic") the names of all objects in the search list matching the regular expression ”topic” help. file=""..str() str() for each variable in the search path dir() show files in the current directory methods(a) shows S3 methods of a methods(class=class(a)) lists all the methods to handle objects of class a options(. class(x) <.2). Use search() to show the search path. error library(x) load add-on packages. file="" means the standard input or output.) prints its arguments. tshort@eprisolutions. x can be a list.c=3i).) in the XDR platformindependent binary format save.. common ones: width. useful for for loops rep(x.table(x. breaks is the number of cut intervals or a vector of cut points Definitely obtain these PDF files from the R homepage or a CRAN mirror Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 13 / 128 .ch=c("a".frame’. the default separator sep="" is any whitespace... meaning it can have different methods for different objects format(x. sep is the character separator between arguments print(a. but with defaults set for reading tab-delimited files read. help(topic) documentation on topic ?topic id.4. widths is an integer vector. or R data file created with save. for columns class(x) get or set the class of x. hdf5. rep(c(1. to sort in decreasing order: rev(sort(x)) cut(x.na(x).. with recursive=TRUE descends through lists combining all elements into one vector from:to generates a sequence.3).5” seq(from. Slicing and extracting data Data creation Input and output load() load the datasets written with save data(x) loads specified data sets read..names=NA to add a blank column header to get the column headers aligned correctly for spreadsheet input sink(file) output to file..."clipboard".b="hi".col.M. .) create a data frame of the named or unnamed arguments.header=TRUE) id..] row i x[. x can be a name or character string of an object previously attached or a package.. list(a=c(1. if quote is TRUE.] row named "name" Indexing data frames (matrix indexing plus the following) x[["name"]] column named "name" x$name id. help. cat(. netCDF for reading other file formats.length=n*k. .3. character or factor columns are surrounded by quotes ("). use each= to repeat “each” element of x each times.fwf(file.frame(x). ."and".is=FALSE) read a table of f ixed width f ormatted data into a ’data. NA treatment.. D.file=""."B".4. convert type. data.breaks) divides x into intervals (factors). but with defaults set for reading comma-delimited files read. column j x[i.header=TRUE) id. 1:4 + 1 is “2. for a complete list..csv("filename".each=2) is 1 1 2 2 3 3 data.dim=) array with data x.. is.n=10). Venables.complex(x). This can often be a character string naming a file or a connection.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".to) generates a sequence by= specifies increment. is.) combine arguments by rows for matrices. DBI.) create a list of the named or unnamed arguments. use methods(is) length(x) number of elements in x dim(x) Retrieve or set the dimension of an object. giving the widths of the fixed-width fields save(file. is.N.max(x) returns the index of the greatest element of x which. use x <.data. and n is the number of replications expand..read.names=TRUE.data. sep is the field separator. detach(x) x from the R search path.levels=) encodes a vector x as a factor gl(n.row.array(x). RPgSQL.is=TRUE to prevent character vectors from being converted to factors. array(x. sep=" ") prints x after converting to a data frame.. RMySQL. length= specifies desired length seq(along=x) generates 1.labels=1:n) generate levels (factors) by specifying the pattern of their levels.) id.2) is 1 2 3 1 2 3.2). as. data frame. until sink() Most of the I/O functions have a file argument. specify dimensions like dim=c(3. Variable conversion as. length(x). as. test for type.k. see the help for options on row naming.grid() a data frame from all combinations of the supplied vectors or factors rbind(. na is the string for missing values.frame(.3)] columns 1 and 3 x["name"... sep=" ") prints the arguments after coercing to character. Smith R Development Core Team R Reference Card Tom Short R Reference Card by Tom Short.2) dimnames(x) Retrieve or set the dimension names of an object nrow(x) number of rows. as. pipes.. See www.c(3.image(file) saves all objects c(. is.. ...2.Essential web resources An Introduction to R W..logical(x).nrow=. 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. as.min(x) returns the index of the smallest element of x rev(x) reverses the elements of x sort(x) sorts the elements of x in increasing order..header=FALSE.3).table(file) reads a file in table format and creates a data frame from it.char="" to prevent "#" from being interpreted as a comment.character(x).j] element at row i..numeric(x)."myclass" unclass(x) remove the class attribute of x attr(x. specify pat="pat" to search on a pattern ls. To read a table copied from Excel.table(x.Rpad. k is the number of levels.widths. data frames.com 2005-07-12 Granted to the public domain. dim(x) <.) format an R object for pretty printing write.org for the source and latest version.as..ncol=) matrix.

2011 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 14 / 128 .R language references R in a Nutshell: A Desktop Quick Reference Joseph Adler O’Reilly Media. 2009 The Art of R Programming Norman Matloff No Starch Press.

2008 A Beginner’s Guide to R Zuur. Ieno. Meesters Springer.Other introductory R texts Introductory Statistics with R 2nd Edition P. Dalgaard Springer. 2009 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 15 / 128 .

David Hiebeler has created a MATLAB/R cross reference document: http://www.pdf For those with experience in SAS. Robert Muenchen has written R books for this audience: http://r4stats.com Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 16 / 128 . SPSS.umaine.edu/~hiebeler/comp/matlabR.math.Experience with other statistical computing languages For those with experience in MATLAB. or Stata.

data manipulation.Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 17 / 128 2 3 . working directory.

exe from command prompt Or run Rterm.exe in batch mode The R GUI on a Windows platform Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 18 / 128 .The R GUI Running R in Windows Typically run Rgui.exe Can also run R.

Interactive R session R is an interpreted language The R GUI is an interactive command driven environment type R commands at the R GUI console Run previously created R scripts (R commands in a text file) Commands entered interactively into the R console Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 19 / 128 .

1323967 0.rnorm(n=2) > r [1] 0.7183 Special purpose assignment global assignment operator: <<- > s = sqrt(2) > s [1] 1.7617530 Deprecated assignment operator underscore character: _ Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 20 / 128 .5 > y [1] 5 > assign("e".Assigning values to variables Typical variable assignment assignment operator: <assignment function: assign equal sign: = must be used to assign arguments in a function call R Code: Variable assignment > y <.7183) > e [1] 2.414214 > r <.2.

"San Francisco") ls() "e" "y" "filename" "m" "r" "s" "tab" [1] "cities" [8] "x" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 21 / 128 .7183) m <.1416.matrix(rnorm(9)."airport").frame(store=c("downtown".c(3.17.2.24)) cities <.Object orientation in R Everything in R is an Object Use functions ls and objects to list all objects in the current workspace R Code: Listing objects > > > > > x <.data."Portland".sales=c(32."eastside".c("Seattle".nrow=3) tab <.

7183 > typeof(x) [1] "double" > cities [1] "Seattle" "Portland" [3] "San Francisco" > typeof(cities) [1] "character" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 22 / 128 .1416 2.Data types All R objects have a type or storage mode Use function typeof to display an object’s type Common types are: double character list integer R Code: Object type (storage mode) > x [1] 3.

] 1.5250413 0.2847256 -0.frame" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 23 / 128 .] 1.6394314 -0.] 0.03179198 > class(m) [1] "matrix" > tab store sales 1 downtown 32 2 eastside 17 3 airport 24 > class(tab) [1] "data.1] [.2] [.52021809 [2.80289579 [3.8032637 0. basic classes are: numeric character data.frame matrix > m [.Object classes R Code: Object class All R objects have a class Use function class to display an object’s class There are many R classes.6197558 0.3] [1.6412772 2.

4142 1."euler".617924 > 10*constants pi euler sqrt2 golden 31.142 16. the combine function most places where single value can be supplied."sqrt2".999962 2."golden") > constants pi euler sqrt2 golden 3.416 27.1.1416.6180 > constants^2 pi euler sqrt2 golden 9.1416 2.c(3.1.183 14.180 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 24 / 128 .6180) > names(constants) <.7183 1.Vectors R is a vector/matrix language vectors can easily be created with c. a vector can be supplied and R will perform a vectorized operation R Code: Creating vectors and vector operations > constants <.c("pi".389155 1.7183.4142.869651 7.2.

6180 > constants > 2 pi TRUE euler TRUE sqrt2 golden FALSE FALSE Vectors indices are placed with square brackets: [] Vectors can be indexed in any of the following ways: vector of positive integers vector of negative integers vector of named items logical vector > constants[constants > 2] pi euler 3.7183 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 25 / 128 .3.Indexing vectors R Code: Indexing vectors > constants[c(1.-2)] sqrt2 golden 1.4142 1.4)] pi sqrt2 golden 3."golden")] pi golden 3.4142 1.1416 1.6180 > constants[c(-1.6180 > constants[c("pi".1416 1.1416 2.

0000 1.6180 > constants*2 pi euler sqrt2 golden 6.The recycling rule When 2 vectors of unequal length are involved in an operation.1) pi euler sqrt2 golden 0.1.1416 2.4366 2.4142 1.2) pi euler sqrt2 golden 0.7183 0.7183 2.8284 0.0000 Guy Yollin (Copyright © 2012) last input generates a warning: longer object length is not a multiple of shorter object length Computational Finance & Risk Management R Introduction 26 / 128 . the shorter one is recycled to equal the length of the longer vector R Code: Illustration of recycling > constants pi euler sqrt2 golden 3.0000 2.7183 1.2360 > constants*c(0.0000 2.6180 > constants*c(0.8284 3.2832 5.

length.Sequences An integer sequence vector can be created with the : operator A general numeric sequence vector can be created with the seq function R Code: seq arguments > args(seq. along.) NULL from starting value to ending value by increment len length of sequence Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 27 / 128 . by = ((to . to = 1.1)).out ..from)/(length.out = NULL.with = NULL.default) function (from = 1.. .

5 20.25 0.75 1.0 12.to=1.0 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 28 / 128 .00 0.5) [1] 0.0 17.50 0.0 2.5 10.5 5.Sequences R Code: Creating sequences > 1:5 [1] 1 2 3 4 5 > -5:5 [1] -5 -4 -3 -2 -1 0 1 2 3 4 5 > seq(from=0.to=20.00 > seq(from=0.len=5) [1] 0.5 15.by=2.0 7.

0.10.5 10.5 5.0 > seq(0.10) [1] 0 2 4 6 8 10 > seq(0.0 7.2) [1] 0 2 4 6 8 10 > seq(by=2.0 2.10.10) [1] 0 1 2 3 4 5 6 7 8 9 10 R Introduction 29 / 128 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management .Passing arguments to functions unnamed arguments are assigned according to their position named arguments are assigned according to their name and can be in any position partial name matching is performed arguments with default values are not required to be passed R Code: Illustration of flexibility in passing arguments > seq(0.len=5) [1] 0.

default) function (x.g.." argument Many functions include in their argument list a .) NULL This is a mechanism to allow additional arguments to be passed which will subsequently be passed on to a sub-function that the main function will call An example of this would be passing graphic parameters (e... xlim = NULL.The ". R Code: The plot function arguments > args(plot.last = NULL. panel. axes = TRUE. lwd=2) to the plot function which will subsequently call and pass these arguments on to the par function Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 30 / 128 . .first = NULL. xlab = NULL. type = "p". frame. main = NULL.. log = "".. sub = NULL. y = NULL. asp = NA. ylim = NULL. ylab = NULL. panel. ann = par("ann").plot = axes..

The rep function The rep function is used to create (or initialize) vectors R Code: Examples of rep > rep(0. each = 2. each = 2) [1] 1 1 2 2 3 3 4 4 > rep(1:4.1)) [1] 1 1 2 3 3 4 > rep(1:4. each = 2. times = 3) # length 24.1. 2) # repeat pattern 2 times [1] 1 2 3 4 1 2 3 4 > rep(1:4. # repeat each element 2 times [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 31 / 128 . 3 complete replications # 8 integers plus two recycled 1 s.2. c(2. len = 10) [1] 1 1 2 2 3 3 4 4 1 1 > rep(1:4.10) # initialize a vector [1] 0 0 0 0 0 0 0 0 0 0 > rep(1:4.

formula" "plot.Generic functions A generic function behaves in a way that is appropriate based on the class of its argument.HoltWinters" "plot.histogram" "plot.frame" "plot.factor" "plot.ecdf" "plot.decomposed.dendrogram" "plot.default" "plot.acf" "plot.data. for example: plot print summary R Code: Some classes handled by the plot function > methods(plot)[1:15] [1] [4] [7] [10] [13] "plot.function" "plot.ts" "plot.density" "plot.isoreg" "plot.lm" generic functions implement simple polymorphism for S3 objects Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 32 / 128 .hclust" "plot.

graphics. Matrix. methods. grDevices. etc. foreign. Additional packages can be downloaded through the R GUI or via the install. rpart.packages function When R is initially loaded. cluster. MASS. only core R packages are loaded by default Additional packages are loaded via the library command Packages datasets are made accessible via the data command Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 33 / 128 . utils.R packages All R functions are stored in packages The standard R distribution includes core packages and recommended packages: Core R packages base. datasets Recommended packages boot. stats.

source. envir = . help.conflicts = TRUE.searching.GlobalEnv) NULL > library(nutshell) > data(top. package = NULL.pkgs"). verbose = getOption("verbose")) NULL > args(data) function (..] city rank 1 Seattle 100 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 34 / 128 . pos = 2.bacon. warn.searching.bacon.loc = NULL.cities) > top. lib..cities[1.only = FALSE. verbose = getOption("verbose"). lib.source = getOption("keep. character.Loading packages and data into your R session The library and data functions are used to load additional libraries and data into the current R session R Code: The library and data function > args(library) function (package.return = FALSE. list = character(). keep. logical.loc = NULL.. quietly = FALSE.

contriburl = contrib. libs_only = FALSE. repos="http://cran. dependencies = NA. configure. configure.. . INSTALL_opts. type). lib.args = getOption("configure. type = getOption("pkgType"). available = NULL.packages) function (pkgs.Installing contributed packages The install.packages function > args(install. Ncpus = getOption("Ncpus". clean = FALSE.org") Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 35 / 128 .url(repos.packages function can be used to install contributed packages R Code: The install.fhcrc.) NULL > install. repos = getOption("repos").args").packages("nutshell".. 1L). destdir = NULL. method.vars = getOption("configure.packages("nutshell") > # or if repository needs to be specified > install.vars").

Installing contributed packages Packages can also be installed through the R GUI Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 36 / 128 .

Packages for basic computational finance The following R add-on packages are recommended for computational finance: Package zoo tseries PerformanceAnalytics quantmod xts Description Time series objects Time series analysis and computational finance Performance and risk analysis Quantitative financial modeling framework Extensible time series Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 37 / 128 .

761905 expression Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 38 / 128 . in fact.105.545455 -4. .110.) R Code: Create a user-defined function > percentChange <.. arg_2.000000 4.c(100.function(arg_1.761905 -4.100) > percentChange(sales) [1] 5. much of R is written in R The syntax for defining a new R function is as follows: name <.105.function(x) { 100*(x[-1]/x[-length(x)]-1) } > sales <.Writing R functions One of the strengths of R is that it can easily be extended by writing new R functions.

Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 39 / 128 2 3 . data manipulation. working directory.

"OR")) Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 40 / 128 .1416.1416 $euler [1] 2.ncol=2).6180) > class(myList) [1] "list" > length(myList) [1] 3 > myList $pi [1] 3.The list object A list object is a container that can hold other objects of different types R Code: Creating lists > myList <.random=matrix(rnorm(4).golden=1.618 > diverseList <.7183 $golden [1] 1. state=c("WA".list(pi=3.euler=2.7183.list(magic=myList.

[[]].Accessing items in a list Items in a list can be accessed using [].7183 > myList[["pi"]] [1] 3.618 > diverseList[[3]][2] [1] "OR" [[]] returns a single element single integer single name $ returns a single element single name Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 41 / 128 .7183 > myList[[2]] [1] 2.1416 > myList$golden [1] 1. or $ syntax as follows: [] returns a sublist vector of positive integers vector of named items logical vector R Code: Indexing lists > myList[2] $euler [1] 2.

2008[.4]) [1] "numeric" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 42 / 128 .2008[1:2.frame object > data(batting.frame is a 2D matrix-like object where the columns can be of different classes.2008) [1] "data.The data.2008) > class(batting.frame object R Code: The data.2008[. for example: column column column column with with with with dates characters integers numeric [1] 1384 32 > batting.2008) A data.1:4] 1 2 nameLast nameFirst weight height Abreu Bobby 200 72 Alou Moises 190 75 > class(batting.3]) [1] "integer" > class(batting.2]) [1] "character" > class(batting.frame" > dim(batting.2008[.

38 74.68 74.72 High 74.56 Volume Adj.53 13450900 32.88 33.05 DIS 2008-09-22 33.25 75.89 75.54 4387900 74.05 32.77 13600300 31.50 74.64 Low 73.68 75.50 74.frame")) function (x... n = 6L.12 75.88 74.49 74.63 73.56 > tail(dow30.54 74.Close 2560400 74.59 31.76 74.85 34.59 32.34 74.) NULL > data(dow30) > head(dow30) 1 2 3 4 5 6 symbol MMM MMM MMM MMM MMM MMM Date 2009-09-21 2009-09-18 2009-09-17 2009-09-16 2009-09-15 2009-09-14 Open 73.32 32."data.89 2722500 75.84 32.3) 7480 7481 7482 symbol Date Open High Low Close Volume Adj.00 73.91 18394300 32.15 32.38 3566900 74.42 Close 74.68 3466400 74.42 Computational Finance & Risk Management R Introduction 43 / 128 Guy Yollin (Copyright © 2012) .45 75.30 DIS 2008-09-23 32.50 74.63 31.62 74.62 3371500 74.Close DIS 2008-09-24 32.91 74. .91 75.The head and tail functions R Code: The head and tail functions > args(getS3method("head".

Size-related and diagnostic helper functions R has a number of size related and diagnostic helper functions Function dim nrow ncol length head tail str Description return dimensions of a multidimensional object number of rows of a multidimensional object number of columns of a multidimensional object length a vector or list display first n rows (elements) display last n rows (elements) summarize structure of an object Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 44 / 128 .

frames and matrices R has extremely powerful data manipulation capabilities especially in the area of vector and matrix indexing data. their columns can be accessed with the $ operator as well Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 45 / 128 .Indexing data.frames are stored internally as lists.frames and matrices can be indexed in any of the following ways vector of positive integers vector of negative integers character vector of columns (row) names a logical vector Since data.

their columns can be accessed with the $ operator as well Guy Yollin (Copyright © 2012) > dow30[dow30[."Volume"]>1."Volume")] 2047 2049 2159 symbol Date Close Volume C 2009-08-07 3.62 MMM 2009-09-17 74."Date".frames and matrices can be indexed in any of the following ways vector of positive integers vector of negative integers character vector of columns (row) names a logical vector 1 2 3 4 MMM MMM MMM MMM 2009-09-21 2009-09-18 2009-09-17 2009-09-16 74.89 MMM 2009-09-16 75."Close")] the area of vector and matrix symbol Date Close indexing data.3) 2 3 4 symbol Date Close MMM 2009-09-18 74. c("symbol".62 74.50 1868209400 R Introduction 46 / 128 Computational Finance & Risk Management .58 2672492000 C 2009-02-27 1.frames and matrices R has extremely powerful data manipulation capabilities especially in R Code: Indexing 2D objects > dow30[1:4.85 1898814600 C 2009-08-05 3.c(1.38 Since data.6)].frames are stored internally as lists.Indexing data.5e9."Close".54 74."Date".c("symbol".89 75.38 > head(dow30[-1.2.

"dog"."fish".Factors A factor is a data type for representing categorial data R Code: Create a factor variable > pet.as.c("dog".str <."cat"."cat"."rabbit") > pets <.factor(pet.numeric(pets) [1] 2 1 1 2 3 2 4 > levels(pets) [1] "cat" "dog" "fish" "rabbit" fish dog rabbit factors are encoded as integers (starting at 1) the levels of a factor variable contain the categorical labels Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 47 / 128 ."dog".str) > pets [1] dog cat cat dog Levels: cat dog fish rabbit > as.

15.home()) > getwd() [1] "C:/R/R-2.1" > setwd(my.getwd()) [1] "C:/Rprojects/UW/RIntro" > setwd(R. R reads and writes files to the working directory R Code: Getting and setting the working directory > (my.wd <.The working directory Unless overridden by a filename which includes a path. so to use backslash in a string enter it as “\\" The forward slash character "/" can also be used as a directory separator on windows systems Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 48 / 128 .wd) > getwd() [1] "C:/Rprojects/UW/RIntro" The backslash character “\" in a character string is used to begin an escape sequence.

The working directory The working directory can also be changed from the R GUI Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 49 / 128 .

names.is tells R not to convert strings into factors Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 50 / 128 . dec = ". fill = !blank. comment. encoding = "unknown". header = FALSE. nrows = -1.skip = TRUE.lines. allowEscapes = FALSE. na.is = !stringsAsFactors. quote = "\" ".g. colClasses = NA.skip.The read. flush = FALSE. blank. col. sep = "".table arguments > args(read.".char = "#".strings = "NA". check.table) function (file.names.lines.table function is used extensively to load data into R R Code: read. as.white = FALSE. fileEncoding = "". row. text) NULL file file name (with path if necessary) header sep TRUE/FALSE if there are column names in the file column separation character (e. stringsAsFactors = default. comma or tab) as. strip.stringsAsFactors(). skip = 0.table function The read.names = TRUE.

54 53482300 19.41 19.36 Close 19.22 19.55 R Introduction 51 / 128 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management .75 19.Reading a text file R Code: Read csv file > dat <.63 19.70 19.76 19.44 19.47 19.is=TRUE) > dat[1:5.52 19.71 Low 19.54 High 19.sep=".70 19.42 19.read.55 Volume Adj.header=TRUE.65 19.Close 36132900 19.89 19.".] 1 2 3 4 5 Date 2009-09-21 2009-09-18 2009-09-17 2009-09-16 2009-09-15 Open 19.65 48173000 19.56 64594100 19.41 49427600 19.54 19.as.table("intc.56 19.csv".37 19.

names = TRUE. vector) file name (with path if necessary) column separation character (e. matrix. col.names object to be written (data.frame.table) function (x.g. file = "".". row. fileEncoding = "") NULL > args(write) function (x. comma or tab) write row names (T/F) write col names (T/F) Computational Finance & Risk Management R Introduction 52 / 128 Guy Yollin (Copyright © 2012) . append = FALSE. qmethod = c("escape".names = TRUE. sep = " ") NULL x file sep row.table and write are used to write text files R Code: write. "double").names col. sep = " ".Writing text files The functions write. ncolumns = if (is. append = FALSE.table and write arguments > args(write.character(x)) 1 else 5. eol = "\n". dec = ". file = "data". quote = TRUE. na = "NA".

][d][a][t]".names=T))[.row.info(list.dat".sep="\t".Writing text files R Code: Write text files > write(x=constants.file="vector.full.dat .table(x=m.names=F.sep="\t") > write.dat size mtime 164 2012-08-31 08:15:15 28 2012-08-31 08:15:15 note the use of the list.files(pattern="[. the file./matrix.names=F) > file./vector.c("size"."mtime")] .file="matrix.files function.dat".col.info function. and the use of regular expressions Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 53 / 128 .

etc. vector. data.frame or matrix rownames row names of a data.frame or matrix colnames column names of a data. dimnames gets the row and column names of a data.Functions to examine objects and their structures These functions help to query and unpack an object class query an objects class str attributes reports structure of an object returns list of objects attributes attr get/set attributes of an object names gets the names of a list.frame or matrix dput makes an ASCII representation of an object unclass removes class attribute of an object unlist converts a list to a vector Computational Finance & Risk Management R Introduction 54 / 128 Guy Yollin (Copyright © 2012) .frame.

b..The paste function The paste function concatenates (pastes) strings and numerical values together its like a flexible version of sprintf R Code: The paste function > args(paste) function (. collapse = NULL) NULL > a <.. sep = " ". a+b. b <. a.1:5.sep="") [1] "variable1" "variable2" "variable3" "variable4" "variable5" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 55 / 128 .2 > paste("We know that: ". " = ". sep ="") [1] "We know that: 2 + 2 = 4" > paste("variable".2. " + "..

] [2.] [3. MARGIN. FUN.) NULL > set.2.The apply function The apply function is an extremely useful function that applies a given function across the rows and/or columns of a matrix R Code: The apply function > args(apply) function (X.] [. .2] [.sum) [1] 17 12 16 There are a number of apply related functions.matrix(sample(9).ncol=3)) [1.seed(1) > (m <.3] 3 6 8 9 2 7 5 4 1 > apply(m. one mark of mastering R is mastering apply related functions Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 56 / 128 ...1] [.

S4 Classes S4 classes are a more modern implementation of object-oriented programming in R compared to S3 classes Data in an S4 class is organized into slots. slots can be accessed using: the @ operator: object@name the slot function: slot(object."fGARCH") Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 57 / 128 .name) Methods for an S4 class can be queried with the showMethods function showMethods(class = "fGARCH") Methods can be retrieved/viewed with the getMethod function getMethod("predict".

working directory. data manipulation.Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 58 / 128 2 3 .

start function R GUI menu item Help|Html help R Code: Starting HTML help > help.1:12534/doc/html/index.The HTML help system R has a comprehensive HTML help facility Run the help. you should open http://127.0.start() If nothing happens.0.html yourself Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 59 / 128 .

The help function Obtain help on a particular topic via the help function help(topic) ?topic R Code: Topic help > help(read.table) Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 60 / 128 .

search function Search help for a particular topic via the help.search(topic) ??topic R Code: Search help > ??predict Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 61 / 128 .search function help.The help.

data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 62 / 128 2 3 . working directory.Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data manipulation.

R Homepage http://www.org List of CRAN mirror sites Manuals FAQs Mailing Lists Links Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 63 / 128 .r-project.

fhcrc.Comprehensive R Archive Network http://cran.org CRAN Mirrors About 88 sites worldwide About 19 sites in US R Binaries R Packages R Sources Task Views Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 64 / 128 .CRAN .

CRAN Task Views Organizes the 3500+ R packages by application Finance Time Series Econometrics Optimization Machine Learning Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 65 / 128 .

Stackoverflow Stackoverflow has become the primary resource for help with R http://stackoverflow.com/ Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 66 / 128 .

R-SIG-FINANCE Nerve center of the R finance community Daily must read Exclusively for Finance-specific questions. not general R questions https://stat.ch/mailman/listinfo/r-sig-finance Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 67 / 128 .ethz.

googlecode.com/svn/trunk/ google-r-style.html Naming convention Coding Syntax Program Organization Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 68 / 128 .Google’s R Style Guide http://google-styleguide.

net Introductory R Lessons R Interface Data Input Data Management Basic Statistics Advanced Statistics Basic Graphs Advanced Graphs Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 69 / 128 .Quick R http://www.statmethods.

R graphics details. colors. and other tech notes R Graphics and other useful information by Earl Glynn of Stowers Institute for Medical Research URL http://research.org/efg/R/index. margins.htm Features R Color Chart Using Color in R (great presentation) Plot area. multiple figures Mixture models Distance measures and clustering Using Windows Explorer to Start R with Specified Working Directory (under tech notes) Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 70 / 128 .stowers-institute.

ucr.Programming in R Online R programming manual from UC Riverside URL http://manuals.edu/home/programming-in-r Selected Topics R Basics Finding Help Code Editors for R Control Structures Functions Object Oriented Programming Building R Packages Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 71 / 128 .bioinformatics.

univie.ac.Statconn http://rcom.at COM interface for R connectivity Excel Word C# VB Delphi Download site for RAndFriends R Statconn Notepad++ Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 72 / 128 .

fr/graphiques Blog from David Smith of Revolution http://blog.r-bloggers.psych.Other useful R sites R Bloggers R Site Search R Seek R Graph Gallery Revolution Blog Inside-R Aggregation of about 290 R blogs http://www.rseek.html R specific search site http://www.revolutionanalytics.inside-r.com R community site by Revolution Analytics http://www.org Computational Finance & Risk Management R Introduction 73 / 128 Guy Yollin (Copyright © 2012) .edu/search.org/ Examples of many possible R graphs http://addictedtor. vignettes.upenn.free. R-help http://finzi.com Search R function help.

Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 74 / 128 2 3 . working directory. data manipulation.

RStudio RStudio is a fully-featured open-source IDE for R R language highlighting Paste/Source code to R object explorer graphics window in main IDE RStudio also provides a server-based version (R running in the cloud) Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 75 / 128 .

Revolution R Enterprize Visual Development Environment Revolution Analytics is a company that sells a commercial distribution of R including a desktop IDE Revolution R Enterprize is free to academic users R language highlighting Paste/Source code to R object explorer runs R in SDI mode http://www.revolutionanalytics.com Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 76 / 128 .

org/Config/modes/R-Sweave.winedt.com http://www.winedt.WinEdt and R-Sweave Based on WinEdt. an excellent shareware editor with support for A L TEX and Sweave development R language highlighting Paste/Source code to R Supports R in MDI mode Paste/Source code to S-PLUS http://www.php Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 77 / 128 .

An Eclipse Plug-In for R StatET is a plug-in for the open-source Eclipse development environment R language highlighting Paste/Source code to R Source code debugger Supports R in SDI mode Excellent documentation by Longhow Lam http://www.walware.StatET .de/goto/statet Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 78 / 128 .

html Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 79 / 128 .univie.Notepad++ and NpptoR NpptoR is an automation widget (based on AuotHotkey) which allows the very useful program editor Notepad++ to interact with R R language highlighting Paste/Source code to R Supports R in SDI mode Can be installed as part of RAndFriends http://notepad-plus-plus.org http://sourceforge.at/download.ac.net/projects/npptor http://rcom.

sciviews.org R GUI Projects http://www.sciviews.org/_rgui ESS other Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 80 / 128 .org/Tinn-R Emacs Speaks Statistics http://ess.r-project.Other R IDE links Tinn-R Popular R IDE http://www.

Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data manipulation. working directory. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 81 / 128 2 3 .

Basic plotting functions Function plot lines segments points text abline curve legend matplot par Description generic function to plot an R object adds lines to the current plot adds lines line segments between point pairs adds points to the current plot adds text to the current plot adds straight lines to the current plot plot a function over a range adds a legend to the current plot plot all columns of a matrix sets graphics parameters Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 82 / 128 .

ann = par("ann"). ylab = NULL.The plot function The plot function is a generic function for plotting of R objects R Code: plot arguments > args(plot. asp = NA. frame.default) function (x.) NULL x y xlim/ylim xlab/ylab main type col asp vector to be plotted (or index if y given) vector to be plotted x & y limited x & y axis labels plot title (can be done with title function "p" = points (default). panel. axes = TRUE. xlim = NULL. sub = NULL..last = NULL. main = NULL. panel.first = NULL. . "n" = no plot color or bars control the aspect ratio Computational Finance & Risk Management R Introduction 83 / 128 Guy Yollin (Copyright © 2012) . "l" = lines..plot = axes. xlab = NULL. y = NULL. log = "". type = "p". "h" = bars. ylim = NULL.

8 1. "rf"] 0.2 0.4 0."rf"]) q q q q q q q qq qq qq q q q q q q q q q qq q q q q q q q q q q q qq q q q q q q q qq q qq q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q qq qq q q qq q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q qq q q q qq qq q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q qq q q q q q q q q q q q q q q q q q q q q q qq q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q qqq q q q qq q q q q q q qq q q q q q q q q q q qq q q q q q q q q q q q q q qq q q q q q qq q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q Capm[.6 0.2 0 100 200 Index 300 400 500 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 84 / 128 .The plot function R Code: Plot with defaults > library(Ecdat) > data(Capm) > plot(Capm[.0 1.

type="l") Capm[.6 0.2 100 200 Index 300 400 500 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 85 / 128 .The plot function R Code: Plot lines > plot(Capm[.4 0.8 1."rf"]. "rf"] 0.2 0 0.0 1.

The plot function
R Code: Plot bars
> plot(Capm[,"rmrf"],type="h")

Capm[, "rmrf"]

−20 0

−10

0

10

100

200 Index

300

400

500

Guy Yollin (Copyright © 2012)

Computational Finance & Risk Management

R Introduction

86 / 128

The plot function
R Code: XY plot
> plot(Capm[,"rmrf"],Capm[,"rcon"])

q

20

q

−20

qq

q q qq q q q q q qqq q q q q q q q q qq q q qq q qq q q q q q q q q q q q q q q q q q q q q q qq qq q q q q q q qq q q q q q q q q q q qq q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q qq q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q qq q qq q qq q qq q q q q q q q q q q q q q q q q qq q qq q q q q q q q q qq q q q q q q qq q q q q q q q qq qq q qq q q q qq q q q q

q q q q q

Capm[, "rcon"]

−30

−10
q

0

10

−20

−10

0 Capm[, "rmrf"]

10

Guy Yollin (Copyright © 2012)

Computational Finance & Risk Management

R Introduction

87 / 128

The points function

The points function adds points to the current plot at the given x, y coordinates
R Code: points arguments
> args(points.default) function (x, y = NULL, type = "p", ...) NULL

x vector of x coordinates y vector of y coordinates

Guy Yollin (Copyright © 2012)

Computational Finance & Risk Management

R Introduction

88 / 128

The lines function

The lines function adds connected line segments to the current plot
R Code: lines arguments
> args(lines.default) function (x, y = NULL, type = "l", ...) NULL

x vector of x coordinates y vector of y coordinates

Guy Yollin (Copyright © 2012)

Computational Finance & Risk Management

R Introduction

89 / 128

vfont = NULL.) NULL x/y labels adj pos offset location to place text text to be display adjustment of label at x.default) function (x. . y coordinates R Code: text arguments > args(text. cex = 1. font = NULL. y offset from pos Computational Finance & Risk Management R Introduction 90 / 128 Guy Yollin (Copyright © 2012) .5.. labels = seq_along(x). y location position of text relative to x. adj = NULL. y = NULL. col = NULL.The text function The text function adds text labels to a plot at given x. offset = 0. pos = NULL..

xlab="market return".Plotting a blank frame R Code: Plotting a blank frame > plot(0.xlim=c(-20.ylab="construction return") construction return −20 −20 −10 0 10 20 −10 0 market return 10 20 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 91 / 128 .type="n".20).20).ylim=c(-20.

xlab="market return".xlim=c(-20.20).type="n".pos=2) q 20 slope = 1 q q qq q q qq q q q q q q qqq q qq qq qq q q qq q qq q q q q q q q q q q q q q q q q q q q q q q qq qq q qq q q q q qq q q q q q qq q qqq q q qq qq qq q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q q q q qq q q q q q q q q q q q q q q q q q qq q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q q q qq q q q q q q q q q q q q q q q q q q q q q q q q q q qq q q q q q qq q q q q q q q q q q q q qq q q q q q qq q q q q q q q qq q q q q q qq qq qq qq q q q q q q q q q q qq q q q q q qq q qqq qq q q qq q qq q q q q q q q q qq q construction return −10 0 10 q −20 −20 q −10 0 market return 10 20 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 92 / 128 .col="darkred") > text(20."rcon"].ylim=c(-20.y=-20:20. and text to a blank frame > plot(0.20).lwd=2.ylab="construction return") > points(x=Capm[.y=Capm[.20.col="gray") > lines(x=-20:20. lines. and text added R Code: Adding points.A blank frame with points.labels="slope = 1"."rmrf"]. lines.

) NULL x0. y0. lwd = par("lwd"). x1 = x0. . y1 = y0.. col = par("fg"). y1 point coordinates from which to draw point coordinates to which to draw Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 93 / 128 .The segments function The segments function draws line segments between point pairs R Code: segments arguments > args(segments) function (x0. y0 x1. lty = par("lty")..

n = 101. xname = "x".The curve function The curve function draws a curve of a function or expression over a range R Code: curve arguments > args(curve) function (expr. ylab = NULL. from = NULL. add = FALSE. .) NULL expr function or expression of x from start of range to add end of range add to current plot (T/F) Computational Finance & Risk Management R Introduction 94 / 128 n number of points over from/to range Guy Yollin (Copyright © 2012) ... log = NULL. xlab = xname. to = NULL. xlim = NULL. type = "l".

) NULL h/v a/b vertical or horizontal coordinate of line intercept and slope of line Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 95 / 128 . reg = NULL. .. h = NULL.The abline function The abline function adds one or more straight lines through the current plot R Code: abline arguments > args(abline) function (a = NULL. coef = NULL. untf = FALSE.. v = NULL. b = NULL.

The matplot function The matplot function plots multiple columns of a matrix versus an index R Code: matplot arguments > args(matplot) function (x. verbose = getOption("verbose")) NULL x/y matrices or vectors to be plotted Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 96 / 128 . xlim = NULL. cex = NULL. col = 1:6.. pch = NULL. lend = par("lend"). add = FALSE. ylim = NULL. ylab = NULL. y.. lty = 1:5. lwd = 1. type = "p". . bg = NA.. xlab = NULL.

axis" "col.sub" "col.lab" "col.lab" "lheight" "mar" "mkh" "pin" "tck" "xpd" "ylog" "cex" "col" "crt" "fg" "font.lab" "cxy" "fin" "lab" "lty" "mfg" "omd" "pty" "xaxp" "yaxt" "ask" "cex.main" "ljoin" "mex" "new" "plt" "tcl" "yaxp" "adj" "cex.sub" "lmitre" "mfcol" "oma" "ps" "usr" "yaxs" "ann" "cex.axis" "csi" "fig" "font.main" "col.main" "din" "font" "las" "lwd" "mfrow" "omi" "smo" "xaxs" "ylbias" "bg" "cex.axis" "lend" "mai" "mgp" "pch" "srt" "xaxt" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 97 / 128 .sub" "err" "font.Graphical parameters controlled via the par function R is capable of producing publication quality graphics by allowing (requiring ) fine-grained control of a number of graphics parameters R Code: Names of graphical parameters > names(par()) [1] [7] [13] [19] [25] [31] [37] [43] [49] [55] [61] [67] "xlog" "bty" "cin" "cra" "family" "font.

g.Commonly used par parameters Parameter col lwd lyt mfrow cex.main pch las bty Description plot color line width line type set/reset multi-plot layout character expansion .lab cex. col.main point character axis label orientation box type around plot or legend some parameters can be passed in a plot function (e. lwd) some parameters can only be changed by a call to par (e.g. mfrow) Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 98 / 128 .axis character expansion .labels character expansion .axis cex.

lwd.bg = NA. y = NULL. box. yjust = 1. box. text. xpd.5. text. pt.The legend function R Code: legend arguments > args(legend) function (x.col = par("fg"). angle = 45. inset = 0.col. plot = TRUE. pch. ncol = 1.5). x. seg. bg = par("bg").intersp = 1.lwd = lwd. merge = do. box. xjust = 0. lty.col = par("col"). title. border = "black". legend.len = 2) NULL x/y legend col lty lwd pch location of the legend (can be give as a position name) vector of labels for the legend vector of colors line type line width character Computational Finance & Risk Management R Introduction 99 / 128 Guy Yollin (Copyright © 2012) .lines && has. pt. fill = NULL. cex = 1.adj = 0. density = NULL. adj = c(0.lty = par("lty"). 0. title. title = NULL.pch.width = NULL.cex = cex. bty = "o".col = text. pt.lwd = par("lwd"). col = par("col").intersp = 1.font = NULL. y. horiz = FALSE. trace = FALSE. text.

plot = TRUE.lty = 0. legend. horiz = FALSE. border = par("fg")... main = NULL. cex. width = 1. xpd = TRUE. col = NULL. sub = NULL.default) function (height.axis"). inside = TRUE. offset = 0. .The barplot function The barplot function can create vertical or horizontal barplots R Code: barplot arguments > args(barplot. axisnames = TRUE.axis = par("cex. names.names = par("cex. ylim = NULL.legend = NULL.axis"). space = NULL.) NULL height vector or matrix (stacked bars or side-by-side bars) of heights names.text = NULL. angle = 45. xlab = NULL.arg = NULL. cex.arg axis labels for the bars beside stacked bars or side-by-side if height is a matrix legend vector of labels for stacked or side-by-side bars Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 100 / 128 . args. beside = FALSE. density = NULL. add = FALSE. xlim = NULL. axes = TRUE. axis. log = "". ylab = NULL.

data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 101 / 128 2 3 .Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures. data manipulation. working directory.

Probability distributions Random variable A random variable is a quantity that can take on any of a set of possible values but only one of those values will actually occur discrete random variables have a finite number of possible values continuous random variables have an infinite number of possible values Probability distribution The set of all possible values of a random variable along with their associated probabilities constitutes a probability distribution of the random variable Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 102 / 128 .

0 0.4 0.6 −∞ fY ( y ) 1.4 b −4 −2 0 2 4 Cumulative distribution function (CDF) 0.3 Pr (a < Y < b ) = fY (y ) 0.0 0.8 FY (y ) = Pr (Y ≤ y ) = 0.PDFs and CDFs Probability density function (PDF) PDF −∞ fY (y )dy = 1 0.2 0.0 y CDF −4 −2 0 2 4 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 103 / 128 .2 a 0.1 ∞ 0.

PDF. CDF. quantile functions General Normal Function Notation Notation R Excel Graph pdf f (x ) φ(z ) dnorm NORMDIST cdf F (x ) Φ(z ) pnorm NORMDIST quantile F −1 (x ) Φ−1 (z ) qnorm NORMINV Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 104 / 128 .

Normal distribution PDF function: dnorm
dnorm computes the normal PDF: φ(z )
R Code: Plot PDF
> args(dnorm) function (x, mean = 0, sd = 1, log = FALSE) NULL > x <- seq(from = -5, to = 5, by = 0.01) > x[1:10] [1] -5.00 -4.99 -4.98 -4.97 -4.96 -4.95 -4.94 -4.93 -4.92 -4.91 > y <- dnorm(x) > y[1:5] [1] 1.486720e-06 1.562867e-06 1.642751e-06 1.726545e-06 1.814431e-06 > par(mar = par()$mar + c(0,1,0,0)) > plot(x=x,y=y,type="l",col="seagreen",lwd=2, xlab="x",ylab="density\ny = dnorm(x)") > grid(col="darkgrey",lwd=2) > title(main="Probability Density Function (PDF)")
Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 105 / 128

Normal distribution PDF function: dnorm
Probability Density Function (PDF)
0.4

Others: dt dstd dsstd dged dsged dst dmst dct
−4 −2 0 x 2 4

density y = dnorm(x)

Guy Yollin (Copyright © 2012)

0.0

0.1

0.2

0.3

Computational Finance & Risk Management

R Introduction

106 / 128

Normal distribution CDF functions: pnorm and qnorm
pnorm computes the normal CDF: Pr(X ≤ z ) = Φ(z ) qnorm computes the inverse of the normal CDF (i.e. quantile): zα = Φ−1 (α)
R Code: Plot CDF
> args(pnorm) function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) NULL > args(qnorm) function (p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) NULL > y <- pnorm(x) > par(mar = par()$mar + c(0,1,0,0)) > plot(x=x,y=y,type="l",col="seagreen",lwd=2, xlab="x = qnorm(y)", ylab="probability\ny = pnorm(x)") ; grid(col="darkgrey",lwd=2) > title(main="Cumulative Distribution Function (CDF)")
Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 107 / 128

2 0.8 Computational Finance & Risk Management R Introduction 108 / 128 .4 0.Normal distribution CDF functions: pnorm and qnorm Cumulative Distribution Function (CDF) 1.6 0.0 Others: pt pstd psstd pged psged pst pmst pct −4 −2 0 x = qnorm(y) 2 4 probability y = pnorm(x) Guy Yollin (Copyright © 2012) 0.0 0.

5952808 0.4623809 n number of observations mean mean of distribution sd standard deviation of distribution Computational Finance & Risk Management R Introduction 109 / 128 Guy Yollin (Copyright © 2012) .6264538 0.9542051 -2.Generating normally distributed random numbers The function rnorm generates random numbers from a normal distribution R Code: rnorm arguments > args(rnorm) function (n.1836433 -0.rnorm(50.7880864 -4.rnorm(150) > x[1:5] [1] -0.3505613 -0. sd = 1) NULL > x <. mean = 0.3295078 > y <.8356286 1.0556795 -0.sd=3) > y[1:5] [1] 1.

angle = 45. axes = TRUE.default) function (x. ylim = NULL. breaks = "Sturges". labels = FALSE. density = NULL.Histograms The generic function hist computes a histogram of the given data values R Code: hist arguments > args(hist.unused = TRUE. xname). ylab. probability = !freq. main = paste("Histogram of". nclass = NULL. plot = TRUE. col = NULL. right = TRUE. break function prob probability densities or counts ylim y-axis range col color or bars Computational Finance & Risk Management R Introduction 110 / 128 Guy Yollin (Copyright © 2012) . freq = NULL. . name of break algorithm.) NULL x vector of histogram data breaks number of breaks. vector of breaks.. xlab = xname.lowest = TRUE. include. xlim = range(breaks).. warn. border = NULL.

Plotting histograms R Code: Plotting histograms > hist(x.col="seagreen") Histogram of x 35 Frequency 0 5 10 15 20 25 30 −2 −1 0 x 1 2 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 111 / 128 .

breaks="FD".2 0. y) 0.0 0.prob=T.y).1 0.4 Density 0. y) 2 4 6 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 112 / 128 .col="seagreen") Histogram of c(x.Plotting histograms R Code: Plotting histograms > hist(c(x.3 −4 −2 0 c(x.

Plotting histograms R Code: Plotting histograms > hist(c(x.prob=T.2 0.1 0.y).3 0. y) 2 4 6 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 113 / 128 .0 0. y) 0.4 −4 −2 0 c(x.breaks=50.col="seagreen") Histogram of c(x.5 Density 0.

Basic stats functions Short list of some common statistics and math functions: mean mean of a vector or matrix median mad var sd cov cor log exp abs median of a vector or matrix median absolute deviation of a vector or matrix variance of a vector or matrix standard deviation of a vector covariance between vectors correlation between vectors log of a vector or matrix exponentiation of a vector or matrix absolute value of a vector or matrix Computational Finance & Risk Management R Introduction 114 / 128 diff difference between elements in a vector Guy Yollin (Copyright © 2012) .

data manipulation. working directory. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 115 / 128 2 3 .Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures.

Time series data Time Series A time series is a sequence of ordered data points measured at specific points in time A time series class in R is a compound data object that includes a data matrix as well as a vector of associated time stamps class ts mts timeSeries zoo xts package base base rmetrics zoo xts overview regularly spaced time series multiple regularly spaced time series default for Rmetrics packages reg/irreg and arbitrary time stamp classes an extension of the zoo class Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 116 / 128 .

Time series methods Time series classes in R will typically implement the following methods: start return start of time series end window return end of time series Extract subset of time series frequency return frequency of time series index return time index of time series time return time index of time series coredata return data of time series diff difference of the time series lag lag of the time series aggregate aggregate to lower resolution time series cbind merge 2 or more time series together Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 117 / 128 .

read.z <.30 2 2009-09-18 25.Date(msft.30 28864500 25. as.df[.72 26.Close 1 2009-09-21 25.df[.26 68016500 25. frequency = NULL) NULL > msft.Creating a zoo object R Code: Creating a zoo object > library(zoo) > msft.40 25.".61 27.by = index(x). order.44 25.40 25.by=as.df.48 25.is = TRUE) > head(msft.zoo(x=msft.11 25."Close"].z) 2008-09-22 2008-09-23 2008-09-24 2008-09-25 2008-09-26 2008-09-29 25. sep = ".46 25.2) Date Open High Low Close Volume Adj.df <. header = TRUE.37 25.26 > args(zoo) function (x = NULL.order."Date"])) > head(msft.table("MSFT.1 25.1 25.01 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 118 / 128 .CSV".

Inspecting a zoo object R Code: Inspecting a zoo object > class(msft.z) [1] "zoo" > start(msft.z)) [1] "Date" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 119 / 128 .z) [1] "2009-09-21" > frequency(msft.z)) [1] "numeric" > class(time(msft.z) [1] 1 > class(coredata(msft.z) [1] "2008-09-22" > end(msft.

lab=format(ticks.Date(unique(as. xaxt= n .z.yearmon(index(msft.xlab=""."%b-%y")) Microsoft Stock Price $ 16 Oct−08 Guy Yollin (Copyright © 2012) 18 20 22 24 26 Dec−08 Feb−09 Apr−09 Jun−09 Aug−09 R Introduction 120 / 128 Computational Finance & Risk Management . at=ticks.main="Microsoft Stock Price") > axis(side=1.as.z)))) > plot(msft.Plotting a zoo object R Code: Plotting a zoo object > ticks <.ylab="$".

zoo function R Code: reading a file into a zoo object > args(read.17 25. aggregate = FALSE. FUN = NULL. FUN2 = NULL.zoo(file="MSFT..CSV".44 92181300 24.80 > class(soft) [1] "zoo" > class(coredata(soft)) [1] "matrix" > class(index(soft)) [1] "Date" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 121 / 128 .2) Open High Low Close Volume Adj.Close 2008-09-22 26.66 26..read.40 105207700 24.. regular = FALSE.32 25. text) NULL > soft <.column = 1.") > head(soft. .sep=".22 26.34 25.header=TRUE.32 25. index. drop = TRUE.76 2008-09-23 25.zoo) function (file. tz = "". split = NULL.The read. format = "".

data manipulation. working directory. data files The R help system Web resources for R IDE editors for R Part 3 Basic plotting Basic statistics and the normal distribution Working with time series in R Variable scoping in R Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 122 / 128 2 3 .Outline 1 Part 1 R overview and history R language references Part 2 R language and environment basics Data structures.

3 types of symbols may be found: formal parameters .variables created outside of the function (note.variables created in the function free variables .2*x print(x) # formal parameter print(y) # local variable print(z) # free variable } Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 123 / 128 .arguments passed in the function call local variables .Free variables In the body of a function. free variables become local variables if you assign to them) R Code: Types of variables in functions > f <.function(x) { y <.

.e. R will search for a free variable in the following order: 1 The environment in which the function was created For functions created in the global environment. what you are interacting with at the R console) is called the global environment According to the scoping rules of R (referred to a lexical scoping). up until the global environment is searched The search path of loaded libraries found using the search() function 3 4 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 124 / 128 . this will be the global environment 2 The parent environment of the environment where the function was created The parent of the parent ..Environments The main workspace in R (i.

The search path The function search returns a list of attached packages which will be searched in order (after the global environment) when trying to resolve a free variable R Code: The search path > search() [1] [4] [7] [10] ".GlobalEnv" "package:nutshell" "package:grDevices" "Autoloads" "package:zoo" "package:stats" "package:datasets" "package:base" "package:Ecdat" "package:graphics" "package:utils" Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 125 / 128 .

function (x) { a<-5 g(x) } > g <.function (x) x + a f(2) [1] 12 > # example 2 > f<.10 x <.function(y) y + a > f(2) [1] 12 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 126 / 128 .5 f <.Variable scoping examples R Code: Variable scoping examples > > > > > # example 1 a <.

function (y) y + a g(x) } > f(2) [1] 7 > # example 4 > f <.function (x) { a<-5 g <.function (x) { x + mean(rivers) # rivers is defined in the dataset package } > f(2) [1] 593.1844 Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 127 / 128 .Variable scoping examples R Code: Variable scoping examples > # example 3 > f <.

edu/compfin Guy Yollin (Copyright © 2012) Computational Finance & Risk Management R Introduction 128 / 128 .washington.Computational Finance and Risk Management http://depts.

Sign up to vote on this title
UsefulNot useful