Вы находитесь на странице: 1из 109

.. , . .

R
R:

3.1.0 (2014-04-10)

2014

An Introduction to R
Notes on R: A Programming Environment for Data Analysis and Graphics
Version 3.1.0 (2014-04-10)

W. N. Venables, D. M. Smith
and the R Core Team
2

R, 3.1.0 (2014-04-10).
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 19992013 R Core Team

,
.

, ,
,
.

,
,
, R.
ISBN 3-900051-12-7

..
:
http://www.aafomenko@yandex.ru

. W. . , . .
R

R
R:
3.0.2 Patched (2014-01-27)
... , 2014. 109 . (
).


, R,
R.

............................................................................................................................ 8

..........................................................................................................................8
...............................................................................................................................................8

1. ...................................................................... 9

1.1. R ..............................................................................................................................................9
1.2. ................................................................9
1.3. R ...................................................................................................................................9
1.4. R .......................................................................................................................10
1.5. R ................................................................................10
1.6. .................................................................................................................................11
1.7. ............................................................................11
1.8. R, .. ...................................................................................................12
1.9. ...................................................................................12
1.10. ......................................12
1.11. ..................................................................................13

2. ; ................................................................... 14

2.1. ................................................................................................................14
2.2. ...................................................................................................................14
2.3. ..............................................................................15
2.4. .......................................................................................................................16
2.5. .................................................................................................................16
2.6. ..........................................................................................................................17
2.7. ; ...................................18
2.8 ....................................................................................................................19

3. , ................................................................................. 20

3.1. : .........................................................................................20
3.2. ......................................................................................................21
3.3. ................................................................................................21
3.4. .................................................................................................................................21

4. ........................................................... 23

4.1. .....................................................................................................................23
4.2. tapply () .........................................................23
4.3. ...............................................................................................................24

5. ........................................................................................................ 25

5.1. ..........................................................................................................................................25
5.2. . .................................................................................25
5.3. .............................................................................................................................26
5.4. array()...............................................................................................................................27
5.4.1. . ......................................... 28

5.5. ........................................................................................28
5.6. .....................................................................................28
5.7. ..............................................................................................................29
5.7.1. ......................................................................................................................... 29
5.7.2. ...................................................................................................... 29
5.7.3. .......................................................................... 30
5.7.4. ..................................................................................... 30
5.7.5. QR ................................................... 30

5.8. cbind () rbind () ..............................................................31


5.9. c() ................................................................................................32
5.10. .......................................................................................................32

6. .............................................................................................. 33

6.1. .............................................................................................................................................33

6.2. .................................................................................................34
6.2.1. ..................................................................................................................... 34

6.3. ..............................................................................................................................34
6.3.1. .............................................................................................................. 34
6.3.2. attach() detach() .............................................................................................................................. 35
6.3.3. ............................................................................................................ 35
6.3.4. ........................................................................................... 36
6.3.5. ............................................................................................................... 36

7. ............................................................................................... 37

7.1. read.table() .......................................................................................................................37


7.2. scan() ................................................................................................................................38
7.3. ........................................................................................38
7.3.1. R ............................................................................................ 39

7.4. .................................................................................................................39

8. ............................................................................................ 40

8.1. R ..................................................................................................40
8.2. .............................................................................41
8.3. ..................................................................................................44

9. , ........................................................... 47

9.1. ...........................................................................................................47
9.2. ..................................................................................................................47
9.2.1. : if............................................................................................... 47
9.2.2. : for, loops, repeat while.......................................................................... 47

10. ............................................................................... 49

10.1. .........................................................................................................................49
10.2. ................................................................................50
10.3. ......................................................................................50
10.4. ....................................................................................................................................51
10.5 .................................................................................................51
10.6. .............................................................................................................51
10.6.1. .............................................................. 51
10.6.2. ...................................................................... 52
10.6.3. ........................................................................................ 53

10.7. .........................................................................................................................53
10.8. ..................................................................................................................55
10.9. , - ...........56

11. R.......................................................................................... 59

11.1 ; ......................................................................59
...................................................................................................................................................... 59
11.1.1. .................................................................................................................... 61

11.2. .........................................................................................................................62
11.3. ...........................................62
11.4. .............................................................................63
11.4.1. ANOVA ............................................................................................................................. 64

11.5. ............................................................................................64
11.6. ..................................................................................................65
11.6.1. ...................................................................................................................................... 66
11.6.2. glm() ................................................................................................................................. 66

11.7. ........................68
11.7.1. .................................................................................................................. 69
11.7.2. ........................................................................................ 70

11.8. .............................................................................................70

12. ............................................................................................... 72

12.1. ......................................................................................72
12.1.1. plot() ................................................................................................................................. 72

12.1.2. ............................................................................................ 73
12.1.3. ........................................................................................................................ 74
12.1.4. .......................................................... 74

12.2. ........................................................................................75
12.2.1 ......................................................................................................... 77
12.2.2 ........................................................................................................... 77

12.3. ...............................................................................................................77
12.4. ...................................................................................78
12.4.1. : par() ........................................................................................ 78
12.4.2. : .................................................. 79

12.5. ................................................................................................79
12.5.1. ................................................................................................................. 80
12.5.2. .................................................................................................................................... 81
12.5.3. ................................................................................................................................... 82
12.5.4. ....................................................................................................... 83

12.6. .......................................................................................................................84
12.6.1. PostScript ........................................................................................ 85
12.6.2. ..................................................................... 85

12.7. ................................................................................................................86

13. .............................................................................................................................. 88

13.1. ....................................................................................................................88
13.2. CRAN .........................................................................................................88
13.3. .......................................................................................................................89
13.4. .......................................................................................89
13.4.1.
- Basics ........................................................................................................ 89
13.4.2.
-Times and Dates ............................................................................................... 90
13.4.3.
- Time Series Classes ....................................................................... 90
13.4.4.
-Forecasting and Univariate Modeling ..................... 91
13.4.5.
- Resampling ................................................................................................. 91
13.4.6.
- Decomposition and Filtering .................................................. 92
13.4.7.
, - Stationarity, Unit Roots, and
Cointegration ............................................................................................................................................... 92
13.4.8.
-Nonlinear Time Series Analysis ................................. 92
13.4.9.
- Dynamic Regression Models ............................................ 93
13.4.10.
- Multivariate Time Series Models ...................... 93
13.4.11.
- Continuous time models .................................................. 93
13.4.12.
- Time Series Data ................................................................... 94
13.4.13.
- Miscellaneous .......................................................................................................... 94

13.5.

: ..............................................................94

A. .................................................................................... 97
B. R .................................................................................................... 101

.1. R ....................................................................................................101
.2. R Windows .................................................................................................................105
.3. R OS X ........................................................................................................................106
.4. R ...................................................................................................................................106

C. ................................................................ 108
F. ..................................................................................................... 109

R ,
S SPlus, 1990-2 .
.
R S, .
( ),

, , , R .
. ,
R-core@R -project.org.

R A.
R , ,
, .
R, , -
. 12 [],
.

,
R.
\ R\doc\manual\R-intro. Rintro , R
.
:

, ,
Windows;
,
;
,
.

1.
1.1. R
R
, . :

;
, ,
, ,
,

,
,
( S), , ,

. ,
S.

"/" ,
,
,
.
R .
. ,
, R, , .
1.2.
R S, Bell
Laboratories , ,
S-Plus.
S
. R S:
, . , .
. . S, 1991,
S, .
. .
. . . F [],
.
, R
, S/S-Plus , ,
R, S. . , "
R? FAQ R .
1.3. R
R ,
R . ,

