You are on page 1of 134

R

R
2.3.0 (2006-04-24)
0.1 (2006-06-15)

W. N. Venables, D. M. Smith
R the R Development Core Team
ii

:
c
Copyright 1990 W. N. Venables
c
Copyright 1992 W. N. Venables & D. M. Smith
c
Copyright 1997 R. Gentleman & R. Ihaka
c
Copyright 1997, 1998 M. Maechler
c
Copyright 19992006 R Development Core Team
Permission is granted to make and distribute verbatim copies of this manual provided the
copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the
conditions for verbatim copying, provided that the entire resulting derived work is distributed
under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another
language, under the above conditions for modified versions, except that this permission notice
may be stated in a translation approved by the R Development Core Team.

c
1990 W. N. Venables
c
1992 W. N. Venables & D. M. Smith
c
1997 R. Gentleman & R. Ihaka
c
1997, 1998 M. Maechler
c
19992006 R Development Core Team

R R Development Core Team

GNU FDLGNU
http://www.gnu.org/copyleft/fdl.html

viii
0.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii
0.2 (05) . . . . . . . . . . . . . . . . . . . . . . . . viii
0.3 (PDF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . x

1 1
1.1 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.8 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.9 . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.10 . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 8
2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.7 . . . . . . . . . . . . . . . . . 13
2.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
iv

3 16
3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4 20
4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.2 tapply() . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

5 23
5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.4 array() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.4.1 . . . . . . . . . . . . . . . 26
5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.7.4 . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.7.5 QR . . . . . . . . . . . . . . . . . . . . . 31
5.8 cbind() rbind() . . . . . . . . . . . . . . . . . . . . 31
5.9 c() . . . . . . . . . . . . . . . . . . . . . . . 32
5.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

6 34
6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3.2 attach() detach() . . . . . . . . . . . . . . . . . . . . . . . . 36
6.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
v

6.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

7 39
7.1 read.table() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.2 scan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.3.1 R . . . . . . . . . . . . . . . . . . . . . . 42
7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

8 43
8.1 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
8.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
8.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

9 52
9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
9.2.1 if . . . . . . . . . . . . . . . . . . . . . . . . . . 52
9.2.2 forrepeat while . . . . . . . . . . . . . . . 53

10 54
10.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
10.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
10.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
10.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
10.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
10.6.1 . . . . . . . . . . . . . . . . . . . . . . . . 57
10.6.2 . . . . . . . . . . . . . . . . . . . . . . . . 58
10.6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 59
10.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
10.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10.9 . . . . . . . . . . . . . . . . . . . . . . . . . . 63

11 R 66
11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
11.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
vi

11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
11.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 70
11.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
11.4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
11.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
11.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
11.6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.6.2 glm() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
11.7 . . . . . . . . . . . . . . . . . . . . 78
11.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
11.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
11.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

12 83
12.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.1.1 plot() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
12.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
12.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . 86
12.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.2.2 Hershey . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
12.4.1 : par() . . . . . . . . . . . . . . . . . . . . . . 90
12.4.2 . . . . . . . . . . . . . . . . . . 91
12.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
12.5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
12.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
12.5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
12.5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
12.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
12.6.1 PostScript . . . . . . . . . . . . . . . . . . . . 97
12.6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
12.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
vii

13 100
13.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.2 CRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

1 102

2 R 108
13.4 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
13.5 Windows R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
13.6 Mac OS X R . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

3 114
13.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
13.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
13.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

4 116

5 118

6 122

Bill Venables David M. Smith (Insightful ) S SPLUS


R S

Bill Venables David Smith


R
R-core@R-project.org
ghding@gmail.com

0.1
R A session<102>
R R sessions
R
R
(<83> )

R
http://cran.r-project.org/mirrors.html
Precompiled Binary Distributions
Windows Windows (95 and later)base
rwxxxx.exerw2010.exeWindows OK
A session<102>

0.2 (05)
R R
R R
R
0.2 (05) ix

ROTATION R
StataMATLABSPSS SAS R
R

R
R MATLAB

R
R R

R /R

R R
R
http://www.r-project.org/
foundation/about.html
Texinfo Texinfo PDF
HTML (UTF-8)
Texinfo LaTeXPDF


email
PDF
R
R An Introduction to R
R Data Import/ExportThe R language definitionWriting R
ExtensionsR Installation and Administration/R FAQ

0.3 (PDF) x

R
R

Shigeru MASE

Emailghding@gmail.com
200563

0.3 (PDF)
RLATEX
R
Email
R R


Emmanuel Paradis R for beginnersXF Wang
R for beginners R: )
Texinfo
LATEX
|!
PDFDr. ZP LiDr. RuiLi

Rui

