Professional Documents
Culture Documents
mm
40
60
80
100
120
Introduction to R
40
Guy Yollin
60
80
2011)
Introduction to R
1 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
2 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
4 / 132
40
60
80
An Introduction to R
100
120
R Reference Card
Tom Short
40
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).
60
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
80
Definitely obtain these PDF files!
Guy Yollin (Copyright
2011)
Introduction to R
5 / 132
40
60
R in a Nutshell: A Desktop
Quick Reference
80
100
120
A Beginners Guide to R
Zuur, Ieno, Meesters
Springer, 2009
Joseph Adler
OReilly
Media, 2009
40
60
80
2011)
Introduction to R
6 / 132
40
60
80
100
120
For those with experience in SAS, SPSS, or Stata, Robert Muenchen has
written R books for this audience:
60
http://r4stats.com
80
2011)
Introduction to R
7 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
8 / 132
What is R?
R ismm
a language and
computing
40 environment
60 for statistical
80
100 and
graphics
120
2011)
Introduction to R
9 / 132
40
60
80
100
120
4.0
HAM1 Performance
HAM1
3.5
EDHEC LS EQ
2.5
1.0
1.5
40
2.0
Data manipulation
Cumulative Return
3.0
SP500 TR
0.00
Monthly Return
0.05
0.0 0.10
Statistical modeling
0.05
Data analysis
0.2
0.3
0.4
Data visualization
Drawdown
0.1
60
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
80
2011)
Introduction to R
10 / 132
S language implementations
R is the most
mm recent and
40 full-featured
60
implementation of the S language
80
100
120
80
2011)
Introduction to R
11 / 132
R timeline
mm
40
1991
Statistical Models in S
(white book)
S3 methods
60
100
120
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
40
1976
80
2000
R 1.0.0
(S3)
1997
R on CRAN
GNU Project
2001
R 1.4.0
(S4)
2002
Modern Applied Statistics
with S
4th Edition
(S+ 6.x, R 1.5.0)
2004
R 2.0.0
2010
R&R
Given ASA
Statistical Computing
and Graphics Award
2011
60
S-PLULS
developed by
Statistical Sciences, Inc.
1988
StatSci Licenses S
1993
Exponential
growth of
R Users and
R packages
80
2011)
Introduction to R
12 / 132
40
60
80
100
American Statistical
Association
John Chambers
received the 1998
40
ACM Software System Award
120
80
2011)
Introduction to R
13 / 132
The R Foundation
mm
40 non-profit
60organization
80 located in100
120
The R Foundation
is the
Vienna, Austria
which is responsible for developing and maintaining R
2011)
Introduction to R
14 / 132
2011)
Introduction to R
120
15 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
16 / 132
The R GUI
mm
40
60
80
100
120
Running R in Windows
40
80
2011)
Introduction to R
17 / 132
Interactive R session
mm
40
60
80
100
120
R is an interpreted language
40GUI is an interactive
The R
command driven environment
type R commands at the R
GUI console
60
Run previously created R
scripts (R commands in a text
file)
80
Commands entered interactively into the R console
2011)
Introduction to R
18 / 132
40
60
80
100
120
> y <- 5
> y
[1] 5
> assign("e",2.7183)
> e
[1] 2.7183
> s = sqrt(2)
> s
[1] 1.414214
2011)
Introduction to R
19 / 132
Object orientation in R
mm
40
60
Everything in R is an Object
80
100
120
x <- c(3.1416,2.7183)
m <- matrix(rnorm(9),nrow=3)
60
tab <- data.frame(store=c("downtown","eastside","airport"),sales=c(32,17,24))
cities <- c("Seattle","Portland","San Francisco")
ls()
"m"
"r"
"s"
"tab"
"x"
"y"
80
2011)
Introduction to R
20 / 132
Data types
mm
40
60
80
100
120
> cities
[1] "Seattle"
"Portland"
[3] "San Francisco"
> typeof(cities)
[1] "character"
80
2011)
Introduction to R
21 / 132
Object classes
mm
40
R Code: Object
60
80 class
100
120
> m
[,1]
[,2]
[,3]
[1,] -0.6147361 -0.2248133 0.1354078
[2,] -0.7835507 2.3798959 0.8825350
[3,] 1.0156090 1.4605885 0.9470563
> class(m)
[1] "matrix"
> tab
store sales
1 downtown
32
2 eastside
17
3 airport
24
> class(tab)
80
[1] "data.frame"
2011)
Introduction to R
22 / 132
Vectors
R is a vector/matrix language
mm can easily40be created 60
80
100
vectors
with c, the combine
function
120
2011)
Introduction to R
23 / 132
Indexing vectors
R Code: Indexing vectors
mm
40
60
sqrt2 golden
1.4142 1.6180
> constants[c("pi","golden")]
pi golden
3.1416 1.6180
logical vector
120
> constants[c(-1,-2)]
100
pi sqrt2 golden
3.1416 1.4142 1.6180
80
> constants[c(1,3,4)]
pi
TRUE
euler
TRUE
sqrt2 golden
FALSE FALSE
80
pi euler
3.1416 2.7183
Guy Yollin (Copyright
2011)
Introduction to R
24 / 132
60
> constants*c(0,1)
pi euler sqrt2 golden
0.0000 2.7183 0.0000 1.6180
> constants*c(0,1,2)
80 sqrt2 golden
pi euler
0.0000 2.7183 2.8284 0.0000
Guy Yollin (Copyright
2011)
Introduction to R
25 / 132
Sequences
An integer
can be60created with
mmsequence vector
40
80the : operator
100
120
A general numeric sequence vector can be created with the seq function
R Code: seq arguments
40
> args(seq.default)
function (from = 1, to = 1, by = ((to - from)/(length.out - 1)),
length.out = NULL, along.with = NULL, ...)
NULL
60
to starting value
2011)
Introduction to R
26 / 132
Sequences
mm
40
60
80
100
120
40
> -5:5
[1] -5 -4 -3 -2 -1
> seq(from=0,to=1,len=5)
[1] 0.00 0.25
60 0.50 0.75 1.00
> seq(from=0,to=20,by=2.5)
[1]
0.0
2.5
5.0
80
2011)
Introduction to R
27 / 132
8 10
60
> seq(by=2,0,10)
[1]
8 10
> seq(0,10,len=5)
[1]
0.0
2.5
5.0
7.5 10.0
80
> seq(0,10)
[1]
2011)
9 10
Introduction to R
28 / 132
40
60
80
100
120
2011)
Introduction to R
29 / 132
40
60
80
100
120
# initialize a vector
[1] 0 0 0 0 0 0 0 0 0 0
> rep(1:4, 2) # repeat pattern 2 times
40
[1] 1 2 3 4 1 2 3 4
> rep(1:4, each = 2)
[1] 1 1 2 2 3 3 4 4
> rep(1:4,60
c(2,1,2,1))
[1] 1 1 2 3 3 4
> rep(1:4, each = 2, len = 10)
[1] 1 1 2 2 3 3 4 4 1 1
> rep(1:4,80
each = 2, times = 3)
[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
2011)
Introduction to R
30 / 132
Generic functions
mm
40
60
80
100
120
60
"plot.acf"
"plot.default"
"plot.ecdf"
"plot.hclust"
"plot.isoreg"
"plot.data.frame"
"plot.dendrogram"
"plot.factor"
"plot.histogram"
"plot.lm"
"plot.decomposed.ts"
"plot.density"
"plot.formula"
"plot.HoltWinters"
"plot.medpolish"
80
2011)
Introduction to R
31 / 132
R packages
40 stored in packages
60
All mm
R functions are
80
100
120
Recommended packages
boot, rpart, foreign, MASS, cluster, Matrix, etc.
2011)
Introduction to R
32 / 132
80
city rank
1 Seattle 100
Guy Yollin (Copyright
2011)
Introduction to R
33 / 132
120
80
see RNUT for more info on installing packages
Guy Yollin (Copyright
2011)
Introduction to R
34 / 132
40
60
80
100
120
40
60
80
2011)
Introduction to R
35 / 132
40
60
80
100
120
Package
zoo
tseries
PerformanceAnalytics
60
quantmod
xts
Description
Time series objects
Time series analysis and computational finance
Performance and risk analysis
Quantitative financial modeling framework
Extensible time series
80
2011)
Introduction to R
36 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
37 / 132
40
> setwd("C:\\Rprojects\\PCA")
> getwd()
[1] "C:/Rprojects/PCA"
> setwd("C:/Rprojects/UW/lecture-01")
> getwd() 60
[1] "C:/Rprojects/UW/lecture-01"
2011)
Introduction to R
38 / 132
40
60
80
100
120
40
60
80
2011)
Introduction to R
39 / 132
40
60
80
100
120
60
2011)
Introduction to R
40 / 132
40
60
80
100
120
40
60
> dat <- read.table("intc.csv",header=FALSE,sep=",",as.is=TRUE)
> dat[1:5,]
1
2
3
4
5
V1
1986-07-09
1986-07-10
1986-07-11
80
1986-07-14
1986-07-15
V2
0.380000
0.383214
0.366494
0.344416
0.338684
2011)
V3
0.384419
0.383214
0.370909
0.344416
0.343026
V4
0.371163
0.367710
0.331169
0.335584
0.325658
V5
0.38
0.37
0.34
0.34
0.33
V6
1564294736
3623351351
8968235294
1615007058
2110865454
Introduction to R
41 / 132
mm
40
60
80
> dim(dat)
[1] 6092
100
120
> dat[1:2,1:3]
The read.table
function
40
returns a data.frame object
V1
V2
V3
1 1986-07-09 0.380000 0.384419
2 1986-07-10 0.383214 0.383214
> typeof(dat)
A data.frame is a 2D
matrix-like
object where the
60
columns can be of different
classes
[1] "list"
> class(dat)
[1] "data.frame"
> class(dat[,1])
[1] "character"
80
> class(dat[,2])
[1] "numeric"
Guy Yollin (Copyright
2011)
Introduction to R
42 / 132
80
100
120
> args(head.matrix)
function (x, n = 6L, ...)
NULL
> head(dat)
40
1
2
3
4
5
6
V1
1986-07-09
1986-07-10
1986-07-11
1986-07-14
1986-07-15
60
1986-07-16
V2
0.380000
0.383214
0.366494
0.344416
0.338684
0.335329
V3
0.384419
0.383214
0.370909
0.344416
0.343026
0.335329
V4
0.371163
0.367710
0.331169
0.335584
0.325658
0.317910
V5
0.38
0.37
0.34
0.34
0.33
0.32
V6
1564294736
3623351351
8968235294
1615007058
2110865454
2303230600
> tail(dat,3)
V1
V2
V3
V4
V5
V6
6090 2010-08-30 18.25 18.31 17.94 17.96 73718900
80
6091 2010-08-31
17.88 17.92 17.60 17.67 111601400
6092 2010-09-01 17.94 18.27 17.89 18.14 73506800
2011)
Introduction to R
43 / 132
40
60
80
100
120
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
80
2011)
Introduction to R
44 / 132
mm
40
60
2011)
80
100
120
> colnames(dat)
<- c("date","open",
"high","low","close","volume")
> tail(dat[,-1],3)
open high
low
6090 18.25 18.31 17.94
6091 17.88 17.92 17.60
6092 17.94 18.27 17.89
close
volume
17.96 73718900
17.67 111601400
18.14 73506800
> tail(dat[,c("date","close")],3)
date close
6090 2010-08-30 17.96
6091 2010-08-31 17.67
6092 2010-09-01 18.14
> dat[dat[,"volume"]>15e9,
c("date","close","volume")]
date close
volume
328 1987-10-22 0.61 16717377049
602 1988-11-21 0.52 16116850769
1715 1993-04-19 2.58 21509122170
Introduction to R
45 / 132
80
100
120
> args(write.table)
function (x, file = "", append = FALSE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE,
qmethod = c("escape", "double"), fileEncoding = "")
40
NULL
> args(write)
function (x, file = "data", ncolumns = if (is.character(x)) 1 else 5,
append = FALSE, sep = " ")
NULL
60
x
file
sep
80
row.names
col.names
2011)
Introduction to R
46 / 132
40
60
80
100
120
40
60
80
2011)
Introduction to R
47 / 132
60
80
100
120
80
2011)
Introduction to R
48 / 132
40
60
> constants[2]
> constants[[2]]
[1] 2.7183
> constants[["pi"]]
120
$euler
[1] 2.7183
[] returns a sublist
single
integer
60
single name
R Code: 80
Indexing lists100
[1] 3.1416
> constants$golden
[1] 1.618
single name
> diverseList[[3]][2]
80
[1] "OR"
2011)
Introduction to R
49 / 132
40
60
80
100
120
2011)
Introduction to R
50 / 132
80
2011)
Introduction to R
51 / 132
> apply(m,2,sum)
[1] 17 12 16
80
2011)
Introduction to R
52 / 132
S4 Classes
mm
60
80
100
S4 classes
are a more40
modern implementation
of object-oriented
programming in R compared to S3 classes
120
2011)
Introduction to R
53 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
54 / 132
Probability distributions
mm
40
60
80
100
120
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
40
discrete random variables have a finite number of possible values
continuous random variables have an infinite number of possible values
Probability distribution
60
The set of all possible values of a random variable along with their
associated probabilities constitutes a probability distribution of the
random variable
80
2011)
Introduction to R
55 / 132
40
60
80
100
120
fY (y )dy = 1
0.0
0.1
40
0.2
0.4
Pr (a < Y < b) =
0.3
0.8
1.0
CDF
0.0
0.2
80
0.6
0.4
60
Cumulative
distribution function (CDF)
Z y
FY (y ) = Pr (Y y ) =
fY (y )
2011)
Introduction to R
56 / 132
40
60
80
100
120
40
2011)
Introduction to R
57 / 132
120
0.0
0.1
60
density
y = dnorm(x)
40
0.2
0.3
0.4
Density Function
40 Probability60
80 (PDF) 100
80
quantile
2011)
Introduction to R
58 / 132
60
80
100
120
60
> 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))
80
> plot(x=x,y=y,type="l",col="seagreen",lwd=2,
xlab="quantile\nx = qnorm(y)",
ylab="probability\ny = pnorm(x)") ; grid(col="darkgrey",lwd=2)
> title(main="Cumulative Distribution Function (CDF)")
Guy Yollin (Copyright
2011)
Introduction to R
59 / 132
120
0.4
0.0
0.2
60
probability
y = pnorm(x)
40
0.6
0.8
1.0
Cumulative Distribution
Function
(CDF)100
40
60
80
80
2011)
0
2
quantile
x = qnorm(y)
Introduction to R
60 / 132
40
60
80
100
120
40
0.1836433 -0.8356286
1.5952808
0.3295078
n number of observations
mean
80 mean of distribution
sd standard deviation of distribution
Guy Yollin (Copyright
2011)
Introduction to R
61 / 132
Histograms
The generic function hist computes a histogram of the given data values
40
R Code: mm
hist arguments
60
80
100
120
> args(hist.default)
function (x, breaks = "Sturges", freq = NULL, probability = !freq,
include.lowest = TRUE, right = TRUE, density = NULL, angle = 45,
col = NULL, border = NULL, main = paste("Histogram of", xname),
40
xlim = range(breaks), ylim = NULL, xlab = xname, ylab, axes = TRUE,
plot = TRUE, labels = FALSE, nclass = NULL, warn.unused = TRUE,
...)
NULL
2011)
Introduction to R
62 / 132
Plotting histograms
R Code: Plotting histograms
mm
> hist(x,col="seagreen")
40
60
80
100
120
30
20
0
10
60
15
Frequency
25
40
35
Histogram of x
80
2011)
Introduction to R
63 / 132
Plotting histograms
R Code: Plotting histograms
mm
> hist(c(x,y),prob=T,breaks="FD",col="seagreen")
40
60
80
100
120
0.2
Density
0.3
40
0.4
Histogram of c(x, y)
0.0
0.1
60
80
c(x, y)
2011)
Introduction to R
64 / 132
Plotting histograms
R Code: Plotting histograms
mm
> hist(c(x,y),prob=T,breaks=50,col="seagreen")
40
60
80
100
120
0.5
Histogram of c(x, y)
0.3
0.0
0.1
60
0.2
Density
0.4
40
80
c(x, y)
2011)
Introduction to R
65 / 132
40
60
80
100
120
2011)
Introduction to R
66 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
67 / 132
40
Function
plot
lines
segments
40
points
text
abline
60 curve
legend
matplot
par
60
80
100
120
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
80
2011)
Introduction to R
68 / 132
60
80
100
120
> args(plot.default)
function (x, y = NULL, type = "p", xlim = NULL, ylim = NULL,
log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL,
40 = NULL, asp = NA, ...)
panel.last
NULL
x
y
60
xlim/ylim
xlab/ylab
main
type
80
col
asp
2011)
Introduction to R
69 / 132
mm
> library(Ecdat)
40
> data(Capm)
> plot(Capm[,"rf"],pch=5)
60
80
100
120
80
0.8
0.2
0.4
60
0.6
Capm[, "rf"]
1.0
1.2
40
100
200
300
400
500
Index
2011)
Introduction to R
70 / 132
mm
> plot(Capm[,"rf"])
1.2
0.8
0.6
Capm[, "rf"]
80
100
120
0.2
0.4
60
60
1.0
40
40
80
100
200
300
400
500
Index
2011)
Introduction to R
71 / 132
80
100
120
0.8
0.2
0.4
60
0.6
Capm[, "rf"]
1.0
40
60
1.2
mm
> plot(Capm[,"rf"],type="l")
40
80
100
200
300
400
500
Index
2011)
Introduction to R
72 / 132
120
Capm[, "rmrf"]
100
20
60
80
10
40
60
10
mm
> plot(Capm[,"rmrf"],type="h")
40
80
100
200
300
400
500
Index
2011)
Introduction to R
73 / 132
mm
> plot(Capm[,"rmrf"],Capm[,"rcon"],pch=5)
40
60
120
20
100
0
30
20
60
10
Capm[, "rcon"]
10
40
80
80
20
10
10
Capm[, "rmrf"]
2011)
Introduction to R
74 / 132
40
60
80
100
120
The points function adds points to the current plot at the given x, y
coordinates
40
60
x vector of x coordinates
y vector of y coordinates
80
2011)
Introduction to R
75 / 132
40
60
80
100
120
The lines function adds connected line segments to the current plot
R Code: lines
40 arguments
> args(lines.default)
function (x, y = NULL, type = "l", ...)
NULL
y vector of y coordinates
80
2011)
Introduction to R
76 / 132
x/y
60 location to place text
labels text to be display
adj adjustment of label at x, y location
pos position of text relative to x, y
80
offset
offset from pos
2011)
Introduction to R
77 / 132
mm
> plot(0,xlim=c(-20,20),ylim=c(-20,20),type="n",
40
60
80
xlab="market return",ylab="construction return")
20
10
0
construction return
80
10
60
120
20
40
100
20
10
10
20
market return
Guy Yollin (Copyright
2011)
Introduction to R
78 / 132
mm
> plot(0,xlim=c(-20,20),ylim=c(-20,20),type="n",
40
60
80
xlab="market return",ylab="construction return")
> points(x=Capm[,"rmrf"],y=Capm[,"rcon"],pch=5,col="gray")
> lines(x=-20:20,y=-20:20,lwd=2,col="darkred")
> text(20,20,labels="slope = 1",pos=2)
100
120
80
10
10
slope = 1
20
60
construction return
20
40
20
10
10
20
market return
2011)
Introduction to R
79 / 132
40
60
80
100
120
40
> args(segments)
function (x0, y0, x1 = x0, y1 = y0, col = par("fg"), lty = par("lty"),
lwd = par("lwd"), ...)
NULL
60
2011)
Introduction to R
80 / 132
40
function (expr,
from = NULL, to = NULL, n = 101, add = FALSE,
type = "l", ylab = NULL, log = NULL, xlim = NULL, ...)
NULL
2011)
Introduction to R
81 / 132
40
60
80
100
120
The abline function adds one or more straight lines through the current
plot
40
R Code: abline
arguments
> args(abline)
function (a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL,
coef = NULL, untf = FALSE, ...)
NULL
60
2011)
Introduction to R
82 / 132
40
60
80
100
120
40
> args(matplot)
function (x, y, type = "p", lty = 1:5, lwd = 1, lend = par("lend"),
pch = NULL, col = 1:6, cex = NULL, bg = NA, xlab = NULL,
ylab = NULL, xlim = NULL, ylim = NULL, ..., add = FALSE,
verbose = getOption("verbose"))
60
NULL
2011)
Introduction to R
83 / 132
120
40
[1]
[7]
[13]
[19]
[25]
[31]
[37]
[43]
[49]
[55]
[61]
[67]
"xlog"
"bty"
"cin"
"cra"
"family"
60
"font.lab"
"lheight"
"mar"
"mkh"
"pin"
"tck"
"xpd"80
"ylog"
"cex"
"col"
"crt"
"fg"
"font.main"
"ljoin"
"mex"
"new"
"plt"
"tcl"
"yaxp"
2011)
"adj"
"cex.axis"
"col.axis"
"csi"
"fig"
"font.sub"
"lmitre"
"mfcol"
"oma"
"ps"
"usr"
"yaxs"
"ann"
"cex.lab"
"col.lab"
"cxy"
"fin"
"lab"
"lty"
"mfg"
"omd"
"pty"
"xaxp"
"yaxt"
Introduction to R
"ask"
"cex.main"
"col.main"
"din"
"font"
"las"
"lwd"
"mfrow"
"omi"
"smo"
"xaxs"
"bg"
"cex.sub"
"col.sub"
"err"
"font.axis"
"lend"
"mai"
"mgp"
"pch"
"srt"
"xaxt"
84 / 132
40
60
Parameter
40
col
lwd
lyt
mfrow
cex.axis
cex.lab
cex.main
pch
las
bty
Description
60
80
100
plot color
line width
line type
set/reset multi-plot layout
character expansion - axis
character expansion - labels
character expansion - main
point character
axis label orientation
box type around plot or legend
120
2011)
Introduction to R
85 / 132
mm
> args(legend)
40
60
80
100
120
60
x/y
legend
col
lty
80
lwd
pch
2011)
Introduction to R
86 / 132
40
60
80
100
120
> args(barplot.default)
2011)
Introduction to R
87 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
88 / 132
package
base
base
tseries
rmetrics
fts
zoo
xts
2011)
overview
regularly spaced time series
multiple regularly spaced time series
irregularly spaced time series
default for Rmetrics packages
R interface to tslib (c++ time series library)
reg/irreg and arbitrary time stamp classes
an extension of the zoo class
Introduction to R
89 / 132
40
60
80
100
120
2011)
Introduction to R
90 / 132
40
60
80
100
120
> library(zoo)
> msft.df <- read.table("table.csv", header = TRUE, sep = ",", as.is = TRUE)
> head(msft.df,2)
Date
Volume Adj.Close
40 Open High Low Close
1 2010-09-13 24.20 25.29 24.09 25.11 114606300
25.11
2 2010-09-10 23.98 24.03 23.79 23.85 58284300
23.85
> args(zoo)
function (x = NULL, order.by = index(x), frequency = NULL)
60
NULL
> msft.z <- zoo(x=msft.df[,"Close"],order.by=as.Date(msft.df[,"Date"]))
> head(msft.z)
2009-01-02 2009-01-05 2009-01-06 2009-01-07 2009-01-08 2009-01-09
20.33
20.52
20.76
19.51
20.12
19.52
80
2011)
Introduction to R
91 / 132
60
80
100
120
> class(msft.z)
[1] "zoo"
> start(msft.z)
[1] "2009-01-02"
40
> end(msft.z)
[1] "2010-09-13"
> frequency(msft.z)
[1] 1
60
> class(coredata(msft.z))
[1] "numeric"
> class(time(msft.z))
80
[1] "Date"
2011)
Introduction to R
92 / 132
mm
> plot(msft.z,xlab="",ylab="$",
main="Microsoft
Stock
40
60
80 Price")
100
120
25
40
30
15
20
60
80
2009
2011)
2010
Introduction to R
93 / 132
80
S&P60
500 daily returns
100
120
log return
40
0.10
0.00 0.05
40
0.20
60
1982
80
1984
1986
1988
1990
year
2011)
Introduction to R
95 / 132
mm
# figure
4.1
library(Ecdat)
data(SP500)
class(SP500)
40
60
80
100
120
[1] "data.frame"
40
> dim(SP500)
[1] 2783
60
[1] -0.0117265
0.0024544
0.0110516
> n = length(SPreturn)
> year_SP = 1981 + (1:n)*(1991.25-1981)/n
> head(year_SP)
[1] 1981.004 1981.007 1981.011 1981.015 1981.018 1981.022
80
2011)
Introduction to R
96 / 132
80 rate
changes in 60
DM/dollar exchange
100
120
0.005
0.015
60
0.005
40
change in rate
0.015
40
1980
80
1982
1984
1986
year
2011)
Introduction to R
98 / 132
Plot DM returns
R Code: Plot DM returns
>
>
>
>
# figure
4.2
mm
library(zoo)
data(Garch)
head(Garch)
1
2
3
4
5
6
date
day
dm
ddm
bp
800102 wednesday 0.5861
NA 2.2490
40
800103 thursday 0.5837 -0.0041032713 2.2365
800104
friday 0.5842 0.0008562377 2.2410
800107
monday 0.5853 0.0018811463 2.2645
800108
tuesday 0.5824 -0.0049670394 2.2560
800109 wednesday 0.5834 0.0017155606 2.2650
40
60
80
cd
0.8547
0.8552
0.8566
0.8538
0.8553
0.8565
100
dy
0.004206
0.004187
0.004269
0.004315
0.004257
0.004245
120
sf
0.6365
0.6357
0.6355
0.6373
0.6329
0.6349
60
> dm <- zoo(x=Garch[,"dm"],
order.by=as.Date(x=as.character(Garch[,"date"]),format="%y%m%d"))
> head(dm)
1980-01-02 1980-01-03 1980-01-04 1980-01-07 1980-01-08 1980-01-09
0.5861
0.5837
0.5842
0.5853
0.5824
0.5834
80diff(dm)
> diffdm <> plot(diffdm,xlab="year",ylab="change in rate",type="h")
> title("changes in DM/dollar exchange rate")
Guy Yollin (Copyright
2011)
Introduction to R
99 / 132
80return
changes in60
riskfree interest
100
120
0.0
0.2
40
change in rate
0.2
40
0.4
60
1960
80
1970
1980
1990
2000
year
2011)
Introduction to R
101 / 132
mm
40
60
80
100
120
2011)
Introduction to R
102 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
103 / 132
Free variables
mm of a function,
40 3 types60
80 be found:
100
In the body
of symbols may
120
60
> f <- function(x)
{
y <- 2*x
print(x) # formal parameter
print(y) # local variable
print(z) # free variable
}
80
2011)
Introduction to R
104 / 132
Environments
mm
The main
workspace in
with
40 R (i.e. what
60 you are interacting
80
100at the R 120
console) is called the global environment
The 60
parent environment of the environment where the function was
created
The 80
search path of loaded libraries found using the search() function
2011)
Introduction to R
105 / 132
40
60
80
100
120
".GlobalEnv"
"package:nutshell"
60
"package:graphics"
"package:datasets"
"package:base"
"package:zoo"
"package:patchDVI"
"package:grDevices"
"package:methods"
"package:Ecdat"
"package:stats"
"package:utils"
"Autoloads"
80
2011)
Introduction to R
106 / 132
40examples
R Code: Variable scoping
>
>
>
>
>
60
80
100
120
# example 1
a <- 10
x <- 5
f <- function (x) x + a
f(2)
40
[1] 12
> # example 2
> f<- function (x)
{
a<-5 60
g(x)
}
> g <- function(y) y + a
> f(2)
[1] 12
80
2011)
Introduction to R
107 / 132
40
60
80
100
120
> # example 3
> f <- function (x) {
a<-5
g <- function (y) y + a
g(x) 40
}
> f(2)
[1] 7
> # example 4
60
> f <- function (x) {
x + mean(rivers) # rivers is defined in the dataset package
}
> f(2)
[1] 593.1844
80
2011)
Introduction to R
108 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
109 / 132
40
60
80
100
120
2011)
Introduction to R
110 / 132
40
60
80
100
120
help(topic)
?topic
R Code: Topic
60 help
> help(read.table)
80
2011)
Introduction to R
111 / 132
40
60
80
100
120
R-help archives
60
R Code: Running RSiteSearch
> library(RSiteSearch)
> RSiteSearch("ODBC")
80
2011)
Introduction to R
112 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
113 / 132
R Homepage
mm
40
60
80
100
120
http://www.r-project.org
40
Links
80
2011)
Introduction to R
114 / 132
40
60
80
100
120
http://cran.fhcrc.org
CRAN Mirrors
40
About 75 sites worldwide
About 16 sites in US
R Binaries
R Packages
60
R Sources
Task Views
80
2011)
Introduction to R
115 / 132
40
60
80
100
120
Finance
Time Series
Econometrics
60
Optimization
Machine Learning
80
2011)
Introduction to R
116 / 132
Statconn
http://rcom.univie.ac.at
mm
40
60
80
100
120
60
2011)
Introduction to R
117 / 132
R-SIG-FINANCE
mm
40
60
80
100
120
https://stat.ethz.ch/mailman/
listinfo/r-sig-finance
40
2011)
Introduction to R
118 / 132
40
60
80
100
120
http://google-styleguide.
googlecode.com/svn/trunk/
40
google-r-style.html
Naming convention
Coding
60 Syntax
Program Organization
80
2011)
Introduction to R
119 / 132
Quick R
mm
40
60
80
100
120
http://www.statmethods.net
Introductory R Lessons
40
R Interface
Data Input
Data Management
Basic Statistics
60
Advanced Statistics
Basic Graphs
Advanced Graphs
80
2011)
Introduction to R
120 / 132
URL
http://research.stowers-institute.org/efg/R/index.htm
40
Features
R Color Chart
Using Color in R (great presentation)
60
Plot area, margins, multiple figures
Mixture models
Distance measures and clustering
Using Windows Explorer to Start R with Specified Working Directory
80
(under tech notes)
2011)
Introduction to R
121 / 132
40
60
80
100
120
80
2011)
Introduction to R
122 / 132
Programming in R
mm
40
60
80
Online R programmingn
manual from
UC Riverside
100
120
URL
http://manuals.bioinformatics.ucr.edu/home/programming-in-r
40
Selected Topics
R Basics
Finding Help
60
Code Editors for R
Control Structures
Functions
Object Oriented Programming
Building R Packages
80
2011)
Introduction to R
123 / 132
100
120
2011)
Introduction to R
124 / 132
Outline
1
R language references
Basic plotting
Variable scoping in R
10
Web resources
for R
80
11
mm
40
60
80
100
120
40
60
2011)
Introduction to R
125 / 132
RStudio
RStudio is an fully-featured open-source IDE
mm
40
60
80
for R
100
120
R language highlighting
Paste/Source
code to R
40
object explorer
graphics window in main IDE
60 provides a server-based version
RStudio also
(R running in the cloud). Request a free
account from josh@rstudio.org (be sure
to reference the UW Computational Finance
program) 80
RStudio is highly recommended
Guy Yollin (Copyright
2011)
Introduction to R
126 / 132
80
100
120
object explorer
runs R in SDI mode
http://www.revolutionanalytics.com
80
2011)
Introduction to R
127 / 132
80
100
120
40
R language highlighting
Paste/Source code to R
Supports R in SDI mode
60 installed as part of
Can be
RAndFriends
http://notepad-plus-plus.org
80
http://sourceforge.net/projects/npptor
http://rcom.univie.ac.at/download.html
Guy Yollin (Copyright
2011)
Introduction to R
128 / 132
40
60
80
100
120
40
R language highlighting
Paste/Source code to R
Supports R in SDI mode
60 documentation by
Excellent
Longhow Lam
http://www.walware.de/goto/statet
80
2011)
Introduction to R
129 / 132
R-WinEdit
mm
40
60
80
100
120
R language highlighting
Paste/Source code to R
Supports R in MDI mode
60
80
http://www.winedt.com
http://cran.r-project.org/web/packages/RWinEdt
Guy Yollin (Copyright
2011)
Introduction to R
130 / 132
40
60
80
100
120
2011)
Introduction to R
131 / 132
The End
mm
40
60
80
100
120
40
60
80
2011)
Introduction to R
132 / 132