. R,
. R 25 (
"" "" ),
CRAN ( http://CRAN.R -project.org) .
(. 13 []).
9


R, ,
.
S (, , R)
. S
, .
, SAS SPSS
, R
R.
1.4. R
R
. ,
. R
Windows , , ,
R.
, ,
. , ,
UNIX. R
Windows Mac OS,
.
,
R, ,
.
.
1.5. R
R .
>, UNIX ,
, , . , ,
R. , UNIX - $.
R UNIX
:
1.

2.
3.
4.

, 'work' ,
R .
R .
$ mkdir work
$ cd work
R
$R

R :

q()
, R.
,
, ,
( ), ,
, R.
R.
10

R .
1. 'work' :
$ cd work
$R
2. R, q() .
R Windows .
, Start In R.
R, .
1.6.
, R ,
, A [
].
1.7.
R .
, solve, :
> help(solve)
:
>? solve
, ,
, " ":
, if, for
function.
> help("[[")

, , "Its important".
.
R HTML,
:
> help.start ()
-,
. Search Engine and Keywords , help.start(),
, ,
.
R .
help.search ( ??)
.
,
>?? solve
?help.search .
:
> example(topic)
R Windows
. :
11

>?help
.

1.8. R, ..
R .
, UNIX, , A
. ,
R,
, R ( , - locale).
- '.' _, ,
. , .,
. ,
256 R 2.13.0.
(expression),
(assignments) . , ,
( ) .
,
.
(;), .
( {
}). ,
"" (#), .
, R ,
:
+
,
. . ,
,
.
, , 4095
( ).
1.9.
R
.
.
, ,
, DEL
.
: . C [ ].
, Emacs
( ESS Emacs Speaks Statistic) R.
R and Emacs The R statistical system FAQ.
1.10.
, command.R
'work', R
:
> source("commands.R ")
Windows Source File. sink:
12

> sink("record.lis")
record.lis.
> sink()
.
1.11.
, R , (object).
, , ,
, .
R (
). R:
> objects()
( ls()) ( ) ,
R. ,
, (workspace).
rm:
> rm(x, y, z, ink, junk, temp, foo, bar)
, R,
R. R
.
, ,
.RData , , ,
.Rhistory.
R .
.
,
R .
x y. ,
, ,
.

13

2. ;
2.1.
R (data structures).
, ,
. x, ,
, , 10.4, 5.6, 3.1, 6.4 21.7, R:
> x <- c (10.4, 5.6, 3.1, 6.4, 21.7)
- , c(),

, .
, ,
.
, ( <-),
< ( ) - (""), ''
, .
= .
, 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
,
. , :
> 1/x
( x,
, ).
:
> y <-c (x, 0, x)
y 11 , x .
2.2.
,
. , ,
. ,
,
. ,
( ), ,
.
. , , :
> v <-2*x + y + 1
v 11,
2*x 2.2 , y , 1 11 .
14

+, -, *, / ^
.
. log, exp, sin, cos, tan, sqrt , .
max min .
range , - , , c(min(x),
max(x)). length(x) x, sum(x) x,
prod(x) .
mean(x),
, sum(x)/length(x), var (x),
sum((x-mean(x))^2)/(length(x)-1)
. var() n--p , p--p, p
-.
sort(x) x ,
;
(. order(), sort.list(), ).
, max min
, .
pmax pmin (,
), ()
.
, ""
, .
,
, .
.
:
sqrt (-17)
NaN ,
sqrt (-17+0i)
.
2.3.
R
. , 1:30 c(1, 2..., 29, 30).
, ,
2*1:15
c(2, 4..., 28, 30). n <-10
1:n-1 1: (n-1).

30:1
.

seq()
. ,
. , ,
, ,
. , seq(2,10)
2:10.
15

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=1.
:
> 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)
.
along=vector,
1, 2..., length(),
, ( ).
rep(),
. :

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


x s5.
> s6 <- rep(x, each=5)
.
2.4.
, R
. TRUE, FALSE,
NA ( , . ). T F,
. , , T F - ,
TRUE FALSE , , ,
. ,
TRUE FALSE.
. :
> temp <- x > 13
temp x FALSE,
x, , TRUE,
.
<, <=, >,>=, ==
!= . , c1 c2 - , c1&c2 -
(""), c1|c2 - (""), !c1 - c1.
,
, FALSE 0 TRUE 1.
,
, . .
2.5.
.
" "
, ,
16

NA. 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,
.
, "" ,
, - NaN.
:
> 0/0

> Inf Inf


NaN, .
, is.na(xx) TRUE NA NaN.
, is.nan(xx) TRUE NaN.
<NA>,
.
2.6.
R,
, .
, , , "x-",
" ".
, (") ()
, , ( ).
escape- C-, \, ESC,
, \\ \\, , " \".
escape-: \n - , \t \b Backspace ?Quotes .
();
.
paste()
. , ,
, ,
.
, sep=string,
, .
:
> labs <- paste(c("X","Y"), 1:10, sep="")
labs
c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10")
17

, ;
, c("X","Y") 5 1:10.
2.7. ;

.
, , ,

.
.
1.

.
, .
, TRUE , , ,
FALSE, . :

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


( ) y,
( ) x . , , x
, 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]
10 x (, length(x) ,
10). :
> c("x","y")[rep(c(1,2,2,1), times=4)]
( ),
16, "x", "y", "y", "x" .

3.

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

4.

. ,
.

2 :
> fruit <- c(5, 10, 1, 20)
> names(fruit) <- c("orange", "banana", "apple", "peach")
> lunch <- fruit[c("apple","orange")]
18

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

> y[y < 0] <- -y[y < 0]
:
> y <- abs(y)
2.8
- R, ,
.

matrices () arrays () -
. , - ,

. . 5 [ ].
factors ()
. . 4 [].
lists () - ,
,
.
. . 6.1 [].
data frames ( ) - ,
.
,
() .
:
, . . 6.3 [
].
functions () - R,
.
R . . 10 [
].

19

3. ,

3.1. :
R .
() ,
. ""
, (mode), ,
numeric, complex, logical, character raw.
. ,
, ,
, (logical, numeric, complex, character
raw). - "",
NA ,
NA. , . ,
character(0)
numeric(0).
R , (list),
(list). ,
. (list) "",
, .
-
(function expression). , R
,
. , , R,
,
(formulae), R.
(mode)
. - "" . . mode(object) length(object),
.
attributes(object),
3.3 [ ]. -
.
, z - 100, mode(z)
"complex", length(z) 100.
R , ,
, . , :
> z <-0:9

> digits <- as.character(z)
digits c("0", "1", "2"..., "9").
, , :
> d <- as.integer(digits)
d z .
as.something() ,
, , , .
20


.
3.2.
. :
> e <- numeric()
e (numeri). character()
, .
, ,
. :
> e[3] <- 17
e 3, ( NA).
, ()
.
,
, scan() (. 7.2 [ scan()]).
.
, alpha - 10,
> alpha <- alpha[2 * 1:5]
5,
. ( , ).
:
> length(alpha) <- 3
( ) .
3.3.
attributes(object) ,
.
attr(object, name) .
, ,
,
R. , , .
,
, R.
,
object
. :
> attr(z, "dim") <- c(10,10)
R z 10--10.
3.4.
R (class), class.
- , , "numeric", "logical", "character"
"list", "matrix", "array", "factor" "data.frame" "
.
, class () ,
- R. ,
"data.frame", ,
21

plot() , ,
, summary(),
, .
, unclass().
, winter "data.frame" :
> winter
, , :
> unclass(winter)
.
,
.
10.9
[ ], .

22

4.
- ,
() . R
, . ""
(. 11.1.1
[]), .
4.1.
, , 30
,
:
> 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")
, , sorted
.
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
level():
> levels(statef)
[1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa"
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()
, ,
, statef,
. - ,
23

.
.
,
. R
. var()
, ,
:
> stderr <- function(x) sqrt(var(x)/length(x))
10 [
], , R
sd(). :
> incster <- tapply(incomes, statef, stderr)
:
> 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(), , qt(),
t-. R t-.
tapply()
. , ,
. ( ) ,
, .
, .
. - ,
levels .
- ,
, .
,
, .
4.3.
, ,
, .
,
. ordered()
, , , factor.

,
.

24

5.

5.1.
(array)
, . R
, - .
.
k, k-, , 2-
. ,
.
R ,
dim. , , z -
1500 .
> dim(z) <- c(3,5,100)
dim, 3--5--100.
, matrix() array()
, 5.4 [
array()].
,
,
, ,
.
, , a, c(3,4,2)
3 * 4 * 2 = 24 a [1,1,1], [2,1,1], ...,
[2,4,2], [3,4,2].
:
(, ),
.
5.2. .
, ,
, .
,
; , -
, .
, [2,,] 42
(4,2) , :
c ([2,1,1], [2,2,1], [2,3,1], [2,4,1],
[2,1,2], [2,2,2], [2,3,2], [2,4,2])
. [,,] ,
.
, Z,
dim(Z) ( ).
, ,
,
; . , ,
25

, ,
.
5.3.
,

,
.
.

. -
. , , X 4--5,
:

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


X .

3--2,
.
> 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
# .
>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
. NA
: , ,
, , NA, NA .
, ,
( ) ,

26

(b ) (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, :
> N <- crossprod(Xb, Xv)

table():
> N <- table(blocks, varieties)
:
( ) .
5.4. array()
, dim,
(array), :
> Z <- array(data_vector, dim_vector)
, h 24 , :
> Z <- array(h, dim=c(3,4,2))
h, 3--4--2 Z. h 24,
:
> Z <- h ; dim(Z) <- c(3,4,2)
, h , 24,
24 (. 5.4.1 [ ]), dim(h) <-c
(3,4,2) . ,
:
> Z <- array(0, c(3,4,2))
Z .
dim(Z) c(3,4,2), Z[1:24]
, h, Z[] Z
.
,
, .
dim ,
. , A, B C
, :
> D <-2*A*B + C + 1
D ,
.
.
27

5.4.1. .
,
, .
.

.

, .
,
dim
.
,
, .
, ,
, -
dim .

5.5.
. a b -
, - , ,
( ),

b.
%o%:
> ab <-%o % b

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



. , f(x; y) = cos(y)/(1 + x2)
x- y-, R x y
, :
> f <- function(x, y) cos(y)/(1 + x^2)
> z <- outer(x, y, f)
-
(
1). , , , .
R 10 [
].
5.6.
aperm(a, perm) a.
perm {1, . , }, k
a. -
a, , perm[j], j-
.
. , A - , ( ,
) B :
> B <-aperm (A, c (2,1))
.
t(), , , , B <-t (A).
28

5.7.
, - .
, . R
, . , t(X)
, . nrow (A) ncol (A)
.
5.7.1.
%*% . n--1 1-n , , n-,
. , , ,
,
, , (
, ).
, , A B - ,
>*B

>%*%B
. x - ,
> x %*% %*%x
.
crossprod() " ", ,
crossprod(X, y) t(X) %*% y, .
crossprod() , , .
diag() . diag(v), v - ,
.
diag(M), ,
. diag() Matlab. ,
, k , diag(k) k--k
!
5.7.2.
.

> b <- A %*% x


A b , x . R
> solve(A, b)
, x ( ). ,
, x = -1b, -1 A,
:
solve(A)
.
, x <- solve(A) %*% b solve(A,b).
xTA-1x, ,
x %*% solve(A,x), A.

29

5.7.3.
eigen(Sm)
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)
.
U ,
V, -
D , = U % * % D % * % t(V). D
. svd(M)
, d, u v .
, , :
> absdetM <- prod(svd(M)$d)
.
, R
> absdet <- function(M) prod(svd(M)$d)
absdet() R.
, ,
, tr()
. [: .
diag().]
R det, , ,
determinant, (
).
5.7.5. QR
lsfit () ,
. :
> ans <- lsfit(X, y)
, y - ,
X . .
, ls.diag(), ,
30

. ,
X. ,
lm() (. 11.2 [ ]) lsfit()
.
qr() .
:
> Xplus <- qr(X)
> b <- qr.coef(Xplus, y)
> fit <- qr.fitted(Xplus, y)
> res <- qr.resid(Xplus, y)
y X fit,
res b, , b
' ' Matlab.
, X .
.
- ,
. ,
,
11 [ R ].
5.8. cbind () rbind ()
,
cbind() rbind(). , cbind()
, , rbind() ,
.
:
> X <-cbind (arg_1, arg_2, arg_3...)
cbind() ,
, .
arg_1, arg_2...,
.
cbind() ,
, ,
, (
, ).
rbind() .
, ,
.
, X1 X2 .
X, , 1,
:
> X <-cbind (1, X1, X2)
rbind() cbind() . , cbind(x)
rbind(x) , , x
.

31

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)
, 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
.

32

6.

6.1.
(list) R, ,
.
, ,
, , ,
, , , .
:
> Lst <- list(name="Fred", wife="Mary", no.children=3,
child.ages=c(4,7,9))
.
, Lst ,
Lst[[1]], Lst [[2]], Lst [[3]] Lst [[4]]. , , Lst [[4]]
Lst [[4]] [1] .
Lst , length(Lst) ( )
, . ,
,
, , ,
:
> name$component_name
.
- ,
, .
, :
Lst$name - Lst [[1]] "",
Lst$wife - Lst [[2]] "",
Lst$child.ages [1] Lst [[4]] [1] 4.
,
, , Lst[[""]] Lst$name.
,
:
> x <- "name"; Lst[[x]]
Lst[[1]] Lst[1]. [[...]] ,
, [...]
. , -
Lst, , .
Lst, . -
, .

, . ,
Lst$coefficients Lst$coe Lst$covariance
Lst$cov.

33

-
. , ,
.
6.2.

list(). :
> Lst <- list(name_1=object_1, ..., name_m=object_m)
Lst m , object 1, . . . , object m
(
). , .
,
.
, ,
. :
> Lst[5] <- list(matrix=Mat)
6.2.1.
c()
,
.
> list.ABC <- c(list.A, list.B, list.C)
, ,
.
, dim, .
6.3.
"data.frame". ,
, ,

(,
), , ,
.
, ,
, ,
, .
, ,
,
- , .
, ,
,
.


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

, ,
, as.data.frame().

read.table() .
7 [ ].
6.3.2. attach() detach()
$, accountants$home,
.

.
attach(() ' ', ,
. , , lentils -
lentils$u, lentils$v, lentils$w. :
> attach(lentils)
2
u, v w 1; u, v w
. :
> u <- v+w
u ,
u 1 .
,
$:
> lentils$u <- v+w
u ,
.
, :
> detach()

2. , u, v w
, , lentils$u
. , 2 , ,
, ,
detach(lentils) or detach("lentils").
: R
2 , , ,
. ,
.
6.3.3.
,
:


;

2, 1
;
35

, , ,
,
$, detach().


.

,
, , x, y z, .
6.3.4.
attach() ,
, .
""
:
> attach(any.old.list)
-, , detach(),
, , .
6.3.5.
search ,
, ( )
. :
> search()
[1] ".GlobalEnv" "Autoloads" "package:base"
.GlobalEnv .
lentils :
> search()
[1] ".GlobalEnv" "lentils" "Autoloads" "package:base"
> ls(2)
[1] "u" "v" "w"
ls ( object)
.
, ,
.
> detach("lentils")
> search()
[1] ".GlobalEnv" "Autoloads" "package:base"

36

7.

,
R . R ,
.
R, , ,
Perl R.
.
, , ,
,
read.table(). scan(),
.
R
. / R .
7.1. read.table()

.

.

.

, ,
, , . ,
, ,
.
:
Price

Floor

01

52.00

02

Area

Rooms Age

Cent.heat

111.0 830

6.2

no

54.75

128.0 710

7.5

no

03

57.50

101.0 1000

4.2

no

04

57.50

131.0 690

8.8

no

05

59.75

93.0

1.9

yes

900

...

( )
, , Cent.heat ,
. .
read.table()
:
> HousePrice <-read.table ("houses.data")

.
.
:
37

Price

Floor

52.00

Area

Rooms Age

Cent.heat

111.0 830

6.2

no

54.75

128.0 710

7.5

no

57.50

101.0 1000

4.2

no

57.50

131.0 690

8.8

no

59.75

93.0

1.9

yes

900

:
> HousePrice <-read.table ("houses.data", header=TRUE)
header=TRUE , - ,
, .
7.2. scan()
,
. , ,
, , - input.dat.
scan() ,
:
> inp <- scan("input.dat", list("",0,0))
- ,
. , 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 (. 6.3.4
[ ]).
- , ,
,
.
> X <- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE)
,
.
7.3.
100 R (
), ( ,
R). ,
, :
data()
38

, R, .
,
data R, :
data (infert)
( ).
R .
, .
, .
7.3.1. R
,
:
data(package="rpart")
data(Puromycin, package="datasets")
,
.

.
7.4.
,
.
.
:
> xnew <- edit (xold)
xold,
xnew. xold,
fix(xold), xold <-edit (xold).
:
> xnew <- edit (data.frame)
.

39

8.

8.1. R
R
.
P(X x), (
q, x , P(X x)> q), .

beta

beta

shape1, shape2, ncp

binomial

binom

size, prob

Cauchy

cauchy

location, scale

chi-squared

chisq

df, ncp

exponential

exp

rate

df1, df2, ncp

gamma

gamma

shape, scale

geometric

geom

prob

hypergeometric

hyper

m, n, k

log-normal

lnorm

meanlog, sdlog

logistic

logis

location, scale

negative

binomial

nbinom size, prob

normal

norm

mean, sd

Poisson

pois

lambda

signed

rank

signrank n

Students t

df, ncp

uniform

unif

min, max

Weibull

weibull

shape, scale

Wilcoxon

wilcox

m, n

, d , p CDF, q
r ( ). - x
dxxx, q pxxx, p qxxx n rxxx ( rhyper, rsignrank rwilcox
- nn).
ncp: . .
pxxx qxxx lower.tail log.p,
dxxx
. , , (
"") H(t) = - log (1 - F (t)) :
- pxxx(t, ..., lower.tail = FALSE, log.p = TRUE)

.

(dxxx(...,

log

TRUE))

, ptukey qtukey
, dmultinom
40

rmultinom .
, SuppDists.
.
> ## 2-tailed p-value for t distribution
> 2*pt(-2.43, df = 13)
> ## upper 1% point for an F(2,7) distribution
> qf(0.01, 2, 7, lower.tail = FALSE)
8.2.
()
. .
summary fivenum
stem ( ).
> 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)
1 |:
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)
## make the bins smaller, make a plot of density
> hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
41