Prof Li
Brian D. Ripley Technische Uni. Wien Friedrich Leisch
R
Ronggui
H Li
- Q Wang K Tu
(CHMRK TuG Li
Q Liu
0.3 (PDF) xi

Roation RR
LATEXR
Email

Emailghding@gmail.com
2006711

1.1 R
R

(S)

S

, environment R
, ,

R
packages R

1.2
R Bell LaboratoriesRick BeckerJohn Chambers
Allan Wilks S S SPLUS
S John Chambers
RRichard A. BeckerJohn M. Chambers Allan R.
1.3 R 2

Wilks The New S Language: A Programming Environment for Data Analysis and
GraphicsJohn M. Chambers and Trevor J. Hastie Statistical Models in S
1991 S 3 1 methods method
classJohn M. Chambers Programming with Data
<122>.
R S/SPLUS
R R S R R

1.3 R
R R
R
R
25 R
CRAN (http://CRAN.R-project.org)
(<100>)
R

S( R) S
object
SAS SPSS R
R
2

1.4 R
graphics workstation
R R
RX window R
. ,
UNIX Windows MacOS R

1
: John M. Chambers 1988S3
2
: R.
1.5 R 3

1.5 R
R
>UNIX shell
R
UNIX shell $
UNIX R

1. work R
R

$ mkdir work
$ cd work

2. R

$ R

3. R ()

4. R

> q()

R
yesno,
cancel
R R

1. work

$ cd work
$ R

2. R q()
1.6 R 4

Windows R
R 3 R

1.6 R
R
<102> R R

1.7
R UNIXman
solve

> help(solve)

> ?solve


iffor function

> help("[[")

escape"Its important"4

R HTML

> help.start()

UNIX
HTML help.start()
Search Engine & Keywords

R
3
R
OK
4
Its important R
1.8 R 5

help.search 5 ?help.search

> example(topic) # topic barplot

Windows R
6

> ?help

1.8 R
R expression language
A a R
R (locale )
. ().
.
expressionsassignments
evaluate

(;)({})
compound expression7
(#)
R

continuation prompt
5
?
6
: BioConductor Vignettes .
7

1.9 6

1.9
UNIX Windows R
command history

DEL <
114>.
UNIX
readline
Emacs 8 R (ESS
Emacs Speaks Statistics)R: R

1.10
work commands.R
R

> source("commands.R")

Windows R FileSource
sink

> sink("record.lis")

9 record.lis

> sink()

1.11
R entity
structures
8
ViUltraEditUltraEdit R http://
www.ultraedit.com/index.php?name=Content&pa=showpage&pid=40 R wordfile R Scripting
- 02/18/2003UltraEdit wordfile.txt
.R
9

1.11 7

R (
) R (ls())

> objects()

R
workspace
rm

> rm(x, y, z, ink, junk, temp, foo, bar)

R R
R
.RData10
.Rhistory
R

R
x y

10
UNIX

2.1
R data structure
vector
x10.45.63.16.4 21.7 R

> x <- c(10.4, 5.6, 3.1, 6.4, 21.7)

c() c()
1
R 1
(<-)< () - ()
2
=
assign()

> assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))

<-

> c(10.4, 5.6, 3.1, 6.4, 21.7) -> x


3
1
c() list <
35>
2
-><--<>-
3
.Last.value

2.2 9

> 1/x

(x )

> y <- c(x, 0, x)

11yx 0

2.2

recycled(
)

> v <- 2*x + y + 1

11v2*x 2.2y 1 11

+-*/ ^
logexpsincostansqrt
max min range
2c(min(x), max(x)) length(x) x sum(x)
x prod(x)
mean(x) sum(x)/length(x)
var(x)var(x)

> sum((x-mean(x))^2)/(length(x)-1)

var() np p-
pp
sort(x) x
(order() sort.list())
max min
parallel
pmax pmin
2.3 10

> sqrt(-17)

NaN

> sqrt(-17+0i)

2.3
R 1:30 c(1, 2, ..., 29, 30)
R2*1:15 c(2, 4, ..., 28, 30)
n <- 101:n-11:(n-1)
30:1 construction
seq()

seq(2,10) 2:10
seq() R
from=value to=value
seq(1,30)seq(from=1, to=30)seq(to=30, from=1) 1:30
seq() by=value length=value
by=11

> seq(-5, 5, by=.2) -> s3

c(-5.0, -4.8, -4.6, ..., 4.6, 4.8, 5.0) s3

> s4 <- seq(length=51, from=-5, by=.2)

s4
2.4 11

along=vector 4
1, 2, ..., length(vector )vector
rep()

> s5 <- rep(x, times=5)

x x s5

> s6 <- rep(x, each=5)

2.4
R
TRUEFALSE NA (, )T F
T F TRUE FALSE reserved
wordTRUE
FALSE
conditions

> temp <- x > 13

, temp x FALSE x
TRUE
R <<=>>= ==
!= c1 c2 c1 & c2
()c1 | c2 ()!c1 c1
FALSE
0 TRUE 1

4

length(vector )
2.5 12

2.5

not available missing value
NA5 NA NA

is.na(x) x TRUE x
NA

> z <- c(1:3,NA); ind <- is.na(z)

x == NA is.na(x) NA
, x == NA x
NA

Not a NumberNaN

> 0/0

> Inf - Inf

NaN
NA NaN is.na(xx) TRUEis.nan(xx)
NaN TRUE
<NA> 6
> a<-c("a","b",NA)
> a
[1] "a" "b" NA
> print(a,quote=F)
[1] a b <NA>
5
0.01, PDF

6

2.6 13

2.6
R
"x-values""New iteration results"
(") ()
()C escape
sequences\ \ \ "
\"\n\t\b
c()

paste()

sep=string
string

> labs <- paste(c("X","Y"), 1:10, sep="")

labs

c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10")

c("X", "Y") 5
1:10 7

2.7
subset

1.
TRUE FALSE

> y <- x[!is.na(x)]


7
paste(..., collapse=ss) ss R
sub substring
2.7 14

()x yx
y x

> (x+1)[(!is.na(x)) & x>0] -> z

z x+1 x

2. {1, 2, . . . , length(x)}


x[6]x

> x[1:10]

x 10(length(x) 10)

> c("x","y")[rep(c(1,2,2,1), times=4)]

16"x", "y", "y", "x" 4

3. 8

> y <- x[-(1:5)]

x y

4. names

> fruit <- c(5, 10, 1, 20)


> names(fruit) <- c("orange", "banana", "apple", "peach")
> lunch <- fruit[c("apple","orange")]

name indicesnumeric indices


data frames
8

2.8 15


vector[indexv ector ]

> x[is.na(x)] <- 0

0x

> y[y < 0] <- -y[y < 0]

> y <- abs(y)

2.8
R

matrixarray

<23>

factor<20>.

listgeneral form

<34>.

data frame


<36>

function R R
<54>

3.1
R object
atomic
1 R 2 numericcomplex
logicalcharacterraw

(NA
NA)
character(0) numeric(0)
R R list

functionexpression R

R R
formulae
mode
propertylength
mode(object) length(object) 3
attributes(object)
<18>.
z 100 mode(z)
"complex" length(z) 100
R ()
1
R typeof()S
2
integerdouble

3
length(object) object
3.2 17

> z <- 0:9

> digits <- as.character(z)

digits c("0", "1", "2", ..., "9")

> d <- as.integer(digits)

d z 4 as.something()

3.2

> e <- numeric()

echaracter()

> e[3] <- 17

3e(NA)

scan()(scan()
<40>.)
alpha
10

> alpha <- alpha[2 * 1:5]

5(
)

> length(alpha) <- 3

4

roundoff
5

3.3 18

3.3
attributes(object) non-intrinsic at-
tributesattr(object, name)
6 ,

object
,

> attr(z, "dim") <- c(10,10)

R z 1010

3.4
R classclass
"numeric""logical""character" "list"
"matrix""array""factor" "data.frame"
7 R
"data.frame" 8 plot()
9 generic functionsummary()

unclass() winter
"data.frame"

> winter

> unclass(winter)
6
:attributes(object). ,
. str(object).
7
methods S4
8
: print
9
Java C++ Java 1.5 5.0

3.4 19

<63>

factor R
orderedunordered
(<69>)

4.1
301
state

> state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
"qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
"sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
"sa", "act", "nsw", "vic", "vic", "act")

2
factor()

> statef <- factor(state)

print()

> statef
[1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa
[16] tas sa nt wa vic qld nsw nsw wa sa act nsw vic vic act
Levels: act nsw nt qld sa tas vic wa

levels() levels

> levels(statef)
[1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa"
1
8Australian Capital Territory, New South Wales, the Northern
Territory, Queensland, South Australia, Tasmania, Victoria Western Australia
2
: , .
4.2 tapply() 21

4.2 tapply()

()

> incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
59, 46, 58, 43)

tapply()

> incmeans <- tapply(incomes, statef, mean)

act nsw nt qld sa tas vic wa


44.500 57.333 55.500 53.600 55.000 60.500 56.000 52.250

tapply() mean()3 statef


incomes

standard error4
R var()

> stderr <- function(x) sqrt(var(x)/length(x))

(<54>
R sd()5 )

> incster <- tapply(incomes, statef, stderr)

3
tapply() tapply(incomes, state)
R as.factor()
4

5
: . R sd(),
stderr(1:4)sd(1:4). stderr <-
function(x){sd(x)/sqrt(length(x))}
4.3 22

> incster
act nsw nt qld sa tas vic wa
1.5 4.3102 4.5 4.1061 2.7386 0.5 5.244 2.6575

95%
tapply() length()t-
qt()( R t-)
tapply()

()


ragged array

4.3
factor

ordered()
ordered() factor
,

5.1
R

dimension vectork
k-2-1

dim R z1500

> dim(z) <- c(3,5,100)

dim 3 5 100
matrix() array() array()
<26>.
data vector FORTRAN

ac(3,4,2)a 342 = 24 a[1,1,1],


a[2,1,1], ..., a[2,4,2], a[3,4,2]
()

5.2

a[2,,] 4 2 c(4,2)

5.3 24

c(a[2,1,1], a[2,2,1], a[2,3,1], a[2,4,1],


a[2,1,2], a[2,2,2], a[2,3,2], a[2,4,2])

a[,,]a
Zdim(Z) (
)

5.3

4 5 X

X[1,3], X[2,2] X[3,1]

X 0

3 2
5.3 25

> x <- array(1:20, dim=c(4,5)) # 4 5


> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> i <- array(c(1:3,3:1), dim=c(3,2))
> i # i 3 2
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
> x[i] #
[1] 9 6 3
> x[i] <- 0 # 0
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 0 13 17
[2,] 2 0 10 14 18
[3,] 0 7 11 15 19
[4,] 4 8 12 16 20
>

blocks(b)varieties
(v )n

> Xb <- matrix(0, n, b)


> Xv <- matrix(0, n, v)
> ib <- cbind(1:n, blocks)
> iv <- cbind(1:n, varieties)
> Xb[ib] <- 1
> Xv[iv] <- 1
> X <- cbind(Xb, Xv)

> N <- crossprod(Xb, Xv)

table()

> N <- table(blocks, varieties)


5.4 array() 26

5.4 array()
dim array

> Z <- array(data vector , dim vector )

h 24

> Z <- array(h, dim=c(3,4,2))

h Z 3 4 2 h 24

> dim(Z) <- c(3,4,2)

h 2424 (
<26>)

> Z <- array(0, c(3,4,2))

Z 0
dim(Z) c(3,4,2)Z[1:24] h
Z[] Z

dim
A, B C

> D <- 2*A*B + C + 1

5.4.1


5.5 27

dim
1


dim

5.5
outer producta b

()a b
%o%

> ab <- a %o% b

> ab <- outer(a, b, "*")


f (x; y) = cos(y)/(1 + x2 ) R x yregular grid

> f <- function(x, y) cos(y)/(1 + x^2)


> z <- outer(x, y, f)

(1)
R <
54>.

2 2

2 2 [a, b; c, d]
0, 1, . . . , 9
ad bchigh
density

outer()
1
: As long as short vectors and arrays only are encountered, the arrays must all have
the same dim attribute or an error results.
5.6 28

> d <- outer(0:9, 0:9)


> fr <- table(outer(d, d, "-"))
> plot(as.numeric(names(fr)), fr, type="h",
xlab="Determinant", ylab="Frequency")

names
for <52>

202

5.6
aperm(a, perm) aperm {1, . . . , k}
k a a
perm[j]j
A( )B

> B <- aperm(A, c(2,1))

A t()
B <- t(A)

5.7

R t(X)
nrow(A) ncol(A) A

5.7.1
%*% n 1 1 n
n

A B

> A * B

2
: It is also perhaps surprising that about 1 in 20 such matrices is singular.
5.7 29

> A %*% B

> x %*% A %*% x

quadratic form3
crossprod() crossproductcrossprod(X,
y) t(X) %*% y crossprod()

diag() v diag(v)
M diag(M) M
Matlab diag() k 4
diag(k) k k

5.7.2

> b <- A %*% x

A bx R

> solve(A,b)

x ()
x = A1 b A1 Ainverse

solve(A)

x <- solve(A) %*% bsolve(A,b)

x0 A1 x5 x %*% solve(A,x)
A
3
x %*% x x0 x xx0 x
x0 x
xx0 cbind(x) %*% x x %*% rbind(x) rbind() cbind()
x0 x xx0 crossprod(x) x %o% x
4
.
5
A = BB 0 B A Cholesky
By = x squared length
Even better would be to form a matrix square root B with A = BB 0 and find the squared length
of the solution of By = x, perhaps using the Cholesky or eigendecomposition of A.
5.7 30

5.7.3
eigen(Sm) Sm
values vectors

> ev <- eigen(Sm)

evev$val Sm ev$vec

> evals <- eigen(Sm)$values

evals

> eigen(Sm)

> evals <- eigen(Sm, only.values = TRUE)$values

5.7.4
svd(M) M, M
M U M V
D M = U %*% D %*% t(V)D
svd(M) d, u v
M

> absdetM <- prod(svd(M)$d)

M
R

> absdet <- function(M) prod(svd(M)$d)

, absdet() R
tracetr() [: ,
diag()]
R det

5.8 cbind() rbind() 31

5.7.5 QR
lsfit() Least squares fitting

> ans <- lsfit(X, y)

y X
regression diagnostics
ls.diag()X
lm(.) (<70>)
lsfit()
qr()

> Xplus <- qr(X)


> b <- qr.coef(Xplus, y)
> fit <- qr.fitted(Xplus, y)
> res <- qr.resid(Xplus, y)

y fit X res b
b Matlab
X


R
<66>

5.8 cbind() rbind()


cbind() rbind()
cbind() rbind()

> X <- cbind(arg 1 , arg 2 , arg 3 , ...)

cbind()
arg1 , arg2 , . . .
cbind()
()
5.9 c() 32

rbind()

X1 X2 X
1

> X <- cbind(1, X1, X2)

rbind() cbind() cbind(x) rbind(x)


x

5.9 c()
cbind() rbind() dim c()
dim dimnames
as.vector()

> vec <- as.vector(X)

c()

> vec <- c(X)

(
)

5.10

table() k
k-
statef

> statefr <- table(statef)

statefr

> statefr <- tapply(statef, statef, length)


5.10 33

incomef
cut()

> factor(cut(incomes, breaks = 35+10*(0:7))) -> incomef

> table(incomef,statef)
statef
incomef act nsw nt qld sa tas vic wa
(35,45] 1 1 0 1 0 0 1 0
(45,55] 1 1 1 1 2 0 1 3
(55,65] 0 3 1 3 2 2 2 1
(65,75] 0 1 0 0 0 0 1 0

6.1
R list
components

> Lst <- list(name="Fred", wife="Mary", no.children=3,


child.ages=c(4,7,9))

numbered
Lst Lst[[1]], Lst[[2]], Lst[[3]] Lst[[4]]
Lst[[4]] Lst[[4]][1]

Lst length(Lst) (
)

> name$component name


Lst$name Lst[[1]] "Fred",
Lst$wife Lst[[2]] "Mary",
Lst$child.ages[1] Lst[[4]][1] 4
6.2 35

Lst$name
Lst[["name"]]

> x <- "name"; Lst[[x]]

Lst[[1]] Lst[1] [[. . . ]]


[. . . ] Lst
, named list
1 Lst

2 Lst$coefficients
Lst$coe Lst$covariance Lst$cov

(names)

6.2
list()

> Lst <- list(name 1 =object 1 , . . . , name m=object m)

m Lstobject 1 , . . . , object m

> Lst[5] <- list(matrix=Mat)

6.2.1
c()

> list.ABC <- c(list.A, list.B, list.C)


1
: ().
2

6.3 36


dim

6.3
data frame"data.frame"

(, , );

6.3.1
data.frame ()

> accountants <- data.frame(home=statef, loot=incomes, shot=incomef)

as.data.frame()
read.table()
<39>

6.3.2 attach() detach()


$ accountants$statef

attach()
lentils lentils$u, lentils$v, lentils$w

> attach(lentils)
6.3 37

2position 23 1u, v w
u, v w

> u <- v+w

u1u
4 $

> lentils$u <- v+w

u 5

> detach()

2u, v w
lentils$u
2detach
detach(lentils) detach("lentils")

R 2
(
)6

6.3.3

,
;

21

$
detach()
3
R 1attach()
2
4
:1u, u <- v + w, 1u.
5
: u, u, .
6
: R, .
6.3 38

x, y z

6.3.4
attach()
"list"

> attach(any.old.list)

detach

6.3.5
search
()

> search()
[1] ".GlobalEnv" "Autoloads" "package:base"

.GlobalEnv 7
lentils

> search()
[1] ".GlobalEnv" "lentils" "Autoloads" "package:base"
> ls(2)
[1] "u" "v" "w"

ls (objects)

> detach("lentils")
> search()
[1] ".GlobalEnv" "Autoloads" "package:base"

7
autoload

R R
R
Perl1
R
read.table()
scan()2 ,
R R /3

7.1 read.table()

1
UNIXSed Awk
2
read.table()scan()Excel

3
R
4
R
read.table() header=TRUE
7.2 scan() 40

Price Floor Area Rooms Age Cent.heat


01 52.00 111.0 830 5 6.2 no
02 54.75 128.0 710 5 7.5 no
03 57.50 101.0 1000 5 4.2 no
04 57.50 131.0 690 6 8.8 no
05 59.75 93.0 900 5 1.9 yes
...

(),
Cent.heat
read.table()

> HousePrice <- read.table("houses.data")

Price Floor Area Rooms Age Cent.heat


52.00 111.0 830 5 6.2 no
54.75 128.0 710 5 7.5 no
57.50 101.0 1000 5 4.2 no
57.50 131.0 690 6 8.8 no
59.75 93.0 900 5 1.9 yes
...

> HousePrice <- read.table("houses.data", header=TRUE)

header=TRUE

7.2 scan()

input.datscan()

> inp <- scan("input.dat", list("",0,0))


7.3 41

dummy list structure


inp

> label <- inp[[1]]; x <- inp[[2]]; y <- inp[[3]]

> inp <- scan("input.dat", list(id="", x=0, y=0))

> label <- inp$id; x <- inp$x; y <- inp$y

2(<38>)

> X <- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE)

7.3
R 100(datasets )(R
)

data()

R 2.0.0R
data R

data(infert)

()
R

7.4 42

7.3.1 R
package

data(package="rpart")
data(Puromycin, package="datasets")

library R

7.4
edit R

> xnew <- edit(xold)

xoldxnew
xoldfix(xold)xold <- edit(xold)

> xnew <- edit(data.frame())

8.1 R
R R P (X
x)(qP (X x) > q x
)

R
beta shape1, shape2, ncp
binom size, prob
Cauchy cauchy location, scale
chisq df, ncp
exp rate
F f df1, df1, ncp
gamma shape, scale
geom prob
hyper m, n, k
lnorm meanlog, sdlog
logistic logis location, scale
nbinom size, prob
norm mean, sd
Poisson pois lambda
t t df, ncp
unif min, max
Weibull weibull shape, scale
Wilcoxon wilcox m, n

d p
cumulative distribution functionCDF q r
8.2 44

(r andom deviates) dxxx xpxxx qqxxx


p rxxx n (rhyper rwilcox nn) non-
centrality parameterncp
1
pxxx qxxx lower.tail log.p, dxxx
log
() hazardH(t) = log(1 F (t))

- pxxx (t, ..., lower.tail = FALSE, log.p = TRUE)

(dxxx (..., log = TRUE))


ptukey qtukey studentized
range

> ## tp
> 2*pt(-2.43, df = 13)
> ## F(2, 7)1%
> qf(0.99, 2, 7)

8.2

summary fivenum stem
()
1
In not quite all cases is the non-centrality parameter ncp are currently available...
8.2 45

> attach(faithful)
> summary(eruptions)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.600 2.163 4.000 3.488 4.454 5.100
> fivenum(eruptions)
[1] 1.6000 2.1585 4.0000 4.4585 5.1000
> stem(eruptions)

The decimal point is 1 digit(s) to the left of the |

16 | 070355555588
18 | 000022233333335577777777888822335777888
20 | 00002223378800035778
22 | 0002335578023578
24 | 00228
26 | 23
28 | 080
30 | 7
32 | 2337
34 | 250077
36 | 0000823577
38 | 2333335582225577
40 | 0000003357788888002233555577778
42 | 03335555778800233333555577778
44 | 02222335557780000000023333357778888
46 | 0000233357700000023578
48 | 00000022335800333
50 | 0370

R hist
> hist(eruptions)
> ##
> hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
> lines(density(eruptions, bw=0.1))
> rug(eruptions) #

density
density trial-and-errorbwbandwidth

(2 bw = "SJ"
2
?density?bw.nrd
8.2 46

Histogram of eruptions

0.7
0.6
0.5
Relative Frequency

0.4
0.3
0.2
0.1
0.0

1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0

eruptions

ecdf empirical cumulative


distribution

> plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)

> long <- eruptions[eruptions > 3]


> plot(ecdf(long), do.points=FALSE, verticals=TRUE)
> x <- seq(3, 5.4, 0.01)
> lines(x, pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3)

Quantile-quantile (Q-Q) plot

par(pty="s") #
qqnorm(long); qqline(long)

QQ
t

x <- rt(250, df = 5)
qqnorm(x); qqline(x)
8.2 47

ecdf(long)

1.0
0.8
0.6
Fn(x)

0.4
0.2
0.0

3.0 3.5 4.0 4.5 5.0

Normal QQ Plot
5.0
4.5
Sample Quantiles

4.0
3.5
3.0

2 1 0 1 2

Theoretical Quantiles

QQ()
Q-Q

qqplot(qt(ppoints(250), df = 5), x, xlab = "Q-Q plot for t dsn")


qqline(x)

RShapiro-Wilk

8.3 48

> shapiro.test(long)

Shapiro-Wilk normality test

data: long
W = 0.9793, p-value = 0.01052

Kolmogorov-Smirnov

> ks.test(long, "pnorm", mean = mean(long), sd = sqrt(var(long)))

One-sample Kolmogorov-Smirnov test

data: long
D = 0.0661, p-value = 0.4284
alternative hypothesis: two.sided

(distribution theory
)

8.3

R stats

latent heat(cal/gm) Rice (1995, p.490)

Method A: 79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97


80.05 80.03 80.02 80.00 80.02
Method B: 80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97

boxplot

A <- scan()
79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97
80.05 80.03 80.02 80.00 80.02

B <- scan()
80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97

boxplot(A, B)
8.3 49

80.04
80.02
80.00
79.98
79.96
79.94

1 2

t-

> t.test(A, B)

Welch Two Sample t-test

data: A and B
t = 3.2499, df = 12.027, p-value = 0.00694
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
0.01385526 0.07018320
sample estimates:
mean of x mean of y
80.02077 79.97875

3 R
SPLUS t.test
F
3
:t-, t-, .
8.3 50

> var.test(A, B)

F test to compare two variances

data: A and B
F = 0.5837, num df = 12, denom df = 7, p-value = 0.3938
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.1251097 2.1052687
sample estimates:
ratio of variances
0.5837405

t-

> t.test(A, B, var.equal=TRUE)

Two Sample t-test

data: A and B
t = 3.4722, df = 19, p-value = 0.002551
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
0.01669058 0.06734788
sample estimates:
mean of x mean of y
80.02077 79.97875

Wilcoxon (Mann-Whitney)
(null hypothesis)

> wilcox.test(A, B)

Wilcoxon rank sum test with continuity correction

data: A and B
W = 89, p-value = 0.007497
alternative hypothesis: true mu is not equal to 0

Warning message:
Cannot compute exact p-value with ties in: wilcox.test(A, B)
8.3 51

, (
)

> plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))


> plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)

qqplot Q-Q
Kolmogorov-Smirnov
Kolmogorov-Smirnov

> ks.test(A, B)

Two-sample Kolmogorov-Smirnov test

data: A and B
D = 0.5962, p-value = 0.05919
alternative hypothesis: two.sided

Warning message:
cannot compute correct p-values with ties in: ks.test(A, B)

9.1
R expression language

{expr 1 ; . . . ; expr m}

9.2
9.2.1 if
R

> if (expr1 ) expr2 else expr3

expr1

short-circuit&& || if
& | 1 && || 1
2
R if/else ifelseifelse(condition,
a, b)condition[i]
a[i]b[i]
1
: .
2
: TRUEFALSE.
9.2 53

9.2.2 forrepeat while


R for

> for (name in expr 1 ) expr 2

name expr 1 ( 1:20 )


expr 2 name name expr 1
expr 2
ind vector of class indicators
y xcoplot()3

> xc <- split(x, ind)


> yc <- split(y, ind)
> for (i in 1:length(yc)) {
plot(xc[[i]], yc[[i]]);
abline(lsfit(xc[[i]], yc[[i]]))
}

(split()
help )

R for()
R whole object4

> repeat expr

> while (condition) expr

breakrepeat

next
<54>

3
lattice xyplot
4
R
, summeanapplylapply
sapplytapply

