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
: Copyright c 1990 W. N. Venables Copyright c 1992 W. N. Venables & D. M. Smith Copyright c 1997 R. Gentleman & R. Ihaka Copyright c 1997, 1998 M. Maechler Copyright c 1999 2006 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 1999 2006 R Development Core Team
R
R Development Core Team
GNU FDL GNU http://www.gnu.org/copyleft/fdl.html
viii 0.1 0.2 0.3 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . R R R R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ( . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii 05 ) . . . . . . . . . . . . . . . . . . . . . . . . viii x 1 1 1 2 2 3 4 4 5 6 6 6 8 8 9 ( PDF ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 . . . . . . . . . . . . . . . . . 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
iv 3 3.1 3.2 3.3 3.4 4 4.1 4.2 4.3 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.7.1 5.7.2 5.7.3 5.7.4 5.7.5 5.8 5.9 5.10 6 6.1 6.2 6.2.1 6.3 6.3.1 6.3.2 6.3.3 attach() cbind() rbind() QR array() 5.4.1 tapply() 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 . . . . . . . . . . . . . . . . . . . . . . . . 21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 . . . . . . . . . . . . . . . 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 . . . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . 31 . . . . . . . . . . . . . . . . . . . . 31 c() . . . . . . . . . . . . . . . . . . . . . . . 32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 34 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 detach() . . . . . . . . . . . . . . . . . . . . . . . . 36 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
v 6.3.4 6.3.5 7 7.1 7.2 7.3 7.3.1 7.4 8 8.1 8.2 8.3 9 9.1 9.2 9.2.1 9.2.2 10 10.1 10.2 10.3 10.4 . . . 10.5 10.6 10.6.1 10.6.2 10.6.3 10.7 10.8 10.9 11 R 11.1 11.1.1 R read.table() scan() . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 39 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 R . . . . . . . . . . . . . . . . . . . . . . 42 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 43 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 52 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 if for . . . . . . . . . . . . . . . . . . . . . . . . . . 52 repeat while . . . . . . . . . . . . . . . 53 54 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 . . . . . . . . . . . . . . . . . . . . . . . . 57 . . . . . . . . . . . . . . . . . . . . . . . . 58 . . . . . . . . . . . . . . . . . . . . . . . . . . 59 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 . . . . . . . . . . . . . . . . . . . . . . . . . . 63 66 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
vi 11.2 11.3 11.4 11.4.1 11.5 11.6 11.6.1 11.7 11.7.1 11.7.2 11.8 12 12.1 12.1.1 plot() 12.1.2 12.1.3 12.1.4 12.2 12.2.1 12.2.2 Hershey 12.3 12.4 12.4.1 12.4.2 12.5 12.5.1 12.5.2 12.5.3 12.5.4 12.6 12.6.1 12.6.2 12.7 11.6.2 glm() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 . . . . . . . . . . . . . . . . . . . . . . . . . . 70 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 . . . . . . . . . . . . . . . . . . . . 78 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 83 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 . . . . . . . . . . . . . . . . . . . . . . . . . 86 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 . . . . . . . . . . . . . . . . . . . . . . . . . . 89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 : par() . . . . . . . . . . . . . . . . . . . . . . 90 . . . . . . . . . . . . . . . . . . 91 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 PostScript . . . . . . . . . . . . . . . . . . . . 97 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
vii 13 13.1 13.2 13.3 1 2 13.4 13.5 13.6 3 13.7 13.8 13.9 4 5 6 Windows Mac OS X R 100 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 CRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 102 108 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 114 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 116 118 122
Bill Venables
David M. Smith (Insightful R S
)
S
SPLUS
Bill Venables R
David Smith R-core@R-project.org ghding@gmail.com
0.1
R R R R ( < 83> R http://cran.r-project.org/mirrors.html “Precompiled Binary Distributions” Windows “rwxxxx.exe” A rw2010.exe session < “Windows (95 and later)” Windows 102> “base” OK ) R sessions A session < 102>
0.2
R
(
05
R
)
R R R
0.2
(
05
) R
ix
ROTATION Stata MATLAB SPSS R • R R • R R SAS
R R
MATLAB
R
•
• R
/
R
R R
R http://www.r-project.org/
foundation/about.html Texinfo HTML ( Texinfo Texinfo UTF-8) LaTeX PDF PDF
email PDF R R R Data Import/Export Extensions An Introduction to R The R language definition Writing R
R Installation and Administration/R FAQ
0.3
(
PDF )
x
R “ ” R
Shigeru MASE Email ghding@gmail.com 2005 6 3
0.3
R Email R
( PDF
A L TEX
)
R R
“ Emmanuel Paradis Texinfo
A L TEX
” R for beginners R for beginners XF Wang R :)
“|” PDF
“!” Dr. ZP Li Dr. Rui Li Rui
Prof Li Brian D. Ripley R Ronggui H Li -ˆ ( CHM R Q Wang K Tu K Tu Q Liu G Li Technische Uni. Wien Friedrich Leisch
0.3 Roation
(
PDF ) R R R Email
xi
A L TEX
Email ghding@gmail.com 2006 7 11
1.1
R
R
• • • • • S , “ ” environment , R packages R R , ( ‘S’)
1.2
R Allan Wilks S R S Bell Laboratories S John Chambers Richard A. Becker John M. Chambers Allan R. Rick Becker SPLUS John Chambers
1.3 R Wilks Graphics 1991 class
2 The New S Language: A Programming Environment for Data Analysis and John M. Chambers and Trevor J. Hastie S3 < R
1
Statistical Models in S method
methods Programming with Data . R R S
John M. Chambers 122>
S/SPLUS R R
1.3
R
R R R R
25
R CRAN ( <
“ 100>
” )
“
”
(http://CRAN.R-project.org) R
S(
R) SAS SPSS R
S object R
2
1.4
R
R
graphics workstation R R UNIX X window R . Windows
1988 . S3
, MacOS R
1 2
: John M. Chambers : R
1.5
R R
3
1.5
R > UNIX
R
shell R UNIX UNIX R R shell $
1. R
$ mkdir work $ cd work
work
2.
$ R
R
3. 4. R
> q()
R
(
)
R yes cancel R R 1.
$ cd work $ R
no,
R
work
2.
R
q()
1.6 Windows R
R R
3
4
R
1.6
< 102>
R
R R R
1.7
R UNIX solve
> help(solve)
man
> ?solve
“ if for
> help("[[")
”
function
escape R
> help.start()
"It’s important"4 HTML
UNIX HTML ’ Search Engine & Keywords R
3
help.start()
‘
“
4
R ” ’It’s important’
“ OKˆ ˆ R
”
“
”
1.8 R help.search
5
5 ?help.search
> example(topic) # topic Windows
6
barplot
R
> ?help
1.8
R
R A R . . expressions evaluate assignments ( a ( ) locale ) . expression language R
(;) compound expression (#) R
({
})
7
+
continuation prompt
5 6 7
? : BioConductor Vignettes .
1.9
6
1.9
UNIX Windows R command history DEL 114>. UNIX readline Emacs Emacs Speaks Statistics)
8
<
R R : R
(
ESS
1.10
work R
> source("commands.R")
commands.R
Windows sink
> sink("record.lis")
R
File
Source
9
record.lis
> sink()
1.11
R
8
entity structures
http:// wordfile “R Scripting wordfile.txt
Vi UltraEdit UltraEdit R www.ultraedit.com/index.php?name=Content&pa=showpage&pid=40 - 02/18/2003” UltraEdit .R
9
R
1.11 R ) R ( ls()) (
7
> objects()
R workspace rm
> rm(x, y, z, ink, junk, temp, foo, bar)
R R .RData10 .Rhistory R R x y
R
10
“ ”
UNIX
2.1
R x data structure vector 10.4 5.6 3.1 6.4 21.7 R
> x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
c()
1
c() 1
R (<-)
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() 35> ‘->’ ‘<-’
list ’‘-<’ ‘>-’ .Last.value
<
2 3
2.2
> 1/x
9
(
x
)
> y <- c(x, 0, x)
11
y
x
0
2.2
recycled )
(
> v <- 2*x + y + 1
11
v
2*x + * / tan
2.2
y ^
1
11
log max 2 x var(x) min
exp
sin cos
sqrt range x sum(x)/length(x) sum(x)
c(min(x), max(x)) prod(x)
length(x)
mean(x) var(x)
> sum((x-mean(x))^2)/(length(x)-1)
var() sort(x) ( max min
n x
p p p order() sort.list() ) parallel
p-
pmax
pmin
2.3
10
“ R
”
> sqrt(-17)
NaN
> sqrt(-17+0i)
2.3
R R n <- 10 30:1 seq() seq(2,10) seq() R from=value seq(1,30) seq(from=1, to=30) seq() by=value by=1 seq(to=30, from=1) 1 length=value to=value 1:30 2:10 1:n-1 1:(n-1) construction 1:30 2*1:15 c(1, 2, ..., 29, 30) c(2, 4, ..., 28, 30)
> seq(-5, 5, by=.2) -> s3
c(-5.0, -4.8, -4.6, ..., 4.6, 4.8, 5.0)
> s4 <- seq(length=51, from=-5, by=.2)
s3
s4
2.4 along=vector 1, 2, ..., length(vector ) rep() vector
4
11
> s5 <- rep(x, times=5)
x
x
s5
> s6 <- rep(x, each=5)
x
2.4
R TRUE T word FALSE conditions
> temp <- x > 13
FALSE F
NA (“ TRUE
”, FALSE
)
T
F TRUE
reserved
, temp TRUE R != (“ 0
4
x < <= c1 c2 (“ ”) !c1 c1 > >=
FALSE
x == c1 & c2 FALSE
”) c1 | c2 TRUE 1
length(vector )
2.5
12
2.5
“ ” not available NA5 “ ” NA missing value NA
is.na(x) NA
> z <- c(1:3,NA);
x
TRUE
x
ind <- is.na(z)
x == NA NA “
is.na(x) , x == NA
NA x
”
Not a Number
NaN
> 0/0
> Inf - Inf
NaN NA NaN
> a<-c("a","b",NA) > a [1] "a" "b" NA > print(a,quote=F) [1] a b
5
NaN TRUE
is.na(xx)
TRUE
is.nan(xx)
6
0.01β
, PDF
6
2.6
13
2.6
R "x-values" "New iteration results" (") ( sequences \" c() paste() sep=string string \ \n ) \ \t C \ \b " (’) escape
> labs <- paste(c("X","Y"), 1:10, sep="")
labs
c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10")
c("X", "Y") 1:10
7
5
2.7
subset
1. TRUE FALSE
> y <- x[!is.na(x)]
7
paste(..., collapse=ss) sub substring
ss
R
2.7 ( y ) x x y
14 x
> (x+1)[(!is.na(x)) & x>0] -> z
z
x+1
x
2.
{1, 2, . . . , length(x)}
x[6]
> x[1:10]
x
x
10
(
length(x)
10)
> c("x","y")[rep(c(1,2,2,1), times=4)]
16 3.
> y <- x[-(1:5)]
"x", "y", "y", "x"
4
8
x 4.
y names
> fruit <- c(5, 10, 1, 20) > names(fruit) <- c("orange", "banana", "apple", "peach") > lunch <- fruit[c("apple","orange")]
name indices data frames
8
numeric indices
2.8
15
vector[indexv ector ]
> x[is.na(x)] <- 0
0
x
> y[y < 0] <- -y[y < 0]
> y <- abs(y)
2.8
R
•
matrix < 23>
array
• •
factor list general form < 34>.
<
20>.
•
data frame ‘ ’ < 36> R < 54> R
•
function
3.1
R “
1
object ” atomic
2
R character
numeric raw (
complex
logical
“ ” numeric(0) R “ ” R list
NA
NA character(0) R
)
function R formulae mode “ ” property length(object)
expression R
length
3
mode(object) < "complex" R
1 2
attributes(object) 18>. z length(z)
R
“ 100 100 (
typeof() integer
”
mode(z) )
S double
3
length(object)
object
3.2
> z <- 0:9
17
> digits <- as.character(z)
digits
c("0", "1", "2", ..., "9")
> d <- as.integer(digits)
d
z
4
as.something()
3.2
“ ”
> e <- numeric()
e
5
character()
> e[3] <- 17
3
e(
NA) scan() (scan()
< 10
40>.) alpha
> alpha <- alpha[2 * 1:5]
5 )
> length(alpha) <- 3
(
4
roundoff
5
3.3
18
3.3
attributes(object) tributes
6
non-intrinsic at, R object
attr(object, name)
,
> attr(z, "dim") <- c(10,10)
R
z
10
10
3.4
R "matrix" "array" "factor" class "numeric" "logical" "data.frame"
7
class "character" "list"
R
8
"data.frame"
9
plot() summary() winter
generic function
unclass() "data.frame"
> winter
> unclass(winter)
6
: methods :
attributes(object) . ‘ print Java C++ ’ ‘S4’
. str(object).
,
7 8 9
Java
1.5
5.0
3.4
19
<
63>
factor ordered ( < unordered 69>) “ ”
R
4.1
30 state
> state <- c("tas", "qld", "sa", "sa", "sa", "vic", "nt", "act", "qld", "nsw", "wa", "nsw", "nsw", "vic", "vic", "vic", "nsw", "qld", "qld", "vic",
2 1
"nt", "wa", "wa", "qld", "sa", "tas", "nsw", "nsw", "wa", "act")
“
” 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(statef) [1] "act" "nsw" "nt"
1
levels
"qld" "sa" "tas" "vic" "wa"
8 Australian 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() incomes
mean()
3
statef
standard error R
4
var()
> stderr <- function(x) sqrt(var(x)/length(x))
( R
<
54> sd()5 )
> incster <- tapply(incomes, statef, stderr)
3
R
4 5
tapply() as.factor() . R sd(1:4) sd()
tapply(incomes, state)
“ :
” ’ . ‘ , stderr <-
stderr(1:4) function(x){sd(x)/sqrt(length(x))}
4.3
> 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
22
95% tapply() qt()( tapply() ( ) R tlength() ) t-
ragged array
4.3
factor ordered() ordered() factor ,
5.1
R dimension vector k2dim R 1 z 1500 k
> dim(z) <- c(3,5,100)
dim matrix() < 26> “ a . data vector ”
3 array()
5
100 array() FORTRAN
c(3,4,2)
a
3×4×2 = 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
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])
24
a[,,] Z ) dim(Z)
a (
5.3
4 • •
5
X X[1,3], X[2,2] X 0 3 2 X[3,1]
5.3
> x <- array(1:20, dim=c(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 > 4 5
25
blocks(b (v
> > > > > > >
) varieties 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
> 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 <
24 26>)
24 (
> Z <- array(0, c(3,4,2))
Z dim(Z) Z[] dim A, B C
0 c(3,4,2) Z Z[1:24] h
> D <- 2*A*B + C + 1
D
5.4.1
• •
5.5 •
1
27 dim
• • dim
5.5
outer product ( ) a %o%
> ab <- a %o% b
a b
b
> ab <- outer(a, b, "*")
f (x; y) = cos(y)/(1 + x2 )
R
x
y
regular grid
> f <- function(x, y) cos(y)/(1 + x^2) > z <- outer(x, y, f)
( R 54>. 2 2 2 0, 1, . . . , 9 ad − bc density outer() 2 [a, b; c, d]
1) <
high
: ”As long as short vectors and arrays only are encountered, the arrays must all have the same dim attribute or an error results.”
1
5.6
> d <- outer(0:9, 0:9) > fr <- table(outer(d, d, "-")) > plot(as.numeric(names(fr)), fr, type="h", xlab="Determinant", ylab="Frequency")
28
names ” for 20 < 52>
2
“
5.6
aperm(a, perm) k perm[j] A ( a j ) B a perm a {1, . . . , k}
> B <- aperm(A, c(2,1))
A B <- t(A)
t()
5.7
R nrow(A) ncol(A) A t(X)
5.7.1
%*% n A
> A * B
n
1
1
n
B
2
:
”It is also perhaps surprising that about 1 in 20 such matrices is singular.”
5.7
> A %*% B
29
x
> x %*% A %*% x
quadratic form crossprod() y) t(X) %*% y diag()
3
“
”
crossproduct crossprod() v
crossprod(X,
diag(v) diag(M) k M
4
M Matlab diag(k) diag() k k
5.7.2
> b <- A %*% x
A
> solve(A,b)
b
x
R
x( x = A−1 b A−1 A inverse
)
solve(A)
x <- solve(A) %*% b x A−1 x A
3
solve(A,b)
5
x %*% solve(A,x)
xx x rbind() x %o% x . A Cholesky cbind()
x %*% x xx xx cbind(x) %*% x x x xx
xx
x %*% rbind(x) crossprod(x)
4
5 A = BB B By = x squared length “Even better would be to form a matrix square root B with A = BB 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) values vectors Sm
> ev <- eigen(Sm)
ev
ev$val
Sm
ev$vec
> evals <- eigen(Sm)$values
evals
> eigen(Sm)
> evals <- eigen(Sm, only.values = TRUE)$values
5.7.4
svd(M) M D svd(M) M
> absdetM <- prod(svd(M)$d)
M U d, u v M
, D
M V
M = U %*% D %*% t(V)
M R
> absdet <- function(M) prod(svd(M)$d)
,
absdet() diag()]
R trace tr() [ det : ,
R
5.8
cbind()
rbind()
31
5.7.5
lsfit()
QR
Least squares fitting
> ans <- lsfit(X, y)
y ls.diag() lm(.) ( lsfit() qr()
> > > > Xplus <- qr(X) b <- qr.coef(Xplus, y) fit <- qr.fitted(Xplus, y) res <- qr.resid(Xplus, y)
X regression diagnostics X < 70> )
y
fit X
X b Matlab ‘
res ’
b
R < 66>
5.8
cbind()
rbind()
cbind() rbind() rbind()
cbind()
> X <- cbind(arg 1 , arg 2 , arg 3 , ...) cbind() arg1 , arg2 , . . . cbind() ( )
5.9 rbind() X1 X2 1
> X <- cbind(1, X1, X2)
c()
32
X
rbind() x
cbind()
cbind(x)
rbind(x)
5.9
cbind() dim rbind() dimnames
c()
dim as.vector() c()
> vec <- as.vector(X)
c()
> vec <- c(X)
( )
5.10
table() kstatef
> statefr <- table(statef)
k
statefr
> statefr <- tapply(statef, statef, length)
5.10 incomef cut()
> factor(cut(incomes, breaks = 35+10*(0:7))) -> incomef
33 “ ”
> 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[[4]] Lst ) length(Lst) Lst[[1]], Lst[[2]], Lst[[3]] Lst[[4]][1] ( Lst[[4]]
> name$component name
Lst$name Lst$wife
Lst[[1]] Lst[[2]]
"Fred", "Mary", Lst[[4]][1] 4
Lst$child.ages[1]
6.2 Lst$name Lst[["name"]]
35
> x <- "name"; Lst[[x]]
Lst[[1]] [. . . ] ,
1
Lst[1] named list
[[. . . ]] Lst
Lst
2
Lst$coefficients Lst$cov (names)
Lst$coe
Lst$covariance
6.2
list() > Lst <- list(name 1 =object 1 , . . . , name m=object m) m Lst object 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
> attach(lentils)
lentils$u, lentils$v, lentils$w
6.3 2 u, v w position 2
3
37 1 u, v w
> u <- v+w
u
4
1 $
u
> lentils$u <- v+w
u
5
> detach()
2 lentils$u 2 detach detach(lentils) R ( )6
u, v
w
detach("lentils") 2
6.3.3
• ; • 2 1
,
•
3
$ detach()
R 1 1 , R , u, u . u <- v + w , , attach() 1 . u.
2
4 5 6
: : u :
6.3 • x, y z
38
6.3.4
attach() "list"
> attach(any.old.list)
detach
6.3.5
search ( )
> search() [1] ".GlobalEnv"
"Autoloads"
7
"package:base"
.GlobalEnv lentils
> search() [1] ".GlobalEnv" > ls(2) [1] "u" "v" "w"
"lentils"
"Autoloads"
"package:base"
ls (
objects)
> detach("lentils") > search() [1] ".GlobalEnv" "Autoloads"
"package:base"
7
autoload
R R Perl1 R
R
read.table() scan()2 , R R /
3
7.1
read.table()
• •
4
1 2
UNIX
Sed
Awk read.table() R scan() Excel
3 4
read.table()
R header=TRUE
7.2 scan()
40
01 02 03 04 05 ...
Price 52.00 54.75 57.50 57.50 59.75
Floor 111.0 128.0 101.0 131.0 93.0
Area 830 710 1000 690 900
Rooms 5 5 5 6 5
Age 6.2 7.5 4.2 8.8 1.9
Cent.heat no no no no yes
( Cent.heat read.table()
)
,
> HousePrice <- read.table("houses.data")
Price 52.00 54.75 57.50 57.50 59.75 ...
Floor 111.0 128.0 101.0 131.0 93.0
Area 830 710 1000 690 900
Rooms 5 5 5 6 5
Age 6.2 7.5 4.2 8.8 1.9
Cent.heat no no no no yes
> HousePrice <- read.table("houses.data", header=TRUE)
header=TRUE
7.2
scan()
input.dat scan()
> inp <- scan("input.dat", list("",0,0))
7.3 dummy list structure inp
41
> 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)
R
7.3
R ) 100 ( datasets ) ( R
data()
R 2.0.0 data
data(infert)
R R
( 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)
xold xold fix(xold)
xnew xold <- edit(xold)
> xnew <- edit(data.frame())
8.1
R x) )
R
R ( q P (X ≤ x) > q x P (X ≤
R beta binom Cauchy cauchy chisq exp F f gamma geom hyper lnorm logistic logis nbinom norm Poisson t Weibull Wilcoxon pois t unif weibull wilcox shape1, shape2, ncp size, prob location, scale df, ncp rate df1, df1, ncp shape, scale prob m, n, k meanlog, sdlog location, scale size, prob mean, sd lambda df, ncp min, max shape, scale m, n
d cumulative distribution function CDF q
p r
8.2 (r andom deviates) p
1
44 dxxx rwilcox nn) x pxxx q qxxx non-
rxxx
n (rhyper
centrality parameter ncp pxxx log (“ ”) hazard H(t) = − log(1 − F (t)) qxxx lower.tail log.p, dxxx
- pxxx (t, ..., lower.tail = FALSE, log.p = TRUE) (dxxx (..., log = TRUE)) ptukey range qtukey studentized
> > > >
## t p 2*pt(-2.43, df = 13) ## F(2, 7) 1% qf(0.99, 2, 7)
8.2
summary (“
1
fivenum
stem
”
)
“In not quite all cases is the non-centrality parameter ncp are currently available...”
8.2
> attach(faithful) > summary(eruptions) Min. 1st Qu. Median Mean 3rd Qu. 1.600 2.163 4.000 3.488 4.454 > fivenum(eruptions) [1] 1.6000 2.1585 4.0000 4.4585 5.1000 > stem(eruptions)
45
Max. 5.100
The decimal point is 1 digit(s) to the left of the | 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 | | | | | | | | | | | | | | | | | | 070355555588 000022233333335577777777888822335777888 00002223378800035778 0002335578023578 00228 23 080 7 2337 250077 0000823577 2333335582225577 0000003357788888002233555577778 03335555778800233333555577778 02222335557780000000023333357778888 0000233357700000023578 00000022335800333 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 (
2
trial-and-error
2
bw bandwidth “ bw = "SJ" ”
?density
?bw.nrd
8.2 )
Histogram of eruptions
0.7
46
Relative Frequency
0.0 1.5
0.1
0.2
0.3
0.4
0.5
0.6
2.0
2.5
3.0
3.5 eruptions
4.0
4.5
5.0
ecdf distribution
> plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)
empirical cumulative
3
> > > >
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 Fn(x) 0.0 0.2 0.4 0.6 0.8
3.0
3.5
4.0 x
4.5
5.0
Normal Q−Q Plot
Sample Quantiles
3.0
3.5
4.0
4.5
5.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)
R
Shapiro-Wilk
8.3
> shapiro.test(long) Shapiro-Wilk normality test data: long W = 0.9793, p-value = 0.01052
48
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 “ ” latent heat (cal/gm)
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
stats Rice (1995, p.490)
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
79.94
79.96
79.98
80.00
80.02
80.04
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 F
3
t.test
, t, .
:t-
8.3
> 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
50
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 ( (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)
Mann-Whitney)
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 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)
Q-Q
9.1
R expression language
{expr 1 ; . . . ; expr m}
9.2
9.2.1
R > if (expr1 ) expr2 else expr3 expr1 “ & R a, b) a[i]
1 2
if
” |
short-circuit
2
&&
1
|| && ||
if 1 ifelse(condition,
if/else b[i]
TRUE . FALSE
ifelse condition[i]
: : “
”
.
9.2
53
9.2.2
R
for
for
repeat
while
> for (name in expr 1 ) expr 2 name expr 2 expr 2 ind y x vector of class indicators coplot()3 expr 1 name ( 1:20 name expr 1 )
> 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 R ‘ ’ whole object for()
4
)
> repeat expr
> while (condition) expr break next “ repeat ” <
3 4
54>
lattice R ,
xyplot sum mean apply lapply
sapply
tapply
R R R R postscript() R R
function
mean(), var(),
> name <- function(arg 1 , arg 2 , ...) expression R (
expression ) 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
> tstat <- twosam(data$male, data$female); tstat
55
Matlab ( )
y
X qr()
n (X X)−
1 XX
y
n
p
X
X\y
(X X)− X y,
generalized inverse
> bslash <- function(X, y) { X <- qr(X) qr.coef(X, y) }
> regcoeff <- bslash(Xmat, yvar)
R
lsfit() qr() qr.coef()
1
binary operator
10.2
bslash() %anything% binary operator !
> "%!%" <- function(X, y) { ... }
(
) %*%
1
X %!% y %o%
66>
( )
R
<
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() < 90> par()
par() ) ...
(
par()
10.5
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) { [ ] if (graph) par(pch="*", ...) [ ] }
57
10.5
X <- qr(X) R “ <<<<60> R assign() help semantics Evalution frame ” superassignment SPLUS <
10.6
10.6.1
( block design ) size N R K b v v v b b < 24> blocks (b replications incidence matrix A = K −1/2 N R−1/2 ) ) varieties (v block
E = Iv − R−1/2 N K −1 N R−1/2 = Iv − A A,
10.6
> 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) }
58
block and variety canonical contrasts
10.6.2
dimnames dimnames X R
> 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
“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 evalutions in regions where the integrand is farthest from linear.”
2
10.7
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) }
60
10.7
SPLUS variables) R (formal parameters) (free variables) (local
,
f <- function(x) { y <- 2*x print(x) print(y) print(z) }
x R
y lexical scope
z cube,
10.7
cube <- function(n) { sq <- function() n*n n*sq() }
61
sq
n (SPLUS) (R) n R cube cube SPLUS n sq SPLUS n
n
R
## \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 state account account
R
total total
< total) stop("You don’t 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)
62
10.8
.First R PROFILE R R
5
.Last R
etc
Rprofile.site .Rprofile5
UNIX
10.9
63
.Rprofile R .RData
6
.Rprofile .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() anova() "data.frame"
6 7
summary()
Linux R ,
~/ Java . “
(interface) ”
.
, .
10.9
[ [<[[ methods(class="data.frame")
plot() "data.frame" methods()
> methods(plot)
"density" "factor"
> coef function (object, ...) UseMethod("coef")
UseMethod methods()
> methods(coef) [1] coef.aov* [5] coef.nls*
coef.Arima* coef.default* coef.summary.nls*
coef.listof*
Non-visible functions are asterisked
8
: "coef.aov" not found’’.
8
( coef.aov), R
‘‘Error:
object
10.9
> 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)] }
65
R
R
1
R R
11.1
p
yi =
j=0
βj xij + ei ,
ei ∼ NID(0, σ 2 ),
i = 1, . . . , n
y = Xβ + e y trix 1 X X x0 , x1 , . . . , xp intercept model matrix determining variable design max0
y, x, x0, x1, x2, . . .
1
X
A, B, C, . . .
: “Later we make some rather more ambitious presumptions, namely that something is known about generalized linear models and nonlinear regression.”
11.1 y ~ x y ~ 1 + x y ~ 0 + x y ~ -1 + x y ~ x - 1 log(y) ~ x1 + x2 y ~ poly(x,2) y ~ 1 + x + I(x^2) y x orthogonal polynomial y ~ X + poly(x,2) y ~ A y ~ A + x y ~ A*B y ~ A + B + A:B y ~ B %in% A y ~ A/B y A B y y y x X x A A y y ) x log(y) x1 ( x2 ( ) y x
67
two factor non-
additive model crossed classification (nested classification) y ~ (A + B + C)^2 y ~ A*B*C - A:B:C interactions y ~ A * x y ~ A/x y ~ A/(1 + x) - 1 A
2
main effects
y
x A
“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.”
2
11.1 y ~ A*B + Error(C) error strata experiment A B C
68
split plot C
~
R
model formula
response ~ op 1 term 1 op 2 term 2 op 3 term 3 . . .
response
/
op i ( term i • • •
+
3
-
1
formula expression 1
formula operators & Rogers R ( Y ~ M M 1 + M 2 M 1 - M 2 M 1 :
3 4
Glim .
Genstat R
Wilkinson :,
(notation) Chambers & Hastie, 1992, p.29): M M 1 M 1 M 1
.
Y
M 2 M 2 tensor product “ ” (subclasses factor)4
M 2
M 2
:
11.1 M 1 %in% M 2 M 1 * M 2 M 1 / M 2 M ^n I(M ) M 1 :M 2 M 1 + M 2 + M 1 :M 2 . M 1 + M 2 %in% M1 . M M M n “ ”
5
69
I()
identity function
11.1.1
( 1 k) A 2 1, . . . , k ... k ) orthogonal polynomial k options contrasts R
6
k−1
( k−1
options(contrasts = c("contr.treatment", "contr.poly"))
R
S SPLUS
S
Helmert
options(contrasts = c("contr.helmert", "contr.poly"))
treatment contrast (R
5
)
“All terms in M together with “interactions” up to order n” “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.”
6
:
11.2 contrasts
7
70 C
R R
11.2
multiple model lm()
> fitted .model <- lm(formula, data = data.frame)
> fm2 <- lm(y ~ x1 + x2, data = production)
y
x1 (
x2 )
( data = production production
) production
11.3
lm() "lm" "lm"
add1 alias anova
coef deviance drop1
effects family formula
kappa labels plot
predict print proj
residuals step summary
“ We have not yet considered interaction terms: these generate the products of the columns introduced for their component terms.”
7
11.3 anova(object 1 , object 2 ) coef(object) deviance(object) formula(object) plot(object)
8
71
(
)
coefficients(object).
predict(object, newdata=data.frame) data.frame predict.gam(object, newdata=data.frame) predict.gam() predict() gam lm, glm
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) < aov() split plot experiments balanced incomplete block design response mean.formula + Error(strata.formula) strata.formula strata.formula 70> multiple error strata lm()
> fm <- aov(yield ~ v + n*p*k + Error(farms/blocks), data=farm.data)
v + n*p*k ” “ ” “ ”
“
11.4.1
(sequence)
11 10
(multistratum experiments) Chambers & Hastie (1992) ANOVA table anova()
> 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 “Hence only for orthogonal experiments will the order of inclusion be inconsequen-
fitted models.” 11 : 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
•
12
y
stimulus variable x1 , x2 , . . .
“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 • linear predictor η = β1 x1 + β2 x2 + · · · + βp xp , xi • y fY (y; µ, ϕ) = exp ϕ y • µ µ = m(η), (inverse function) () smooth invertible function η = m−1 (µ) = (µ) link function A {yλ(µ) − γ (λ(µ))} + τ (y, ϕ) ϕ ( ) µ y A βi = 0 y y
74
scale parameter
(estimation and inference) McCullagh & Nelder (1989) Dobson (1990)
11.6.1
R (poisson), variance function (inverse gaussian) (gamma) quasi-likelihood (gaussian), ,
binomial gaussian
logit, probit, log, cloglog identity, log, inverse
11.6 Gamma inverse.aussian poisson quasi identity, inverse, log 1/mu^2, identity, inverse, log identity, log, sqrt logit, probit, cloglog, identity, inverse, log, 1/mu^2, sqrt
75
family
11.6.2
glm()
R
glm()
> fitted .model <- glm(formula, family=family.generator , data=data.frame) lm()
13
family.generator
< “ ” quasi
74>
gaussian
> fm <- glm(y ~ x1 + x2, family = gaussian, data = sales)
> fm <- lm(y ~ x1+x2, data=sales)
: “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.”
13
11.6
76
Silvey (1970) Kalythos Aegean
Age: No.: tested: No.: blind:
20 35 45 55 70 50 50 50 50 50 6 17 26 37 44
logistic LD50 y n x 50%
probit
y ∼ B(n, F (β0 + β1 x)) probit ) F (z) = ez /(1 + F (z) = Φ(z) 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))
logit
(
glm() • • binary 0/1
•
(0)
‘
’(1)
11.6
> kalythos$Ymat <- cbind(kalythos$y, kalythos$n - kalythos$y)
77
> 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.663 Poisson Poisson Poisson log
43.601
Poisson Poisson
> fmod <- glm(y ~ A + B + x, family = poisson(link=sqrt), data = worm.counts)
multiplier poisson Var[y] = µ gaussian gaussian
11.7
78
y=
θ 1 z1 +e z2 − θ 2
y=
1 +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() nlm() nlm()
14
nlminb()
R2.2.0
SPLUS lack-of-fit
ms()
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
SSE
out$estimate (SE)
> sqrt(diag(2*out$minimum/(length(y) - 2) * solve(out$hessian)))
2
95%
± 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
> 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
80
11.7.2
Maximum likelihood Dobson (1990), pp. : 108–111 logistic
> x <- c(1.6907, 1.8369, > y <- c( 6, 13, > n <- c(59, 60, 1.7242, 1.8610, 18, 28, 62, 56,
glm()
1.7552, 1.7842, 1.8113, 1.8839) 52, 53, 61, 60) 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
> out <- nlm(fn, p = c(-50,20), hessian = TRUE)
81
out$minimum
out$estimate
> sqrt(diag(solve(out$hessian)))
95%
± 1.96 SE
11.8
R
•
Mixed models
nlme mixed-effects models
lme()
nlme()
•
(Local approximating regressions)
loess()
loess stats • (Robust regression)
projection pursuit regression
MASS rlm • (Additive models) smooth additive function acepack bruto gam • mars mgcv
lqs MASS
avas
ace
mda
(Tree-based models)
11.8
82
tree() plot() R text() rpart tree
R
R graphics window UNIX X11() R Windows
device driver windows()
•
•
•
R ‘ lattice plot ’ S Trellis grid grid multi-panel
12.1
( )
12.1
84
12.1.1
R
plot()
plot()
plot(x , y) plot(xy) ( plot(x ) x plot(f ) plot(f , y) plot(df ) plot(~ expr ) plot(y ~ expr ) df ( ) y a + b + c) ( y expr expr ‘+’ f y y f f x x y x ) x y plot(x , y) y x
12.1.2
R X
> pairs(X)
X X coplot c
> coplot(a ~ b | c)
pairwise scatterplot matrix X n(n − 1) a ( ) b
12.1 c c b co.intervals()
> coplot(a ~ b | c + d)
85 a b c c coplot() a b a given.values= c a
b conditioning intervals
c
d pairs()
a
b panel= panel= panel.smooth()
coplot() x coplot panel y
points()
12.1.3
tsplot(x 1, x 2, ...) xi 1, 2, 3, . . . qqnorm(x) qqline(x) qqplot(x, y) Normal order scores ) x hist(x) hist(x, nclass=n) hist(x, breaks=b, ...) nclass= breaks= probability=TRUE
1
1
x ( y
x
breakpoint
: “ 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 dotchart(x, ...) x ximage(x, y, z, ...) contour(x, y, z, ...) persp(x, y, z, ...) line z image z contour persp 3D yx
86
contour
12.1.4
add=TRUE ( axes=FALSE log="x" log="y" log="xy" type= x y type= type="p" type="l" type="b" type="o" type="h" density)) type="s" type="S" type="n" ( ) x ( (high( ) — axes=TRUE ) axis()
xlab=string ylab=string x y
12.2 main=string sub=string x-
87
12.2
( )
points(x, y) lines(x, y) type= "p" text(x, y, labels, ...) lines() x, y (x[i], y[i]) "l" plot() ( points() ) labels labels[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 ) h=y yx2 ) polygon(x, y, ...) (x, y) (hatch lines) v=x lm.obj coefficients ( b a
12.2 legend(x, y, legend, ...) legend v ( ) legend( , fill=v ) legend( , col=v ) legend( , lty=v ) legend( , lwd=v ) legend( , pch=v ) ( title(main, sub) main sub axis(side, ...) ) axes=FALSE plot() ( x y ) (1 4 ) legend legend
88
x
y )
x
y
locator()(
12.2.1
R expression title
> text(x, y, expression(paste(bgroup("(", atop(n, x), ")"), p^x, q^{n-x})))
text
mtext axis
12.3 R
> help(plotmath) > example(plotmath) > demo(plotmath)
89
12.2.2
Hershey
text contour Hershey Hershey
• Hershey
• Hershey
• Hershey R
> > > > help(Hershey) demo(Hershey) help(Japanese) demo(Japanese)
cyrillic Hershey
12.3
R locator() locator(n, type) n ( 512) type locator() x locator() outlying point
> text(locator(1), "Outlier", adj=0)
y
12.4 ( postscript identify(x, y, labels) ) labels x locator() labels y(
90 )
x
y
(x, y)
identify()
> plot(x, y) > identify(x, y)
identify() ( x/y labels ) ( x y ) identify() identify() plot = FALSE
12.4
R R ( ‘col’ ) ( )
12.4.1
par() par()
: par()
par(c("col", "lty")) )
(
12.5 par(col=4, lty=2) ( )
91
par() “ ” par() par() R
2
par()
> 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 name=value value axes plot xaxt yaxt name par()
92
12.5.1
R graphical element pch="+" ‘◦’ "." pch=4 pch 0 25
> legend(locator(1), as.character(0:25), pch = 0:25) 21 pch lty=2 ) 2 lwd=2 “ ” lines() col=2 ( ?palette) col.axis col.lab col.main col.sub font=2 1 5 ( 2 ) 3 4 x y 1 25 points 32:255 ( 0
12.5 font.axis font.lab font.main font.sub adj=-0.1 0.5 -0.1 10% cex=1.5 ( ) x y 0 1
93
12.5.2
R axis line ( lty ) ) lab=c(5, 7, 12) x y tick mark ( ) tick label ( axis()
las=1 2 mgp=c(3, 1, 0)
0
1
( tck=0.01 ( 0.5) x y tck=0.01 xaxs="r" yaxs="i" x (S y R "i" ( "r" )
0) tck 1 mgp=c(1,-1.5,0)
"r" ( )
)
12.5
94
12.5.3
R figure plot region
−−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−−
3.0
mar[3]
Plot region
1.5
mai[2]
−1.5 −3.0 −3.0
0.0
y
−1.5
0.0 x
1.5
3.0
mai[1]
Margin
mai=c(1, 0.5, 0.5, 0) mar=c(4, 2, 2, 1) mar mai mai
height=4 mar mai
postscript()
50%
12.5.4
R n outer margin m
12.5
−−−−−−−−−−−−−−− −−−−−−−−−−−−−−− −−−−−−−−−−−−−−− −−−−−−−−−−−−−−− −−−−−−−−−−−−−−−
95
oma[3]
omi[4]
mfg=c(3,2,3,2)
mfrow=c(3,2)
omi[1]
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 oma=c(2, 0, 3, 0) omi=c(0, 0, 0.8, 0) mar mai
96
mtext() oma grid lattice omi split.screen()
outer=TRUE
layout()4
12.6
R R device driver R ( “draw a line,”) ( )
help(Devices)
> postscript()
PostScript X11() windows() quartz() postscript() pdf() png() jpeg() PNG JPEG ) UNIX Windows MacOS X PostScript PDF ( image ( PDF ) PostScript X11
> dev.off()
4
12.6
97
(
)
12.6.1
file PostScript
PostScript
postscript() horizontal=FALSE width ) height (
> postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)
5
PostScript R
PostScript R ( EPSF ) conformant output S
encapsulated PostScript onefile=FALSE
5
> postscript("plot1.eps", horizontal=FALSE, onefile=FALSE, height=8, width=6, pointsize=10)
12.6.2
R
X11() windows() win.printer() win.metafile() quartz() postscript() pdf()
5
[UNIX]
[Windows] [MacOS X]
GSview
PostScript
PostScript
12.7 ...
98
( ) dev.list() 1 vice dev.next() dev.prev() dev.set(which=k ) dev.off(k ) postscript k k null de-
dev.copy(device, ..., which=k ) dev.print(device, ..., which=k ) ... k postscript dev.print device
graphics.off()
12.7
R ) XGobi http://www.research.att.com/areas/stat/xgobi/ R R
6
“
6
” brush ( Buja
Swayne, Cook
xgobi
XGobi
UNIX
Windows
X
SAS
12.7 XGobi org GGobi
7
99 http://www.ggobi.
7
ggobi
downloads/
Rggobi R Ggobi Ggobi R $R HOME/library/Rggobi/libs
R Ggobi Rggobi Rggobi Ggobi
BioConductor http://www.ggobi.org/ Ggobi
R ) R
packages (
> library()
( & Hinkley (1997))
> library(boot)
boot
Davison
CRAN.packages() Packages )
(
Windows
RAqua
> search()
( 101>)
<
> help.start()
HTML
Reference
13.1
101
13.1
( ) R R R R
13.2
CRAN
R (
) org/ org/) ) R
R
CRAN (http://CRAN.R-project. Bioconductor (http://www.bioconductor.
13.3
namespaces datasets
t()
R :: base::t
t
base ::: R getAnywhere() inter-dependent
1
1
R
$ R
R2 R ( R help.start() HTML ( ) )
x <- rnorm(50) y <- rnorm(x)
x plot(x, y)
y
ls() R rm(x, y)
1 2
: Windows
Linux R
Windows 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) ( 0 1)
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") skewness kurtosis outlier
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 ) 20 (Run ) Morley sl (Expt
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 ≤ π
50
y
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)
f 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
13.4
UNIX Windows
R
R
R R
R [options] [outfile], R CMD )R UNIX R ( ) • --no-environ R R ENVIRON $R HOME/etc/Renviron.site ( .Renviron ) / ) • R R PROFILE $R HOME/etc/Rprofile.site • --no-init-file .Rprofile R --no-site-file ) R PRINTCMD ( ) ) name=value R LIBS ( (help(Startup) R PAPERSIZE ( R Startup WindowsTMPDIR “ ” ( R
13.4 • • ) .RData
R ( --no-restore ( R .RData R ( R Memory
109 --no-restore-data) .Last
.First
) R --help -h --version --encoding=enc RHOME man ) --save --no-save R stdin R R
iconv “ ” ( shell
q()
--no-environ --no-site-file --no-init-file --restore --no-restore --no-restore-data .RData) --no-restore-* --no-restore-history .Rhistory --vanilla ) ( R ) R HISTFILE ( R (--no-restore
--no-save, --no-environ --no-site-file, --no-init-file --no-restore
13.4 --no-readline
R ( UNIX) Emacs readline
110
ESS (“Emacs Speaks Statistics”) < 114> ESS
R
--ess --min-vsize=N --max-vsize=N
(
Windows) Rterm “ N
R-inferior-mode
” vector heap
N ‘Giga’
(2ˆ30) ‘Mega’ (2ˆ20) (1000 --min-nsize=N --max-nsize=N “cons 32 64 --max-ppsize=N 10000 56 ”
‘Kilo’ (2ˆ10 k N
)
‘kilo’
) G M K
cons cells cons N 100000
N 28
--max-mem-size=N
(
Windows) 1024Mb 16Mb
R
--quiet --silent -q --slave --verbose --debugger=name -d name ( UNIX) name R R --gui=type R R R verbose TRUE R
13.4 -g type
R ( UNIX) ) Tcl/Tk none type type Tk gnome ( X11
111
GNOME
--args commandArgs() ( < R CMD R CMD command args command args >) (stderr) R “ ” Windows 9X/ME
BATCH COMPILE SHLIB INSTALL REMOVE build check LINK Rprof Rdconv Rd2dvi Rd2txt Sd2Rd config ( ( R Rd UNIX) ( ( UNIX)
R R shared library
)
Post-process HTML Nroff Rd Rd S UNIX) Rd
A L TEX
S
DVI/PDF
R CMD \var{command} --help
R CMD
13.6
Mac OS X
R
112
13.5
Windows
Windows ) R.exe )
R
R ( cmd.exe Rterm.exe command.com
(Rgui.exe) Windows ’ home directory R USER R Windows HOMEDRIVE ) name=value RGui.exe --mdi --sdi --no-mdi ) ) --debug Rgui MDI SDI Rgui “Break to debugger” ( ( HOMEPATH ” ” ( ( Windows XP Windows NT/2000/XP C:\Documents and Settings\username\My Documents ) UNIX Windows HOME ‘
Windows TEXINPUTS
R CMD latex.exe mydoc
R CMD R HOME latex.exe
*.bat R VERSION
*.exe R CMD R OSTYPE PATH PERL5LIB
mydoc.tex
A L TEX
R
share/texmf
TEXINPUTS
13.6
Mac OS X
Mac OS X R
R
Terminal.app (R.app) R
13.6
Mac OS X
R Mac OS X UNIX ‘ ’ R.framework
113
Applications Mac OS X
3
13.7
UNIX R UNIX GNOME ESS GUI README.Rterm Meta character m CTRL META ESC b ESC
4 3)
GNU readline
R
UNIX
--no-readline ( Windows Console readline R Rterm.exe R
Help
ControlMeta-b META M-b
m M-b
C-m
b
ESC
13.8
R Emacsvi M-i M-a RET
3 4
ESC
‘Emacs Speaks Statistics’ PC Alt
URL http://ESS.R-project.org ‘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 C-f text DEL C-d M-d C-k C-y C-t M-l M-c RET RET R “ ” text (
text ) “ “ ” ”
4
t
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
. . . . . . . . . . . . . . . . . . . . . . . . . . . 72 . . . . . . . . . . . . . . . . . . . . . . 41 . . . . . . . . . . . . . . . . . . 78 . . . . . . . . . . . . . . . . . . . . . . . . . 46 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 . . . . . . . . . . . . . . . . . . . . . . 73 ............................ 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 . . . . . . . . . . . . . . . . . . . . . . 73 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 . . . . . . . . . . . . . . . . . . 46 . . . . . . . . . . . . . . . . . . . . . . 81 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18, 63 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
CRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Kolmogorov-Smirnov QR . . . . . . . . . . 48
. . . . . . . . . . . . . . . . . . . . . . . . . . . 31 . . . . . . . . . . . . . . . . . . 47 . . . . . . . . . . . . . . . . . . . . . .50
Shapiro-Wilk Wilcoxon
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2, 100 . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 . . . . . . . . . . . . . . . . . . . . . . . . . 52 . . . . . . . . . . . . . . . . . . 39 . . . . . . . . . . . . . . . . 48 ..................... 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 . . . . . . . . . . . . . . . . . . . . . . . . . 55 . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 . . . . . . . . . . . . . . . . . . . . . . . . . . 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 . . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 ............................ 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 . . . . . . . . . . . . . . . . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . 27 . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 ...................9 . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 . . . . . . . . . . . . . . . . . . . . . . 96 . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 . . . . . . . . . . . . . . . . . . . . 52 . . . . . . . . . . . . . . . . . . . . 9, 26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20, 69
117 . . . . . . . . . . . . . . . . . . . . . . . 20, 69 . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 . . . . . . . . . . . . . . . . . . . . . . . . . 80 . . . . . . . . . . . . . . . . . . . . 31 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5
* .................................... 9 + ................................... 9 -. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 .First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 .Last . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 / .................................... 9 : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 :: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 ::: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 == . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 ? .................................... 4 %*% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 %o% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 && . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 & . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 < . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 <<-. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 <= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 > . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 >= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ^....................................9 ~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 abline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 add1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 anova . . . . . . . . . . . . . . . . . . . . . . . . . . . 71, 72
aov . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 aperm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 as.data.frame . . . . . . . . . . . . . . . . . . . . . . . 36 as.vector . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 attach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 attr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 18 avas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 axis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 boxplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 bruto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 c . . . . . . . . . . . . . . . . . . . . . . . . . 8, 13, 32, 35 cbind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 coef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 coefficients . . . . . . . . . . . . . . . . . . . . . . . . . 71 contour . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 contrasts . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 coplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 cos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 crossprod . . . . . . . . . . . . . . . . . . . . . . . 25, 29 cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5 data.frame . . . . . . . . . . . . . . . . . . . . . . . . . 36 density . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 det . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 detach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 determinant . . . . . . . . . . . . . . . . . . . . . . . . 30 dev.list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 dev.next . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 dev.off. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 dev.prev . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 dev.set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 deviance . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 diag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 dim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 dotchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 drop1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 ecdf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 eigen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 FALSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 fivenum . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 formula. . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 getAnywhere . . . . . . . . . . . . . . . . . . . . . . . 64 getS3method . . . . . . . . . . . . . . . . . . . . . . . 64 glm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 help.search . . . . . . . . . . . . . . . . . . . . . . . . . . 4
119 help.start . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 hist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45, 85 identify . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 ifelse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 is.na. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 is.nan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 jpeg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 ks.test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 legend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 length . . . . . . . . . . . . . . . . . . . . . . . . . . . 9, 16 levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 lm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 lme. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 locator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 loess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 lqs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 lsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 mars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 NA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 NaN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 ncol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 nlm . . . . . . . . . . . . . . . . . . . . . . . . . 78, 79, 81 nlme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5 nrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 optim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 ordered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 outer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 par . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 pdf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 persp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 plot. . . . . . . . . . . . . . . . . . . . . . . . . . . . .71, 84 pmax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 pmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 png . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 postscript . . . . . . . . . . . . . . . . . . . . . . . . . . 96 predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 prod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 qqline. . . . . . . . . . . . . . . . . . . . . . . . . . .46, 85 qqnorm . . . . . . . . . . . . . . . . . . . . . . . . . 46, 85 qqplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 qr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 quartz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 rbind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 read.table . . . . . . . . . . . . . . . . . . . . . . . . . . 39 rep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 resid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 residuals. . . . . . . . . . . . . . . . . . . . . . . . . . . .71 rlm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
120 scan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 sd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 seq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 shapiro.test . . . . . . . . . . . . . . . . . . . . . . . . .47 sin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 sink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 stem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 step . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71, 73 sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 summary . . . . . . . . . . . . . . . . . . . . . . . 44, 71 svd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 t.test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 table. . . . . . . . . . . . . . . . . . . . . . . . . . . .25, 32 tan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 tapply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 TRUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 tsplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 unclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 var . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9, 21 var.test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5 wilcox.test . . . . . . . . . . . . . . . . . . . . . . . . . . 50 windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 X11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
121
6
D. M. Bates and D. G. Watts (1988), Nonlinear Regression Analysis and Its Applications. John Wiley & Sons, New York. Richard A. Becker, John M. Chambers and Allan R. Wilks (1988), The New S Language. 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 edition, 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