> lines(density(eruptions, bw=0.1))


> rug(eruptions)
# show the actual data points
density,
, .
,
( "" ). (
,
=, "SJ" .)


ecdf.
> plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)
.
, , 3 ?
CDF.
> 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)

42

- (Q-Q)
:
par(pty="s") # arrange for a square figure region
qqnorm(long); qqline(long)
, ,
.
t :

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

( ) ,
. Q-Q
:
qqplot(qt(ppoints(250), df = 5), x, xlab = "Q-Q plot for t dsn")
qqline(x)
,
( ) R _:
> shapiro.test(long)
Shapiro-Wilk normality test
data: long
W = 0.9793, p-value = 0.01052
-:
> 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
(, ,
.)
8.3.
.
. , R,
"" , ,
.

(cal/gm) (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
Boxplots .
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)
, , .

44

,
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
,
. R
( S-Plus t.test).
F, , ,
.
> 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

45

, ,
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
( - ) ,
.
> wilcox.test(A, B)
Wilcoxon rank sum test with continuity correction
data: A and B
W = 89, p-value = 0.007497
alternative hypothesis: true location shift is not equal to 0
Warning message:
Cannot compute exact p-value with ties in: wilcox.test(A, B)
: ,
, (, -
).
.
boxplots. :
> plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))
> plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)
CDF, qqplot Q-Q .
-
ecdf, :
> 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)