R function
R R

R
R mean(), var(),
postscript() R

> name <- function(arg 1 , arg 2 , ...) expression

expression R ()argi

name(expr1 , expr2 , ...)

10.1
t-

> twosam <- function(y1, y2) {


n1 <- length(y1); n2 <- length(y2)
yb1 <- mean(y1); yb2 <- mean(y2)
s1 <- var(y1); s2 <- var(y2)
s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
tst <- (yb1 - yb2)/sqrt(s*(1/n1 + 1/n2))
tst
}

t-
10.2 55

> tstat <- twosam(data$male, data$female); tstat

Matlab y X
() qr()

n 1 y n p XX\y (X 0 X) X 0 y,
(X 0 X) X 0 Xgeneralized inverse

> bslash <- function(X, y) {


X <- qr(X)
qr.coef(X, y)
}

> regcoeff <- bslash(Xmat, yvar)

R lsfit() 1
qr() qr.coef()

binary operator

10.2
bslash()

%anything%

binary operator
!

> "%!%" <- function(X, y) { ... }

()X %!% y(
)
%*% %o%
1
R<66>
10.3 56

10.3
<10> name=object

fun1

> fun1 <- function(data, data.frame, graph, limit) {


[]
}

> ans <- fun1(d, df, TRUE, 20)


> ans <- fun1(d, df, graph=TRUE, limit=20)
> ans <- fun1(data=d, limit=20, graph=TRUE, data.frame=df)


fun1

> fun1 <- function(data, data.frame, graph=TRUE, limit=20) { ... }

> ans <- fun1(d, df)

> ans <- fun1(d, df, limit=10)

10.4 . . .

par() plot()
par() (par()<90> par()
)
. . .
10.5 57

fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {


[]
if (graph)
par(pch="*", ...)
[]
}

10.5

X <- qr(X)
R Evalution frame

superassignment
<<- assign()helpSPLUS
<<- R semantics<
60>

10.6
10.6.1

(<24>)
block designblocks (b ) varieties (v
)R K v v b b replicationsblock
sizeN b v incidence matrix
E = Iv R1/2 N 0 K 1 N R1/2 = Iv A0 A, A = K 1/2 N R1/2

10.6 58

> bdeff <- function(blocks, varieties) {


blocks <- as.factor(blocks) # minor safety move
b <- length(levels(blocks))
varieties <- as.factor(varieties) # minor safety move
v <- length(levels(varieties))
K <- as.vector(table(blocks)) # dim
R <- as.vector(table(varieties)) # dim
N <- table(blocks, varieties)
A <- 1/sqrt(K) * N * rep(1/sqrt(R), rep(b, v))
sv <- svd(A)
list(eff=1 - sv$d^2, blockcv=sv$u, varietycv=sv$v)
}



block and variety canonical contrasts

10.6.2

dimnames R
dimnames X

> temp <- X


> dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)))
> temp; rm(temp)

no.dimnames()
wrap around

no.dimnames <- function(a) {


##
d <- list()
l <- 0
for(i in dim(a)) {
d[[l <- l + 1]] <- rep("", i)
}
dimnames(a) <- d
a
}
10.6 59

> no.dimnames(X)

pattern

10.6.3

evaluation frame
2

one-panel trapezium rule
(two panel)

3 ,

R
2
that such functions, or indeed variables, are not inherited by called functions in
higher evaluation frames as they would be if they were on the search path.
3
The result is an adaptive integration process that concentrates funtion evalu-
tions in regions where the integrand is farthest from linear.
10.7 60

area <- function(f, a, b, eps = 1.0e-06, lim = 10) {


fun1 <- function(f, a, b, fa, fb, a0, eps, lim, fun) {
## ~fun1
d <- (a + b)/2
h <- (b - a)/4
fd <- f(d)
a1 <- h * (fa + fd)
a2 <- h * (fd + fb)
if(abs(a0 - a1 - a2) < eps || lim == 0)
return(a1 + a2)
else {
return(fun(f, a, d, fa, fd, a1, eps, lim - 1, fun) +
fun(f, d, b, fd, fb, a2, eps, lim - 1, fun))
}
}
fa <- f(a)
fb <- f(b)
a0 <- ((fa + fb) * (b - a))/2
fun1(f, a, b, fa, fb, a0, eps, lim, fun1)
}

10.7

SPLUS R
(formal parameters)(local
variables)(free variables)

f <- function(x) {
y <- 2*x
print(x)
print(y)
print(z)
}

x y z
R
lexical scopecube,
10.7 61

cube <- function(n) {


sq <- function() n*n
n*sq()
}

sq n
(SPLUS)n
(R)cube sq
nR SPLUS SPLUS
n R cuben

## \sm{S}
S> cube(2)
Error in sq(): Object "n" not found
Dumped
S> n <- 3
S> cube(2)
[1] 18
## \R{} }
R> cube(2)
[1] 8

mutable stateR

account
account total
total

<<- total
total

total<<-
4 <<-

4
SPLUS SPLUS

10.8 62

open.account <- function(total) {


list(
deposit = function(amount) {
if(amount <= 0)
stop("Deposits must be positive!\n")
total <<- total + amount
cat(amount, "deposited. Your balance is", total, "\n\n")
},
withdraw = function(amount) {
if(amount > total)
stop("You dont have that much money!\n")
total <<- total - amount
cat(amount, "withdrawn. Your balance is", total, "\n\n")
},
balance = function() {
cat("Your balance is", total, "\n\n")
}
)
}

ross <- open.account(100)


robert <- open.account(200)

ross$withdraw(30)
ross$balance()
robert$balance()

ross$deposit(50)
ross$balance()
ross$withdraw(500)

10.8

.First .Last
R PROFILE
Retc Rprofile.siteR
.Rprofile5
R
5
UNIX
10.9 63

.RprofileR 6 .Rprofile ()
.RData .First() R
$

Rprofile.site.Rprofile.RData .First()

> .First <- function() {


options(prompt="$ ", continue="+\t") # $
options(digits=5, length=999) #
x11() #
par(pch = "+") #
source(file.path(Sys.getenv("HOME"), "R", "mystuff.R"))
#
library(MASS) #
}

.Last()()

> .Last <- function() {


graphics.off() #
cat(paste(date(),"\nAdios\n")) #
}

10.9
7


plot() summary()
anova()

"data.frame"
6
Linux ~/
7
R Java(interface).,
, . .
10.9 64