46

9. ,
9.1.
R - , -
, . - ,
, ,
; .
, {expr_1;...; expr_m},
- .
- , , ,
.
9.2.
9.2.1. : if
:
> if (expr_1) expr_2 else expr_3
expr 1 ,
.
" " && ||
if. , & |
, && || ,
.
if/else, ifelse.
ifelse(condition, a, b), ,
a[i], condition[i] , b[i].
9.2.2. : for, loops, repeat while
for, :
> for (name in expr_1) expr_2
name - . expr_1 (
1:20), expr_2
, . expr_2
, name
expr_1.
, , ind - ,
y x .
, coplot(), ,
. -
:
> 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(), ,
, .
47

, boxplots. .
.
: for() R ,
. ,
, , R .
:
> repeat expr

> while (condition) expr
break ,
. - .
next ,
"".

, 10 [ ],
.

48

10.

, R
. R,

. ,
,
R .
, ,
R, mean(), var(), postscript() , R ,
, .
:
> name <- function(arg_1, arg_2, ...) expression
expression R, ( ),
arg_i .
, .

name(expr_1, expr_2...) , .
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- ,
:
> tstat <-twosam (data$male, data$female); tstat
,
Matlab,
y X.

. qr();
, ,
.
, n--1 y n--p X, X y
(XTX)-XTy, (XTX)- XX.
> bslash <- function(X, y) {
X <- qr(X)
49

qr.coef(X, y)
}
, :
> regcoeff <-bslash (Xmat, yvar)
.
R lsfit() .
qr() qr.coef()
. , , ,

. ,
.
10.2.
bslash() , :
%anything%
, , ,
. , , !
. :
> "%!%" <- function(X, y) { ... }
.
X%!%y. ,
.
%*%, %o%
, .
10.3.
2.3 [ ],
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)
:
50

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


.
, ,
;
.
10.4. ...

. ,
par(), plot(),
par(), . .
12.4.1 [Par() ], par().
...,
. .
fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[ ]
if (graph)
par(pch="*", ...)
[ ]
}
. list(.)
, 1, 2,
.. n n-
.
10.5
, , ,
.
, X <-qr (X)
.
, R,
.
, ,
.
,
"" <<-, assign().
. S-Plus , <<-
R . 10.7
[].
10.6.
10.6.1.
,
.
5.3 [ ].
, (b )
(v ). R K v--v b--b
, , N b--v ,
:
51

E = Iv R-1/2NTK-1NR-1/2 = Iv - AT A;
A=-1/2NR-1/2.
:
> 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))
# remove dim attr
R <- as.vector(table(varieties))
# remove dim attr
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)
}

, .
- ,
,
,
.
10.6.2.

. dimnames
, dimnames,
. , X:
> temp <- X
> dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)))
> temp; rm(temp)
, no.dimnames(), ,
"" . ,
.
no.dimnames <- function(a) {
## Remove all dimension names from an array for compact printing.
d <- list()
l <- 0
for(i in dim(a)) {
d[[l <- l + 1]] <- rep("", i)
}
dimnames(a) <- d
a
}

:
52

> no.dimnames(X)
,
, .
10.6.3.
,
. , , ,
,
, .

.
.
, .
. - ,
,
. , , ,
,
, .

.

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


fun1 <- function(f, a, b, fa, fb, a0, eps, lim, fun) {
## function fun1 is only visible inside area
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.
,
.
S-Plus R .
:
, .
, .
53


. ,
. ,
, .
,
. .
f <- function(x) {
y <- 2*x
print(x)
print(y)
print(z)
}
x - , y - z .
R
, . .
cube.
cube <- function(n) {
sq <- function() n*n
n*sq()
}
n sq . , ,
, .
(S-Plus) n.
(R) cube,
n sq.
R S-Plus , S-Plus n, R
n , cube.
## 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

. , R
.
, ,
.
(account), , . (account)
, total ,
54

. , total,
.
`<<-`
, total.
, total, ,
.
, total,
. <<-
. <<- ,
,
, .
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.

. ,

55

. ,
.First .Last.

R_PROFILE. ,
Rprofile.site 'etc' R.
, R
. , .Rprofile,
. R ,
.
.
.Rprofile , R .Rprofile
( ).
R_PROFILE_USER, , ,
.Rprofile.
.First() ,
.RData .
R . ,
$
,
.
, , ,
Rprofile.site, Rprofile, .RData, .First().
.
> .First <- function() {
options(prompt="$ ", continue="+\t")
# $
options(digits=5, length=999)
#
x11()
#
par(pch = "+")
# plotting character
source(file.path(Sys.getenv("HOME"), "R", "mystuff.R "))
#
library(MASS)
#
}
, .Last(), ()
. .
> .Last <- function() {
graphics.off()
cat(paste(date(),"\nAdios\n"))
}

# .
# ?

10.9. , -

, .
,
.
, , ,
.

56

.
.
plot()
, summary() , anova()
.
,
, . , ,
"data.frame", :
[ [[<- any as.matrix
[<- mean plot summary

methods():
> methods(class="data.frame")
, , ,
. , plot()
"data.frame", density", "factor" .
method():
> methods(plot)
,
:
> coef
function (object, ...)
UseMethod("coef")
UseMethod , .
, , method():
> methods(coef)
[1] coef.aov*
coef.Arima* coef.default* coef.listof*
[5] coef.nls*
coef.summary.nls*
Non-visible functions are asterisked
, ,
. , :
> 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, ...)
57

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

}
R
.

58

11. R

,
,
. , ,
- .

,
.
R ,
. ,
,
.
11.1 ;
-
, :

:
y = X + e
y - , X x0; x1
. ; xp . x0 ,
.

,
.
, y, x, x0, x1, x2... , X A, B, C...
. ,
.
y~x
y~1+x
y
x. , - .
y~0+x
y ~-1 + x
y~x-1
y x ( ,
).
log (y) ~ x1 + x2
log(y) x1 x2 (
).
y ~ poly (x, 2)
y ~ 1 + x + I(x^2)

59

y x 2.
, , .
y ~ X + poly (x, 2)
y , X,
x 2.
y~A
y ,
A.
y ~ A+ x
y ,
A, x.
y ~ A*B
y ~ + B + A:B
y ~ B %in % A
y ~ A/B
y A B.
,
.

.
y ~ (A+ B + C) ^2
y ~ A*B*C - A:B:C
, ,
.
.
y~A*x
y ~ A/x
y ~ A / (1 + x) - 1
y x
A .
,
A.
y ~ A*B + Error(C)
A B,
, C. ,
(, , ), C.
~ R .
:
response ~ op_1 term_1 op_2 term_2 op_3 term_3 ...
:
response - (
), () .

60

op_i - , + -,
( ).
term_i :

, 1,
,
, , ,
.

,
. 1
, .

,
Glim Genstat. ,
'.' : R.
( Chambers & Hastie, 1992, p.29):
Y ~ Y .
M_1 + M_2

_1 _2.

M_1 - M_2

_1 _2.

M_1: M_2

_1 _2. , "".

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 % M_1.
M^n
"" n
I(M)

.
,
.

, ,
, .
I() ,
, .
,
, .
, .
11.1.1.
, , ,
. ,
(
, ).
k- A?
. k-1
, ..., k- . (
,
61

, .) , k-1
1, ..., k, .
, . -,
, , ,
k , . -,
. R
:
options(contrasts = c("contr.treatment", "contr.poly"))
, R S
, S Helmert.
, ,
S-Plus, :
options(contrasts = c("contr.helmert", "contr.poly"))
- , (
R) .
,
,
C.
:
, .
,
R ,
, , . ,
, ,
, ,
.
11.2.

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() model;
"lm".
, ,
, "lm". :
add1
alias

deviance
drop1

formula
kappa

predict
print

step
summary
62

anova
effects
labels
proj
vcov
coef
family
plot
residuals
.
anova(object_1, object_2)

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

. ,
data.frame.
print(object)
.
.
residuals(object)
() , .
: resid ().
step(object)
,
.
AIC ( ),
.
summary(object)
.
vcov(object)

.
11.4.
aov(, data=data.frame)
lm(),
63

, 11.3 [
].
, aov()
, ,

. :
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
, : " farms", " farms, bloks" "
blocks".
11.4.1. ANOVA
, ( )
.

. ,
.

, , .
. Chambers & Hastie (1992).
ANOVA ,

anova().
> anova(fitted.model.1, fitted.model.2, ...)
ANOVA
, . , ,
, . ,
, .
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, ,
64

,
.

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

add1(), drop1() andstep().
, . .
11.6.

, - ,
.
:

y x1, x2 .,
.
y
.
, :
, Xi y
i = 0.

, ( ),
, A
, ,
, y.
, , y
, , , .

, :
l() .

,
, , ,
, .

, , McCullagh & Nelder (1989) (1990).
65

11.6.1.

,
R, gaussian, binomial, poisson, inverse gaussian gamma
response , ,
.
,
.
,
.
:

binomial
gaussian
Gamma
inverse.gaussian
poisson
quasi

logit, probit, log, cloglog


identity, log, inverse
identity, inverse, log
1/mu^2, identity, inverse, log
identity, log, sqrt
logit, probit, cloglog, identity, inverse,
log, 1/mu^2, sqrt
,
,
.
11.6.2. glm()

, ,
,
. .
R glm()
:
> fitted.model <- glm(formula, family=family.generator, data=data.frame)
- family.generator,
. - ,
, .
,
.
,
"" 11.6.1 []. ,
.

.
.

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



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

. ,
,
. ,
, .

Silvey (1970).

Kalythos
, .
-
. :
:

20

35

45

55

70

50

50

50

50

50

17

26

37

44

, , , ,
, LD50,
, 50%.
y n ,
y ~ B(n, F(0 + 1 x)), F(z) = Phi(z)
, ( ), F(z) =
e^z/(1+e^z). LD50 LD50 = - beta_0/beta_1, ,
.
- ,
> kalythos <- data.frame(x = c(20,35,45,55,70), n = rep(50,5), y = c(6,17,26,37,44))
binomial glm()
:

,
0/1.
- ,
,
.
, -
"" (0) "" (1).

,
:
> 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)

67

( ). LD50
:
> ld50 <- function(b) -b[1]/b[2]
> ldp <- ld50(coef(fmp)); ldl <- ld50(coef(fml)); c(ldp, ldl)
43,663 43,601
.

- log,
log , . - , .

.
,
log
. ,
:
> fmod <- glm(y ~ A + B + x, family = poisson(link=sqrt),
data = worm.counts)


. ; VAR[y]
= .

, ,
.
,

, .
, :
y = 1z1/(z2-2) + e
:
y = 1/(1x1 + 2x2) + e

:
> nlfit <- glm(y ~ x1 + x2 - 1,
family = quasi(link=inverse, variance=constant),
data = biochem)
11.7.

(glm).
.
R - optim(), nlm() nlminb(), (
) S- ms() nlminb().
, -- ,
68

.
, , ,
.
,
.
11.7.1.

(SSE) . ,
, , .
& (1988). :
> 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)
.
,
, , .
> 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), :
> out <- nlm(fn, p = c(200, 0.1), hessian = TRUE)
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, ,
:
> df <- data.frame(x=x, y=y)
> fit <- nls(y ~ SSmicmen(x, Vm, K), df)
> fit
Nonlinear regression model
69

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.
- ,
, . ,
, ,
.
(1990), 108-111.
, glm().
:
> 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)) ))
:
> out <- nlm(fn, p = c(-50,20), hessian = TRUE)
out$minimum
, out$estimate
. SE, :
> sqrt(diag(solve(out$hessian)))
95% SE 1.96