[ [[<- any as.matrix


[<- mean plot summary

methods()

> methods(class="data.frame")

plot()
"data.frame""density""factor"
methods()

> methods(plot)

> coef
function (object, ...)
UseMethod("coef")

UseMethod
methods()

> methods(coef)
[1] coef.aov* coef.Arima* coef.default* coef.listof*
[5] coef.nls* coef.summary.nls*

Non-visible functions are asterisked

8
: (coef.aov), R Error: object
"coef.aov" not found.
10.9 65

> getAnywhere("coef.aov")
A single object matching coef.aov was found
It was found in the following places
registered S3 method for coef from namespace stats
namespace:stats
with value

function (object, ...)


{
z <- object$coef
z[!is.na(z)]
}

> getS3method("coef", "aov")


function (object, ...)
{
z <- object$coef
z[!is.na(z)]
}

R
R


1
R

11.1

p
X
yi = j xij + ei , ei NID(0, 2 ), i = 1, . . . , n
j=0

y = X + e

y X model matrix design ma-


trixX x0 , x1 , . . . , xp determining variablex0
1intercept


y, x, x0, x1, x2, . . . X A, B, C, . . .

1
: Later we make some rather more ambitious presumptions, namely that something
is known about generalized linear models and nonlinear regression.
11.1 67

y ~ x
y ~ 1 + x y x

y ~ 0 + x
y ~ -1 + x
y ~ x - 1 y x ()
log(y) ~ x1 + x2 y log(y) x1 x2 (
)
y ~ poly(x,2)
y ~ 1 + x + I(x^2) y x
orthogonal polynomial

y ~ X + poly(x,2) y X x
y ~ A y A
y ~ A + x y A
x
y ~ A*B
y ~ A + B + A:B
y ~ B %in% A
y ~ A/B y A Btwo factor non-
additive model
crossed classification
(nested classification)

y ~ (A + B + C)^2
y ~ A*B*C - A:B:C main effects
interactions
y ~ A * x
y ~ A/x
y ~ A/(1 + x) - 1 Ay x
A
2

2
Separate simple linear regression models of y on x within the levels of A, with
different codings. The last form produces explicit estimates of as many different intercepts and slopes
as there are levels in A.
11.1 68

y ~ A*B + Error(C) A BC
error stratasplit plot
experiment C

~ R model formula

response ~ op 1 term 1 op 2 term 2 op 3 term 3 . . .

response /

op i + -
(3

term i

1


formula expression

formula operatorsGlim Genstat Wilkinson


& Rogers (notation) . R :,
R
(Chambers & Hastie, 1992, p.29):

Y ~ M Y M
M 1 + M 2 M 1 M 2
M 1 - M 2 M 1 M 2
M 1 : M 2 M 1 M 2 tensor product
(subclasses factor)4
3
: .
4

11.1 69

M 1 %in% M 2 M 1 :M 2
M 1 * M 2 M 1 + M 2 + M 1 :M 2 .
M 1 / M 2 M 1 + M 2 %in% M1 .
M ^n M n5
I(M ) M M


I() identity function

11.1.1

(
1)
k-A
2. . . k k 1 (
)k 1
1, . . . , k orthogonal polynomial

k 6
options contrasts R

options(contrasts = c("contr.treatment", "contr.poly"))

R S SHelmert
SPLUS

options(contrasts = c("contr.helmert", "contr.poly"))

treatment contrast(R)

5
: All terms in M together with interactions up to order n
6
First, if the intercept is omitted in a model that contains a factor term, the first
such term is encoded into k columns giving the indicators for all the levels.
11.2 70

contrasts C

7
R
R

11.2
multiple modellm()

> fitted .model <- lm(formula, data = data.frame)

> fm2 <- lm(y ~ x1 + x2, data = production)

y x1 x2 ()
()data = production
productionproduction

11.3
lm() "lm"
"lm"

add1 coef effects kappa predict residuals


alias deviance family labels print step
anova drop1 formula plot proj summary

7
We have not yet considered interaction terms: these generate the products of
the columns introduced for their component terms.
11.3 71

anova(object 1 , object 2 )

coef(object) ()
coefficients(object).
deviance(object)
formula(object)
plot(object)
8
predict(object, newdata=data.frame)

data.frame

predict.gam(object,
newdata=data.frame) predict.gam()
predict()lm, glm
gam



print(object)
9
residuals(object) ( )

resid(object)
step(object)

AIC (Akaike
)

summary(object)

8

9
print()
11.4 72

11.4
aov(formula, data=data.frame) lm()
<70>
aov() multiple error strata
split plot experiments
balanced incomplete block design

response mean.formula + Error(strata.formula)

strata.formula strata.formula

> fm <- aov(yield ~ v + n*p*k + Error(farms/blocks), data=farm.data)

v + n*p*k

11.4.1
(sequence)10

11
(multistratum experiments)
Chambers &
Hastie (1992)
ANOVA tableanova()

> anova(fitted .model .1 , fitted .model .2 , ...)


hierarchical sequence

10
Note also that the analysis of variance table (or tables) are for a sequence of
fitted models.
11
: Hence only for orthogonal experiments will the order of inclusion be inconsequen-
tial.
11.5 73

11.5
update()

> new .model <- update(old .model , new .formula)

new .formula .

> fm05 <- lm(y ~ x1 + x2 + x3 + x4 + x5, data = production)


> fm6 <- update(fm05, . ~ . + x6)
> smf6 <- update(fm6, sqrt(.) ~ .)

production

data=
update()
.

> fmfull <- lm(y ~ . , data = production)

y production
add1(), drop1() step()

11.6

12

ystimulus variablex1 , x2 , . . .

12
Generalized linear modeling is a development of linear models to accommodate
both non-normal response distributions and transformations to linearity in a clean and straightforward
way.
11.6 74

y
linear predictor

= 1 x1 + 2 x2 + + p xp ,

xi i = 0 y

y

A
fY (y; , ) = exp {y() (())} + (y, )

scale parameter()A
y
y

smooth invertible function

= m(), = m1 () = `()

(inverse function) `() link function


(estimation and inference)
McCullagh & Nelder
(1989) Dobson (1990)

11.6.1
R (gaussian), ,
(poisson), (inverse gaussian) (gamma)
quasi-likelihood
variance function


binomial logit, probit, log, cloglog
gaussian identity, log, inverse
11.6 75

Gamma identity, inverse, log


inverse.aussian 1/mu^2, identity, inverse, log
poisson identity, log, sqrt
quasi logit, probit, cloglog, identity,
inverse, log, 1/mu^2, sqrt


family

11.6.2 glm()

R glm()

> fitted .model <- glm(formula, family=family.generator , data=data.frame)

lm() family.generator

13
<74>

quasi

gaussian

> fm <- glm(y ~ x1 + x2, family = gaussian, data = sales)

> fm <- lm(y ~ x1+x2, data=sales)

13
: The only new feature is the family.generator , which is the instrument by which
the family is described. It is the name of a function that generates a list of functions and expressions
that together define and control the model and estimation process.
11.6 76

Silvey (1970)
Kalythos Aegean

Age: 20 35 45 55 70
No.: tested: 50 50 50 50 50
No.: blind: 6 17 26 37 44

logistic probit
LD5050%
y n x

y B(n, F (0 + 1 x))

probit F (z) = (z) logit (


)F (z) = ez /(1 + ez )

LD50 = 0 /1

0
,

> kalythos <- data.frame(x = c(20,35,45,55,70), n = rep(50,5),


y = c(6,17,26,37,44))

glm()

binary0/1

(0) (1)


11.6 77

> kalythos$Ymat <- cbind(kalythos$y, kalythos$n - kalythos$y)

> fmp <- glm(Ymat ~ x, family = binomial(link=probit), data = kalythos)


> fml <- glm(Ymat ~ x, family = binomial, data = kalythos)

logit

> summary(fmp)
> summary(fml)

LD50

> ld50 <- function(b) -b[1]/b[2]


> ldp <- ld50(coef(fmp)); ldl <- ld50(coef(fml)); c(ldp, ldl)

43.66343.601

Poisson

Poisson log
Poisson
-

Poisson
Poisson

> fmod <- glm(y ~ A + B + x, family = poisson(link=sqrt),


data = worm.counts)

multiplier
poisson
Var[y] =

gaussian

gaussian
11.7 78

1 z1
y= +e
z2 2

1
y= +e
1 x1 + 2 x2
x1 = z2 /z1 , x2 = 1/x1 , 1 = 1/1 and 2 = 2 /1

> nlfit <- glm(y ~ x1 + x2 - 1,


family = quasi(link=inverse, variance=constant),
data = biochem)

11.7
(glm())
R
optim()nlm() nlminb()R2.2.0 SPLUS ms()
nlminb()lack-of-fit
nlm()
nlm()
14

11.7.1
SSE

Bates & Watts (1988)51

> x <- c(0.02, 0.02, 0.06, 0.06, 0.11, 0.11, 0.22, 0.22, 0.56, 0.56,
1.10, 1.10)
> y <- c(76, 47, 97, 107, 123, 139, 159, 152, 191, 201, 207, 200)

> fn <- function(p) sum((y - (p[1] * x)/(p[2] + x))^2)


14
:
11.7 79

> plot(x, y)
> xfit <- seq(.02, 1.1, .05)
> yfit <- 200 * xfit/(0.1 + xfit)
> lines(spline(xfit, yfit))

200 0.1

> out <- nlm(fn, p = c(200, 0.1), hessian = TRUE)

out$minimum SSEout$estimate
(SE)

> sqrt(diag(2*out$minimum/(length(y) - 2) * solve(out$hessian)))

295% 1.96 SE

> plot(x, y)
> xfit <- seq(.02, 1.1, .05)
> yfit <- 212.68384222 * xfit/(0.06412146 + xfit)
> lines(spline(xfit, yfit))

stats
Michaelis-Menten
11.7 80

> df <- data.frame(x=x, y=y)


> fit <- nls(y ~ SSmicmen(x, Vm, K), df)
> fit
Nonlinear regression model
model: y ~ SSmicmen(x, Vm, K)
data: df
Vm K
212.68370711 0.06412123
residual sum-of-squares: 1195.449
> summary(fit)

Formula: y ~ SSmicmen(x, Vm, K)

Parameters:
Estimate Std. Error t value Pr(>|t|)
Vm 2.127e+02 6.947e+00 30.615 3.24e-11
K 6.412e-02 8.281e-03 7.743 1.57e-05

Residual standard error: 10.93 on 10 degrees of freedom

Correlation of Parameter Estimates:


Vm
K 0.7651

11.7.2
Maximum likelihood

Dobson (1990), pp. : 108111


logisticglm()

> x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113,


1.8369, 1.8610, 1.8839)
> y <- c( 6, 13, 18, 28, 52, 53, 61, 60)
> n <- c(59, 60, 62, 56, 63, 59, 62, 60)


> fn <- function(p)
sum( - (y*(p[1]+p[2]*x) - n*log(1+exp(p[1]+p[2]*x))
+ log(choose(n, y)) ))

11.8 81

> out <- nlm(fn, p = c(-50,20), hessian = TRUE)

out$minimum out$estimate

> sqrt(diag(solve(out$hessian)))

95% 1.96 SE

11.8
R

Mixed modelsnlme lme() nlme()


mixed-effects models

(Local approximating regressions)loess()




loess projection pursuit regression
stats

(Robust regression)
MASS lqs
MASS
rlm

(Additive models)
smooth additive function
acepack avas ace mda
bruto mars
gam mgcv

(Tree-based models)

11.8 82



tree()
plot() text()
R rpart tree


R device driver
graphics window
UNIX X11() Windows windows()
R

R
grid
grid
lattice S Trellis multi-panel
plot

12.1

()

12.1 84

12.1.1 plot()
R plot()

plot(x , y)
plot(xy) x y plot(x , y) y x
x y
()
plot(x ) x x

x

plot(f )
plot(f , y) f y f
y f
plot(df )
plot(~ expr )
plot(y ~ expr ) df y expr +
(a + b + c)
(
)y expr

12.1.2
R X

> pairs(X)

X pairwise scatterplot matrix


XX n(n 1)

coplot a b
c ()

> coplot(a ~ b | c)
12.1 85

c a b c a
c b c
conditioning intervals c ab a
b coplot() given.values=
co.intervals()

> coplot(a ~ b | c + d)

c d a b
coplot() pairs() panel=
points()
x y panel=
coplotpanel panel.smooth()

12.1.3

tsplot(x 1, x 2, ...)
x i
1, 2, 3, . . . 1
qqnorm(x)
qqline(x)
qqplot(x, y) x
Normal order scores(
)
x y

hist(x)
hist(x, nclass=n)
hist(x, breaks=b, ...) x

nclass=
breaks= breakpoint
probability=TRUE

1
: This automatic simultaneous scaling feature is also useful when the xi s are
ordinary numeric vectors, in which case they are plotted against the numbers 1, 2, 3, . . . .
12.1 86

dotchart(x, ...) x y-x


x-

image(x, y, z, ...)
contour(x, y, z, ...)
persp(x, y, z, ...) image
z contour contour
linez persp 3D

12.1.4

add=TRUE
()
axes=FALSE axis()
axes=TRUE
log="x"
log="y"
log="xy" x y

type= type=
type="p"
type="l"
type="b" ()
type="o"
type="h" x ( (high-
density))
type="s"
type="S"

type="n" ()

xlab=string
ylab=string x y

12.2 87

main=string
sub=string x-

12.2

()

points(x, y)
lines(x, y) plot()
type= ( points()
"p"lines() "l")
text(x, y, labels, ...) x, y labels
labels[i]
(x[i], y[i]) 1:length(x)

> plot(x, y, type="n"); text(x, y, names)

type="n"
text()
names

abline(a, b)
abline(h=y)
abline(v=x )
abline(lm.obj ) b a
h=y
y- v=x
x- lm.obj
2coefficients (
)
polygon(x, y, ...) (x, y)
(hatch lines)

12.2 88

legend(x, y, legend, ...) legend



legend
v (legend
)
legend( , fill=v )

legend( , col=v )

legend( , lty=v )

legend( , lwd=v )

legend( , pch=v )
()
title(main, sub) main
sub

axis(side, ...) (1 4
)

axes=FALSE
plot()

(x y )

x y x y

locator()()

12.2.1
R
expression textmtextaxis
title

> text(x, y, expression(paste(bgroup("(", atop(n, x), ")"), p^x, q^{n-x})))


12.3 89

> help(plotmath)
> example(plotmath)
> demo(plotmath)

12.2.2 Hershey
text contour Hershey Hershey

Hershey

Hershey

Hershey cyrillic

R Hershey

> help(Hershey)
> demo(Hershey)
> help(Japanese)
> demo(Japanese)

12.3
R
locator()

locator(n, type)
n (512)
type
locator()
x y

locator()

outlying point

> text(locator(1), "Outlier", adj=0)


12.4 90

(postscriptlocator() )

identify(x, y, labels) labels labels


x y (
)

x y (x, y)identify()

> plot(x, y)
> identify(x, y)

identify()
(
x/y )identify()
labels ()plot = FALSE
identify()
x y

12.4
R
R

(col)()

12.4.1 : par()
par()

par()

par(c("col", "lty")) (
)
12.5 91

par(col=4, lty=2) ()

par()

par()
par()
R par()
2
> oldpar <- par(col=4, lty=2)
... ...
> par(oldpar)

3
> oldpar <- par(no.readonly=TRUE)
... ...
> par(oldpar)

12.4.2
()par()

> plot(x, y, pch="+")

par()

12.5
R par()

2

3

12.5 92

name=value name par()


value

axes plot xaxt yaxt

12.5.1
R
graphical element

pch="+"

"."
pch=4 pch 025

> legend(locator(1), as.character(0:25), pch = 0:25)

2125
points
pch 32:255

lty=2 (
)10
2
lwd=2
lines()

col=2
(?palette)
col.axis
col.lab
col.main
col.sub x y

font=2
1 2 3 4
5 ()
12.5 93

font.axis
font.lab
font.main
font.sub x y
adj=-0.1 0 1
0.5
-0.1
10%
cex=1.5 ()

12.5.2
R axis()
axis line(
lty)tick mark() tick label(
)

lab=c(5, 7, 12) x y


las=1 0 1
2
mgp=c(3, 1, 0)

(0)

tck=0.01 tck
(0.5)x y 1
tck=0.01 mgp=c(1,-1.5,0)

xaxs="r"
yaxs="i" x y "i" () "r" ()
"r"
(S R )
12.5 94

12.5.3
R figure
plot region




mar[3]


3.0

Plot region
1.5
0.0
y

mai[2]
1.5
3.0

3.0 1.5 0.0 1.5 3.0

mai[1] x
Margin

mai=c(1, 0.5, 0.5, 0)


mar=c(4, 2, 2, 1) mai

mar mai

height=4 postscript() 50%


mar mai

12.5.4
R n m
outer margin
12.5 95



oma[3]

omi[4]
mfg=c(3,2,3,2)

omi[1]
mfrow=c(3,2)

mfcol=c(3, 2)
mfrow=c(2, 4)
mfcol
mfrow
mfrow=c(3,2)


(par("cex"))
0.83
0.66
mfg=c(2, 2, 3, 2)


unequally-sized figures

fig=c(4, 9, 1, 4)/10



new=TRUE (S
)
12.6 96

oma=c(2, 0, 3, 0)
omi=c(0, 0, 0.8, 0) mar mai

mtext() outer=TRUE
oma omi
split.screen() layout()4
grid lattice

12.6
R ()
R
device driverR (draw a line,)


help(Devices)

> postscript()

PostScript

X11() UNIX X11


windows() Windows
quartz() MacOS X
postscript() PostScript PostScript
pdf() PDF PDF
png() PNG ()
jpeg() JPEG image(
)

> dev.off()
4

12.6 97


()

12.6.1 PostScript
file postscript()
PostScript horizontal=FALSE
width height (
)

> postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)

5PostScript
R

PostScript encapsulatedPostScript
R conformant outputonefile=FALSE
S
(EPSF )5

> postscript("plot1.eps", horizontal=FALSE, onefile=FALSE,


height=8, width=6, pointsize=10)

12.6.2
R

X11() [UNIX]
windows()
win.printer()
win.metafile() [Windows]
quartz() [MacOS X]
postscript()
pdf()

5
GSview PostScript PostScript

12.7 98

...


(
)
dev.list()
1 null de-
vice
dev.next()
dev.prev()

dev.set(which=k ) k

dev.off(k ) k
postscript


dev.copy(device, ..., which=k )
dev.print(device, ..., which=k ) k device
postscript
. . . dev.print


graphics.off()

12.7
R brush(
)6 Swayne, Cook Buja
XGobi

http://www.research.att.com/areas/stat/xgobi/

R xgobi XGobi UNIX Windows X


R
6
SAS

12.7 99

XGobi GGobi 7 http://www.ggobi.


org

7
ggobiRggobi RRBioConductor
GgobiGgobiRggobihttp://www.ggobi.org/
downloads/GgobiRRggobiGgobi
$R HOME/library/Rggobi/libsGgobi

R packages
(
)
R

> library()

(boot Davison
& Hinkley (1997))

> library(boot)

CRAN.packages() (Windows RAqua


Packages )

> search()

( <
101>)

> help.start()

HTML Reference

13.1 101

13.1
()R R
R
R

13.2 CRAN
R
(
)R CRAN (http://CRAN.R-project.
org/ )Bioconductor (http://www.bioconductor.
org/) R

13.3
namespaces
datasets

t() R t

::
base::t
base
::: R
getAnywhere()
inter-dependent

1 R

$ R

R2
R
(R )

help.start()

HTML ()

x <- rnorm(50)
y <- rnorm(x)

x y

plot(x, y)

ls()

rm(x, y)
1
: Linux Windows
2
Windows R Windows
103

()

x <- 1:20

x = (1, 2, . . . , 20)

w <- 1 + sqrt(x)/2

dummy <- data.frame(x=x, y= x + rnorm(x)*w)


dummy

x y

fm <- lm(y ~ x, data=dummy)


summary(fm)

y x

fm1 <- lm(y ~ x, data=dummy, weight=1/w^2)


summary(fm1)

attach(dummy)

lrf <- lowess(x, y)

plot(x, y)

lines(x, lrf$y)

abline(0, 1, lty=3)

(01)
104

abline(coef(fm))

abline(coef(fm1), col = "red")

detach()

plot(fitted(fm), resid(fm),
xlab="Fitted values",
ylab="Residuals",
main="Residuals vs Fitted")

heteroscedasticity

qqnorm(resid(fm), main="Residuals Rankit Plot")

skewnesskurtosisoutlier

rm(fm, fm1, lrf, x, dummy)

Michaelson Morley
morley read.table

filepath <- system.file("data", "morley.tab" , package="datasets")


filepath

file.show(filepath)

mm <- read.table(filepath)
mm
105

Michaelson Morley (Expt


)20 (Run )sl

mm$Expt <- factor(mm$Expt)


mm$Run <- factor(mm$Run)

Expt Run

attach(mm)

3 ()

plot(Expt, Speed, main="Speed of Light Data", xlab="Experiment No.")

fm <- aov(Speed ~ Run + Expt, data=mm)


summary(fm)

runs experiments

fm0 <- update(fm, . ~ . - Run)


anova(fm0, fm)

runs

detach()
rm(fm, fm0)

x <- seq(-pi, pi, len=50)


y <- x

x x 50y

f <- outer(x, y, function(x, y) cos(y)/(1 + x^2))

f x y cos(y)/(1 + x2 )

oldpar <- par(no.readonly = TRUE)


par(pty="s")
106

contour(x, y, f)
contour(x, y, f, nlevels=15, add=TRUE)

fa <- (f-t(f))/2

fa f (t() )

contour(x, y, fa, nlevels=15)

. . .

par(oldpar)

. . .
image(x, y, f)
image(x, y, fa)

(). . .

objects(); rm(x, y, f, fa)

. . .
R
th <- seq(-pi, pi, len=100)
z <- exp(1i*th)

1i i
par(pty="s")
plot(z, type="l")

w <- rnorm(100) + rnorm(100)*1i

. . .

w <- ifelse(Mod(w) > 1, 1/w, w)

. . .
107

plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+",xlab="x", ylab="y")


lines(z)

w <- sqrt(runif(100))*exp(2*pi*runif(100)*1i)
plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+", xlab="x", ylab="y")
lines(z)

rm(th, w, z)

q()

R R

2 R

13.4 R
UNIX Windows R R

R [options] [<infile] [>outfile],

R CMD (R
) R
UNIX TMPDIR

R
(Startup Windows-
)

--no-environR
R ENVIRON
$R HOME/etc/Renviron.site ()
.Renvironname=value (help(Startup)
)R PAPERSIZE (
/)R PRINTCMD () R LIBS (R
)

R --no-site-file
R PROFILE
$R HOME/etc/Rprofile.site

--no-init-file R
.Rprofile
13.4 R 109

.RData (--no-restore --no-restore-data)

.First (R .Last
) .RData

R (Memory
)R
R

--help
-h
--version
--encoding=enc stdin iconv

RHOME R shell
man R (
)
--save
--no-save R
q()


--no-environ
--no-site-file
--no-init-file
--restore
--no-restore
--no-restore-data (R
.RData)(--no-restore
--no-restore-*)
--no-restore-history (R
.RhistoryR HISTFILE )

--vanilla --no-save, --no-environ --no-site-file,
--no-init-file --no-restore
13.4 R 110

--no-readline (UNIX) readline


Emacs ESS (Emacs Speaks Statistics) R
<114>

--ess (Windows)R-inferior-mode ESS
Rterm
--min-vsize=N
--max-vsize=N vector heap N

N Giga
(230)Mega (220)Kilo (210 ) kilo
(1000)GMK k
--min-nsize=N
--max-nsize=N cons cons cellsN
N
32cons 28
6456
--max-ppsize=N N
10000
100000
--max-mem-size=N (Windows) R
1024Mb
16Mb
--quiet
--silent
-q
--slave R R

--verbose R verbose TRUE
R
--debugger=name
-d name (UNIX) name R
R

--gui=type
13.4 R 111

-g type (UNIX) type (


)type X11
Tcl/TkTkgnomeGNOME
none
--args
commandArgs()

(< >) Windows 9X/ME


(stderr)
R CMD R

R CMD command args

command args

BATCH R
COMPILE (UNIX)R
SHLIB shared library
INSTALL
REMOVE
build ()
check
LINK (UNIX)
Rprof R Post-process
Rdconv Rd
HTMLNroffLATEXS
Rd2dvi Rd DVI/PDF
Rd2txt Rd
Sd2Rd S Rd
config (UNIX)

R CMD \var{command} --help

R CMD
13.6 Mac OS X R 112

13.5 Windows R
Windows R(cmd.execommand.com
)R.exe Rterm.exe
)
(Rgui.exe)
Windows UNIX
home directoryWindows
R USERHOME
R
Windows (Windows XP
C:\Documents and Settings\username\My Documents )
HOMEDRIVE HOMEPATH (Windows NT/2000/XP
)
name=value
RGui.exe

--mdi
--sdi
--no-mdi MDI (
)SDI (
)Rgui
--debug Rgui Break to debugger

Windows R CMD *.bat *.exe


R HOMER VERSIONR CMDR OSTYPEPATHPERL5LIB
TEXINPUTSlatex.exe

R CMD latex.exe mydoc

mydoc.tex LATEX R share/texmf TEXINPUTS

13.6 Mac OS X R
Mac OS X R Terminal.app R
(R.app)
13.6 Mac OS X R 113

Applications Mac OS X
Mac OS X UNIX R.framework

13.7
UNIX GNU readline R UNIX
R
UNIX GNOME

--no-readline (ESS 3 )

Windows R GUI Help


ConsoleRterm.exe README.Rterm
readline R
Meta characterControl-
m CTRL m C-m Meta-b
META 4 b M-b META
ESC M-b
ESC b ESC

13.8
R
Emacs-
vi
M-i M-a ESC
RET

3
Emacs Speaks Statistics URL http://ESS.R-project.org
4
PC Alt Windows
13.9 115

13.9

C-p ()
C-n ()
C-r text text

C-p C-n

C-a
C-e
M-b
M-f
C-b
C-f

C-b C-f

text text
C-f text text
DEL ()
C-d
M-d
C-k
C-y
C-t
M-l
M-c
RET R

RET
4

t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
. . . . . . . . . . . . . . . . . . . . . . 41
CRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
. . . . . . . . . . . . . . . . . . 78
Kolmogorov-Smirnov . . . . . . . . . . 48 . . . . . . . . . . . . . . . . . . . . . . . . . 46

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
QR . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
. . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Shapiro-Wilk . . . . . . . . . . . . . . . . . . 47 . . . . . . . . . . . . . . . . . . . . . . 73

Wilcoxon . . . . . . . . . . . . . . . . . . . . . .50 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2, 100 . . . . . . . . . . . . . . . . . . . . . . 73
. . . . . . . . . . . . . . . . . . . . . . . . . . . 54 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

. . . . . . . . . . . . . . . . . . . . . . . . . . . 56 . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

. . . . . . . . . . . . . . . . . . . . . . . . . 52 . . . . . . . . . . . . . . . . . . 46
. . . . . . . . . . . . . . . . . . . . . . 81
. . . . . . . . . . . . . . . . . . 39
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
. . . . . . . . . . . . . . . . 48 . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

. . . . . . . . . . . . . . . . . . . . . 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
. . . . . . . . . . . . . . . . . . . . . . . . . . . 62
. . . . . . . . . . . . . . . . . . . . . . . . . . . 81
. . . . . . . . . . . . . . . . . . . . . . . . . . . 98
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18, 63
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
. . . . . . . . . . . . . . . . . . . . . . . . . 55 . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

. . . . . . . . . . . . . . . . . . . . . . . . . . . 63 . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4 117

. . . . . . . . . . . . . . . . . . . . . . . . . . . 63 . . . . . . . . . . . . . . . . . . . . . . . 20, 69
. . . . . . . . . . . . . . . . . . . . . . . . . . 101
. . . . . . . . . . . . . . . . . . . . . . . . . . . 10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
. . . . . . . . . . . . . . . . . . . . . . . . . 30
. . . . . . . . . . . . . . . . . . . . . . . . . . . 13
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
. . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 . . . . . . . . . . . . . . . . . . . . 31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
. . . . . . . . . . . . . . . . . . . . . . . . . . . 81
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
. . . . . . . . . . . . . . . . . . . . 28
. . . . . . . . . . . . . . . . . . . . . . . . . 27
. . . . . . . . . . . . . . . . . . . . . . . . . . . 23

. . . . . . . . . . . . . . . . . . . . . . . . . . . 38
. . . . . . . . . . . . . . . . . . . 9
. . . . . . . . . . . . . . . . . . . . . . . . . . . 13

. . . . . . . . . . . . . . . . . . 30

. . . . . . . . . . . . . . . . . . . . . . . . . . . 66
. . . . . . . . . . . . . . . . . . . . . . . . . . . 90
. . . . . . . . . . . . . . . . . . . . . . 96

. . . . . . . . . . . . . . . . . . . . . . . . . . . 81

. . . . . . . . . . . . . . . . . . . . . . . . . . . 29
. . . . . . . . . . . . . . . . . . . . . . . . . . . 70
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

. . . . . . . . . . . . . . . . . . . . 52
. . . . . . . . . . . . . . . . . . . . 9, 26

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 69
5

* .................................... 9 aov . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
+ ................................... 9 aperm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
-. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 as.data.frame . . . . . . . . . . . . . . . . . . . . . . . 36
.First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 as.vector . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
.Last . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 attach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
/ .................................... 9 attr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 18
:: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 avas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
::: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
? .................................... 4 boxplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
%*% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
%o% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 bruto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
&& . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
& . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
c . . . . . . . . . . . . . . . . . . . . . . . . . 8, 13, 32, 35
< . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
cbind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
<<-. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
coef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
<= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
coefficients . . . . . . . . . . . . . . . . . . . . . . . . . 71
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
contour . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
>= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
contrasts . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
^....................................9
coplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
abline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 crossprod . . . . . . . . . . . . . . . . . . . . . . . 25, 29
ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
add1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
anova . . . . . . . . . . . . . . . . . . . . . . . . . . . 71, 72 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5 119

data.frame . . . . . . . . . . . . . . . . . . . . . . . . . 36 help.start . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
density . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 hist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45, 85
det . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
identify . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90
detach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
determinant . . . . . . . . . . . . . . . . . . . . . . . . 30
ifelse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
dev.list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
dev.next . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
is.na. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
dev.off. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
is.nan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
dev.prev . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
dev.set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 jpeg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
deviance . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
diag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ks.test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
dim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 legend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
dotchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 length . . . . . . . . . . . . . . . . . . . . . . . . . . . 9, 16
drop1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
ecdf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
eigen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 lm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 lme. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 locator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 loess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
lqs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 lsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
mars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
FALSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
fivenum . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
formula. . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
NA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
getAnywhere . . . . . . . . . . . . . . . . . . . . . . . 64
NaN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
getS3method . . . . . . . . . . . . . . . . . . . . . . . 64
ncol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
glm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 nlm . . . . . . . . . . . . . . . . . . . . . . . . . 78, 79, 81
help.search . . . . . . . . . . . . . . . . . . . . . . . . . . 4 nlme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5 120

nrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 scan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
sd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
optim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
seq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
ordered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
shapiro.test . . . . . . . . . . . . . . . . . . . . . . . . .47
outer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 sink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
par . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
pdf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
persp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
plot. . . . . . . . . . . . . . . . . . . . . . . . . . . . .71, 84 sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
pmax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 stem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
pmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 step . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71, 73
png . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 summary . . . . . . . . . . . . . . . . . . . . . . . 44, 71
polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 svd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
postscript . . . . . . . . . . . . . . . . . . . . . . . . . . 96
T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
t.test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
prod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
table. . . . . . . . . . . . . . . . . . . . . . . . . . . .25, 32
qqline. . . . . . . . . . . . . . . . . . . . . . . . . . .46, 85 tan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
qqnorm . . . . . . . . . . . . . . . . . . . . . . . . . 46, 85 tapply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
qqplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
qr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
quartz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
TRUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 tsplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
rbind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
read.table . . . . . . . . . . . . . . . . . . . . . . . . . . 39 unclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
rep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
var . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9, 21
resid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
var.test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
residuals. . . . . . . . . . . . . . . . . . . . . . . . . . . .71
vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
rlm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5 121

wilcox.test . . . . . . . . . . . . . . . . . . . . . . . . . . 50
windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

X11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
6

D. M. Bates and D. G. Watts (1988), Nonlinear Regression Analysis and Its Applica-
tions. John Wiley & Sons, New York.
Richard A. Becker, John M. Chambers and Allan R. Wilks (1988), The New S Lan-
guage. Chapman & Hall, New York. This book is often called the Blue Book .
John M. Chambers and Trevor J. Hastie eds. (1992), Statistical Models in S. Chapman
& Hall, New York. This is also called the White Book .
John M. Chambers (1998) Programming with Data. Springer, New York. This is also
called the Green Book .
A. C. Davison and D. V. Hinkley (1997), Bootstrap Methods and Their Applications,
Cambridge University Press.
Annette J. Dobson (1990), An Introduction to Generalized Linear Models, Chapman
and Hall, London.
Peter McCullagh and John A. Nelder (1989), Generalized Linear Models. Second edi-
tion, Chapman and Hall, London.
John A. Rice (1995), Mathematical Statistics and Data Analysis. Second edition.
Duxbury Press, Belmont, CA.
S. D. Silvey (1970), Statistical Inference. Penguin, London.
123

You might also like