11.8.
,
R, .

. nlme
lme() nlme()
70

, ,

.
.
. loess()

.

.
loess() stats
.
() .
,
. lqs MASS
.
, ,
, rlm MASS.
.

, , .
avas ace acepack bruto mars
mda
R. ,
gam mgcv.
.
,

, , ,
, ,
, .
,
.
.
tree(),
, plot() text(),
.
R rpart tree.

71

12.

-
R.
, .
,
,
. ,
R ,
.
, , X11() UNIX, windows()
Windows quartz() OS X.
dev.new().
R

.
:


, , ,
.

,
, .

, ,
, , .

, R ,
.
, '' .
grid -
, . lattice,
grid
, Trellis S.
12.1.

, .
, ( ).
,
.
12.1.1. plot()
R - plot().
:
.
plot(x, y)
plot(xy)

x y - , plot(x, y) y x.
,
( ) ,
x y .
72

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 - ,
+ (, + b + c).

( ) (
). y ,
expr.
12.1.2.
R
. X , :
> pairs(X)
, X,
, X X
n (n - 1)
.
coplot
. a b - , c -
( ), :
> coplot(a ~ b | c)
b c. c , , b
c. c ,
b c
. given.values =
, coplot() - co.intervals() .
, :
> coplot(a ~ b | c + d)
b
c d.
coplot() pairs() panel=,
, .
points() ,
x y
panel =, , .
panel coplots panel.smooth().
73

12.1.3.

. :
qqnorm(x)
qqline(x)
qqplot(x, y)

-.

( ,
),
,
. y
.

hist(x)
hist(x, nclass=n)
hist(x, breaks=b, ...) x.
,
nclass = . ,
breaks = .
probability=TRUE,
,
.
dotchart(x, ...)

x.
Y x, X
. ,
,
.

image(x, y, z, ...)
contour(x, y, z, ...)
persp(x, y, z, ...)
. image
,
z, contour
z, persp 3D .
12.1.4.
,
, :
add=TRUE


,
( ).

axes=FALSE -
axes(). , axes=TRUE
.
log="x"
log="y"
log="xy"

, .
.
74

type= ,
:

type=

type=p

( )

type=l

type=b

type=o

type=h


( )

type=s
type=S

.
, .

type=n

. (
),
.

xlab=string
ylab=string

x y.
, ,
.

main=string .
sub=string

12.2.
.
,
( , )
.
:
points(x, y)
lines(x, y)


. plot() type =
( "p" points() "l"
lines()).

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(),
,
.
75

abline(a, b)
abline(h=y)
abline(v=x)
abline(lm.obj)

b
. h=y y ,
v=x x- .
lm.obj
2 ( ),

.

polygon(x, y, ...)

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

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


. , ,
..,
. , v (
)

:

legend( , fill=v)

legend( , cov=v)

legend( , lty=v)

legend( , lwd=v)

legend( , pch=v)

( )

title(main, sub)


, () sub
.

axis(side, ...)

,
(1 - 4,
.)
,
.
plot()
axes=FALSE.


(, y)
.
,
.
x y ,
x y. -
. , , locator() (. )
.
76

12.2.1

. R text, mtext,
axis title, . ,
:
> text(x, y, expression(paste(bgroup("(", atop(n, x), ")"), p^x, q^{n-x})))
,
, R, :
> help(plotmath)
> example(plotmath)
> demo(plotmath)
12.2.2
,
.
:


/ .
, , ,
. ,
.
(
) .

, ,
R :
> help(Hershey)
> demo(Hershey)
> help(Japanese)
> demo(Japanese)
12.3.
R ,
.
locator():
locator(n, type)

, .
, n ( 512) ,
. type
,
; : . locator()
,
x y.
locator() .
,
, , .
, ,

77

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


. locator() , ,
postscript .
identify(x, y, labels)
,
x y ( )
( ,
).
.
,
. ,
, .
(x; y) x y
identify() :
> plot(x, y)
> identify(x, y)
identify() ,

. ,
( , x/y) .
, (
) labels identify(),
plot = FALSE. (.
), identify() ;
x y.
12.4.
, , R
, .
, . R
, ,
, ,
. ( 'col' )
( , .)

,
. :
, ,
; ,
.
12.4.1. : par()
par()
.
par()

,
.

par(c("col", "lty"))
,
(, .)
78

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

:
> oldpar <- par(no.readonly=TRUE)
. . . . . .
> par(oldpar)
12.4.2. :
()
. ,
par(), ,
. :
> plot(x, y, pch="+")
, "" ,
.
, ,
, par().
12.5.

. R
par() ; .
:
name=value
. name
, ,
79

par() . value - ,
.
, ,
plot: . xaxt yaxt.
12.5.1.
R , , (
).
, :
pch="+"

, .
, ,
, .

, "." ,
.

pch=4

pch 0 25,
. ,

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


21 25 ,
-: . points
.
, pch
32:255 .

lty=2

.
(
), 1 , 0
, 2 -
, .

lwd=2

. ""
. ,
lines(), .. ,
, .

col=2

, , , ,
.
(.?palette) .

col.axis
col.lab
col.main
col.sub

,
y, , .

font=2

, , .
, , 1
, 2
, 3
, 4 5
( ).
80

font.axis

font.lab
font.main
font.sub

,
y , .

adj=-0.1

. 0
, 1 0.5

. - ,
, ,
-0.1 10%
.

cex=1.5

. -
( )
.

cex.axis
cex.lab
cex.main
cex.sub

,
y ,
.

12.5.2.
R ,
axis().
: (
lty), ( )
( .)
.
lab=c(5, 7, 12)
- x y,
. -
( .)
,
!
las=1

. 0 , 1
2
.

mgp=c(3, 1, 0)
. -
.
- ,
- (
).

,
- .
tck=0.01

, . tck
( 0.5), x y
81

. 1
.
. tck=0.01 mgp=c (1,-1.5,0)
.
xaxs="r"
yaxs="i"

x y, . "i" ()
"r" ( )
, "r"
. (S ,
R).

12.5.3.
R figure ,
( , , ..) ()
.

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

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

mai, ,
.

mar mai ,
. , ,
; , ,
. ,
. ,
: , postscript() height=4
, 50%- ,
82

mar mai . (. ),
, , , ,
.
12.5.4.
R n m .
,
, .

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

.
- , - .
,
mfcol ;
mfrow .
figure
mfrow=c(3,2);
.

(
par("cex") pointsize ).

0.83:
, 0.66.

mfg=c(2, 2, 3, 2)


.
;
83

.
.

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

.
, ,
,
, .

.
.
,
new=TRUE ( S).

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


. mar mai,
, - ,
, .
..

mtext() outer=TRUE.
,
oma omi.

split.screen() layout(), grid lattice.
12.6.
R ( )
. R
, . .
R
( , ) ,
.
.
:
help(Devices)) . , :
> postscript()
PostScript.
:
X11()

X11 Unix-

windows()

Windows

quartz()

OS X

postscript()

PostScript,
PostScript.

pdf()

PDF,
PDF.
84

png()

PNG. ( . .
).

jpeg()

JPEG,
. ( . . ).


:
> dev.off()
, ; ,

. ( ).
12.6.1. PostScript
postscript(),
PostScript .
, horizontal=FALSE,
width height (
.) , :
> postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)
, PostScript ,
. , , ,
, , . ,
R.
PostScript .
, PostScript: R
,
onefile=FALSE. - S-:
, ( EPSF
). ,
- :
> 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() [OS X]
postscript()
85

pdf()
png()
jpeg()
tiff()
bitmap()

, .
,
. (
).
dev.list()


. 1
null ,
.

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. -
, postscript,

,
`...'. dev.print ,
,
, ,
.
graphics.off()

,
null .

12.7.
R ,
, " " (
) . ,
GGobi Swayne, Cook Buja, :
http://www.ggobi.org/
R rggobi,
http://www.ggobi.org/ rggobi.

86

, rgl 3-D
, .

87

13.

R . ,
, . (
, ),
,
. , "
R . , .
, :
> library()
. (, ,
Davison & Hinkley (1997)), :
> library(boot)
, ,
install.packages() update.packages() ( Packages Windows GUI,
. R)
.
, :
> search()
. ,
(. 13.3 [ ]):
, :
> loadedNamespaces ()
,
:
> help.start ()
HTML,
.
13.1.
( base) R.
, R ,
,
. R. .
R FAQ R .
13.2. CRAN
R,
. -
, ,
, .
( ) R.
CRAN (http://CRAN.R -project.org/
) , (http://www.bioconductor.org/)
Omegahat (http://www.omegahat.org/). FAQ R CRAN,
, .

88

13.3.
, base
.
: ,
, ,
, ( ) ,
,
.
, t() R,
, t.
, ,
, .
, .
:: .
, base :: t,
base. ,
.
:::
R: ,
. , ,
getAnywhere(), .
,
. , ,
.
.
13.4.
: http://cran.r-project.org/web/views/TimeSeries.html
.
R
. CRAN,
.
.
. ,
, , .
13.4.1. - Basics
: R
. - "ts",

( ). ,
, , ..
:
ARIMA arima(), AR(p) VAR(p) ar(),
StructTS(), plot(), ()
acf() pacf(), decompose(), STL stl(),
filter(),
Holt-Winters HoltWinters().

89

13.4.2. -Times and Dates


"ts" ,
/ .
R: R
R 4 (1), 29-32.
"yearmon" "yearqtr" zoo
, .
"Date"
. 01.01.1970.
chron dates(), hours() / ().
. "chron"
01.01.1970.
"POSIXct" "POSIXlt" POSIX - ()
/, .
,
. "POSIXct" - ,
01.01.1970 GMT 0:00:00. lubridate ,
POSIXbased.
"timeDate" timeDate (: fCalendar).
/
" ". ,
"POSIXct" GMT.
, ,
.
tis "ti" /.
"mondate" mondate
.
rtv ,
.

TSAgg .
tempdisagg
.

13.4.3. - Time Series Classes


, "ts" -
, .
zoo
,
( , ).
, "ts". "zoo"
, .
xts zoo
R.
,
"POSIXct",
. "irts" tseries "fts" fts.
90

"timeSeries" timeSeries (: fSeries)


"timeDate" .
"tis" tis "ti" .
tframe
.
13.4.4. -Forecasting and Univariate
Modeling
forecast
, ,
, .
: HoltWinters()
, ets() forecast
.
: ar() ( ), FitAR
AR, pear
.
ARIMA:
arima() ARIMA, SARIMA,
ARIMAX, ARIMA.
auto.arima() .
arma() tseries
ARMA ;
FitARMA MLE ARMA.
ARFIMA
fracdiff. afmtools ,
ARFIMA. armaFit() fArma
ARIMA ARFIMA. gsarima
SARIMA. mar1s
AR(1) .
GARCH:
garch() tseries GARCH, garchFit() fGarch
ARIMA GARCH.
bayesGARCH GARCH(1,1) t .
gogarch GARCH
(GOGARCH).
R-Forge
rgarch
GARCH ,
GARCH. -
.
: ltsa , dlm
, timsac
, BootPR
.
13.4.5. - Resampling
: boot tsboot()
, .
91

tsbootstrap() tseries .
meboot.
13.4.6. - Decomposition and Filtering
: filters()
.
robfilter ,
mFilter ,
.
: decomposition(),
- stl(),
.
:
, .
, (2000), wmtsa.
rwt, waveslim wavethresh.
:
signalextraction (
). bspec
. kza -Zurbenko
, , KZ.
quantspec
.
Rssa
.
13.4.7. , - Stationarity, Unit
Roots, and Cointegration
: tseries
dickey-fuller, PhillipsPerron, KPSS. ADF KPSS urca
, , -, - -. fUnitRoots
.
CADFtest ADF
ADF (CADF).
: -
- tseries urca.
.
13.4.8. -Nonlinear Time Series Analysis
:
tsDyn, AR, , SETAR LSTAR.
bentcableAR Bent-Cable. BAYSTAR
.
TISEAN
. RTisean R ,
tseriesChaos R .
: fNonlinear.
92

13.4.9. - Dynamic Regression Models


:
OLS, dynlm;
,
, dyn. tslars
,
LARS.
dse.
dlm ( ,
/ Bayesian).

tpr .
dlnm .
13.4.10. - Multivariate Time Series
Models
(VAR) ar()
, AIC.
. , VAR
mAr, VAR .
vars , estVARXls()
dse, MSBVAR. fastVAR
, VAR (
).
VARIMA dse .
EvalEst -,
.
urca vars,
.
tsfa.
FKF
( ).
fkf() :
, .
KFAS,
, , .
dlm ,
,
.
MARSS

, EM.

.
-
,
. pomp.
13.4.11. - Continuous time models
cts.
93

ARMA ctarma.
13.4.12. - Time Series Data
Data from Makridakis, Wheelwright and Hyndman (1998) Forecasting: methods and
applications are provided in the fma package.
Data from Hyndman, Koehler, Ord and Snyder (2008) Forecasting with exponential
smoothing are in the expsmooth package.
Data from the M-competition and M3-competition are provided in the Mcomp
package.
Data from Tsay (2005) Analysis of financial time series are in the FinTS package,
along with some functions and script files required to work some of the examples.
TSdbi provides a common interface to time series databases.
fame provides an interface for FAME time series databases
VhayuR provides an interface to he Vhayu Velocity time series database
AER and Ecdat both contain many data sets (including time series data) from many
econometrics text books
13.4.13. - Miscellaneous
dtw:
.
fractal: .
fractalrock:
.

GeneCycle GeneNet: .
pastecs: , - .
ptw: , .
sde: .
tiger:
() .
wavethresh:
( ,
).
13.5. :

AER

afmtools

bayesGARCH

BAYSTAR

bentcableAR

boot

BootPR

bspec

CADFtest

chron

ctarma

cts

deseasonalize

dlm

dlnm

94

dse

dtw

dynlm

EvalEst

dyn

Ecdat

ensembleBMA

expsmooth

fame

fArma

fastVAR

fGarch

FinTS

FitAR

FitARMA

FKF

fma

fNonlinear

forecast (core)

fracdiff

fractal

fractalrock

fts

fUnitRoots

GeneCycle

GeneNet

gogarch

gsarima

hydroGOF

hydroTSM

Interpol.T

its

KFAS

kza

ltsa

lubridate

mAr

mar1s

MARSS

Mcomp

meboot

mFilter

mondate

MSBVAR

paleoTS

pastecs

pear

pomp

ptw

quantspec

RMAWGEN

robfilter

RSEIS

Rssa

RTisean

rtv

rwt

sde

season

signalextraction

tempdisagg

tiger

timeDate

timeSeries

timsac

tis

tpr

TSAgg

TSdbi

tsDyn

tseries (core)

tseriesChaos
95

tsfa

tslars

tsModel

urca

vars

VhayuR

wavelets

waveslim

wavethresh

wmtsa

wq

xts

zoo (core)

96

A.

R
.
, .
R (. B [
R]).
R .
( R
).
help.start()
HTML (
).
.
.
<- rnorm(50)
y <- rnorm()
- .
.
.
ls()
R ,
R.
rm(, )
. ().
<- 1:20
= (1, 2, ..., 20).
w <- 1 + sqrt()/2
"" .
dummy <- data.frame(x=x, y= x + rnorm(x)*w) dummy

.
plot(, )

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



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

plot(x, y)


.

.
.

lines(x, lrf$y)

abline(0, 1, lty=3)

: ( 0,
1).
.

attach(dummy)
lrf <- lowess(x, y)

abline(coef(fm))

97

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


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

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

, . (
).
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
Michaelson Morley,
, . ,
, (
Expt), 20 ( Run), sl
.
mm$Expt <- factor(mm$Expt) mm$Run <- factor(mm$Run)
Expt Run .
3 (
).
plot(Expt, Speed, main="Speed of Light Data", xlab="Experiment No.")

.
attach(mm)

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

98

50 [-pi\, pi]. y
.
f <- outer(x, y, function(x, y) cos(y)/(1 + x^2))
f - , ,
, cos(y)/(1 + x^2).
oldpar <- par(no.readonly = TRUE) par(pty="s")
""
.
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)
-, (
, , )
rm (, , 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)
... .
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)
99

.
.
rm(th, w, z)
.
q()
R . ,
R, , .

100

B. R

R Windows Mac OS X
,
.
.1. R
UNIX Windows, 'R'
R :
R [] [<infile] [> outfile],
R CMD, R (,
R
), "" .
Windows rterm.exe R.
, TMPDIR ,
.
R.
(. ''
,
Windows ).

--no-environ R
.
R_ENVIRON; ,

R_HOME/etc/renviron.site ( ).
R_ENVIRON_USER,
; .Renviron
( ).
name=value. . ""
. , ,
R_PAPERSIZE ( ), R_PRINTCMD (
) R_LIBS (
R ).
R ,
--no-site-file.
R_PROFILE. ,
R_HOME/etc/rprofile.site,
.
, --no-init-file , R
.
V_PROFILE_USER; ,
.Rprofile
( ).
.RData
, ( --no-restore
--no-restore-data ).
, .First() , .
( .Last(), R)
,
.RData.
101

, , R (.
'' ).
,
, R.
R .
--help
-h
.
--version
.
--encoding=enc
,
stdin. , iconv: .
. (--encoding=enc .)
R
(,
c , UTF-8).
RHOME

" " R
. ,
R ( , , ..)
.
--save
--no-save
,
R. ,
q();

(. ).
--no-environ
-
--no-site-file
.
--no-init-file
.
--restore
--no-restore
--no-restore-data
,
( .RData , R ).
. --no-restore
--no-restore-*.
--no-restore-history
102

,
. .Rhistory , R
, R_HISTFILE
.
--no-rconsole
( Windows). rconsole .
--vanilla
--no-save, --no-environ, --no-site-file, --no-init-file -no-restore. Windows --no-rconsole.
-f file
--file=file
( rgui.exe). : - stdin. -no-save, --save.
-e expression
( rgui.exe). expression .
-e, -f --file.
--no-save, --save. 10
000 , .
, ,
.
--max-ppsize=N
N
. 10000, ,
.
100000.
--max-mem-size=N
(Windows ). ,
R .
RAM 32 R - 1.5Gb, 32 ,
Windows.
--quiet
--silent
-q

--slave
R .
, R,
. --quiet --nosave.
--ess
(Windows ). RteRm R-inferior-mode
ESS,
.
--args
103


: ,
commandArgs(TRUE).
,
( < >), 4095 , .

(stderr).
R CMD ,
R, "" . :
R CMD command args
command , args ,
.
:
BATCH

R . R --restore --save
(
?BATCH).

SHLIB

INSTALL

REMOVE

build

(.. ) .

check

RpRof

R.

rdconv
rd2txt

rd , HTML,
LATEX, , . rd2txt
rd2conv -t txt.

rd2pdf

rd PDF.

Stangle

S/R c Sweaven

Sweave

Sweave

rdiff

config

open

( Windows)
Windows

texify

( Windows) (La)TeX files


R

:
R CMD command help
,
R CMD.
104

, -arch =, --no-environ, --no-init-file, -no-site-file and --vanilla R CMD: R,


. ( --vanilla --no-environ --no-site-file -no-init-file.).) , R CMD
R ( , renviron), R,
, ( BATCH) --no-restore.
--vanilla R:
- INSTALL, REMOVE, Sweave SHLIB ( --no-site-file --noinit-file).
R CMD cmd args
cmd
: , R ,
, , ldd pdflatex. Windows cmd
,
.sh .pl ( ).
.2. R Windows
R Windows.
(, cmd.exe ), ,
, R.exe Rterm.exe.
(Rgui.exe) GUI.
Windows UNIX, '
' , Windows.
R_USER , . ,
HOME , .
R ,
. Windows
"" ( C:\Documents and Settings\user name\My Documents
Windows XP). , HOMEDRIVE
HOMEPATH ( ), .
.
, TMPDIR, TMP
TEMP
.
name=value
.
, .RData, ( )
, :
--restore
. --
RGui.exe, Rterm.exe.
, , .

RGui.exe.
--mdi
--sdi
--no-mdi

105

, Rgui MDI (
)
SDI (
, ).
Rconsole .
--debug
Break to debugger Rgui,
.
Windows R CMD
.bat, .exe, .sh .pl. (Perl
.pl) ,
R_HOME, R_OSTYPE, , BSTINPUTS TEXINPUTS. ,
latex.exe , :
R CMD latex.exe mydoc
LATEX mydoc.tex, R share/texmf,
TEXINPUTS. MiKTeX
LATEX, R CMD texify mydoc .
.3. R OS X
R OS X. Terminal.app
R , .
GUI (R.app),
. - OS X.
OS X UNIX. ' ' -
, R.framework,
,
, GUI.
.4. R
R foo.R ,
, R CMD BATCH foo.R.

, : Unix
R CMD BATCH foo. R & 0 .

: (
):
R CMD BATCH "--args arg1 arg2" foo.R&
,
args <-commandArgs (TRUE)
Rscript, :
Rscript foo. rarg1 arg2
, (
, Unix- , Windows)
#! /path/to/rscript
args <- commandArgs(TRUE)
...
106

q(status=<exit status code>)


RUnfoo,
(chmod 755 RUnfoo),
runfoo arg1 arg2
help("rscript"). R
stdout stderr, ,
.
rscript,
( R Windows, ,
Mac OS X, , /usr/local/bin ):
#!/usr/bin/env rscript
...
, Bourne #!
, #!/usr/bin/env rscript --vanilla.
, stdin(). ,
R :
chem <- scan(n=24)
2.90 3.10 3.40 3.40 3.70 3.70 2.80 2.50 2.40 2.40 2.70 2.20
5.28 3.37 3.03 3.03 28.95 3.77 3.40 2.20 3.50 3.60 3.70 3.70
stdin() ,
. stdin, "stdin"
file, , scan("stdin"...).
( Francois
Pinard) , :
#!/bin/sh
[environment variables can be set here]
R --slave [otheroptions] <<EOF
rprogram goes here...
EOF
stdin() , "stdin" .
Rscript
-e.
, Unix- ( foo.R),
, .

107

C.
R:

R
R

RGui.exe R,
,
R.
.
.
R
.
R. ,
, .

.

.
, .
Notepad++,
, R.
R.

108

F.

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 TrevorJ. 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 TheirApplications,
Cambridge University Press.
Annette J. Dobson (1990), An Introduction to Generalized LinearModels, Chapman
and Hall, London.
PeterMcCullagh and John A. Nelder(1989), Generalized LinearModels. 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.

109

Вам также может понравиться