8 8¹ ¹

advertisement
8¹
1 SŠ
Šó 0 1.1 S¯„\€ . . . . . . . . . . .
1.2 S•þ . . . . . . . . . . . . .
1.2.1 ~þ . . . . . . . . . .
1.2.2 •þ(Vector)†DŠ .
1.2.3 •þ$Ž . . . . . . .
1.2.4 )k5Æê . .
1.2.5 Ü6•þ . . . . . . .
1.2.6 iÎ.•þ . . . . . .
1.2.7 Eê•þ . . . . . . .
1.2.8 •þeI$Ž . . . . .
1.3 Ý
†õ‘ê| . . . . . . . .
1.3.1 Ý
. . . . . . . . . .
1.3.2 –¯Ý
ƒÚfÝ
1.3.3 Ý
$Ž . . . . . . .
1.3.4 apply¼ê . . . . . . .
1.3.5 ê|(array) . . . . . .
1.3.6 ê|È . . . . . .
1.4 Ïf . . . . . . . . . . . . . .
1.5 L(list) . . . . . . . . . . .
1.5.1 L½Â . . . . . . .
1.5.2 ?UL . . . . . . .
1.5.3 A‡ˆ£L¼ê .
1.6 êâµ(data.frame) . . . . . .
1.6.1 êâµ)¤ . . . . . .
1.6.2 êâµÚ^ . . . . . .
1.6.3 attach()¼ê . . . . .
1.7 Ñ\ÑÑ . . . . . . . . . . .
1.7.1 ÑÑ . . . . . . . . . .
1.7.2 Ñ\ . . . . . . . . . .
1.8 §S››( . . . . . . . . .
1.8.1 ©|( . . . . . . .
1.8.2 ̂( . . . . . . .
1.9 S§SO . . . . . . . . . . .
1.9.1 óŠ˜m+n . . . . .
1.9.2 ¼ê½Â . . . . . . .
1.9.3 ëê(gCþ) . . . . .
1.9.4 Š^ . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
i
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
4
5
5
5
6
7
8
9
10
12
12
13
15
16
16
17
19
21
21
22
23
24
24
24
25
25
25
27
28
28
29
31
31
32
33
33
ii
8¹
1.10
1.11
1.12
1.13
1.14
1.15
1.9.5 §SNÁ . . . . . . . . .
1.9.6 §SOÞ~ . . . . . . .
ã/ . . . . . . . . . . . . . . . .
1.10.1 ~^ã/ . . . . . . . . .
1.10.2 p?ã/¼ê . . . . . . .
1.10.3 p?ã/¼ê~^À‘
1.10.4 $?ã/¼ê . . . . . . .
1.10.5 pã/¼ê . . . . . . .
1.10.6 ã/ëê¦^ . . . . .
1.10.7 ã/ëê) . . . . . . .
1.10.8 ã/ . . . . . . . . .
SÐÚO . . . . . . . . . . . . .
1.11.1 üCþêâ©Û . . . . .
1.11.2 bu . . . . . . . . .
SÚO.{0 . . . . . . . . . .
1.12.1 ÚO.L« . . . . .
1.12.2 ‚5£8. . . . . . . .
1.12.3 J&EÏ^¼ê . . .
1.12.4 ©Û . . . . . . . . .
ÚO©Û¢~ . . . . . . . . . . .
1.13.1 êâÑ\ . . . . . . . . .
1.13.2 &¢5êâ©Û(EDA) . .
1.13.3 |m' . . . . . . . . .
1.13.4 £8©Û . . . . . . . . .
^SŠ‘Å[OŽ . . . . . . . .
S~^¼êë . . . . . . . . . .
1.15.1 Ä . . . . . . . . . . . .
1.15.2 êÆ . . . . . . . . . . . .
1.15.3 §SO . . . . . . . . .
1.15.4 ÚOOŽ . . . . . . . . .
ÚO^‡§
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
34
35
39
39
43
44
45
45
47
49
53
54
54
56
58
58
59
60
61
61
62
62
64
65
68
70
70
71
72
73
1 ˜Ù SŠ
Šó 0 SŠó´d¢¿(y38áuKՉE) mu˜«^5?1êâ&
¢!ÚO©Û!Šã)º.Šó"§´Lêâa.(•þ!ê|!L!
é–)AOk|u¢y#ÚOŽ{, Ùpª$1ª9rŒã/9p
ã/õU¦·‚Œ±B/&¢êâ"
8cSŠó¢y‡Ì‡´S-PLUS"§ÄuSŠó, ¿dInsightfulúi?
˜Ú*¿Úõ"ŠÚOÆ[9˜„ïÄ<
Ï^{óä‡, SrNêâ
ŒÀz!êâ&¢!ÚOï!ÚO{?§§±9Œ*Ð5"
S-PLUSŒ±†^5?1IOÚO©Û¤I(J, ´§Ì‡
A:´§Œ±p/lˆ‡¡uyêâ¥&E, ¿Œ±éN´/¢y˜
‡#ÚO{"
S-PLUSk‡Å‡ÚóŠÕ‡, §´˜‡û’^‡"AucklandŒÆRobert
Gentleman ÚRoss Ihaka 9Ù¦“<
mu
˜‡RXÚ, ي{/ª†SŠ
óăÓ, ¢yØÓ, ü«Šó§Sk˜½oN5"R´˜‡GPLgd
^‡, y3‡´2.2.1‡, 3A¤köŠXÚь±"R'S-PLUS„
NõõU, ®²äk
ér¢^5"R?§OŽ„ݲ~'S-PLUS¯"
·‚3ùp¦þ0S-PLUSÚRÑU¦^õU, …±RÌ"e¡·‚
^SÚ¡S-PLUSÚR"
1.1
S¯
¯„ \ €
SXÚÄ.¡´˜‡pª·-I, ·-J«Î´˜‡ŒuÒ, ·-
(Jêþw«3·-e¡"S·-̇kü«/ªµLˆª½DŠ$Ž(^<-L
«)"3·-J«Î …\˜‡LˆªL«OŽdLˆª¿w«(J"DŠ$Ž
rDŠÒm>ŠOŽÑ5D‰†>Cþ"Œ±^•þ1I…5飱c$
1·-2g$1½?U 2$1"
S´«©Œ, ¤±xÚX´ØÓ¶i"
·‚^˜
~f5wSA:"b·‚®²?\
R(½S-PLUS)pª
I"Ñ\±eŠé(ŒuÒ´XÚJ«,Ø´gCÑ\)µ
>
>
>
>
x1 <- 0:100
x2 <- x1*2*pi/100
y <- sin(x2)
plot(x,y, type=’l’)
ù
ŠéŒ±±›u­‚ã"Ù¥, “<-”´DŠ$ŽÎ"0:100L«˜‡
l0100 ê•þ"l1‡ŠéŒ±wÑ, ·‚Œ±é•þ†?1
oK$Ž, OŽx2´•þx1¤kƒ¦±~ê2*pi/100(J"l1
n‡ŠéŒ±w¼êŒ±±•þÑ\, ¿Œ±Ñј‡•þ, (J•þy
z˜‡©þ´gCþx2éA©þu¼êŠ"l ˜‡ŠéŒ±wѼ
1
2
1˜Ù SŠó0
êN^égd, Œ±U ˜‰ÑgCþ, Œ±^“gCþ¶=”/ª½
gCþŠ, ùŒ±¦^"ŽŠ"
e¡·‚w˜wSÚOõU"
> marks <- c(10, 6, 4, 7, 8)
> mean(marks)
[1] 7
> sd(marks)
[1] 2.236068
> median(marks)
[1] 7
> min(marks)
[1] 4
> max(marks)
[1] 10
> boxplot(marks)
>
1˜‡ŠéÑ\eZê☇•þ, ¼êc()^5rêâ|܏˜‡•þ"
¡^
A‡¼ê5OŽêâþŠ!IO!¥ ê!Š!ŒŠ"
 ¼ê±›êâÝ/ã"~¥sd()´R¥âk¼ê, 3S-PLUS¥‡
^sqrt(var())5OŽ"3S·-ª¥‡w«˜‡LˆªŠ‡…\§"

ü«S£8OŽ, ·‚rSAS¥~fêâSASUSER. CLASS ÑÑ
˜‡©©‡CLASS. TXT, SAS§SXeµ
data _null_;
set sasuser.class;
file ’class.txt’;
put name sex age height weight;
run;
·‚3R¥rù‡©‡Ö\˜‡êâµ(data frame, ƒuSAS¥ê
â8), ¿?1£8, ±›êâÑ:ãÚ£8†‚"§S9(JXe, 5¿11
\Ò´XډÑY1J«"
> cl <- read.table("c:/work/class.txt",
+ col.names=c("Name", "Sex", "Age", "Height", "Weight"))
> cl
Name Sex Age Height Weight
1
Alice
F 13
56.5
84.0
2
Becka
F 13
65.3
98.0
3
Gail
F 14
64.3
90.0
4
Karen
F 12
56.3
77.0
5
Kathy
F 12
59.8
84.5
6
Mary
F 15
66.5 112.0
7
Sandy
F 11
51.3
50.5
8
Sharon
F 15
62.5 112.5
9
Tammy
F 14
62.8 102.5
10 Alfred
M 14
69.0 112.5
11
Duke
M 14
63.5 102.5
12
Guido
M 15
67.0 133.0
13
James
M 12
57.3
83.0
14 Jeffrey
M 13
62.5
84.0
15
John
M 12
59.0
99.5
16 Philip
M 16
72.0 150.0
ÚO^‡§
3
§1.1 S¯„\€
140
●
●
120
●
●
●
● ●
100
Weight
●
●
●
●
80
● ●●
●
●
60
●
●
55
60
65
70
Height
ã 1.1: ^R)£8ã/
17 Robert
18 Thomas
19 William
>
>
>
>
M
M
M
12
11
15
64.8
57.5
66.5
128.0
85.0
112.0
attach(cl)
plot(Height, Weight)
fit1 <- lm(Weight ~ Height)
fit1
Call:
lm(formula = Weight ~ Height)
Coefficients:
(Intercept)
-143.027
Height
3.899
> abline(fit1)
>
(JŒ±)ã1.1"·‚lù‡~fŒ±w, ©Ûd¼ê¤, (J´é
–, Œ±Š?˜Ú©ÛÑ\"
S-PLUSJø
˜„OŽõU"'X, ¦˜‡Ý
_µ
> A <- matrix(c(1,2, 7,3), ncol=2, byrow=T)
> A
[,1] [,2]
[1,]
1
2
[2,]
7
3
> Ai <- solve(A)
> Ai
[,1]
[,2]
[1,] -0.2727273 0.18181818
[2,] 0.6363636 -0.09090909
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
4
1˜Ù SŠó0
„Œ±?1Ý
$Ž, Xµ
> b <- c(2,3)
> x <- Ai %*% b
> x
[,1]
[1,] -2.220446e-16
[2,] 1.000000e+00
ù¢Sþ´)
˜‡‚5§|"“%*%”L«Ý
¦{"
Œ±reZ1·-3˜‡©©‡('XC:\work\myprog.r)¥, ,
source ¼ê5$1‡©‡µ
^
> source("C:\\work\\myprog.r")
5¿iÎG¥‡m‡¤ü‡"R¥#N^mŠ´»©…Î:
> source("C:/work/myprog.r")
^sink()¼êŒ±r±
Xµ
ÑÑl¶4I=•˜‡Ü©©‡, ~
> sink("C:/work/myprog.out")
‡¡EÑѶ4I, ¦^µ
> sink()
3S¥Œ±^“?”Ò
¡‹‡Î¼ê5w«Ï&E, ~X
> ?c
> ?"=="
(ÎAÏÎ҇^§Ò)å5)",, 3MS Windows‡S-PLUS¥‘kWindows
Ï©‡, 3R¥Œ±^HTMLèAì5ΐÏ(^“help.start()¼ê½Help
- R language (html)”éÄ)"^ÐSŠó­‡˜:Ò´‡^ÙXڐÏõU,
ϏSá½ÃþÑØUѤk[!, XJѤk[!KLu„¡
"
‡òÑS-PLUS½R, Œ±^q()¼ê, Œ±^èü·-"R3òўJ¯´
ÄcóŠ˜m, §Œ±rc½Â¤ké–(k¶i•þ!Ý
!
L!¼ê)˜‡©‡"S-PLUSŒ±ÀJ=
é–"
1.2
S•
•þ
SŠó´Äu閊ó, ØL§Äêℴ˜
ýk½ÂÐê
âa., X•þ!Ý
!L(list)"E,êâ^é–L«, 'X, êâµ
é–, žmSé–, .é–, ã/é–, "
SŠóLˆªŒ±¦^~þÚCþ"Cþ¶5K´µdi1!êi!é:
|¤, 1˜‡iÎ7L´i1½é:, Ývk›"Œ´ØÓ"AO‡
5¿é:Œ±Š¶iÜ{Ü©, 3Ù§¡•é–Šó¥é:²~^5©
…é–†¤
¶",, SŠó¥ey‚Ø#N^3¶i¥, y3R‡#
N¶i¥key‚"
ÚO^‡§
5
§1.2 S•þ
1.2.1
~þ
~þŒ±<Ú/©Ü6.!êŠ.ÚiÎ.n«, ¢SþêŠ.êâq
Œ±©.!ü°Ý!V°Ý, šAÏI‡Ø7'%ÙäNa."~X,
123, 123.45, 1.2345e30´êŠ.~þ, ”Weight”, ”o²”´iÎ.(^ü‡V§Ò
½ü‡ü§ÒŒ)"Ü6ýŠTRUE(5¿«©Œ, truev¿Â), Ü
6bŠFALSE"Eê~þÒ^3.5 − 2.1iù{L«"
S¥ê⌱"”Š, ^ÎÒNA“L"”Š"¼êis.na(x)ˆ£x´Ä"
”Š(ý„´b)"
1.2.2
• þ (Vector)†
†D Š
•þ´äkƒÓÄa.ƒS, ŒNƒuÙ¦Šó¥˜‘ê|"
¢Sþ3S¥IþwŠ´ݏ1•þ"
½Â•þ~^{´¦^¼êc(), §reZ‡êŠ½iÎG|܏˜‡
•þ, 'Xµ
> marks <- c(10, 6, 4, 7, 8)
> x <- c(1:3, 10:13)
> x
[1] 1 2 3 10 11 12 13
> x1 <- c(1, 2)
> x2 <- c(3, 4)
> x <- c(x1, x2)
> x
[1] 1 2 3 4
3w«•þŠž·‚5¿†>oÑy˜‡“[1]”, ù´“LTw«11˜
‡êeI, ~Xµ
> 1234501:1234520
[1] 1234501 1234502 1234503 1234504 1234505 1234506 1234507 1234508 1234509
[10] 1234510 1234511 1234512 1234513 1234514 1234515 1234516 1234517 1234518
[19] 1234519 1234520
11ÑÑl110‡êm©, ¤±31†>w«“[10]”"
S¥^ÎÒ“<-”(ù´uÒ;˜‡~Ò)5CþDŠ:
> x1 <- c(1, 2)
¼êlength(x)Œ±OŽ•þxÝ"
1.2.3
•þ$Ž
Œ±é•þ?1\(¦)!~(−)!¦(*)!Ø(/)!¦(^)$Ž, Ù¹¿´é•
þz˜‡ƒ?1$Ž"~Xµ
> x <- c(1, 4, 6.25)
> y <- x*2+1
> y
[1] 3.0 9.0 13.5
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
6
1˜Ù SŠó0
,, %/%L«êØ{('X5 %/% 31), %%L«¦{ê(X5 %% 32)"
ü‡•þƒm\!~!¦!Ø!¦$Ž´éAƒmoK$
Ž§X:
> c(1,2,3) + c(10,20,30) / c(2,4,6)
[1] 6 7 8
ü‡ÝØӕþŠ$Žž§Ýᘇò̂¦^§X:
> c(100,200) + c(1,2,3,4)
[1] 101 202 103 204
Œ±^•þŠ¼êgCþ, sqrt!log!exp!sin!cos!tan¼êь
±^•þŠgCþ, (J´é•þz˜‡ƒƒA¼êŠ, Xµ
> sqrt(x)
[1] 1.0 2.0 2.5
¼êminÚmax©OgCþ•þŠÚŒŠ, ¼êsumOŽgCþ
•þƒÚ, ¼êmeanOŽþŠ, ¼êvarOŽ, ¼êsdOŽIO
(3Splus¥^sqrt(var())OŽ), ¼êrangeˆ£¹ü‡Š•þ, 1˜‡Š´
Š, 1‡Š´ŒŠ"~Xµ
> max(x)
[1] 6.25
XJ¦var(x)x´n × pÝ
, K(J
"
sort(x)ˆ£xƒlŒüS(J•þ"order(x)ˆ£¦xlŒ
üƒeI•þ(x[order(x)]usort(x))"
?Ûê†"”Š$Ž(JE"”Š"~X,
> 2*c(1, NA, 2)
[1] 2 NA 4
> sum(c(1, NA, 2))
[1] NA
1.2.4
)k5Æê
N^S¼ênumeric(n)Œ±)W¿
n‡"•þ"3S¥éN´)˜
‡ê"~X, 1:n)l1nê, -2:3)l−2 3ê, 5:2
)‡•êµ
> n <- 5
> 1:n
[1] 1 2 3 4 5
> -2:3
[1] -2 -1 0 1 2 3
> 5:2
[1] 5 4 3 2
‡5¿1:n-1Ø´“L1n-1´•þ1:n~1, ù´˜‡~‹†Øµ
ÚO^‡§
7
§1.2 S•þ
> 1:n-1
[1] 0 1 2 3 4
> 1:(n-1)
[1] 1 2 3 4
seq¼ê´˜„ê¼ê"XJ½˜‡gCþn>0, Kseq(n)ƒ
u1:n"½ü‡gCþž, 1˜gCþ´m©Š, 1gCþ´(åŠ,
Xseq(-2,3)´l-23"ù‡~f‡N
S¼êN^˜
AÏ`³§'XS¼
êN^Œ±¦^ØӇêgCþ, ¼êŒ±éØÓa.gCþ‰ÑØÓ
(J, gCþŒ±^“gCþ¶=gCþŠ”/ª½"~X, seq(-2,3)Œ±
¤seq(from=-2, to=3)"Œ±^˜‡byëꍽêO\Š, ~Xµ
> seq(0, 2, 0.7)
[1] 0.0 0.7 1.4
Œ±¤seq(from=0, to=2, by=0.7)"këê¶ëêgS?¿, Xµ
> seq(0, by=0.7, to=2)
[1] 0.0 0.7 1.4
Œ±^lengthëꍽêÝ, Xseq(from=10, length=5))1014"seq¼
ꄌ±^˜«seq(along=•þ¶)‚ª, ùžU^ù˜‡ëê, )T•þ
eIS, Xµ
> x
[1] 1.00 4.00 6.25
> seq(along=x)
[1] 1 2 3
,˜‡aq¼ê´rep, §Œ±­E1˜‡gCþeZg, ~Xµ
> rep(x,3)
[1] 1.00 4.00 6.25 1.00 4.00 6.25 1.00 4.00 6.25
> rep(c(100, 200), c(3, 2))
[1] 100 100 100 200 200
1˜‡ë궏x, 1‡ë궏times(­Egê)"
1.2.5
Ü6•þ
•þŒ±Ü6Š, Xµ
> l <- c(TRUE, TRUE, FALSE)
> l
[1] TRUE TRUE FALSE
,, Ü6•þ´˜‡'(J, Xµ
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
8
1˜Ù SŠó0
> x
[1] 1.00 4.00 6.25
> l <- x > 3
> l
[1] FALSE TRUE TRUE
˜‡•þ†~þ'Œ, (J„´˜‡•þ, ƒz˜é'(JÜ
6Š"
ü‡•þŒ±', Xµ
> log(10*x)
[1] 2.302585 3.688879 4.135167
> log(10*x) > x
[1] TRUE FALSE FALSE
'$ŽÎ)<, <=, >, >=, ==(ƒ), !=(Ø)"
ü‡Ü6•þŒ±?1†(&)!½(|)$Ž, (J´éAƒ$Ž(J"é
Ü6•þxOŽ!xL«z‡ƒš"~Xµ
> (salaries >= 1000) & (salaries < 8000)
䘇Ü6•þ´ÄяýŠ¼ê´all, Xµ
> all(log(10*x) > x)
[1] FALSE
ä´ÄÙ¥kýŠ¼ê´any, Xµ
> any(log(10*x) > x)
[1] TRUE
¼êis.na(x)^5äxz˜‡ƒ´Ä"”"X
> is.na(c(1, NA, 3))
[1] FALSE TRUE FALSE
Ü6ŠŒ±r›=†êŠ, TRUEC¤1, FALSEC¤0"~X, ·‚
±age>65 Pc<, ÄKc”<, Œ±^c(”young”, ”old”)[(age>65)+1]ù
L«"c#Œu65žage>65uTRUE, \1KrTRUE=†êŠ.1,
(J2, u´ˆ£1‡eI?“old”"ÄKu0+1eI?“young”"
1.2.6
iÎ.•þ
•þƒŒ±iÎGŠ"~Xµ
> c1 <- c("x", "sin(x)")
> c1
[1] "x" "sin(x)"
> ns <- c("Weight", "Height", "c#")
> ns
[1] "Weight" "Height" "c#"
ÚO^‡§
9
§1.2 S•þ
paste¼ê^5r§gCþ뤘‡iÎG, ¥m^˜‚©m, ~Xµ
> paste("My", "Job")
[1] "My Job"
ëgCþŒ±´•þ, ùžˆéAƒëå5, Ý؃Ӟá•þ
­E¦^"gCþŒ±´êŠ•þ, ëžgÄ=†¤·iÎGL«"
~Xµ
> paste(c("X", "Y"), "=", 1:4)
[1] "X = 1" "Y = 2" "X = 3" "Y = 4"
©…^iΌ±^sepëꍽ, ~Xe~)eZ‡©‡¶µ
> paste("result.", 1:5, sep="")
[1] "result.1" "result.2" "result.3" "result.4" "result.5"
XJ‰paste()¼ê½
collapseëê, Kpaste()Œ±r˜‡iÎG•þ
ˆ‡ƒë¤˜‡iÎG, ¥m^collapse½Š©…"'Xpaste(c(’a’,
’b’), collapse=’.’)’a.b’"
Eê•þ
1.2.7
S|±Eê$Ž"Eê~þ‡^–3.5+2.1iù‚ª=Œ"E•þz
˜‡ƒÑ´Eê"Œ±^complex()¼ê)¤E•þ, X‰½¢ÜÚJܕþ
½ÂEê•þµ
>
>
>
>
x <- (0:100)/100*2*pi
y <- sin(x)
z <- complex(re=x, im=y)
plot(z)
Œ±‰½Ú˽ÂEê•þµ
> zz <- complex(mod=1, arg=(0:11)/12*2*pi)
> plot(zz)
Re()OŽ¢Ü, Im()OŽJÜ, Mod()OŽEê, Arg()OŽEêÌ, Conj()O
ŽÝ"ÄêƼê|±Eê$Ž, 'Xµ
> sqrt(-2+0i)
[1] 0+1.414214i
5¿gCþ´Eêžsqrt¼êâ?1Eê$Ž"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
10
1˜Ù SŠó0
1.2.8
•þeI$Ž
SJø
›©(¹–¯•þƒÚ•þf8õU§k$^ù
õUâ
UužSŠó`³"
,˜‡ƒ‡^x[i]‚ª–¯, Ù¥x´˜‡•þ¶, ½˜‡•þŠ
Lˆª, i´˜‡eI, Xµ
> x
[1] 1.00 4.00 6.25
> x[2]
[1] 4
> (c(1, 3, 5) + 5)[2]
[1] 8
Œ±üÕUC˜‡ƒŠ, ~Xµ
> x[2] <- 125
> x
[1] 1.00 125.00 6.25
¯¢þ, SJø
o«{5–¯•þ˜Ü©, ‚ªx[v], x•þ½•þ
ŠLˆª, v´Xe˜«eI•þµ
˜!êŠeI•þ
3x[v]¥, v˜‡•þ, ƒŠ31length(x)ƒm, Š#N­E, ~X,
> x[c(1,3)]
[1] 1.00 6.25
> x[1:2]
[1] 1 125
> x[c(1,3,2,1)]
[1] 1.00 6.25 125.00 1.00
> c("a", "b", "c")[rep(c(2,1,3), 3)]
[1] "b" "a" "c" "b" "a" "c" "b" "a" "c"
!KêŠeI•þ
3x[v]¥, v˜‡•þ, ƒŠ3−length(x)−1ƒm, L«žØƒA ˜ƒ"~Xµ
> x[-(1:2)]
[1] 6.25
n!Ü6ŠeI•þ
3x[v]¥, vÚxÜ6•þ, x[v]L«Ñ¤kvýŠƒ, Xµ
> x
[1] 1.00 125.00 6.25
> x<10
[1] TRUE FALSE TRUE
ÚO^‡§
11
§1.2 S•þ
> x[x<10]
[1] 1.00 6.25
> x[x<0]
numeric(0)
Œ„x[x<10]Ѥku10ƒ|¤f8"ù«Ü6ŠeI´˜«rkå
u¢óä, ~Xx[sin(x)>0]Œ±Ñx¥¤ku¼êŠƒ|¤•
þ"
XJeIÑ´bŠK(J´˜‡"ݕþ, w«numeric(0)"
o!iÎ.ŠeI•þ
3½Â•þžŒ±‰ƒ\þ¶i, ~Xµ
> ages <- c(Li=33, Zhang=29, Liu=18)
> ages
Li Zhang
Liu
33
29
18
ù½Â•þŒ±^Ï~{–¯, ,„Œ±^ƒ¶i5–¯ƒ½
ƒf8, ~Xµ
> ages["Zhang"]
Zhang
29
> ages[c("Li", "Liu")]
Li Liu
33 18
•þƒ¶Œ±
\, ~Xµ
> ages1 <- c(33, 29, 18)
> names(ages1) <- c("Li", "Zhang", "Liu")
> ages1
Li Zhang
Liu
33
29
18
þ¡·‚w
Xۖ¯•þÜ©ƒ"3S¥„Œ±UC˜Ü©ƒ
Š, ~Xµ
> x
[1] 1.00 125.00 6.25
> x[c(1,3)] <- c(144, 169)
> x
[1] 144 125 169
5¿DŠÝ7LƒÓ, ~´Œ±rÜ©ƒD˜‡Ú˜Šµ
> x[c(1,3)] <- 0
> x
[1] 0 125 0
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
12
1˜Ù SŠó0
‡r•þ¤kƒD˜‡ƒÓŠq؎UCÙÝ, Œ±^x[]
{µ
> x[] <- 0
5¿ù†“x <- 0”´ØÓ, cöDŠ •þÝØC, ö¦•þCI
þ0"
UCÜ©ƒŠEâ†Ü6ŠeI{(܌±½Â•þ©ã¼ê, ~
X, ‡½Ây=f(x)x<0ž1−x, ÄK1+x, Œ±^µ
> y <- numeric(length(x))
> y[x<0] <- 1 - x[x<0]
> y[x>=0] <- 1 + x[x>=0]
1.3
1.3.1
Ý
†õ‘ê|
Ý
S¥Äêâa.“•þ”aqu‚5“꥕þVg, Ý
´‚5“
ê¥,˜‡~^óä§SŠó¥Jø
Ý
(matrix)êâa."SÝ
a.Ú
•þaq§´äkƒÓÄa.(Xê!2:ê!iÎ.)êâ8ܧ
Ù¥ƒ^ü‡eI–¯"'X§˜‡m × nÝ
AŒ±3SÝ
C
þA¥§A1i11jƒA[i,j]"
¼êmatrix()^5)¤)¤Ý
§Ù‚ª
matrix(data = NA, nrow = 1, ncol = 1,
byrow = FALSE, dimnames = NULL)
Ù¥1˜gCþdataê|êâ•þ("ŽŠ"”ŠNA), nrow1ê,
ncolê, byrowL«êâW\Ý
žU1gS„´gS, ‡5¿"Žœ¹
eUgS, ù†·‚Ý
S.´ØÓ"dimnames"Ž´˜Š, ÄK´
˜‡ݏ2L(„1.5), L1˜‡¤
´݆1êƒiÎ.•þ,
L«z1I\, L1‡¤
´݆êƒÓiÎ.•þ, L«z
I\"~X, ½Â˜‡314, d1:12U1gSüÝ
, Œ±^µ
> A <- matrix(1:12, ncol=4, byrow=TRUE)
> A
[,1] [,2] [,3] [,4]
[1,]
1
2
3
4
[2,]
5
6
7
8
[3,]
9
10
11
12
3kê✹eI½1ê½êƒ˜"½êâ‡ê#Nu¤
Iêâ‡ê, ùžÌ‚¦^Jøêâ"~Xµ
> B <- matrix(0, nrow=3, ncol=4)
ÚO^‡§
13
§1.3 Ý
†õ‘ê|
)¤314ƒÑ0Ý
"
5¿: SÝ
ƒÑgS´¤¢“gS”, ÚFORTRANŠó¥‘ê
|ƒÑgSƒÓ"^c(A)Œ±Ý
Aƒ•þ§X
> c(A)
[1] 1
5
9
2
6 10
3
7 11
4
8 12
ù«$Ž3‚5“ꥡÝ
“U.†”"
¼êcbind()rÙgCþ¤˜‡ŒÝ
, ¼êrbind()rÙgCþp•©
¤˜‡ŒÝ
"cbind()gCþ´Ý
½öwŠ•þ•þ, gCþpÝ
ATƒ(éu•þ, pÝ=Ý, éuÝ
, pÝ=1ê)"rbindgCþ´Ý
½wŠ1•þ•þ, gCþ°ÝATƒ(éu•þ, °Ý=Ý, éuÝ
, °Ý=ê)"XJë†Ü¿gCþ'Ù§gCþáK̂Öv Ü¿"
~Xµ
> x1 <- rbind(c(1,2), c(3,4))
> x1
[,1] [,2]
[1,]
1
2
[2,]
3
4
> x2 <- 10+x1
> x3 <- cbind(x1, x2)
> x3
[,1] [,2] [,3] [,4]
[1,]
1
2
11
12
[2,]
3
4
13
14
> x4 <- rbind(x1, x2)
> x4
[,1] [,2]
[1,]
1
2
[2,]
3
4
[3,]
11
12
[4,]
13
14
> cbind(1, x1)
[,1] [,2] [,3]
[1,]
1
1
2
[2,]
1
3
4
Ϗcbind()Úrbind()(Jo´Ý
a., ¤±Œ±^^cbind(x)r•þxL
«n × 1Ý
(•þ), ½^rbind(x)rxL«1 × nÝ
(1•þ)"
1.3.2
–¯Ý
ƒÚfÝ
‡–¯Ý
,‡ƒ, ‡ÑÝ
Cþ¶Ú)ÒS^ÏÒ©meI
=Œ, XA[3,2]10"Œ±‰˜‡Ý
ƒDŠ±?UTƒ"

–¯Ý
A1i1§‡^A[i,]ù‚ª§(J´˜‡S•þ
Ø´AÏ1•þ§S•þa.´Ø©1•þڕþ"~X:
> A[2,]
[1] 5 6 7 8
ƒA§
–¯Ý
A1j§‡^A[,j]ù‚ª§X:
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
14
1˜Ù SŠó0
> A[,2]
[1] 2 6 10
5¿ù´˜‡S•þØ´Aϕþ"

–¯Ý
A¥d1i1 , i2 , . . . , is 1Ú1j1 , j2 , . . . , jt ¤fÝ
§
‡^A[c(i1 , i2 , . . . , is ), c(j1 , j2 , . . . , jt )] ù/ª"X
> A
[1,]
[2,]
[3,]
[,1] [,2] [,3] [,4]
1
2
3
4
5
6
7
8
9
10
11
12
> A[1:2,]
[,1] [,2] [,3] [,4]
[1,]
1
2
3
4
[2,]
5
6
7
8
> A[1:2, c(1,2,4)]
[,1] [,2] [,3]
[1,]
1
2
4
[2,]
5
6
8
> A[1, c(1,2,4)]
[1] 1 2 4
Ù¥ ˜‡^{Ϗk˜1¤±(Jòz˜‡•þ§…Ø´AÏ1
•þ"
Œ±Ý
˜‡f8D#Š§¤DŠATTд¤I‡‡ê§½´
˜‡Ú˜Š"
ØUCÝ
AŒr¤kƒDŠ˜‡Ú˜Š§^
X:
> A[] <- 0
ڕþ˜§Ý
1!eIŒ±^¶i“O"Ý
z1Œ±k˜
‡1¶§zŒ±k˜‡¶§aqu•þ@Œ±^¶iŠeI"½Â1
¶^rownames()¼ê§½Â¶^colnames()¼ê§X:
> rownames(A) <- c("a", "b", "c")
> colnames(A) <- paste("X", 1:4, sep="")
> A
X1 X2 X3 X4
a 1 2 3 4
b 5 6 7 8
c 9 10 11 12
> A["b", "X4"]
[1] 8
þ¡½Â1¶Ú¶{†Ù§§SŠóS.é؃ӧ¢Sþ§rownames(A)
ƒuÙ§§SŠó¥A->rownames ½A.rownames§=–¯é–Aá5
rownames"
ÚO^‡§
15
§1.3 Ý
†õ‘ê|
1.3.3
Ý
$Ž
Ý
Œ±?1oK$Ž(+, −, *, /, ^), )ºÝ
éAƒoK$Ž, ë
\$ŽÝ
˜„AT´ƒÓ/G"$Ž5K†•þoK$Ž5Kaq"
~X, bA, B, C´n‡/GƒÓÝ
, K
> D <- C + 2*A/B
OŽ(J´Az˜‡ƒØ±BéAƒ2¦±2, \þCéA
ƒƒÓ/GÝ
"
/Gؘ—•þÚÝ
Œ±?1oK$Ž, ˜„5K´Ý
êâ•
þ(Uü)éAƒ?1$Ž, rá̂¦^5†š, ¿¦ŒU3
Ó/G"~Xµ
> A + c(100, 200, 300)
[,1] [,2] [,3] [,4]
[1,] 101 102 103 104
[2,] 205 206 207 208
[3,] 309 310 311 312
(J´Ý
AU.† •þ†•þc(100,200,300) ƒ\12‡ƒ, 2
¡E†Ý
AƒÓ/GÝ
"ƒ\ƒ‡êØÓ¤±á˜‡­E|
^"ؚ\˜Ù/5KA;¦^ù{(Iþ†Ý
½•þoK$
ŽØ)"
SÝ
a.Ø
Œ±ŠƒmoK$Ž„|±‚5“ê¥Ý
¦
$Ž"¼êt(A)ˆ£Ý
A=˜"nrow(A)Ý
A1ê, ncol(A)Ý
Aê"
‡?1Ý
¦{, ¦^$ŽÎ%*%, A %*% BL«Ý
A¦±Ý
B(,‡
¦AêuB1ê)"~Xµ
> B <- matrix(c(1,0), nrow=4, ncol=2, byrow=T)
> B
[,1] [,2]
[1,]
1
0
[2,]
1
0
[3,]
1
0
[4,]
1
0
> A %*% B
[,1] [,2]
[1,]
10
0
[2,]
26
0
[3,]
42
0
,, •þ^3Ý
¦{¥Œ±Š1•þw–Œ±Š•þw–, ù
‡w=˜«*:U
?1Ý
¦{$Ž"~X, x´˜‡ݏn•þ, A´
˜‡n × nÝ
, K“x %*% A %*% x”L«g.x0 Ax"´, kž•þ3Ý
¦{¥/ ¿Ø˜Ù, 'X•þxS•þx§SLˆª“x %*% x”Ò
QŒUL«SÈx0 xŒUL«n × n
xx0 "Ϗcö~^, ¤±SÀJL«
cö, SȁЄ´^crossprod(x)5OŽ"‡L«xx0 , Œ±^“cbind(x) %*%
x”½“x %*% rbind(x)”"
¼êcrossprod(X, Y)L«˜„¦È(SÈ)X 0 Y , =Xz˜†Y
z˜SÈ|¤Ý
"XJXÚYÑ´•þK´˜„SÈ"˜‡ë
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
16
1˜Ù SŠó0
êX crossprod(X)OŽXgSÈX 0 X"5¿=¦Ý
¦{½crossprod¼
ê(J´IþžSE¬ˆ£˜‡1 × 1Ý
§ùž˜„AT^c()¼êr(J=
†¤Iþ(ݏ1•þ)"
Ù§Ý
$Ž„ksolve(A,b))‚5§|Ax = b, solve(A)¦
A_Ý
, svd()OŽÛɊ©), qr()OŽQR©), eigen()OŽA•þÚAŠ"
„‘ŐÏ, ~Xµ
> ?qr
¼êdiag()Š^6uÙgCþ"diag(vector)ˆ£±gCþ(•þ)Ìé
ƒéÝ
"diag(matrix)ˆ£dÝ
Ìéƒ|¤•þ"diag(k)(k
Iþ)ˆ£kü "
1.3.4
apply¼
¼ê
éu•þ, ·‚ksum!mean¼êéÙ?1OŽ"éuÝ
, XJ·‚Ž
éz1(½z)?1,«OŽ, Œ±^apply¼ê"٘„/ªµ
apply(X, MARGIN, FUN, ...)
Ù¥X˜‡Ý
, MARGIN=1L«éz1OŽ§MARGIN=2L«ézO
Ž" FUN ´^5OŽ¼ê"~X, A!~f¥3 × 4Ý
, Kapply(A,
1, sum)¿Â´éaˆ1¦Ú(31˜‘=1˜‡eIØC), (J´˜‡
ݏ3 •þ(z‡ƒéAÝ
˜1):
> apply(A, 1, sum)
[1] 10 26 42
apply(a, 2, sum)¿Â´éaˆ¦Ú, (J´˜‡ݏ4•þ(z‡ƒ
éAÝ
˜):
> apply(A, 2, sum)
[1] 15 18 21 24
1.3.5
ê | (array)
Ý
ƒ^ü‡eI–¯§S¥^õ‡eI–¯êâ(´ê|(array)§
Ý
´ê|˜‡A~"
ê|k˜‡Aá5‰‘ê•þ(dimá5), ‘ê•þ´˜‡ƒ
ꊕþ, ÙÝ´ê|‘ê, 'X‘ê•þkü‡ƒžê|‘ê
|(Ý
)"‘ê•þz˜‡ƒ½
TeIþ., eIe.o1"
¤¢á5´¡•é–§SOgŽVg§3¡•é–§SO¥§
^é–L«˜‡¢N§S¥?˜‡CþŒ±w¤˜‡é–¶z‡é–¥k˜
Äêâ§'XÝ
ˆƒŠ¶z‡é–ØÄêℌU
†
dé–k'&E§‰é–á5"?ۘ‡Sé–Ñkü‡ká5: a
.(mode)ÚÝ(length)"ØÓSé–|±ØÓá5§'X§ê|kdimá
5§½Â
ƒ¶•þknamesá5"–¯é–xá5att^att(x)‚ª–
¯§‰á5DŠ˜„´^“att(x) <- #Š”{"
ÚO^‡§
17
§1.3 Ý
†õ‘ê|
S閌©üX(atomic)ÚEÜ(recursive)ü«, üX閤k
ƒÑ´Ó˜«Äa.(XêŠ!iÎG), ƒØ2´é–, ùé–a
.(mode)k logical(Ü6.)!numeric(êŠ.)!complex(Eê.)!character(i
Î.)¶EÜ閃Œ±´ØÓa., z˜‡ƒ´˜‡é–, ù
閁~^´L"~X, •þ(vector)´üXé–, §¤kƒÑ7L´
ƒÓa., êŠ.•þ¤kƒ7LêŠ., iÎ.•þ¤kƒ7L
iÎ.¶L(list)´EÜé–, a.(mode)L(list, „1.5), Lz˜‡
ƒ(Cþ)ь±´˜‡Sé–, 'XLƒŒ±˜‡ê, ˜‡iÎG, ˜‡•
þ, $–˜‡L"
˜|Šk½Â
‘ê•þ(dimá5) âUwŠ´ê|"'Xµ
> z <- 1:24
> dim(z) <- c(2, 3, 4)
ùžz®²¤
˜‡‘ê•þc(2,3,4)n‘ê|"Œ±r•þ½Â˜
‘ê|, ~Xµ
> dim(z) <- 24
ê|ƒügS´æ^FORTRANê|ƒgS(UgS), =1
˜eICz¯,  eICzú, éuÝ
(‘ê|)K´U˜"~X,
ê|zˆƒgSz[1,1,1], z[2,1,1], z[1,2,1], z[2,2,1], z[1,3,1], z[2,3,1], z[1,2,2],
..., a[2,3,4]"
^¼êarray()Œ±†½Âê|"array()¼ê¦^
array(x, dim=length(x), dimnames=NULL)
Ù¥x´1˜gCþ, AT´˜‡•þ, L«ê|ƒŠ|¤•þ"dimëê
ŒŽ, ŽÑž½Â˜‘ê|(˜‘ê|ØÓu•þ)"dimnamesá5Œ±ŽÑ,
؎ў´˜‡݆‘êƒÓL, Lz‡¤
˜‘¶i"~X
þ¡zŒ±ù½Âµ
> z <- array(1:24, dim=c(2,3,4))
–¯ê|ƒXz[1,2,3], Œ±–¯ê|¥aqfÝ
f8§'Xz[1:2,2:3,2:3]
´˜‡‘êc(2,2,2)n‘ê|",˜eI˜‡ŠKê|‘êòz§'
Xz[,,4] ´˜‡2 × 3Ý
, z[,2,4]´˜‡ݏ2•þ"
ê|oK$Ž†Ý
oK$Žƒq"
1.3.6
ê|È
ü‡ê|(½•þ)aÚbÈ´daz˜‡ƒ†bz˜‡ƒ‡3˜
僦˜‡#ƒ, ù˜‡‘ê•þua‘ê•þ†b‘ê•
þëå5•þ, =edaÚbÈ, Kdim(d)=c(dim(a), dim(b))"
aÚbÈPŠa %o% b"X
> d <- a %o% b
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
18
1˜Ù SŠó0
Œ±¤˜‡¼êN^/ªµ
> d <- outer(a, b, ’*’)
~X:
> c(2,3) %o% c(5,7,9)
[,1] [,2] [,3]
[1,]
10
14
18
[2,]
15
21
27
(Jz1†c(2,3)ü‡ƒéA§(Jz†c(5,7,9)n‡ƒéA"
5¿outer(a, b, f)´˜‡˜„5ȼê, §Œ±ra?˜‡ƒ†b
?˜‡ƒ‡å5ŠfgCþOŽ#ƒŠ, È´ü‡ƒƒ¦
œ¹"¼ê,Œ±´\!~!Ø, ½Ù§˜„¼ê"¼ê¦ÈžfŒ
±ŽÑØ"
~X, ·‚F"OŽ¼êz = cos(y)/(1 + x2 )3˜‡xÚy‚þŠ^5
±›n‘­¡ã, Œ±^Xe{)¤‚9¼êŠµ
>
>
>
>
>
x <- seq(-2, 2, length=20)
y <- seq(-pi, pi, length=20)
f <- function(x, y) cos(y)/(1+x^2)
z <- outer(x, y, f)
persp(x,y,z)
^ù‡˜„¼êŒ±éN´/rü‡ê|¤kƒÑüü|ܘH?1½
$Ž"
a b
e¡ʇk¿g~f"·‚Ä{ü2 × 2Ý
, ك
c d
þ30,1,...,9¥Š"bo‡ƒa, b, c, dÑ´ƒpÕáÑllÑþ!©Ù
‘ÅCþ, ·‚{¦Ý
1ªad − bc©Ù"Äk, ‘ÅCþadÚbcÓ©
Ù, §Šd±eÈÝ
‰Ñ, z˜‡ŠVÇþ1/100:
> d <- outer(0:9, 0:9)
ù‡Šé)˜‡10 × 10ÈÝ
"

OŽad100‡Š(k­E)†bc100‡Šƒ~10000‡(J, Œ±
¦^Xeȼêµ
> d2 <- outer(d, d, "-")
ù˜‡‘ê•þc(10,10,10,10)o‘ê|, z˜‡ƒ1ª˜
‡ŒUŠ, VǏ©ƒ˜"Ϗù
Š¥kéõ­E, ·‚Œ±^˜
‡table()¼ê5OŽz˜‡ŠÑygê(ªê)µ
> fr <- table(d2)
ÚO^‡§
19
300
0
100
200
Frequency
400
500
§1.4 Ïf
−50
0
50
Determinant
ã 1.2: ‘ÅÝ
1ª©Ù
(J´˜‡‘kƒ¶•þfr, frƒ¶1ª˜‡Š, fr
ƒŠTŠÑyªê"'Xfr[1]ƒ¶−81, Š19, L«Š−813ê
|d2¥Ñy
19g"ÏLOŽlength(fr)Œ±k163‡Øӊ"„Œ±r
ù
Š±›˜‡ªê©Ùã(ر10000K¢SVÇ), „ã1.2µ
> plot(as.numeric(names(fr)), fr, type="h",
+ xlab="Determinant", ylab="Frequency")
Ù¥as.numeric()r•þfr¥ƒ¶q=†¤
êŠ., ^5ŠŠãî¶
‹I, fr¥ƒŠ=ªêŠp¶, type=”h”L«´xR‚.ã"
1.4
Ïf
VÇØ¥‘ÅCþŒ±©ëY.ÚlÑ.§ƒA/§ÚO¥CþŒ±
©«mCþÚ¶ÂCþ!kSCþ"«mCþëYêŠ, Œ±?1¦
Ú!²þ$Ž"¶ÂCþÚkSCþlъ, Œ±^ꊓLŒ±´i
Î.Š, ÙäNêŠvk\~¦Ø¿Â, ØU^5OŽU^5©a½ö
Oê"¶ÂCþ~fk5O!Ž°!…’, kSCþ~fk?¶g!c#|
O"
ϏlÑCþkˆ«ØÓL«{, 3S¥Ú˜å„¦^Ïf(factor)5L
«ù«©aCþ"„Jø
kSÏf(ordered factor)5L«kSCþ"Ïf´
˜«Aϕþ, Ù¥z˜‡ƒ˜|lъ¥˜‡, Ïfé–k˜‡AÏ
á5levelsL«ù|lъ"~Xµ
> x <- c("I", "å", "I", "I",
> y <- factor(x)
> y
[1] I å I I å
Levels: I å
?Í: oÀº
"å")
http://www.math.pku.edu.cn/teachers/lidf/
20
1˜Ù SŠó0
¼êfactor()^5r˜‡•þ?褏˜‡Ïf"٘„/ªµ
factor(x, levels = sort(unique(x), na.last = TRUE),
labels, exclude = NA, ordered = FALSE)
Œ±g1½ˆlъ(Y²levels), ؍½ždxØӊ5¦"labelsŒ
±^5½ˆY²I\, ؍½ž^ˆlъéAiÎG"excludeëê
^5½‡=†"”Š(NA)ƒŠ8Ü"XJ½
levels, KÏf
1i‡ƒ§uY²¥1j‡žƒŠ”j”, XJ§ŠvkÑy3levels¥
KéAÏfƒŠNA"orderedýŠžL«ÏfY²´kgS(U?èg
S)"~Xµ
> f <- factor(c(1,0,1,1,0),
levels=c(1,0), labels=c("I","å"))
(JÚfâ˜"
Œ±^is.factor()u閴ÄÏf, ^as.factor()r˜‡•þ=†¤˜‡Ï
f"
ÏfÄÚO´ªêÚO, ^¼êtable()5Oê"~X,
> sex <- factor(c("I", "å", "I", "I", "å"))
> res.tab <- table(sex)
> res.tab
sex
I å
3 2
L«I53<, å52<"table()(J´˜‡‘ƒ¶•þ, ƒ¶ÏfY
², ƒŠTY²Ñyªê"
lù‡{ü~f·‚Œ±wÑS†SAS˜‡«OµSAS(J˜„w
«Ñ5, Uêâ8˜«a., ØBué¥m(J?˜Ú?§?n¶
S(JØ
Œ±w«Ñ´Sé–(Xùp•þ(J), Œ±éB/
?˜Ú?n"
Œ±^ü‡½õ‡Ïf?1©a"'X, 5O(sex)څ’(job)©|
Œ±^table(sex, job)5ÚOz˜aªê, (J˜‡Ý
, Ý
‘k1¶
Ú¶, ©Oü‡ÏfˆY²¶"
ÏfŒ±^5Š,ÓÝCþ©aCþ"'X, bþ¡sex´5‡
Æ)5O, > h <- c(165, 170, 168, 172, 159)
´ù5‡Æ)p, K
> tapply(h, sex, mean)
Œ±¦U5O©ap²þŠ"ù^˜‡Ïf•þ阇ꊕþ
©|{‰Ø5Kê|(ragged array)" ¡·‚„Œ±wõÏf
A^"
ÚO^‡§
21
§1.5 L(LIST)
1.5
L (list)
1.5.1
L½Â
L´˜«AOé–8Ü, §ƒdSÒ(eI)«©, ´ˆƒ
a.Œ±´?¿é–, ØӃØ7´Ó˜a."ƒ#N´Ù§E,
êâa., 'X, L˜‡ƒ#N´L"ùêâa.¡´4
8(recursive)êâa."~Xµ
> rec <- list(name="o²", age=30, scores=c(85, 76, 90))
> rec
$name
[1] "o²"
$age
[1] 30
$scores
[1] 85 76 90
LƒoŒ±^“L¶[[eI]]”‚ªÚ^"~Xµ
> rec[[2]]
[1] 30
> rec[[3]][2]
[1] 76
´, LØÓu•þ, ·‚zgUÚ^˜‡ƒ, Xrec[[1:2]]^{´Ø#N
"
5¿µ“L¶[eI]”½“L¶[eI‰Œ]”^{´Ü{, ٿ†
^ü­)ÒP{ØÓ, ü­)ÒPÒÑL˜‡ƒ, (J†T
ƒa.ƒÓ, XJ¦^˜­)Ò, K(J´L˜‡fL((Ja.E
L)"
3½ÂLžXJ½
ƒ¶i(Xrec¥name, age, scores), KÚ^
Lƒ„Œ±^§¶iŠeI, ‚ª“L¶[[”ƒ¶”]]”, Xµ
> rec[["age"]]
[1] 30
,˜«‚ª´“L¶$ƒ¶”, Xµ
> rec$age
[1] 30
Ù¥“ƒ¶”Œ±{†Ù§ƒ¶U
«©á§Ý, 'X“rec$s”Œ±
“L“rec$score”"ù«{B
p$1, ?§Sž˜„Ø^{±ü
$§SŒÖ5"
¦^ƒ¶Ú^{Œ±4·‚Ø7P4,˜‡eI“L@˜‡ƒ, †^´Pƒ¶5Ú^ƒ"¯¢þ, •þÚÝ
Œ±½ƒ¶!1
¶!¶"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
22
1˜Ù SŠó0
½ÂL¦^list()¼ê, z˜‡gCþC¤L˜‡ƒ, gCþŒ±
^“¶i=Š”ª‰Ñ, =‰ÑLƒ¶"gCþŠE›Lƒ
¥, gCþXJ´Cþ¿Ø¬†TLƒïá'X(UCTLƒØ¬UC
gCþŠ)"
1.5.2
?UL
LƒŒ±?U, ‡rƒÚ^DŠ=Œ"Xµ
> rec$age <- 45
$–
> rec$age <- list(19, 29, 31)
(Œ±?¿?U˜‡Lƒ)"XJDŠƒ5Ø3, KLò±
¹T#ƒ"~X, recy3kn‡ƒ, ·‚½Â˜‡#·¶ƒ, KL
ÝC4, 2½Â18҃KLÝC6µ
> rec$sex <- "I"
> rec[[6]] <- 161
> rec
$name
[1] "o²"
$age
[1] 30
$scores
[1] 85 76 90
$sex
[1] "I"
[[5]]
NULL
[[6]]
[1] 161
1Ê҃Ϗvk½Â¤±ÙŠ´“NULL”, ù´˜é–PÒ"XJrec´
˜‡•þ, K٘ƒ“NA”, ù´"”ŠPÒ"lùp·‚Œ±N
¬“NULL”† “NA”«O"
A‡LŒ±^ë¼êc()ëå5, (JE˜‡L, كˆgC
þLƒ"Xµ
> list.ABC <- c(list.A, list.B, list.C)
5¿3S¥é:´¶iÜ{Ü©, ˜„vkAÏ¿Â"
ÚO^‡§
23
§1.5 L(LIST)
1.5.3
A‡ˆ£L¼ê
L­‡Š^´rƒ'eZêâ3˜‡êâ閥, ù3?
¼êž·‚ÒŒ±ˆ£ù˜‡¹õ‘ÑÑL"Ϗ¼êˆ£(
JŒ±/˜3˜‡L¥, ·‚Œ±UYé(J?1©Û, ù
´S'SAS(¹˜‡/"e¡‰ÑA‡ˆ£L~f"
˜!AŠÚA•þ
¼êeigen(x)éé¡Ý
xOŽÙAŠÚA•þ, ˆ£(J˜‡L,
Lü‡¤
(ƒ)valuesÚvectors"~Xµ
> ev <- eigen((1:3) %o% (1:3))
> ev
$values
[1] 1.400000e+01 0.000000e+00 -8.881784e-16
$vectors
[,1]
[,2]
[,3]
[1,] 0.2672612 0.8944272 -0.3585686
[2,] 0.5345225 -0.4472136 -0.7171372
[3,] 0.8017837 0.0000000 0.5976143
Œ„n‡AŠk1˜‡Ø"(duêŠOŽ°Ý¤, 1n‡AŠA
"(J´Cq")"A•þUÝ
˜, z˜˜‡A•þ"
!ÛɊ©)91ª
¼êsvd()?1ÛɊ©)X = U DV 0 , Ù¥X´?¿n × m
, U n × n
, V m × m
, Dn × mé
(kÌ邃؏"…š
K)"svd(x)ˆ£kn‡¤
d, u, vL, d¹ÛɊ•þ(=DÌé
‚ƒ), u, v©Oþ¡ü‡
"´„XJÝ
x´
Kx1ª
ýéŠuÛɊ¦È, ¤±µ
> absdetx <- prod(svd(x)$d)
½ö·‚Œ±d½Â˜‡¼êµ
> absdet <- function(x) prod(svd(x)$d)
n ! ¦[ Ü †QR©
©)
¼êlsfit(x,y)ˆ£¦[Ü(J"¦.‚5.
y = Xβ + ε
lsfit(x,y)1˜‡ëêx.¥O
X, 1‡ëêy.¥ÏC
þy(Œ±´˜‡•þŒ±´˜‡Ý
), ˆ£˜‡L, ¤
coefficientsþ¡
.β (¦Xê), ¤
residuals[Üí, ¤
intercept^5«´
Äkå‘, ¤
qrO
XQR©), §´˜‡L".[Ü"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
24
1˜Ù SŠó0
Žœ¹kå‘, Œ±^intercept=FALSEÀ‘½Ãå‘"'u¦[
܄Œë„ls.diag()¼ê(wÏ)"
¼êqr(x)ˆ£xQR©)(J"Ý
XQR©)X = QR, Q
,
Rþn
"¼ê(J˜‡L, ¤
qr´Ø QÚR3S˜‡Ý
, 
)é‚3SþnÜ©R, Q&EØ ˜3enÜ©"(JÙ§
¤
˜
9Ï&E"^qr.Q()Úqr.R()Œ±lqr()(J¥JQÚR"
1.6
êâ µ(data.frame)
êâµ´S¥aqSASêâ8˜«êâ("§Ï~´Ý
/ªêâ,
Ý
ˆŒ±´ØÓa."êâµz´˜‡Cþ, z1´˜‡*ÿ"
´, êâµk˜„½Â"§´˜«AÏLé–, k˜‡Š
“data.frame” classá5, ˆL¤
7L´•þ(êŠ.!iÎ.!Ü6.)!
Ïf!êŠ.Ý
!L, ½Ù§êâµ"•þ!Ïf¤
êâµJø˜‡
Cþ, XJ•þšêŠ.K¬r›=†Ïf, Ý
!L!êâµù
¤
#êâµJø
ÚÙê!¤
ê!CþêƒÓ‡êCþ"Šêâ
µCþ•þ!Ïf½Ý
7LäkƒÓÝ(1ê)"
¦+Xd, ·‚˜„„´Œ±rêâµwŠ´˜«í2
Ý
, §Œ±^
Ý
/ªw«, Œ±^éÝ
eIÚ^{5Ú^ك½f8"
1.6.1
êâµ)¤
êⵌ±^data.frame()¼ê)¤, Ù^{†list()¼êƒÓ, ˆgCþC¤
êⵤ©, gCþŒ±·¶, ¤Cþ¶"~Xµ
> d <- data.frame(name=c("o²", "Üh", "ï"),
+ age=c(30, 35, 28), height=c(180, 162, 175))
> d
name age height
1 o² 30
180
2 Üh 35
162
3 ï 28
175
XJ˜‡Lˆ‡¤©÷vêⵤ©‡¦, §Œ±^as.data.frame()¼
êr›=†êâµ"'X, þ¡dXJk^list()¼ê½Â¤
˜‡L, Ò
Œ±r›˜‡êâµ"
˜‡Ý
Œ±^data.frame()=†˜‡êâµ, XJ§5k¶KÙ
¶ŠêâµCþ¶, ÄKXÚgďÝ
ˆå˜‡Cþ¶(XX1,
X2)"
1.6.2
êâµÚ^
Ú^êⵃ{†Ú^Ý
ƒ{ƒÓ, Œ±¦^eI½eI•
þ, Œ±¦^¶i½¶i•þ"Xd[1:2, 2:3]"êⵈCþŒ±^U
LÚ^(=^V)Ò[[]]½$ÎÒÚ^)"
êâµCþ¶dá5names½Â, dá5˜½´š˜"êⵈ1
Œ±½Â¶i, Œ±^rownamesá5½Â"Xµ
> names(d)
[1] "name" "age" "height"
ÚO^‡§
25
§1.7 Ñ\ÑÑ
> rownames(d)
[1] "1" "2" "3"
1.6.3
attach()¼
¼ê
êâµÌ‡^å´ÚOïI‡êâ"SÚOïõUÑI‡±
êⵏÑ\êâ"·‚Œ±rêⵤ˜«Ý
5?n"
3¦^êâµCþžŒ±^“ê⵶$Cþ¶”P{"´, ù¦^
æ†, SJø
attach()¼êŒ±rêⵓë”\c¶i˜m"~X,
> attach(d)
> r <- height / age
˜Šéò3cóŠ˜mïᘇ#Cþr, §Ø¬gÄ?\êâµd, ‡r#
CþDŠêâµ¥, Œ±^
> d$r <- height / age
ù‚ª"

žë, ‡N^detach()(Ãëê=Œ)"
5¿µS-PLUSÚR¥¶i˜m+n´'ÕA"§3$1ž±˜
‡Cþ|¢´»L, 3Ö,‡Cþžù‡Cþ|¢´»L¥dc• é, éc˜‡¶3DŠžo´3 ˜1DŠ(ؚAO½3Ù§ ˜D
Š)"attach()"Ž ˜´3Cþ|¢´»L ˜2, detach()"Ž´K
˜2"¤±, S?§˜‡~„¯K´\Ø^
˜‡gC¿vkDŠCþ
žkŒUØц, Ϗù‡Cþ®3|¢´»¥,‡ ˜k½Â, ùØ|u§
SNÁ, I‡3%ù¯K"
Ø
Œ±ëêâµ, Œ±ëL"
1.7
1.7.1
Ñ\ÑÑ
ÑÑ
3Sp$1ž‡w«,˜‡é–Š‡…\Ù¶i=Œ, Xµ
> x <- 1:10
> x
[1] 1 2 3 4 5 6 7 8 9 10
ù¢Sþ´N^
print()¼ê, =print(x)"3šp$1(§S)¥A¦^print()5
ÑÑ"print()¼êŒ±‘˜‡digits=ëꍽz‡êÑÑkêi ê, Œ
±‘˜‡quote=ëꍽiÎGÑў´Ä‘ü>§Ò, Œ±‘˜‡print.gap=ë
ꍽÝ
½ê|Ñўƒmmå"
print()¼ê´˜‡Ï^¼ê, =§éØÓgCþkØӇA"鈫A
Ïé–Xê|!.(Jь±5½printÑтª"
cat()¼ ê  ^ 5 Ñ Ñ, § Œ ± r õ ‡ ë ê ë å 5 2 Ñ Ñ(ä k a q
upaste()õU)"~Xµ
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
26
1˜Ù SŠó0
> cat("i = ", i, "\n")
5¿¦^cat()ž‡gC\þ†1Δ\n”"§rˆ‘=†¤iÎG, ¥m…±˜
‚ëå5, , w«"XJ‡¦^g½Â©…Î, Œ±^sep=ëê, ~Xµ
> cat(c("AB", "C"), c("E", "F"), "\n", sep="")
ABCDEF
cat()„Œ±½˜‡ëê“file=©‡¶”, Œ±r(J½©‡¥,
Xµ
> cat("i = ", 1, "\n", file="c:/work/result.txt")
XJ½©‡®²3K5SNCX"\þ˜‡append=TRUEëêŒ
±ØCX©‡´3©‡"—N\, ùé·^u$1¥(JP¹"
cat()¼êÚprint()ÑØäkérg½Â‚ªõU, dŒ±¦^cat()†
format() ¼êÜ¢y"format()¼ê˜‡êŠ•þ阫Ów«‚
ª, r•þ=†iÎ."~Xµ
> format(c(1, 100, 10000))
[1] "
1" " 100" "10000"
R„Jø
˜‡formatC¼êŒ±JøaqCŠóprintf‚ªõU, …é
Ñ\•þz˜‡ƒüÕ?1‚ª=†Ø)¤Ú˜‚ª, ~Xµ
> formatC(c(1, 10000))
[1] "1"
"1e+004"
3formatC()¼ê¥Œ±^format=ëꍽC‚ªa., X”d”(ê), ”f”(½
:¢ê), ”e”, ”E”(‰ÆPê{), ”g”,”G”(ÀJ êÑтª), ”fg”(½:
¢ê^ëêdigits½k êØ´o°Ý), ”s”(iÎG)"Œ±^width
½ÑÑ°Ý, ^digits½k ê(‚ªe,E,g,G,fgž)½ê: ê(‚ª
f)ž"Œ±^flagëꍽ˜‡ÑÑÀ‘iÎG, iÎG¥k”-”L«Ñцé
à, k”0”L«†˜x^0W¿, k”+”L«‡ÑÑKÒ, "~X,
> formatC(1234.123456, digits=3, width=10, format="f")
[1] " 1234.123"
SÑÑ"Žw«3pI"Œ±^sink()¼ê½˜‡©‡±r
ÑÑ=•ù‡©‡, ¿Œ^appendëꍽ´Ä‡3©‡"—N\µ
>
>
>
>
sink("c:/work/result.txt", append=TRUE)
ls()
d
sink()
ÚO^‡§
Y
27
§1.7 Ñ\ÑÑ
N^Ãëêsink()rÑÑ¡EpI"
XJ‡r˜‡Ý
xÑÑ©‡¥, Œ±^write(t(x), file = ”©‡¶”, ncol
= ncol(x))/ª"ùpƒ¤±‡rx=˜ 2ÑѴϏR¥Ý
´`k,
XJØ=˜KÑÑ´UÑÑ"XJ؍½êK"Ž¦^5, =¦x
êõu½u5"©‡¶"Ž¦^”data”"
‡r˜‡êâµdÑÑ©‡¥, Œ±^write.table(x, file=”©‡¶”), ÑÑ
©‡¥¹Cþ¶LÞÚ1¶"
1.7.2
Ñ\

lÜ©‡Ö\˜‡êŠ.•þ, SJø
scan()¼ê"XJ½
fileë
ê(´1˜ëê), Kl½©‡Ö\, "Žœ¹eÖ\˜‡êŠ•þ, ©‡¥
ˆêⱘx©…, Ö©‡—Ž"~Xµ
> cat(1:12, ’\n’, file=’c:/work/result.txt’)
> x <- scan(’c:/work/result.txt’)
XJ©‡¥´˜‡^˜x©…Ý
(½ê|), ·‚Œ±k^scan()r§Ö
\˜‡•þ, ^matrix()¼ê(½array()¼ê)=†"Xµ
> y <- matrix(scan(’c:/work/result.txt’),
ncol=3, byrow=T)
¢Sþ, scan()U
Ö\˜‡õL‚, ‡^whatëꍽ˜‡L,
TLz‘a.I‡Öa."^skipëꌱaL©‡m©eZ1
ØÖ"^sepëꌱ½êâm©…Î"„Ï"
scan()؍½Ö©‡¶ž´pÖ\, Ö\ž^˜‡˜1(å"
XJ‡Ö˜‡êâµ, SJø
˜‡read.table()¼ê"§‡‰Ñ˜‡©
‡¶, Ҍ±r©‡¥^˜x©…L‚êâz1Ö\êⵘ1"'X,
©‡c:\work\d.txt¥SNXeµ
Zhou
15 3
"Li Ming" 9 o²
Zhang 10.2 Wang
^read.tableÖ\µ
> x <- read.table(’c:/work/d.txt’,
+ colClasses=c(’character’, ’numeric’, ’character’))
> x
V1
V2
V3
1
Zhou 15.0
3
2 Li Ming 9.0 o²
3
Zhang 10.2 Wang
Ö\(Jêâµ"¼êgďêâµCþ½“V1”!“V2”ùCþ¶, 
½ “1”!“2”ù1¶"Œ±^col.namesëꍽ˜‡iÎ.•þŠêâ
µCþ¶, ^ row.namesëꍽ˜‡iÎ.•þŠêâµ1¶"¼êŒ
±gÄ£OL´êŠ.„´iÎ., ¿3"Žœ¹eriÎ.êâ=†Ï
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
28
1˜Ù SŠó0
f, ùp1˜Ú1n·‚¿ØŽ=†, ¤±</\þ
˜‡colClassesë
ê`²z˜êâa."
éuÏÒ©…©‡^read.csv()Ö"\þheader=TRUEëꌱÖ\‘
kLÞ©‡"~X, 
Ö\Xe‘kLÞÏÒ©…©‡c:\work\d.csvµ
Name,score, cn
Zhou,15,3
Li Ming, 9, o²
Zhang, 10.2, Wang
¦^XeŠéµ
> x <- read.csv(’c:/work/d.csv’, header=T,
+ colClasses=c(’character’, ’numeric’, ’character’))
> x
Name score
cn
1
Zhou 15.0
3
2
Li Ming
9.0 o²
3
Zhang 10.2 Wang
Ù§˜
^{„Ï"
1.8
§S››(
S´˜‡LˆªŠó, Ù?ۘ‡Šéь±w¤´˜‡Lˆª"Lˆªƒ
m±©Ò©…½^†1©…"LˆªŒ±Y1, ‡c˜1Ø´Lˆª('
X"—´\~¦Ø$ŽÎ, ½k™é)Ò)Ke˜1þ˜1UY"
eZ‡LˆªŒ±˜3˜å|¤˜‡EÜLˆª, Š˜‡Lˆª¦^"|
Ü^Œ)ÒL«, Xµ
{
x <- 15
x
}
SŠóJø
Ù§p?§SŠók©|!̂§S››("
1.8.1
©|(
©|()if(µ
if (^‡) Lˆª1
½
if (^‡) Lˆª1 else Lˆª2
Ù¥“^‡”˜‡Iþý½bŠ, LˆªŒ±´^Œ)ҝŒEÜLˆ
ª"kelsef鞘„¤µ
ÚO^‡§
29
§1.8 §S››(
if(^‡) {
Lˆª|. . . . . .
} else {
Lˆª|. . . . . .
}
ù{Œ±¦elseؖuølc¡if"
~X, XJCþlambda"”ŠÒ‰§D˜‡"ŽŠ, Œ^µ
if(is.na(lambda)) lambda <- 0.5;
q'X‡OŽ•þx­éê, ùk3ƒÑ…éêяžâU‰
, ÏdI‡kuµ
if(all(x>0) && all(log(x))>0) {
y <- log(log(x));
print(cbind(x,y));
} else {
cat(’Unable to comply\n’);
}
5¿“&&”L«“†”, §´˜‡á´$ŽÎ, =1˜‡^‡bžÒØOŽ
1‡^‡, XJØùd~¥OŽéêҌ±kÊ"3^‡¥Œ±
^“||”(ü‡ëYç‚ÎÒ)L«“½”, §´á´$ŽÎ, 1˜‡^‡ý
žÒØ2OŽ1‡^‡"
5µéuOŽ­éê¯K‡äall(x>1)Œ±, ~f´
`²á´
†"
3^S?§Sž˜½‡žOPS´˜‡•þŠó, A¤köŠÑ´é•þ
?1"S¥ifŠé%´˜‡„~, §ä^‡´IþýŠ½b
Š"'X, ·‚‡½Â˜‡©ã¼êf(x), xžˆ£1, ÄKˆ£0, êþŒ
±Ž^ifŠé¢yXeµ
if(x>0) 1 else 0
x´Iþžù‡½Â´k, ´gCþx´˜‡•þž, '(J´
˜‡•þ, ùž^‡Ã{¦^"¤±, ù‡©ã¼êATù?§µ
y <- numeric(length(x))
y[x>0] <- 1
y[x<=0] <- 0
y
kõ‡ifŠéželse†C˜‡é"Œ±¦^“if · · · else if · · · else if · · ·
else · · ·” õ­ä(L«õ©|"õ©|Œ±¦^switch()¼ê"
1.8.2
̂(
̂(¥~^´for̂, ´é˜‡•þ½LÅg?n, ‚ª“for (
name in values) Lˆª”, Xµ
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
30
1˜Ù SŠó0
s <- 0
for(i in seq(along=x)){
cat(’x(’, i, ’) = ’, x[i], ’\n’, sep=’’);
s <- s+x[i];
}
ù‡~f·‚I‡¦^eIŠ, ¤±^seq(along=x))¤
xeI•þ"X
JØI‡eIŠ, Œ±†Xd¦^µ
s <- 0
for(xi in x){
cat(xi, ’\n’)
s <- s + xi
}
,, XJ´‡¦ˆƒÚ, ‡N^sum(x)=Œ"lùp·‚Œ±
wÑ, wªÌ‚²~´Œ±;, |^¼êéz‡ƒOŽŠ!¦^sum
ÚO¼ê9apply!lapply!sapply!tapply¼ê
Œ±“Ô"ϏÌ
‚3S¥´éú(S-PLUSÚRÑ´)ºŠó), ¤±A¦ŒU;¦^wªÌ
‚"
·‚2ޘ‡~f"'X, ·‚‡OŽÓ)FVÇ"b˜k365‡
)F(Ä!F), …ˆ)FVÇ´ƒ(ùpÑ
ccœ¹±
9ŒU3Ñ)FÏ©ÙØþ!)"˜‡kn‡<, nŒu365ž{–
ü‡<k)FƒÓ}´7,¯‡"nuu365ž, ·‚Œ±OŽPr{–
kü<Ó)F}= 1 - Pr{n‡<)F*dØÓ}, ùž, n‡<)FŒŠê
365n , n‡<*dØӌUê365¥n‡üê, *dØӌUê
365 × 364 × · · · × (365 − (n − 1)) = 365!/(365 − n)!"Ïd, 
OŽn=1, 2,
. . . , 365œ¹eÓ)FVÇ, Œ±^Xê¢yµ
x <- numeric(365)
for(n in 1:365){
x[n] <- 1
for(j in 0:(n-1)){
x[n] <- x[n] * (365-j)/365
}
x[n] <- 1 - x[n]
}
ùã§S$1
0.980¦"·‚Œ±¦þ^•þ$Ž5¢y, „݇¯õµ
x <- numeric(365)
for(n in 1:365){
x[n] <- 1 - prod((365:(365-n+1))/365)
}
ùã§S^
0.030¦§'1˜‡§S¯32"5¿ØU†OŽ365!, ù
¬‡ÑêŠL«‰Œ"
ù‡¯K„Œ±^Scumprod¼ê?˜Ú{zµ
x <- 1 - cumprod((365:1)/365)
ÚO^‡§
31
§1.9 S§SO
ùã§Svk?Û̂§$1žm®²Ã{ÿþ"õg­E$1Œ±uy§
'1ã§S¯165"ddŒ„¦^•þ$Ž±9|^XÚJø¼êéJ
pS§SÇ¿Â"
,‡5¿¦^for(i in 1:n)‚ªOê̂ž‡;˜‡~„†Ø, =
n"½Kêž1:n´˜‡lŒÌ‚, ·‚²~I‡´n"½K
êžÒØ?\̂"ˆù˜:, Œ±3̂ ä̂(労Äu
m©Š"
whilê´3m©?ä̂^‡.̂, Xµ
while(b-a>eps){
cc <- (a+b)/2;
if(f(cc)>0) b <- cc else a <- cc
}
´˜ã©{)§§S, ‡¦¼êf(x)´üNþ,"Œ±éN´/rù‡
§S?U¤#Nf(x)üNeü"
„Œ±¦^
repeat Lˆª
̂, 3̂NS^breakaÑ"
3˜‡Ì‚NS^nextLˆªŒ±?\e˜Ó̂"
©|Ú̂(̇^u½Â¼ê"
1.9
S§
§S O
éuE,˜
OŽ¯K·‚AT?¤¼ê"ù‰Ð?´?˜g
Œ±­E¦^, ¿…Œ±éN´/?U, ,Ð?´¼êSCþ¶´ÛÜ
, $1¼êج¦¼êSÛÜCþcóŠ˜m, Œ±;3
pGe†DŠ½ÂéõCþ¦óŠ˜m,ÏÃÙ"
1.9.1
óŠ˜m+n
c¡·‚®²J, S3$1ž±˜‡Cþ|¢´»L, ‡Ö,Cþž
g3d´»L¥é, ˆ£é1˜‡¶‰CþDŠž3|¢´»1˜‡
˜DŠ"3¼êSÜ, |¢´»L1˜‡ ˜´ÛÜCþ¶˜m, ¤±CþD
Š´ÛÜDŠ, DŠCþ3¼ê$1Ïmk"3¼êܧ|¢´»
L1˜‡ ˜´cóŠ˜m§ù‡óŠ˜mò¤k3¼êܽÂ
Cþ±9¼ê§…Œ±3òÑXڞóŠ˜m§e˜g„Œ±w±c
½ÂCþÚ¼ê"
ù§ÏóŠ˜mpé–5õ§Ñ†Å¬OŒ
"¦þróŠÑ
^¼ê¢yŒ±;ù‡¯K§¼êS½ÂCþ´Ûܧج?\có
Š˜m"
Œ±†+nóŠ˜m¥é–"^ls()¼êŒ±wcóŠ˜m
CþÚ¼ê, ^rm()¼êŒ±GØ؎‡é–"Xµ
> ls()
[1] "A"
"Ai"
[10] "p1"
"rec"
> rm(x, x1, x2, x3)
> ls()
[1] "A"
"Ai"
[10] "p1"
"rec"
?Í: oÀº
"b"
"cl"
"tmp.x" "x"
"cl.f"
"x1"
"fit1"
"x2"
"g1"
"x3"
"marks" "ns"
"y"
"b"
"cl"
"tmp.x" "y"
"cl.f"
"fit1"
"g1"
"marks" "ns"
http://www.math.pku.edu.cn/teachers/lidf/
32
1˜Ù SŠó0
ls()Œ±½˜‡patternëê, dëê½Â˜‡šª, ˆ£Îܪ
閶"ª‚ª´UNIX¥grep‚ª"'X, ls(pattern=”tmp[.]”)Œ±ˆ£
¤k±“tmp.”mÞ閶"
rm()Œ±½˜‡¶listëê‰Ñ‡íØ閶, ¤±rm(list=ls(pattern
= ”tmp[.]”))Œ±íؤk±“tmp.”mÞ閶"
1.9.2
¼ê½Â
S¥¼ê½Â˜„‚ª“¼ê¶ <- function(ëêL) Lˆª”"½Â¼ê
Œ±3·-1?1, ~Xµ
> hello <- function(){
+ cat("Hello, world!\n")
+ cat("\n")
+ }
> hello
function(){
cat("Hello, world!\n")
cat("\n")
}
¼êN˜‡EÜLˆª, ˆLˆªƒm^†1½©Ò©m"ؑ)ÒN
^¼êžw«¼ê½Â, Ø´N^¼ê"
3·-1Ñ\¼ê§SéؐB?U, ¤±·‚˜„´‹m˜‡Ù¦?
6§S(XWindowsP¯), Ñ\±þ¼ê½Â, ©‡, 'X
C:\work\ hello.r, ·‚ÒŒ±^
> source("c:\\work\\hello.r")
$1©‡¥§S"¢Sþ, ^source()$1§S؁u¼ê½Â, ?ÛS§S
ь±^ù«ª?Ð2$1, J†3·-1†Ñ\ăÓ, ØLØg
Äw«LˆªŠ"
éu˜‡®k½Â¼ê, Œ±^fix()¼ê5?U, Xµ
> fix(hello)
ò‹m˜‡?6Iw«¼ê½Â, ?U '4I¼êÒ?U
"fix()N
^?6§S"ŽP¯, Œ±^“options(editor=”?6§S¶”)”5½g
CU?6§S"
¼êŒ±‘ëê, Œ±ˆ£Š, ~Xµ
fsub <- function(x, y){
z <- x-y
z
}
Sˆ£Š¼êN ˜‡LˆªŠ, ØI‡¦^return() ¼ê"ØL,
Œ±¦^“return(é–)”¼êl¼êNˆ£N^ö"SUˆ£˜‡é–§¤
±‡ˆ£õ‡(JžI‡^˜‡Lrù
(JCå5ˆ£"
ÚO^‡§
33
§1.9 S§SO
1.9.3
ë ê (g
gC þ )
S¼êN^ªé(¹, ~X, ¼êfsub kü‡JëêxÚy, ·‚^§O
Ž100 − 45, Œ±N^fsub(100,45), ½fsub(x=100, y=45), ½fsub(y=45, x=100),
½fsub(y=45, 100)"=N^ž¢ë†J댱UgS(Ü, Œ±†½J
ë¶(Ü"¢ëk†½
¶iJë(Ü, vk½¶iUgS†e
Jë(Ü"
¼ê3N^žŒ±Ø‰Ñ¤k¢ë, ùI‡3½ÂžJ덽"ŽŠ"
~Xþ¡¼êUµ
fsub <- function(x, y=0) x-y
KN^žØ
Œ±^±þªN^„Œ±^fsub(100), fsub(x=100)ª
N^, ‰Ñvk"ŽŠ¢ë"
=¦vk‰J덽"ŽŠŒ±3N^žŽÑ,‡Jë, , ¼êNSŒ
±^missing()¼êädJë´ÄkéA¢ë, Xµ
trans <- function(x, scale){
if(!missing(scale)) x <- scale*x
#############
}
d¼ê‰
scaleŠžégCþx¦±dŠ, ÄK±Š"ù«^{3Ù§
Šó¥´4ل, SŒ±¢yù˜:´ÏS¼êN^3^ëꊞ
âOŽù‡ëêŠ(¡“Bý¦Š”), ¤±Œ±3N^ž",
ëêØ
áý"
1.9.4
Š^
¼êJë´UŠD4, UCJëŠØUUCéA¢ëŠ"~
Xµ
> x <- list(1, "abc")
> x
[[1]]
[1] 1
[[2]]
[1] "abc"
> f <- function(x) x[[2]] <- "!!"
> f(x)
> x
[[1]]
[1] 1
[[2]]
[1] "abc"
¼êNSCþ´ÛÜ, é¼êNSCþDŠ¼ê(å$1
ŠÒíØ
, ØK5Ó¶CþŠ"~Xµ
?Í: oÀº
Cþ
http://www.math.pku.edu.cn/teachers/lidf/
34
1˜Ù SŠó0
> x <- 2
> f <- function(){
+ print(x)
+ x <- 20
+ }
> f()
[1] 2
> x
[1] 2
ù‡~f¥5k˜‡CþxŠ2, ¼ê¥CþxDŠ20, ¼ê$1 5xŠ¿™Cz"´‡5¿, ¼ê¥w«¼êN^žÛÜCþx„vk
DŠ, w«´ÛCþxŠ"ù´S?§'N´Ñ¯K/µ\^
˜‡ÛÜCþŠ, \vk¿£ù‡ÛÜCþ„vkDŠ, §S%vkц,
Ϗù‡Cþ®kÛ½Â"
1.9.5
§SNÁ
S-PLUSÚR8c„ؖ٧Ì6§SOŠó@äkã/.¡üڋ
l!˜ä:!*Lˆªr§NÁõU"NÁE,S§S, Œ±^˜
Ï^§SNÁ{, ,SJø
˜
NÁ^¼ê"
é?Û§SŠó, ÄNÁÃã,´3I‡/w«CþŠ"
Œ±^print()½cat()w«"~X, ·‚
NÁc¡½Âfsub()¼ê(,§
ù{ü§S¢S´ØI‡NÁ), Œ±w«ü‡gCþŠ9¥mCþ
Šµ
fsub <- function(x, y){
cat(’x = ’, x, ’\n’)
cat(’y = ’, y, ’\n’)
z <- x - y
cat(’z = ’, z, ’\n’)
z
}
SJø
˜‡browser()¼ê, N^ž§S6Ê, Œ±^ls()ÑÛÜCþ, ^
rŒ±wCþ½LˆªŠ, „Œ±?UCþ"~Xµ
fsub <- function(x, y){
browser()
z <- x - y
z
}
·‚$1d§Sµ
> fsub(11:12, 1:3)
Called from: fsub(11:12, 1:3)
Browse[1]> x
[1] 11 12
Browse[1]> y
[1] 1 2 3
Browse[1]> n
ÚO^‡§
35
§1.9 S§SO
debug: z <- x - y
Browse[1]>
debug: z
Browse[1]>
[1] 10 10 8
Warning message:
8Ié–ÝØ´á8Ié–Ýê in: x - y
§S3‘browser()N^ž?\NÁG§Œ±wÙ¥ÛÜCþŠ(Œ
±?U), ^n·-Œ±üڋl$1§31˜g^n·- ‡£ÒUYüÚ
‹l$1"òÑRbrowser()èüŒ^c(3S¥^return())"^QŒ±ˆ£·
-1G"
RJø
˜‡debug()¼ê, debug(f)Œ±‹mé¼êf()NÁ, ‰1¼
êfžgÄ?\üډ1browser()èü, £ÒŒ±üډ1"^undebug(f)'
4NÁ"
1.9.6
§SOÞ~
OS§S´éN´, 3Ðƞ·‚‡¦^·‚l˜„§SO¥Æ
5£¿¿©|^S¥y¤ˆ«Ž{9±ã¼êҌ±
"´, XJ‡
^S?›OŽþŒ§S, ½ö§SI‡uL, ÒI‡5¿˜
S§SOE
|"
^SŠómuŽ{, ­‡˜:´‡P4S´˜‡•þŠó, OŽAT¦þ
ÏL•þ!Ý
$Ž5?1, ¿¿©|^SJøy¤¼ê, ;¦^wªÌ
‚"wªÌ‚¬ŒŒü$S$Ž„Ý, ϏS´)º‰1"
~1. ÄØ£8¯K"Ø£8´šëê£8˜«, bCþY †CþXƒ
m'Xµ
Y = f (X) + ε
Ù¥¼êf ™"*ÿXÚY˜|Xi , Yi , i=1,...,n
µ
,
n
P
x−Xi
n
P
Y
K
i
i
f (x) =
h
K x−X
i=1
h
, éf ˜«O
i=1
Ù¥K‰Ø¼ê, ˜„´˜‡šKó¼ê, :?¼êŠŒ, 3üý×
„ªu""~X—ݼê, ½¤¢Vng¼êص
( 3
3
1
−
|x|
|x| ≤ 1
K(x) =
0
Ù§
R¼êdensity()Œ±?1Ø£8O, ùpŠÞ~·‚éù‡Ž{?1?
§"k5?›Vngؼê§Sµ
kernel.dcube <- function(u){
y <- numeric(length(u))
y[abs(u)<1] <- (1 - abs(u[abs(u)<1])^3)^3
y[abs(u)>=1] <- 0
y
}
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
36
1˜Ù SŠó0
5¿þ¡©ã¼êØ^ifŠé´æ^Ü6•þŠeI{, ù½ÂѼ
ê#N±•þÚê|ŠgCþ"
b·‚‡xÑØO­‚, ˜„·‚˜‡‰Œ†ˆXi ‰ŒƒÓmå
•þx, OŽOÑf (x)Š"*ÿêâgCþ3•þX¥, ÏCþ
3•þY¥, KU·‚˜„§SOg´, Œ±¤e¡S§Sµ
kernel.smooth1 <- function(X, Y, kernel=kernel.dcube,
h=1, m=100, plot.it=T){
x <- seq(min(X), max(X), length=m)
fx <- numeric(m)
for(j in 1:m){
# OŽ1j‡må:£8¼êOŠ
fx[j] <- sum(kernel((x[j]-X)/h)*Y) /
sum(kernel((x[j]-X)/h))
}
if(plot.it){
plot(X, Y, type="p")
lines(x, fx)
}
cbind(x=x, fx=fx)
}
5¿þ¡§S¥^
sum¼ê5;˜­éî"´, þ¡{¥Ek
˜­éĵ, ¦§S$1ú"XÛU§Srù‡Ì‚žQº{
´rOŽw¤´Ý
$Ž"Äk, XJx´˜‡Iþ, Kf(x)Œ±¤µ
f (x) = (K · Y )/(K · 1)
Ù¥K = K x−X
´˜‡†X݃(=ݏn)1•þ, 1´˜1"y
h
3, x¢S´˜‡ݏm•þ, éxz˜‡ƒŒ±OŽ˜‡ݏn1
•
þ
K x[j]−X
,
rù
1•þþeÜ¿˜‡Ý
K(m
×
n),
KKY
ݏm
h
•þ, z˜‡ƒéAu˜‡x[j]"Ü¿Ý
Œ±^Souter()¼ê5OŽµ
f
K
#
#
<- function(u, v, kernel, h) kernel((u-v)/h)
<- outer(x, X, f, kernel=kernel, h=h)
outer()1˜‡gCþéAu(JÝ
1,
1‡gCþéAu
©1
Žz˜1ڐ‡éKm¦1, u´(JOŠ•þµ
fx <- c( (K %*% Y) /
(K %*% matrix(1,ncol=1,nrow=length(Y)) )
ù?Ukernel.smooth1Œ±°{¼êkernel.smooth2"
Ø£8¥I°hÀJ´'J, ·‚Ø£8¼êAT#N^rÑ
\h˜‡•þ, é•þ¥z˜‡I°OŽ˜^[Ü­‚¿x3ã¥, (JŠ
¼êˆ£Š˜"ÖöŒ±ŠöS¢yù‡¼ê, ¿[XÚY*ÿ,
xØO­‚, f (x)^sin(x)"éh·‚ŒU7L^̂5?n
"
~ 2. DaubechiesżêOŽ
ÚO^‡§
37
§1.9 S§SO
Å´Cc525¿˜‡êÆóä, Œ±A^uˆ«êÆ©Û¯
K"Å¥˜‡­‡¼ê‰ºÝ¼ê(scale function), §÷v¤¢VºÝ
§µ
√ X
φ(x) = 2
hk φ(2x − k)
k
˜«AϺݼ괐3k«mþš", ‰;|8";|8ºÝ¼
ꌱ3‰½{hk } ^±eS“úª)¤µ
η0 (x) = I[−0.5,0.5] (x)
ηn+1 (x) =
√
2
2N
−1
X
hk ηn (2x − k)
k=0
Ù¥N ´ê, N =2žh0 =0.482962913145, h1 =0.836516303738, h2 =0.224143868042,
h3 = −0.129409522551"®φ(x)|8(؏"«m)[0, 2N − 1], ηn (x)
|8¹u[−0.5, 2N − 1]¥"Š~f, ·‚5?OŽφ(x)S§S"
(1)Ϗφ(x)´¼ê, ¤±·‚Ux3φ(x)|8¥˜
m…:þ
¼êŠ"
^S“OŽù
Š, ·‚kю{S–“è(ħS(
¦^SŠ{,
E,öŠ^g,Šó£ã)µ
Daubechies.phi <- function(nsample=256, nrep=20){
# nsample --- I‡OŽ:‡ê
# nrep
--- S“gê
x <- I‡OŽx‹I•þ
N <- 2
h <- c(0.482962913145, 0.836516303738,
0.224143868042, -0.129409522551)
y0 <- •þ, eta(x)3n=0žÐŠ,
x3[-0.5, 0.5]Sž1,Ù§0
for(r in 1:nrep){ # S“=̂
y <- †x݃Ó0•þ
for(k in 1:(2*N-1)){ # OŽúª¥¦Ú
yk <- eta(.)3gCþ2x-k?Š*sqrt(2),
^þ˜Úy05Lˆ
y <- y + yk
} # for k
y0 <- y # r#eta(.)˜?y0¥
# Še˜ÚS“m©Š
} # for r
rxÚy3x3[0, 2*N-1]¥Ü©
ˆ£xÚyŠ(x[i], phi(x[i])
}
(2)e ¡ · ‚ Å Ú [ z ù ‡ Ž {, ¿ Š â I ‡ é Ž { ? 1 7 ‡ ? U "
x[i] = a + (i-1)/(nsample-1)*(b-a), ·‚uy, ù‡Ž{¥'…¯K´XÛ
r2x − k?¼êŠly0¥"nŽœ¹´2*x[i]-kTÐu,˜‡x[j], ù
žeta(2*x[i]-k) = eta(x[j]) = y0[j]"XJ2*x[i]-k3ü‡x[j]ƒm, Œ±C
˜‡x[j], ¦^y0[j]"²Á, ù‰{ØUyS“Âñ"¤±, ATN
Ž{¦z˜‡2*x[i]-kÑTÐá3,‡x[j]þ¡"ù‡¦
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
38
1˜Ù SŠó0
2a +
j−1
2(i − 1)
(b − a) = a +
(b − a)
n−1
n−1
Ù¥næ:ê"í
j = 1 + 2(i − 1) − (k + 1)
n−1
b−a
¤±‡n − 1Ub − aØ=Œ"d, a = −1, b = 2N − 1, n = 1 + (2N )m,
mê"´, ùn‡:¹
«m[−1, 0), 3 (J¥´‡¿
ï, ¤±XJ文nsample‡:{ATyn*(2N-1)/(2N)Œu
unsample"
(3) XJ7L‡¦U½æ:ê‰(J{Œ±^‚5Š5¤I
(J"‚5Š¼êapprox(x, y, n), Ù¥(x, y)‡ŠÑ:(ü‡•
þ), n´‡¦æ:ê(lxŠŒŠþ!æ)"
(4)  §SXeµ
# S-examp2.r
# Daubechies S“k|8ÅÄE
Daubechies.phi <- function(nsample=256, nrep=20,
plot.it=T, debugging=F,
nsample.exact=F){
# nsample: æ:ê
# plot.it: ´Äzg±ã
# debuggin: ´ÄÑÑNÁ&E
# nsample.exact: ´Ä¦æ:ê.
#
FALSEžŒ±·˜Œ.
# 5¿: ¤keta(x)| ¿8[-0.5, 2N-1],
# 
S“žOŽ:Ñá3æ:þ¤±‡r
# †à:-1, ræ‡ê·˜Œ.
N <- 2
a <- -1
b <- 2*N-1 # æ«m[-1, 2N-1]
ns <- ceiling((nsample*(2*N)/(2*N-1)-1)/(2*N))*(2*N) + 1
# ns: #æ:ê
m <- (ns-1)/(2*N)
# m: phi(x)æ—Ý(x¶zü kõ‡:)
h <- c(0.482962913145, 0.836516303738,
0.224143868042, -0.129409522551) * sqrt(2)
x <- seq(from=-1, to=2*N-1, length=ns)
# x: phi(x)æ ˜
y0 <- numeric(ns) # Щ¼ê
cond1 <- (x>=-0.5 & x<=0.5)
y0[cond1] <- 1.0
y0[!cond1] <- 0.0
for(r in 1:nrep){ # S“gê
if(debugging && plot.it){
plot(x, y0, type="l", main=paste("Iteration", r))
#locator(n=1)
}
ÚO^‡§
39
§1.10 ã/
y <- numeric(ns) # S“(J
for(k in 0:(2*N-1)){
# ÄkOŽeta(2x-k),
# xéAua, a+1/m, a+2/m, ..., b=a+(ns-1)/m
# ns‡:, ·‚‡éÑ2x-kéAeI=Œ.
# ù
eIp¡k
´Ñ.,
# ‡é31nsS=Œ.
yk <- numeric(ns) # eta(2x-k)
i <- seq(along=x)
j <- 2*i - 1 - (k+1)*m
cond2 <- (j>=1) & (j<=ns)
yk[cond2] <- y0[j[cond2]]*h[k+1]
y <- y + yk
} # k
y0 <- y # #S“Њ
}
# r
xphi <- x[(1+m):ns]
yphi <- y[(1+m):ns]
ns <- ns - m
# cphi(x)l02N-1æ‡ê.
# K
[-1, 0)m‡:
if(plot.it){
plot(xphi, yphi, type="l",
main=expression(phi(x)), xlab="x", ylab="")
abline(h=0)
}
if(nsample.exact){
# 7L‡¦nsample‡:.
# ¦^‚5Š(approx, approxfun)
# ½^Š(spline, splinefun)
xy1 <- approx(xphi, yphi, n=nsample)
xphi <- xy1$x
yphi <- xy1$y
}
invisible(list(xphi=xphi, yphi=yphi))
}
(J„ã1.3"
1.10
ã/
1.10.1
~^ã/
Skérã/õU, §Œ±^{ü¼êN^ׄŠÑê∫ã/, \ÙG
Sã/E⃠Œ±½Nõã/À‘UgC‡¦½›ã/"
§,˜‡AڴӘ‡±ã¼êéØÓêâ閌±ŠÑØÓã/"~
X, ^6.1.2Ö\clêâµµ
> attach(cl)
> plot(Height)
> plot(Sex)
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
40
1˜Ù SŠó0
0.0
0.5
1.0
φ(x)
0.0
0.5
1.0
1.5
2.0
2.5
3.0
x
10
ã 1.3: N=2Daubechiesźݼê
70
8
●
●
●
6
●
●
65
●
●
●
●
60
●
●
4
Height
●
●
●
2
●
●
●
0
55
●
●
5
F
10
M
15
Index
ã 1.4: pÑ:ã
ã 1.5: 5Oªê^/ã
1˜‡plot()±›pÑ:ã(ã1.4), 1‡plot()±›5Oªê^/ã(ã1.5)"
~^±ã¼êplot(), ^plot()Šü‡Cþx†yÑ:ã, ¦^Xe~
{µ
> plot(Height, Weight, main="N­ép£8",
+ xlab="p", ylab="N­")
„ã1.6"þ~ü«
S¥XÛÑ\Šéµ‡Šé²w/™¤('
X, "m)Ò), XÚò‰Ñ˜‡\ҊY1J«"XJÑ\“x <- 1+2”ž‡
ÚO^‡§
41
§1.10 ã/
1, Œ±3DŠÒ , Œ±3\҆2ƒm, ´XJ3x
cŠ, XJ31†\҃mUr1D‰x"
0
−5
(((−50):50)/25)^3
5
KUw«x
−2
−1
0
1
2
((−50):50)/25
ã 1.6: N­épÑ:ã
ã 1.7: ngõ‘ª­‚

±›ë‚ã, ‡3plot()¼ê¥\type=”l”À‘, Xµ
> plot(((-50):50)/25, (((-50):50)/25)^3, type="l")
„ã1.7"Œ±±›Cþª“ã, Xµ
> stem(Height)
The decimal point is 1 digit(s)
to the right of the |
5
5
6
6
7
|
|
|
|
|
1
67789
033344
557779
2
±›˜‡CþÝ/ã, Xµ
> boxplot(Weight)
(J„ã1.8"Œ±±›A‡Cþ¿üÝ/ã, 'XkOŽWeightéHeight
£8r[Ü(J\p1, , ±›¿üÝ/ãµ
> fit1 <- lm(Weight ~ Height)
> p1 <- predict(fit1, cl)
> boxplot(list("N­"=Weight, "ý"=p1))
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
42
55
60
65
70
1˜Ù SŠó0
ã 1.9: ¿üÝ/ã
ã 1.8: N­Ý/ã
„ã1.9"^hist()¼êŒ±±›†ã"~Xµ
> hist(Weight)
Normal Q−Q Plot
Histogram of Weight
8
140
●
●
100
Sample Quantiles
●
●
●
●
● ●
●
●
●
●
●
80
4
Frequency
6
120
●
●
●
●
0
60
2
●
●
40
60
80
100
120
140
160
−2
Weight
ã 1.10: N­†ã
„ã1.11"
ÚO^‡§
0
1
Theoretical Quantiles
ã 1.11: N­VÇã
„ã1.11"^qqnorm()¼ê±›VÇã, Xµ
> qqnorm(Weight)
> qqline(Weight, col="red")
−1
2
43
§1.10 ã/
p?ã/¼ê
1.10.2
Sã/¼ê©üaµp?ã/¼ê—†±›ã/¿ŒgÄ)¤‹I¶
Náã/ƒ¶$?ã/¼ê—Œ±?U®kã/½ö±ã5½˜
À
J‘"p?ã/¼êo´m©˜‡#ã"e¡·‚0~^p?ã/¼ê,
±9^5?»ù
p?ã/¼ê~^ŒÀëê"
~^´plot()¼ê"'X, plot(x,y)(Ù¥x, y´•þ)éü‡CþxÑ:
ã"XJz´˜‡½Â
xCþÚyCþL, ½ö˜‡üÝ
K^plot(z)
Œ±xÑ:ã"XJx´˜‡žmSé–(žmSé–^ts()¼ê)¤),
plot(x)±›žmS­‚ã"XJx´˜‡Êϕþ, K±›xŠéÙeI
Ñ:ã"XJx´Eê•þK±›JÜé¢ÜÑ:ã"XJf´˜‡Ïf,
Kplot(f)±›f^/ã(z‡ÏfY²‡ê)"XJf´Ïf, y´ÓÝêŠ
•þ, Kplot(f,y)éfz˜ÏfY²±›y¥ƒAêŠÝ/ã"XJd´˜‡
kêŠ.êâêâµ, Kplot(d)édzü‡CþƒmŠã(Ñ:ã)"
XJX´˜‡êŠ.Ý
½êâµ, ^pairs(X)Œ±±›züƒmÑ:
ãÝ
"ù3Cþ‡êØõžŒ±Óžwõ‡Cþüü'X, Cþõ
žKz‡ã§¿Â،"
Óã(coplot)´˜«õCþ&¢5©Ûã/"Ù/ªcoplot (y ∼ x
| z), Ù¥xÚy´êŠ.•þ, z´ÓÝÏf"ézz˜Y², ±›ƒA|
xÚyÑ:ã"X:
> attach(cl)
> coplot(Weight ~ Height | Sex)
Given : Sex
M
F
z
55
60
65
70
y
140
●
●
100
●
●
●
●
●
●
●
●
●
●
80
Weight
120
●
●
●
●
x
●
60
●
●
55
60
65
70
Height
ã 1.12: ±5O^‡N­ép
Ñ:ã
ã 1.13: ‘—Ý­¡
)ã1.12, éØÓ5O©O±›
N­épÑ:ã"XJz´˜‡êŠ.
Cþ, Kcoplot()kézŠ©|, , ézz˜|Š©O±ã"$–Œ±^
Xcoplot (y∼ x | x1+x2)L«éx1Úx2z˜Y²|ܱã"coplot()Úpairs()¼
ê"Ž±›Ñ:ã, Œ±^˜‡panel=ëꍽ٧$?±ã¼ê, Xlines,
panel.smooth "
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
44
1˜Ù SŠó0
−3
0
−3
−2
−2
−1
−1
0
y
1
1
2
3
2
3
qqnorm(x), qqline(x), qqplot(x,y)Š© ê-© êã"qqnorm(x)é•þxŠ
VÇ(p¶gSÚOþŠ, éATgSÚOþIO©Ù© êŠ)"qqline(x)3qqnorm(x)ãþ¡x˜^[Ü­‚"qqplot(x,y)rxÚyg
SÚOþ©Ox3x¶Úy¶±'ü‡Cþ©Ù"
hist(x)Š•þx†ã""ŽžgÄ(½©|, Œ±^nclass=ëꍽ
©|‡ê, ½ö^breaks=ëꍽ˜‡©|:•þ"XJ½
prob=TKp
¶w«—ÝO"
SŒ±Šn‘ã!Š‚ãڊÚã, ¼êpersp(), contour() Úimage(),
X:
−3
−3
−2
−1
0
1
2
−2
−1
0
1
2
3
3
x
ã 1.14: Š‚ã
ã 1.15: ŠÚã
x <- seq(-3,3, length=100)
y <- x
f <- function(x,y,ssq1=1, ssq2=2, rho=0.5){
det1 <- ssq1*ssq2*(1 - rho^2)
s1 <- sqrt(ssq1)
s2 <- sqrt(ssq2)
1/(2*pi*sqrt(det1)) * exp(-0.5 / det1 * (
ssq2*x^2 + ssq1*y^2 - 2*rho*s1*s2*x*y))
}
z <- outer(x, y, f)
persp(x, y, z)
contour(x, y, z)
image(x, y, z)
„ã1.13, ã1.14, ã1.15"
1.10.3
p?ã/¼ê~^À‘
p?ã/¼êk˜
ÓÀ‘, Š¼êŒÀëê(gCþ)"~Xµ
> plot(x, main="Graph of x")
ÚO^‡§
45
§1.10 ã/
Ù¥mainÒ´˜‡ŒÀëê, ^5½ã/IK"vkdÀ‘žã/Òv
kIK"L1.1Ñ
ùÀ‘"
add=T
axes=F
log=”x”
log=”y”
log=”xy”
type=
type=”p”
type=”l”
type=”b”
type=”o”
type=”h”
type=”s”
type=”S”
type=”n”
xlab=”iÎG”
ylab=”iÎG”
main=”i Î
G”
sub=”iÎG”
1.10.4
L 1.1: p?ã/À‘
¦¼ê•$?ã/¼ê@Ø´m©˜‡#ã/´3
ãÄ:þV\"
6Øx‹I¶, ‘ Œ±^axis()¼ê°(/5½‹I
¶x{""ŽŠ´axes=T, =k‹I¶"
rx¶, y¶½ü‡‹I¶^éêÝ±›"
5½±ãªµ
±:
x‚
±:¿3¥m^‚ë
±:¿x‚BLˆ:
l:î¶xR‚
F¼ê¶†ëY
F¼ê¶mëY
Øx?Û:!‚, Ex‹I¶¿ïá‹IX, ·^u
¡^$?ã/¼êŠã"
½Âx¶Úy¶I\""Žž¦^閶"
ã/IK"
ã/IK, ^iNx3x¶e"
$?ã/¼ê
p?ã/¼êŒ±×„{B/±›~„a.ã/, ´, ,
œ¹e\Œ
UF"±›˜
kAχ¦ã/"'X, \F"‹I¶UìgCO±›,
3®kãþO\,˜|êâ, 3ã¥\\˜1©5º, ±Ñõ‡­‚“L
êâI\, "$?ã/¼ê4\3®kãÄ:þ?1V\"
L1.2Ñ
~^$?ã/¼ê"
$?ã/¼ê˜„I‡½ ˜&E, Ù¥‹I´¤¢^r‹I, =
c¡p?ã/¼ê¤ïá‹IX¥‹I"‹IŒ±^ü‡•þxÚy‰Ñ,
Œ±d˜‡üÝ
‰Ñ"XJpŠãŒ±^e¡0locator()¼ê
5p/lã/¥†Ñ\‹I ˜"
1.10.5
pã/¼ê
S$?ã/¼êŒ±3®kã/Ä:þV\#SN, ,, S„Jø
ü
‡¼êlocatorÚidentifyŒ±4^rÏL3ã¥^àI:Â5(½ ˜"
¼êlocator(n, type)$1ž¬Êe5–^r3ã¥:Â, , ˆ£ã/¥
àI: ˜‹I"–:ž^àIm…:Œ±ÀJʎ–, á=
ˆ£"ëên½:Âõg gÄʎ, "Ž500g¶ëêtypeXJ¦^K
Œ½±:a., †plot()¼ê¥typeëê^{ƒÓ, 3àI:Â?±:(‚!
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
46
points(x,y)
lines(x,y)
rug(x)
rug(x,side=2)
text(x,y, labels, ...)
abline(a, b)
abline(h=y)
abline(v=x)
abline( lm.obj)
polygon(x, y, ...)
legend(x, y, legend,
...)
legend(, angle=v)
legend(, density=v)
legend( , fill=v)
legend(, col=v)
legend(, lty=v)
legend(, pch=v)
legend(, marks=v)
title(main, sub)
axis(side, ...)
ÚO^‡§
1˜Ù SŠó0
L 1.2: $?ã/¼ê
3 c ã / þ U \ ˜ | : ½ ‚ " Œ ± ¦ ^plot()
type=ë ê 5  ½ ± ›  {, " Ž žpoints() x :,
lines()x‚"
rug(x)3x¶ ^ ˜ á  Ý ‚ L « * ÿ xê
Š"rug(x,side=2)Œ±r*ÿá‚x3y¶"
3 d ‹ IxÚy‰ Ñ ˜ I Ñ dlabels ½ i
Î G "labelsŒ ± ´ ê Š . ½ i Î . • þ, labels[i]3x[i],y[i]?IÑ"
3cã/þx˜^†‚"ü‡ëêa, b©Ù‰Ñ
åÚÇ"½h=ëꞱ›Y²‚,½v=ëêž
±›R†‚"±˜‡¦[Ü(Jlm.objŠë
êždlm.obj$coefficients ¤
‰Ñ†‚åÚ
Ç"
U•þx‰Ñî‹Iڕþy‰Ñp‹I(½º:
±›õ>/"Œ±^col=ëꍽ˜‡ôÚW¿õ>
/SÜ"
legend¼ê^53cã/½‹I ˜±›ã
~"ã~`²©id•þlegendJø"–e¡
vŠ‡‰Ñ±(½‡éŸoã~?1`², v´
݆legendƒÓ•þ"
angleëꍽA«ÒK"
densityëꍽA«ÒK—Ý"
fillëꍽA«W¿ôÚ"
colëꍽA«ôÚ"
ltyëꍽA«‚."
pchëꍽA«Ñ:ÎÒ"iÎ.•þ"
marksëꏍ½A«Ñ:ÎÒ,¦^Ñ:ÎÒêŠ
“Ò,êŠ.•þ"
±›dmain½IKÚdsub½IK"
±›˜^‹I¶"ùƒc±ã¼ê7L®²
^axes=FÀ‘³›
gċI¶"ëêside ½
3=˜>±›‹I¶,Š14,1e>,, _ž
ê"Œ±^at=ëꍽÝ ˜,^labelsëꍽ
Ý?I\"
47
§1.10 ã/
R‚, )"locator()ˆ£Š´˜‡L, kü‡Cþ(ƒ)xÚy, ©O
: ˜î‹IÚp‹I"
~X, 
3®²±›­‚ã¥é˜‡˜/Iþ˜1©, ‡¦^X
e§Sµ
> text(locator(1), "Normal density", adj=0)
text()¼ ê adjë ê ^ ˜ ‡ ê i L « © G ƒ é u ‰ ½ ‹ I x {,
adj=0 L«‰½‹I©G†ý‹I, adj=1L«‰½‹I©Gm
ý‹I, adj= 0.5L«‰½‹I©G¥m‹I"
¼êidentify(x, y, labels)3$1ž¬Êe5–^r:Â, †U
àI
m…, , ˆ£^r3ã/¥^àI:Â:SÒ, :žé:Â:\I
\"ëêxÚy‰Ñ‡£Oˆ‡:‹I"labelsëꍽ:Â,‡:ž‡3
>±›©I\, "ŽžIÑd:SÒ, XJI‡ˆ£ŠØŽx?
ÛIPKŒ±3N^d¼êž\˜‡plot=Fëê"5¿identify()†locator()Ø
Ó, locator()ˆ£ã¥?¿: ˜‹I, identify()ˆ£l: ˜C
:SÒ"
~X, ·‚3•þxÚy¥keZ‡:‹I, $1Xe§Sµ
> plot(x, y)
> identify(x, y)
ùžw«=£ã/I, ?\–G, ^rŒ±:Âã¥AO:, T:
SÒÒ¬3>IÑ"
(å, ‡üÂm…¿ÀJʎ"ˆ£(J\:
ˆ‡:SÒ"
1.10.6
ã/ëê¦^
c¡·‚®²w
XÛ^main=, xlab=ëê55½p?ã/¼ê˜
˜"3¢S±ã, AO´±›^uü«½Ñ‡ã/ž, S^"Ž˜±›
ã/
ØU÷v·‚‡¦"SJø
˜X¤¢ã/ëê, ÏL¦^ã/
ëꌱ?Uã/w«¤kˆ¡˜"ã/ëê)'u‚.!ôÚ!
ã/ü!©éàªˆ«˜"z‡ã/ëêk˜‡¶i, 'Xcol“L
ôÚ, col=”red”L«ùÚ"z‡ã/(Šã8/)k˜@üÕã/ë
ê"
˜ã/ë꩏ü«µ˜†ž˜"˜¦^par()¼ê?
1˜, ˜ 3òÑc˜†±k¶ž˜K´3ã/¼ê¥\\ã/
ëê, Xþ¡~fµ
> text(locator(1), "Normal density", adj=0)
¥adjëê"
par()¼ê^5–¯½?Ucã/ã/ëê"XJؑëêN^,
Xµ
> par()
$adj
[1] 0.5
$ann
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
48
1˜Ù SŠó0
[1] TRUE
#################
$ylog
[1] FALSE
(J˜‡L, Lˆƒ¶ã/ëê¶i, ƒŠƒAã/ëê
Š"
XJN^ž½˜‡ã/ë궕þŠëê, Kˆ£½ã/ë
êLµ
> par(c("col", "lty"))
$col
[1] "black"
$lty
[1] "solid"
N^ž½¶iã/ëê¶k¶ëê, K?U½ã/ëê, ¿ˆ£
ŠLµ
> oldpar <- par(col=4, lty=2)
> oldpar
$col
[1] "black"
$lty
[1] "solid"
Ϗ^par()?Uã/ëê´±òѱcÑk, …=¦´3¼êS
d?UE´Û, ¤±·‚Œ±|^Xe.^{, 3¤?Ö ¡E5
ã/ëêµ
> oldpar <- par(col=4, lty=2)
###### (I‡?Uã/ëê±ã?Ö)
> par(oldpar) # ¡E©ã/ëê
Ø
–þ¡@^par()¼ê[È?Uã/ëê, ·‚„Œ±3A?Ûã/
¼ê¥½ã/ëꊏk¶ëê, ù?U´ž, éd¼êåŠ^"
~Xµ
> plot(x, y, pch="+")
Ò^ã/ëêpch½
±Ñ:Îҏ\Ò"ù‡½éù˜Üãk, é
± ã/vkK"
ÚO^‡§
49
§1.10 ã/
1.10.7
ã/ëê)
u±›kAÏI‡ã/´S˜‡r‘, ¦^ã/ëê´¤da?
Ö­‡Ãã, ·‚3ùp[/0Sˆ«ã/ëê"ù
ã/ëꌱ
ŒNþ©±eA‡Œa, ·‚ò©O0µ
• ã/ƒ››
• ‹I¶†‹IÝ
• ã/>˜
• ˜õã
˜!ã/ƒ
ã/d:!‚!©!õ>/ƒ¤"eã/ëê^5››ã/
ƒ±›[!µ
• pch=”+” ½^u±›Ñ:ÎÒ"±›:
‹I ˜,kpch=”.”vkù‡¯K"
Ñpu½$u½
• pch=4 XJpchŠl018ƒm˜‡êi,ò¦^Aϱ:Î
Ò"e~Œ±w«¤kAϱ:ÎÒµ
> plot(c(0, 100), c(0, 100), type="n",
+
axes=F, xlab=’’, ylab=’’)
> legend(10,90, as.character(0:9), pch=0:9)
> legend(50,90, as.character(10:18), pch=10:18)
• lty=2
½x‚^‚.""ŽŠlty=1´¢‚"l2m©´ˆ«J‚"
• lwd=2 ½‚o[,±IO‚o[ü "ù‡ëêKê⭂‚
°±9‹I¶‚°"e~±›u­‚ãµ
>
>
>
>
>
>
oldpar <- par(lwd=2)
x <- (0:100)/100*2*pi
plot(x, sin(x), type="l", axes=F)
abline(h=0)
abline(v=0)
par(oldpar)
• col=2 ½ôÚ,ŒA^u±:!‚!©!W¿«!ã–"ôڊ
Œ±^–”red”,”blue” ùôÚ¶½"¼êcolors()(JR¥½
ÂôÚ¶•þ"
• font=2 ^5½iNê"˜„font=1´N,2´ç
ç N, 3´N,4´
çN"
• font.axis, font.lab, font.main, font.sub
I\!IK!IK¤^iN"
©O^5½‹IÝ!‹I¶
• adj=-0.1 ½©ƒéu‰½‹Iéàª"0L«†éà,1L
«méà,0.5L«Ø¥"dëꊢS“L´Ñy3‰½‹I†>
©'~, ¤±adj=-0.1J´©Ñy3‰½‹I ˜m>¿
˜Ñƒu©10%Ýål"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
50
1˜Ù SŠó0
• cex=1.5
½iȌê"
!‹I¶†‹IÝ
Nõp?ã/‘k‹I¶, „Œ±kØx‹I¶, ^axis()üÕ\"¼
êbox() ^5x‹I«o±µ‚"
‹I¶)n‡Ü‡µ¶‚(^ltyŒ±››‚.), Ý‚, ÝI\"§‚
Œ±^Xeã/ëê5››µ
• lab=c(5, 7, 12) 1˜‡êx¶F"xA‡Ý‚,1‡êy¶F"
xA‡Ý‚,ùü‡ê´ïÆ5¶1n‡ê´‹IÝI\°Ý
õ‡iÎ,)ê:,ù‡ê¬¦ÝI\oÊ\¤˜Š"
• las=1 ‹IÝI\•"0L«o´²1u‹I¶,1L«o´Y
²,2L«o´R†u‹I¶"
• mgp=c(3,1,0) ‹I¶ˆÜ‡ ˜"1˜‡ƒ‹I¶ ˜‹I
¶I\ål,±©1pü "1‡ƒ‹I¶ ˜‹I
ÝI\ål"1n‡ƒ‹I¶ ˜¢Sx‹I¶ål,Ï~
´0"
• tck=0.01 ‹I¶Ý‚Ý,ü ´±ã«Œ,ŠÓ±ã«'
~" tcku0.5žx¶Úy ¶Ý‚òژƒÓÝ"1ž=x‚
f‚"KŠžÝ‚x3±ã«¡"
• xaxs=”s”, yaxs=”d”
››x¶Úy¶x¶{"
Š”s”£=standard¤½”e”£=extended¤žÿê≌››3
ÝځŒÝƒm"”e”žXJkêâ:›©‚C>¶‰Œ¬
ч*Œ"ù«x¶ªkž¬3¶˜>3eŒ˜x"
Š”i”£=internal¤½”r”£d"Ž¤¦Ý‚Ñá3ê≌
SÜ,”r”ª¤3>˜"
Š”d”ž¬£½d‹I¶, Yã/Ѧ^†§ƒÓ‹I
¶, ù3‡)¤˜XŒ'ã/žÿ´k^"‡)Ø£½I‡r
ù‡ã/ëêÙ§Š"
n!ã/>˜
S¥˜‡üÕãd±ã«(±ã:!‚x3ù‡«¥)ڝŒ±ã
«>˜|¤, >˜¥Œ±¹‹I¶I\!‹I¶ÝI\!IK!I
K, ±ã«˜„‹I¶Œ"„ã1.16"
>˜Œdmaiëê½marëꛛ, §‚Ñ´o‡ƒ•þ, ©O5½
e!†!þ!m>˜Œ, Ù¥maiŠü ´=€, mar
Šü ´©1pÝ"~Xµ
> par(mai=c(1, 0.5, 0.5, 0))
> par(mar=c(4, 2, 2, 1))
ùü‡ã/ëêØ´Õá, ½˜‡¬K,˜‡"S"Žã/>˜~~
Œ, ±–ukžã/Iž>˜Ó
‡ã/錘ܩ"Ï~·‚Œ
±žm>˜, ¿…3Ø^IKžŒ±ŒŒ þ>˜"~Xe~Œ±)¤›
©;nã/µ
ÚO^‡§
51
§1.10 ã/
ã 1.16: ã/>˜
> oldpar <- par(mar=c(2,2,1,0.2))
> plot(x,y)
3˜‡¡þxõ‡ãž>˜gÄ~Œ, ·‚
âU¦õ‡ãk¿Â"
„I‡?˜Ú~>˜
o!˜õã
RŒ±3Ә¡meZ‡U1!üI‚, 3z‡I‚¥Œ±Š˜Ì
ã"z‡ãkgC>˜, ¤k㡌±˜‡“>˜”, „ã1.17"
˜õã^mfrowëê½mfcolëê5½, Xµ
> par(mfrow=c(3,2))
L«Ó˜kn1ü8‡ã, …gSU1W˜"aq/,
> par(mfcol=c(3,2))
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
52
1˜Ù SŠó0
ã 1.17: ˜õã
5½ƒÓI‚(, ´gSUW˜, =kW÷1˜n‡2W1
"‡ž˜õ㐇2$1
> par(mfrow=c(1,1))
=Œ"
"ŽžÃ>˜"
5½>˜Œ, Œ±3par()¥^omiëê½omaë
ê" omiëê¦^=€ü , omaë걩1pü , ü‡ëêþo‡
ƒ•þ, ©O‰Ñe!†!þ!m>˜Œ"Xµ
> par(oma=c(2,0,3,0))
¼êmtext^53>˜¥\©iI5"Ù^{
mtext(text, side = 3, line = 0, outer = FALSE)
Ù¥text‡\©SN, sideL«3=˜>(1e, 2†, 3þ, 4m),
line L«>˜lp•ê1A1, p¡˜1´10Ò, outer=TRUEž¦^
>˜, ÄK¬¦^cã>˜"~Xµ
ÚO^‡§
53
§1.10 ã/
> par(mfrow=c(2,2), oma=c(0,0,3,0), mar=c(2,1,1,0.1))
> plot(x);plot(y);boxplot(list(x=x,y=y));plot(x,y)
> mtext("Simulation Data", outer=T, cex=1.5)
3õジ¥„Œ±^mfgëê5†a,˜‡I‚, 'X
> par(mfg=c(2,2,3,2))
L«3n1üõジ¥†a111 ˜"mfgëê ü‡
L«õジ1!ê, cü‡L«‡a ˜"
Œ±Ø¦^õジ†3¡¥?¿ ˜)˜‡I‚5±ã, ëê
fig, Xµ
> par(fig=c(4,9,1,4)/10)
dëꏘ‡•þ, ©O‰ÑI‚†!m!e!þ> ˜, ŠÓ
¡'~, 'Xþ¡~f3¡mem˜‡I‚Šã"
1.10.8
ã/
SŠã|±ˆ«ã/, Ù¥~^´w«ìÚPostScript‹<Å"3˜
‡S$1ÏmŒ±kõ‡ã/Ӟ3"3R¥, ^
> x11()
‹mã/I±ã, 3S-PLUS¥, ^
> win.graph()
‹mã/I±ã"2gN^ù¼êò‹m1‡ã/I"^
> dev.list()
Œw«±‹mã/L"
‡'4˜‡ã/, ^
> dev.off()
ùŒ±¦ã/±¤, ~Xéupostscript'4žŒ¤‹<½
"^graphics.off()¼êŒ±'4¤k‹mã/"
MS WindowseRŒ±rw«I¥ã/E›}b†½ˆ«‚ª
ã/©‡, )WMF, PostScript, PDF, PNG, BMP, JPEG, ù·‚Œ±
^R)¤¤Iã/, I‡‚ª"MS WindowseS-PLUSäkaq
õU"
ˆ‡RÚS-PLUSÑ|±)¤PostScriptã/õU, )¤ã/Œ±†
^uLATEXü‡"XJ^MS Wordü‡KŒr¶4ã/WMF‚ª")
¤ PostScript ©‡Œ±^Xe¼ê‹mµ
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
54
1˜Ù SŠó0
> postscript(file="result1.ps",
+ horizontal=FALSE, width=5, height=3)
ùž^ã/·-)¤˜‡¡ã/, , ^dev.off()'4, KŒ)¤©
‡result1.ps"postscript()¼ê¥horizotalëꍽ´Äòã^=90ݦx¶
²1u’>, widthÚheight5½ã°Úp, ü ´=€(1=€¸2.54f
’)"
3‹m
õ‡ã/ Œ±^dev.set()¼ê5ÀJc, dev.next()
Ú dev.prev()©Oˆ£e˜‡Úþ˜‡ã/"'Xdev.set(dev.prev())ÀJ
þ˜‡ã/"
SÐ
Ð Ú O
1.11
S-PLUSÚR¥®²8¤
~^ÚOõU, ˜
'#ÚO{
¢y"
N^S¼êŒ±¤ÄÚO"ùp·‚0XÛ^S?1£ãÚO!&
¢5êâ©Û!©ÙïÄ!~„bu"
1.11.1
üCþêâ©Û
é˜3•þ½êâµ¥Cþ·‚Œ±^summary()¼ê5OŽA‡£ãÚ
Oþ"~X:
> summary(cl)
Name
Length:19
Mode :character
Sex
F: 9
M:10
Age
Min.
:11.00
1st Qu.:12.00
Median :13.00
Mean
:13.32
3rd Qu.:14.50
Max.
:16.00
Height
Min.
:51.30
1st Qu.:58.25
Median :62.80
Mean
:62.34
3rd Qu.:65.90
Max.
:72.00
Weight
Min.
: 50.50
1st Qu.: 84.25
Median : 99.50
Mean
:100.03
3rd Qu.:112.25
Max.
:150.00
éêŠ.CþOŽ
²þŠ, Œ!Š, ¥ ê, o©ƒ˜Úo©ƒn© ê¶éÏfOŽ
ªêÚO"
éêŠ.CþxŒ±^mean, sd, var, median, min, max¼êOŽÙ{ü
ÚOþ"Œ±^stem(x)±›xª“ã, ^hist(x)x†ã, ^boxplot(x)xÝ
/ã, ^qqnorm(x)Úqqline(x)xQQã"

OêŠ.Cþ©Ù—Ý, Ø
^hist()x†ã„Œ±^density()¼
ê?1šëê—ÝO"~X:
plot(density(Height), main="Height density")
„ã1.18"„Œ±{r—Ý­‚\†ãþ, Xµ
h1 <- hist(Height, prob=T, plot=T)$density
h2 <- density(Height)
hist(Height, prob=T, ylim=range(h1, h2$y))
lines(h2)
ÚO^‡§
55
§1.11 SÐÚO
0.04
0.00
0.02
Density
0.06
Height density
50
60
70
80
N = 19 Bandwidth = 2.561
ã 1.18: pšëê—ÝO
0.04
0.00
0.02
Density
0.06
Histogram of Height
50
55
60
65
70
75
Height
ã 1.19: p†ã9—ÝO
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
56
1˜Ù SŠó0
„ã1.19"
éÏf(©aCþ)fŒ±^table(f)ÚOÙªê©Ù, ^plot(f)xÙªê©Ù
ã"X
> table(Sex)
Sex
F M
9 10
·‚Œ±½Â¼ê5xÑ~„&¢5êâ©Ûã/"éëY.CþŒ±
x†ã!Ý/ã!©Ù—ÝOãÚVÇã"élÑ.Cþ‡xÙ©
Ùªê^/ã=Œ, ©Ùªê^table¼êOŽ"ïÄSƒ'5Œ±ŠžmS
ãÚgƒ'¼êã"Ϗù
ã²~­E¦^, ·‚r§‚½Â¼ê, 3Ó
˜¡xѵ
eda.shape <- function(x) {
oldpar <- par(mfcol = c(2, 2),
mar=c(2,2,0.2,0.2), mgp=c(1.2,0.2,0))
hist(x, main="", xlab="", ylab="")
plot(density(x), xlab = "x",
ylab = "", main="")
boxplot(x)
qqnorm(x, main="", xlab="", ylab="")
qqline(x)
par(oldpar)
invisible()
}
eda.ts <- function(x) {
oldpar <- par(mfrow=c(2,1),
mar=c(2,2,1,0.2), mgp=c(1.2, 0.2, 0))
plot.ts(x, main="", xlab="")
acf(x, main="", xlab="")
par(oldpar)
invisible()
}
¼ê¥ invisible()L«3·-1N^d¼êžØ‡w«?ۈ£Š"¼ê
density ^5ŠØ—Ý­‚O"
1.11.2
bu
Rstats§S(package)Jø
~„buõU, stats"ŽžgÄN
\"

u5, ‡N^shapiro.test()¼êҌ±Shapiro-Wilkuµ
> shapiro.test(Height)
Shapiro-Wilk normality test
data: Height
W = 0.9791, p-value = 0.9312
ÚO^‡§
57
§1.11 SÐÚO
Œ„pêâ5éÐ"
¼êt.test()Œ±?1üoNtu!üÕáoNtuÚ¤étu"~
X,e¡§SupþŠ´Äµ = 60:
> t.test(Height, mu=60)
One Sample t-test
data: Height
t = 1.9867, df = 18, p-value = 0.06239
alternative hypothesis: true mean is not equal to 60
95 percent confidence interval:
59.86567 64.80801
sample estimates:
mean of x
62.33684
"Ž?1´Výu"
?1üýuŒ±½alternative=”greater”
½”less”"X:
> t.test(Height, mu=60, alternative="less")
One Sample t-test
data: Height
t = 1.9867, df = 18, p-value = 0.9688
alternative hypothesis: true mean is less than 60
95 percent confidence interval:
-Inf 64.3765
sample estimates:
mean of x
62.33684
éu^©|CþL«©|ü‡Õá|, Œ±^t.test(©ÛCþ∼ ©|C
þ)N^/ª?1Õáü|tu"Xµ
> t.test(Height ~ Sex)
Welch Two Sample t-test
data: Height by Sex
t = -1.4513, df = 16.727, p-value = 0.1652
alternative hypothesis:
true difference in means is not equal to 0
95 percent confidence interval:
-8.155098 1.512875
sample estimates:
mean in group F mean in group M
60.58889
63.91000
5¿þ¡^´ØI‡b½ü|ƒWelchu"XJb½ü|ƒ
Œ±3¼êN^¥\ëê var.equal=TRUE"XJü‡Õá|˜3
ü‡C
þX1ÚX2 ¥KŒ±†^ t.test(X1, X2)?1'"
^wilcox.test(©ÛCþ∼ ©|Cþ) ½wilcox.test (X1, X2) Œ±?1Õáü
|Wilcoxon Úu"
éu¤é*ÿCþX, Y,‡'‡^t.test(X, Y, paired=TRUE)"^
wilcox. test (X, Y, paired=TRUE)Œ±?1WilcoxonÎҝu"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
58
1˜Ù SŠó0
1.12
SÚ
ÚO . { 0
ù˜!·‚{ü0SÚO."S¥¢y
A¤k~„ÚO., …õ«.Œ±^˜«Ú˜*:L«Ú?n"ù¡S-PLUS¡, §¢y
Nõ#ÚOïĤJ, RϏ´˜‡gd^‡¤±ÚO.Ü©„ƒé
j""¯¢þ, NõÚOÆ[ïÄÑÚOŽ{ѱS-PLUS§SuL, Ï
SŠó´˜«AOk|uÚOOŽ?§Šó"
ÆSù˜!I‡·‚ä‚..!‚.£8!©ÛÄ£"
1.12.1
ÚO.L«
éõÚO.Œ±^˜‡‚..5L«µ
yi =
p
X
βj xij + ei , ei ∼ iidN (0, σ 2 ), i = 1, 2, . . . , n
j=0
¤Ý
/ª
y = Xβ + e
Ù¥yÏCþ, X.Ý
½¡O
, ˆx0 , x1 , . . . , xp ˆgCþ,
x0 ~~´˜1, ½Â˜‡.å‘"
3S¥.´˜«é–, ÙLˆ/ª‰˜‡úª(formula), ·‚kÞA‡~
f5w˜w"b½y, x, x0, x1, x2, . . . ´êŠ.Cþ, X´Ý
, A, B, C, . . . ´
Ïf"
• y ∼ x, y ∼ 1+x ü‡ªfÑL«yéx{ü˜‚.£8"1˜‡ª
f‘kÛ¹å‘,1‡ªfrå‘wª/
Ñ5"
• y ∼ −1+x, y ∼ x−1 ÑL«yéxÏL:£8,=ؑ呣
8"
• log(y) ∼ x1 + x2
L«log(y)éx1Úx2£8,‘kÛ¹å‘"
• y ∼ poly(x, 2), y ∼ 1 + x + I(x^2) L«yéx˜gõ‘ª£8"
1˜«/ª¦^õ‘ª,1«/ª†¦^xˆ˜g"
• y ∼ X + poly(x,2) ÏCþyõ£8,.Ý
)Ý
X,±9x
gõ‘ªˆ‘"
• y∼A
˜«ª©|©Û,Iy,©|σA"
• y∼A+x
þx"
˜«ª©|©Û,Iy,©|σA,‘kC
• y ∼ A*B, y ∼ A + B + A:B, y ∼ B %in% A, y ∼ A/B šŒ\üσ
©Û.,Iy,A,B´ü‡Ïƒ"cü‡úªL«ƒÓ©a
O, ü‡úªL«ƒÓi@©aO"
• y ∼ (A + B + C)^2, y ∼ A*B*C − A:B:C L«nσÁ,Äü
üpŠ^ØÄn‡ÏƒmpŠ^"ü‡úª´d"
• y ∼ A * x, y ∼ A / x, y ∼ A / (1 + x) - 1 ÑL«éÏfAz˜Y²
[Üyéx‚.£8,n‡úª?萪ØÓ" ˜«/ªéA
z˜Y²Ñ©OOå‘ÚǑ"
ÚO^‡§
59
§1.12 SÚO.{0
• y ∼ A*B + Error(C)
O"
L«kü‡?nσAÚB,Ø© dσC(½
3S¥∼$ŽÎ^5½Â.úª"˜„‚..úª/ª
ÏCþ∼ 1˜‘[±] 1‘[±] 1n‘[±] . . .
Ù¥ÏCþŒ±´•þ½Ý
, ½ö(J•þ½Ý
Lˆª"[±]´\
Ò+½ö~Ò−, L«3.¥\\˜‘½K˜‘, 1˜‘c¡XJ´\Ҍ
±ŽÑ"
úª¥ˆ‘Œ±µ
• ˜‡Š•þ½Ý
Lˆª, ½1"
• ˜‡Ïf
• ˜‡“úªLˆª”, d“úª$ŽÎ”rÏf!•þ!Ý
ë¤"
z˜‘½Â
‡\\.Ý
½l.Ý
¥íØeZ"˜‡1L«˜‡
å‘, ؚwª/íØo´Û¹/)3.úª¥"
“úª$ŽÎ”½ÂÚGlim!Genstat^‡¥½Âaq, ØL@p“.”$
ŽÎùpU¤
“:”, Ϗ3S¥é:´¶iÜ{iÎ"eLÑ
ˆ$ŽÎ
{‡`²"
• Y ∼M
YŠÏCþdM)º"
• M1 + M2
\\M1 ÚM2 "
• M1 − M2
\\M1 KM2 ½‘"
• M1 : M2
^"
M1 ÚM2 ÜþÈ"XJü‘Ñ´Ïf,KÏfpŠ
• M1 %in% M2
• M1 ∗ M2
• M ^n
†M1 : M2 aq.Ý
?萪ØÓ"
uM1 + M2 + M1 : M2 "
M ¥¤kˆ‘±9¤knŽpŠ^‘"
• I(M ) òM …l,¦M ¥$ŽÎUì5Žâ$ŽÎ)ºØ´
Uúª$ŽÎ)º"LˆªM (JŠúª˜‘"
5¿3¼êN^)ÒSLˆªUÊÏoK$Ž)º"¼êI()Œ±r˜
‡OŽLˆªµCå5Š.˜‘¦^"
5¿S.L«‰Ñ
ÏCþÚgCþ9gCþm'X, ù(½
‚...Ý
, .ëê•þ´Û¹, ¿vk.úª¥NyÑ
5"ù«‰{·^u‚5., ØäkÊH5, ~Xš‚5.ÒØUùL
«"
1.12.2
‚5£8.
[ÜÊς5.¼êlm(), Ù{ü^{µ
fitted.model <- lm(formula, data=data.frame)
Ù¥data.frameˆCþ¤3êâµ, formula.úª, fitted.model´‚5
.[Ü(Jé–(Ùclassá5lm)"~Xµ
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
60
1˜Ù SŠó0
mod1 <- lm(Weight ~ Height + Age, data=cl)
Œ±[ܘ‡yéx1Úx2£8(‘kÛ¹å‘), êâ5gêâµ production"[Ü(J\
é–mod1¥"5¿ØØêâµproduction´Ä±
^attach() ë\c$1‚¸ÑŒlm()¦^"lm()Äw«›©{öµ
> mod1
Call:
lm(formula = Weight ~ Height + Age, data = cl)
Coefficients:
(Intercept)
-141.224
Height
3.597
Age
1.278
w«
N^úªÚëêO(J"
1.12.3
J&EÏ^¼ê
lm()¼êˆ£Š‰.[Ü(Jé–, Ÿþ´˜‡äkaá5Šlm
L, kmodel!coefficients!residuals¤
"lm()(Jw«›©{ü, 
¼õ[Ü&E, Œ±¦^élmaé–kAÏöŠÏ^¼ê, ù
¼ê
)µ
add1
coef
effects
kappa
predict
residuals
alias
deviance family
labels
print
summary
anova
drop1
formula
plot
proj
e¡Ñ
lma([Ü.a)~^Ï^¼ê{ü`²"
• anova(é–1,é–2)
• coefficients(é–)
• deviance(é–)
ˆ££8Xê(Ý
)"Œ{coef(é–)"
ˆ£í²Ú,Xk­K\"
ˆ£.úª"
• formula(é–)
• plot(é–)
r˜‡f.†.',)¤©ÛL"
±›.äA«ã, XíéýÿŠã"
• predict(é–,newdata=êâµ), predict.gam (é–,newdata=êâµ) k
.[Ü(J é#êâ?1ý"½#êâ7L†ïž^
êâäkƒÓCþ("¼ê(Jéêâµ¥z˜*ÿÏCþý
(J(•þ½Ý
)"
predict.gam()†predict()Š^ƒÓ·^52, ŒA^ulm!glmÚgam
[Ü(J"'X,õ‘ªÄ¼ê^
õ‘ªž, \\
#êâ—
õ‘ªÄ¼êUC, ^predict.gam() ¼êŒ±;ddÚå "
• print(é–)
¶5w«"
{üw«.[Ü(J"˜„Ø^print()†…\é–
• residuals(é–)
resid(é–)"
ˆ£.í(Ý
),ek­K·\"Œ{
• summary(é–)
Œw«[.[Ü(J"
ÚO^‡§
61
§1.13 ÚO©Û¢~
©Û
1.12.4
©Û´ïÄlъσ阇êŠ.IK²;ó
ä" S ?1©Û¼ê´aov(), ‚ªaov(úª, data=êâµ), ^{
†lm()aq, J&EˆÏ^¼êEk"
·‚±c¡^LØÓýf7†›'êâ~"bveneerêâµ

Têâµ
> veneer
Brand Wear
1
ACME 2.3
2
ACME 2.1
3
ACME 2.4
4
ACME 2.5
5 CHAMP 2.2
6 CHAMP 2.3
7 CHAMP 2.4
8 CHAMP 2.6
9
AJAX 2.2
10 AJAX 2.0
11 AJAX 1.9
12 AJAX 2.1
13 TUFFY 2.4
14 TUFFY 2.7
15 TUFFY 2.6
16 TUFFY 2.7
17 XTRA 2.3
18 XTRA 2.5
19 XTRA 2.3
20 XTRA 2.4
Äk·‚rz‡ýf7†›œ¹xÝ/㿅˜3Ә¡¥, Šã
Xeµ
plot(Wear ~ Brand, data=veneer)
„ã1.20"ù«ãŒ±†*/'˜‡Cþ3õ‡|©Ù, ½ö'A‡aq
Cþ"l㥌±wÑ, AJAXýfÐ, TUFFY, Ù§n‡ýfOØ
²w"

uýfù‡ÏƒéI›þkÃwÍK, ‡^aov()¼êµ
> aov.veneer <- aov(Wear ~ Brand, data=veneer)
> summary(aov.veneer)
Df Sum Sq Mean Sq F value
Pr(>F)
Brand
4 0.61700 0.15425
7.404 0.001683 **
Residuals
15 0.31250 0.02083
--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Œ„σ´wÍ"
1.13
ÚO©Û¢~
e¡·‚±1.1¥@‡Æ)œ¹~?1˜
©Û"·‚F"
)N
­!p!c#!5OCþĜ¹9pƒƒm'X"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
62
2.0
2.2
Wear
2.4
2.6
1˜Ù SŠó0
ACME
AJAX
CHAMP
TUFFY
XTRA
Brand
ã 1.20: ØÓ¬ý7†›þ'
êâÑ\
1.13.1
bêâ˜3
©©‡c:\work\class.txt¥, vkIK, ˆCþþeé
à"·‚krêâÖ\˜‡Sêâµé–¥µ
>
+
+
>
cl <- read.table("c:/work/class.txt",
col.names=c("Name", "Sex", "Age", "Height", "Weight"),
row.names="Name")
cl
Sex Age Height Weight
Alice
F 13
56.5
84.0
Becka
F 13
65.3
98.0
Gail
F 14
64.3
90.0
#######
William
M 15
66.5 112.0
& ¢5 ê â © Û (EDA)
1.13.2
Äk·‚kïĈCþ©Ùœ¹, w©Ù´ÄC, kòwÉ~
Š, kvk²wSƒ', "
k^1.11.1¥½Âeda.shape()¼ê55ïĈêŠ.Cþ©Ùœ¹"3
N^ckrêâµclë\c|¢´»¥±†¦^cl¥Cþ¶µ
>
>
>
>
>
>
>
attach(cl)
summary(cl)
eda.shape(Age)
eda.shape(Height)
eda.shape(Weight)
tab.sex <- table(Sex)
barplot(tab.sex)
Ϗêâ´ØӇN*ÿ¤±ØŒUkSƒ', ™xžmSã"ùp‰
Ñ
p©Ùã(ã1.21)"Œ±wÑ, pÚN­ÑƒC…òw
É~:, N­Ïlъ¤±†ãØC, lؗÝO­‚wEŒ
Š?n"
ÚO^‡§
63
0
1
55
2
60
3
4
65
5
70
6
§1.13 ÚO©Û¢~
50
55
60
65
70
75
0.06
70
●
●
0.04
65
●
●
0.00
0.02
60
● ● ●
●
●
●
●
●
●
●
●
●
●
55
●
●
50
55
60
65
70
75
−2
−1
0
1
2
ã 1.21: p©Ù&¢5êâ©Û

ïÄêŠ.CþWeight!Height!Agem'X, ·‚x§‚Ñ:ã
Ý
µ
> pairs(cbind(Height, Weight, Age))
80
100
120
140
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
140
●
●
100
●
●●
●
●
●
●
●
●
●
●● ●
●
●
●
●
●
Weight
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●●
●
● ● ●●
●
●
●
●
●
55
●
●
●
60
65
70
●
● ●
●
●
●●
●
●
Age
●
11
●
●
●
15
●
13
60
●
●
●
●
55
Height
●
●
● ●●
● ●
●
●
●
●
●
●
65
60
●
11
12
13
14
15
16
ã 1.22: p!N­!c#Ñ:ãÝ
lÑ:ãÝ
(ã1.22) Œ±wÑn‡CþƒmьUk‚5ƒ''X"

ïÄÏfSexéÙ§CþK, Œ±xSexØÓY²þˆCþÝ/ã,
Xµ
> oldpar <- par(mfcol=c(1,3))
> boxplot(Weight ~ Sex, ylab="Weight")
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
64
1˜Ù SŠó0
11
60
55
12
80
13
60
100
14
65
120
15
70
140
16
> boxplot(Height ~ Sex, ylab="Height")
> boxplot(Age ~ Sex, ylab="Age")
> par(oldpar)
●
F
M
F
M
F
M
ã 1.23: 5OéÙ¦CþK
l ã1.23Œ ± w Ñ, I å N ­ ! p k ² w O, c # K O Ø
²w"·‚Œ±©ØÓ5Oé,˜Cþ©OŠã½OŽ, ùp‡¦^
–Weight[Sex== ”F”], Weight[Sex==”M”]ùf8{Ҍ±r*ÿ
©|"?˜Ú„Œ±^¼êtapply†U˜‡Ïfé*ÿ©|, Š^,‡
¼êµ
> tapply(Weight, Sex, hist)

ïÄÏfSexØÓY²éÙ§Cþmƒ''XK, Œ±ŠÓ
ãµ
> coplot(Weight ~ Height | Sex)
(J(„c¡ã1.12)vk‡N²wO"
1.13.3
|m'
·‚5©ÛIåpkÃwÍÉ, ù´ü|'¯K"þ¡EDAÜ©
¿üÝ/㮲J«Iåpk²wÉ, ùp·‚^ÚObu‰ÑÚ
O(Ø"
Iåü|Œ±@´Õá, …z|S*ÿŒ±@´ƒpÕá"
ŠâEDA(JŒ±@ü|Ñ5goN"ù, ·‚Œ±¦^ütu
"Ϗ´Äƒ™, ·‚Zy^؇¦ƒCqütuµ
> t.test(Height ~ Sex)
ÚO^‡§
65
§1.13 ÚO©Û¢~
(JpŠ0.1652, U·‚˜„æ^0.05Y²´ØwÍ"¤±lù|w
IåpvkuywÍÉ"
aqŒ±?1IåN­', pŠ0.06799, ØwÍ"
1.13.4
£8©Û
e¡·‚ïÄéN­ý"lÑ:ãÝ
w, N­†pƒmk²w‚
5ƒ', ¤±·‚k[ܘ‡N­ép˜‚5£8.µ
> lm.fit1 <- lm(Weight ~ Height, data=cl)
> lm.fit1
Call:
lm(formula = Weight ~ Height, data = cl)
Coefficients:
(Intercept)
-143.027
Height
3.899
> summary(lm.fit1)
Call:
lm(formula = Weight ~ Height, data = cl)
Residuals:
Min
1Q
-17.6807 -6.0642
Median
0.5115
3Q
9.2846
Max
18.3698
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -143.0269
32.2746 -4.432 0.000366 ***
Height
3.8990
0.5161
7.555 7.89e-07 ***
--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 11.23 on 17 degrees of freedom
Multiple R-Squared: 0.7705,
Adjusted R-squared: 0.757
F-statistic: 57.08 on 1 and 17 DF, p-value: 7.887e-007
[Ü.§Weight = -143.0269 + 3.8990× Height, Eƒ'X겐
0.7705, u.Ǐ0pŠ7.887e−007, Œ„.´wÍ"éu˜
£8, ·‚Œ±3ÏCþégCþÑ:ãþU\£8†‚5w£8[Ü
Jµ
> plot(Weight ~ Height)
> abline(lm.fit1)
˜„/, lm[Ü(Jé–plot()¼êŒ±ŠÑeZÜu[ÜJã/,
RŒ±Šo‡ãµíé[܊ã!íVÇã!IOzíý銲
Šé[܊ã!Cookålã"
> oldpar <- par(mfrow=c(2,2),
+
mar=c(2.5,2,1.5,0.2), mgp=c(1.2, 0.2, 0))
> plot(lm.fit1)
> par(oldpar)
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
66
20
1˜Ù SŠó0
●
10
●
●
●
●
●
●
●
●
●
0.5
●
●
●
●
●
0
17 ●
1.5
17 ●
●
●
●
●
−10
80
3
−1.5
−20
60
●
●
●
100
Fitted values
120
●
3●
●
14 ●
140 −2
−1
Scale−Location plot
1.2
●
●
●
●
●
14
●
−0.5
●
●
●
0
1
Theoretical Quantiles
●
●
317
14 ●
2
Cook’s distance plot
16
●
●
●
●
●
0.20
●
●
●
10
0.10
●
●
0.4
●
●
●
0.0
●
60
7
80
100
Fitted values
120
0.00
0.8
●
140
5
10
Obs. number
15
ã 1.24: £8äã
„ã1.24"XJŽz‡ãÑ^I5wK؇˜ã/ëê"o‡ã¥, í
é[܊㌱‡Ní¥í3(, XJ.¿©{íAT´‘Å
Czvk?Ûª"íVÇ㌱u‚5£8bu­‡b
½—ؑÑl©Ù´ÄÜn, Œ±wÑí©Ù­—!”—!† !
m œ¹"IOzíý銲Šé[܊㌱uyíÉ~Š:,
=[܁:"Cookålïþz˜*ÿé[Ü(JKŒ, ꊌ
rK:"ã¥gÄIÑ
âÑ:"
llm.fit1£8äãwívk²wª, í©Ùk”—–•"v
k²wÉ~Š:"
e¡·‚w\\Ù§CþUÄ?˜ÚUõ.ýUå"^add1¼êŒ
±ä\\#CþŒ±Uõ.µ
> add1(lm.fit1, ~ . + Age + Sex)
Single term additions
Model:
Weight ~ Height
Df Sum of Sq
RSS
<none>
2142.49
Age
1
22.39 2120.10
Sex
1
184.71 1957.77
AIC
93.78
95.58
94.07
add1 ( J w « ˜ ‡  © Û L, ˆ 1 ¥<none>˜ 1  Ø \ C þ œ ¹,
Age˜1\\˜‡CþAge œ¹, Sex˜1\\˜‡CþSexœ¹"
ˆ¥DFdCþgdÝ, Sum of SqTCþéA²Ú, RSS\\
TCþ í²Ú, AIC\\TCþ AICÚOþŠ"AIC
.Ð, ¤±XJ\\,‡Cþ AIC~Ҍ±\\dCþ"ùp\
\AgeÚ\\SexѦAICCŒ, ¤±ØA\\ùü‡Cþ"
X J ˜ m © Ò \ \ ¤ k C þ, Œ ± ^drop1()¼ ê  K ˜ ‡ C þ
AIC´ÄŒ±Cµ
> lm.fit2 <- lm(Weight ~ Height + Age + Sex, data=cl)
> summary(lm.fit2)
ÚO^‡§
67
§1.13 ÚO©Û¢~
Call:
lm(formula = Weight ~ Height + Age + Sex, data = cl)
Residuals:
Min
1Q
-19.6540 -6.5737
Median
0.4602
3Q
7.6708
Max
20.8515
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -125.1151
33.9038 -3.690 0.00218 **
Height
2.8729
0.9971
2.881 0.01142 *
Age
3.1131
3.2362
0.962 0.35132
SexM
8.7443
5.8350
1.499 0.15472
--Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 11.09 on 15 degrees of freedom
Multiple R-Squared: 0.8025,
Adjusted R-squared: 0.763
F-statistic: 20.31 on 3 and 15 DF, p-value: 1.536e-005
> drop1(lm.fit2)
Single term deletions
Model:
Weight ~ Height + Age + Sex
Df Sum of Sq
RSS
<none>
1844.01
Height 1
1020.61 2864.62
Age
1
113.76 1957.77
Sex
1
276.09 2120.10
AIC
94.93
101.30
94.07
95.58
lsummary()(JwAgeÚSexÒØwÍ"^drop1()uyKAgeŒ±¦AIC
l94.93C94.07, ¤±ATKAge"éKAge .2^drop1()u
ySexATK"¤±·‚ .´lm.fit1"
3?U.½êâUC ­#[ܞ„Œ±¦^update¼ê, 'X‡llm.fit2
¥KAgeҌ±^µ
> lm.fit3 <- update(lm.fit2, . ~ . - Age)
3gCþ‡êõžSJø
˜‡step()¼ê^5?1ÅÚ£8, §l˜‡Ð
©.m©gÄäO\½KCþ,  Ð.µ
> lm.fit0 <- lm(Weight ~ 1, data=cl)
> lm.step <- step(lm.fit0, ~ . + Height + Age + Sex)
Start: AIC= 119.75
Weight ~ 1
+ Height
+ Age
+ Sex
<none>
Df Sum of Sq
RSS
1
7193.2 2142.5
1
5124.5 4211.2
1
1681.1 7654.6
9335.7
AIC
93.8
106.6
118.0
119.7
Step: AIC= 93.78
Weight ~ Height
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
68
1˜Ù SŠó0
Df Sum of Sq
<none>
+ Sex
+ Age
- Height
1
1
1
RSS
2142.5
184.7 1957.8
22.4 2120.1
7193.2 9335.7
AIC
93.8
94.1
95.6
119.7
·.± , ·‚Œ±^.?1[ܽý"[ܐ‡é.[
Ü(J^predict()¼ê, X:
> predict(lm.fit1)
é#êâXJŽŠý, ‡3predict¼êN^ž\\#êâêⵊ
ëêµ
> new.data <- data.frame(Height=c(50, 51.2, 68, 69.7))
> predict(lm.fit1, new.data)
1
2
3
4
51.92459 56.60343 122.10714 128.73549
1.14
^ SŠ
Š‘ Å [ O Ž
ŠÚOóŠö, ·‚Ø
Œ±^Sׄ¢y#ÚO{, „Œ±^S?1
‘Å["‘Å[Œ±y·‚Ž{, 'Øӎ{`":, uyU?
ÚO{•, ´?1ÚOïākåOŽó䃘"
‘Å[ÄI‡´)–‘Åê, S¥®Jø
Œõê~^©Ù–
‘Åê¼ê, Œ±ˆ£˜‡–‘ÅêS•þ"ù
–‘Åê¼ê±i1rmÞ,
'Xrnorm()´–‘Åê¼ê, runif()´þ!©Ù–‘Åê¼ê, Ù1˜‡
gCþ´–‘ÅêSÝn"'uù
¼êŒ±ë„1.15±9Xڐϩ‡"
e~)1000‡IO–‘Åêµ
> y <- rnorm(1000)
ù
–‘Åê¼êŒ±½†©Ùk'ëê, 'Xe~)1000‡þŠ
150!IO100–‘Åêµ
> y <- rnorm(1000, mean=150, sd=100)
)–‘ÅêS´Ø­E, ¢Sþ, S3)–‘Åêžl˜‡«fÑ
u, ØäS“#«f, ¤±)eZ‘Åê SܑÅê«fÒ®²U
C
"kž·‚I‡[(J´Œ­E, d·‚Œ±3[m©ƒcN
^setseed(ê) Ҍ±yzg)‘Å괃ÓS"
Š~f, ·‚5)Ñl˜‡{ü‚5£8êâ"
# {ü‚5£8[
lm.simu <- function(n){
# k)¤gCþ"bgCþxŠ‰Œ
# 3150180ƒm,Œ—Ñl©Ù"
x <- rnorm(n, mean=165, sd=7.5)
ÚO^‡§
69
§1.14 ^SŠ‘Å[OŽ
# 2)¤.Ø"bÑlN(0, 1.2)©Ù
eps <- rnorm(n, 0, 1.2)
# ^.)¤ÏCþ
y <- 0.8 * x + eps
return(data.frame(y,x))
}
SvkJøõ‘ÅCþ[§S, ùp‰Ñ˜‡?1n‘ÅCþ
[~f"b‡)n‘Å•þX ∼ N (µ, Σ)n‡Õá*ÿ, Œ±
k)n‡ÑlnIO©Ù*ÿ, ˜3˜‡n13Ý
¥µ
U <- matrix(rnorm(3*n), ncol=3, byrow=T)
Œ±@Ý
Uz˜1´˜‡IOn©Ù*ÿ"Ý
ΣCholeski©
)Σ = A0 A, AþnÝ
, e‘Å•þξ ∼ N (0, I), Kµ + A0 ξ ∼ N (µ, Σ)"
Ïd, µ + A0 U 0 Š˜‡n1nÝ
z˜Ñ´ÑlN (µ, Σ)©Ù, …ˆ
ƒmÕá"²L=˜, )X
X <- matrix(rep(mu,n), ncol=3, byrow=T) + U %*% A
´˜‡n1nÝ
"
kž[I‡OŽþéŒ, õžÿ$–‡OŽAUžm"éuù«¯
K·‚‡õur¯K©¤Œ±üÕOŽ¯K, , üÕOŽz‡¯K,
r(J3S閥½©©‡¥,  nÜ(Jª(J"
XJ,˜‡¯KI‡OŽžm', ·‚3?§žŒ±æ^±eE
|µz…˜½žmÒw«˜e?Ö?Ý, ±OŽ®²Ñ†½?\k̂„
ضATr¥m(Jz…˜ãžmÒP¹˜‡©©‡¥(cat()¼êŒ
±‘˜‡fileëêÚappendëê, éù«P¹{Jø
|±), XJI‡¥ä§
S, ¥m(JŒU´k^, k
œ¹e„Œ±ŠâP¹¥m(Jl§S¥ä
/UY‰1"
ϏS´˜‡)º.Šó, ¤±k
I‡ŒþS“?ÖX^S?§Çé
$"d·‚Œ±ṙܩ^S?§, rI‡S“Ü©^C!FORTRAN½C++?
§, 3S¥N^dÙ¦Šó?ÈDLL¥"ùp{ü0XÛ3MS Windows ‡R¥N^C½C++?ÈDLL¥"5¿Borlandúi¤Jø
˜‡MS WindowseC++?ȧS, k˜‡gd^‡DevC++8¤?§
‚¸Œ±?1CÚC++?6Ú?È"
b·‚
˜‡OŽòÈC§S:
void convolve(double *a, int *na,
double *b, int *nb, double *ab){
int i, j, nab = *na + *nb - 1;
for(i = 0; i < nab; i++)
ab[i] = 0.0;
for(i = 0; i < *na; i++)
for(j = 0; j < *nb; j++)
ab[i + j] += a[i] * b[j];
}
5¿C¼ê¤kgCþÑ´, ù´ÏSÄêâa.´•þ§ƒ
uCŠó˜‘ê|"bdC§S?Ȥ
˜‡DLL¥testdll.dll˜3có
Š8¹"5¿?Ȟ‡)¤Windows››DLLØ´I‚¸DLL"
3R¥N^ù‡C¼ê, I‡XeÚ½µ
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
70
1˜Ù SŠó0
conv <- function(a, b){
dll.filename <file.path(paste("testdll",
.Platform$dynlib.ext, sep=""))
if(!is.loaded(symbol.C("convolve"),
PACKAGE="testdll")){
dyn.load(dll.filename)
}
.C("convolve",
as.double(a), as.integer(length(a)),
as.double(b), as.integer(length(b)),
ab=double(length(a)+length(b)-1))$ab
}
ù‡S¼êrC¼ê“convolve”?1
C"Ù¥, ¼êis.loaded(symbol.C(¼ê
¶)) uDLL¥C¼ê´Ä®²N\
R¥, XJvkN\҇^dyn.load(DLL©
‡¶) N\"Ϗ3Windows‚¸eÚUnix‚¸eDLL¥*жØÓ¤±é
©‡¶‰
oN5?n"
N^C¼ê, ¦^“.C”¼ê"“.C”¼ê1˜‡ë
ê´DLL¥C¼ê¶(5¿ŒU‘ey‚), Ù¦ëê´C¼êgCþ"¤k
lRD‰CgCþч^as.doubleÚas.integerKå5"“.C”¼êˆ£˜‡
L,LƒD\gCþ²L¼ê$1 Š, XJN^“.C”ž‰,
g
Cþ\
¶i(Xþ¡ab)Kˆ£L¥ò¦^ù
¶i"
éu‡EN^¼êŒ±{ýkN\Äó¥Ø´zg3¼êS
ä"
1.15
S~
~^ ¼ ê ë 
ù˜!©aÑ~^¼ê, I‡žŒ±ëwÏ"!´Šë§Œ
U„k­‡¼êvkJ9"
1.15.1
Ä
˜!êâ+n
vector:
logical:
list:
c:
subset:
from:to:
rep:
NULL:
order:
rev:
attr, attributes:
names:
ÚO^‡§
•þ
Ü6.•þ
L
돕þ½L
¦f8
S
­E
˜é–
üS
6
é–á5
閶iá5
numeric:
character:
data.frame:
length:
seq:
sequence:
NA:
sort:
unique:
unlist:
mode, typeof:
êŠ.•þ
iÎ.•þ
êâµ
¦Ý
S
S
"”Š
üS
˜z
вL
é–;ª†a.
71
§1.15 S~^¼êë
!iÎG?n
character:
substr:
paste, strsplit:
grep, sub, gsub:
iÎ.•þ
fG
뽩
ªš†O†
nchar: iÎê
format, formatC: ré–^‚ª=†iÎG
charmatch, pmatch: iÎGš
n!Eê
complex: )¤E•þ
Im: ¦JÜ
Arg: ¦Ë
Re: ¦¢Ü
Mod: ¦
Conj: ¦EÝ
o!Ïf
factor:
levels:
cut:
split:
tapply:
)¤Ïf
ÏfˆY²¶i
rêŠ.閩«m=†Ïf
UÏf©|
é“Ø5K”ê|A^¼ê
1.15.2
codes:
nlevels:
table:
aggregate:
Ïf?è
ÏfY²‡ê
ªêL
OŽˆêâf8V)ÚOþ
êÆ
˜!OŽ
+, -, *, /, , %%, %/%:
signif, trunc, zapsmall:
range:
prod:
cummax, cummin:
approxÚapproxfun:
sign:
oK$Ž
\
ŒŠÚŠ
•þƒÈ
\OŒ!
Š
ÎÒ¼ê
ceiling, floor, round:
max, min, pmax, pmin:
sum:
cumsum, cumprod:
sort:
diff:
\
ŒŠ
•þƒÚ
\\!\¦
üS
©
!êƼê
abs, sqrt:
sin, cos, tan:
sinh, cosh, tanh:
beta, lbeta:
choose, lchoose:
poly:
besselI:
deriv:
ýéŠ, ²Š
n¼ê
V­¼ê
©¼ê
|Üê
õ‘ª
Bessel¼ê
{ü‡©
log, exp, log10, log2:
asin, acos, atan, atan2:
asinh, acosh, atanh:
gamma, lgamma:
fft, mvfft, convolve:
polyroot:
spline, splinefun:
éꆍê¼ê
‡n¼ê
‡V­¼ê
³ç¼ê
L|“C†ÚòÈ
õ‘ª¦Š
^Š
†³ç¼êk'¼ê„kdigamma, trigamma, tetragamma, pentagamma,
†Bessel¼êk'¼ê„kbesselK, besselJ,besselY, gammaCody"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
72
1˜Ù SŠó0
n!ê|
array: ïáê|
matrix: )¤Ý
data.matrix: rêâµ=†êŠ.Ý
lower.tri: Ý
enܩ
mat.or.vec: )¤Ý
½•þ
t: Ý
=˜
cbind: rÜ¿Ý
rbind: r1Ü¿Ý
diag: éÝ
aperm: ê|=˜
nrow: ê|1ê
ncol: ê|ê
dim: ê|‘•þ
dimnames: 閑¶
rownames: 1¶
colnames: ¶
%*%: Ý
¦{
crossprod: Ý
¦È(SÈ)
outer: ê|È
kronecker: ê|KroneckerÈ
apply: éê|,
‘A^¼ê
tapply: é“Ø5K”ê|A^¼ê
sweep: OŽê|V)ÚOþ
aggregate: OŽêâf8V)ÚOþ
scale: Ý
IOz
matplot: éÝ
ˆ±ã
cor: ƒ'
½
contrasts: éìÝ
row: Ý
1eI8
col: Ý
eI8
o!‚5“ê
solve:
svd:
chol:
chol2inv:
)‚5§|½¦_
Ý
ÛɊ©)
Choleski©)
dCholeski©)¦_
eigen: Ý
AŠ©)
backsolve: )n§|
qr: Ý
QR©)
Ê!Ü6$Ž
<, >, <=:
>=, ==, !=:
logical:
ifelse():
unique:
'$ŽÎ
)¤Ü6•þ
öJ˜
éÑp؃Ӄ
!, &, &&, |, ||, xor():
all, any:
match, %in%:
which:
duplicated:
8!`z9¦Š
optimize, uniroot, polyroot: ˜‘`z†¦Š
1.15.3
§SO
˜!››(
if, else, ifelse, switch: ©|
for, while, repeat, break, next: ̂
apply, lapply, sapply, tapply, sweep: O“Ì‚¼ê
!¼ê
function:
source:
call:
.C, .Fortran:
Recall:
ÚO^‡§
¼ê½Â
N^©‡
¼êN^
N^C½öFortranf§SÄó¥
48N^
Ü6$ŽÎ
Ü6•þяý½3ý
é
éýŠeI8Ü
é­Eƒ
73
§1.15 S~^¼êë
browser, debug, trace, traceback: §SNÁ
options: ½XÚëê
missing: äJë´ÄkéA¢ë
nargs: ëê‡ê
stop: ªŽ¼ê‰1
on.exit: ½òў‰1
eval, expression: LˆªOŽ
system.time: LˆªOŽOž
invisible: ¦CþØw«
menu: ÀJèü(iÎLèü)
Ù§†¼êk'„k:delay, delete.response, deparse, do.call, dput, environment, , formals, format.info, interactive, is.finite, is.function, is.language,
is.recursive, match.arg, match.call, match.fun, model.extract, name, parse, substitute, sys.parent, warning, machine"
n!Ñ\ÑÑ
cat, print:
sink:
dump, save, dput, write, write.table:
scan, read.table, load, dget:
w«é–
ÑÑ=•½©‡
ÑÑé–
Ö\
o!󊂸
ls, objects:
q, quit:
.Last:
?, help:
data:
1.15.4
w«é–L
òÑXÚ
òÑ$1¼ê
Ï
Ñêâ8
rm, remove:
.First:
options:
help.start, apropos:
library:
íØé–
Щ$1¼ê
XÚÀ‘
Ï
Ѥk^‡
ÚOOŽ
˜!ÚO©Ù
z˜«©Ùko‡¼ê:
• d — density(—ݼê),
• p — ©Ù¼ê,
• q — © ê¼ê,
• r — ‘Åê¼ê"
'X, ©Ùùo‡¼êdnorm, pnorm, qnorm, rnorm"e¡·‚Ñ
ˆ©Ù M, c¡\cMd!p!q½rÒ¤¼ê¶:
norm: t: t©Ù
f: F©Ù
chisq: k
unif: þ!
exp: ê
weibull: %Ù
gamma: ³ç
beta: ©
lnorm: éê
logis: Ü6©Ù
cauchy: …Ü
binom: ‘©Ù
geom: AÛ©Ù
hyper: ‡AÛ
nbinom: K‘
pois: Ñt
signrank: Îҝ
wilcox: Ú
tukey: Æ)z4
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
74
1˜Ù SŠó0
!{üÚOþ
sum:
sd:
range:
sort:
ave:
quantile:
¦Ú
IO
4
üS
©|²þ
© ê
mean:
min:
median:
order:
fivenum:
stem:
²þŠ
Š
¥ ê
üS
ÊêV)
ª“ã
var:
max:
IQR:
rank:
mad:

ŒŠ
o© må

MADÚOþ
n!ÚOu
R¥®¢yÜ©uXe:
shapiro.test: 5u
wilcox.test: WilcoxonÚ9Îҝu
var.test: Fu
ks.test: [Ü`ÝK-Su
t.test: üoNÚü|!¤étu
prop.test: '~u
chisq.test: [Ü`Ýku
o!õ©Û
cor, cov.wt, var:
biplot, biplot.princomp:
cancor:
princomp:
hclust:
kmeans:
cmdscale:

9ƒ'
OŽ
õêâbiplotã
;Kƒ'
̤©©Û
ÌXàa
k-þŠàa
²;õ‘IÝ
Ê!žmS
ts:
time:
HoltWinters:
ar:
arima.sim:
FP.test:
žmSé–
žmSæžm
Holt-Winters{
[ÜAR.
[ARIMA
ü Šu
diff:
window:
acf:
arima:
Box.text:
spectrum:
OŽ©
žmI
gƒ'¼ê
ARIMAï
xD(u
ÌO
8!ÚO.
lm, glm, aov: ‚5.!2‚5.!©Û
öS
1. (1) у3, −1.5, 3E−10•þ"
(2) Ñl3m©zgO\3,ݏ100•þ"
(3) Ñ(0, 2)­E10g•þ"
(4) é•þx,ÑكŒuu0u1^‡"
(5) é•þx,ÑكÑu0^‡"
(6) ѝ¹12‡°¶¡•þ"
(7) )¤˜‡¹©‡¶tab1.txttab18.txtiÎG•þ"
ÚO^‡§
75
§1.15 S~^¼êë
(8) ѝ¹§z 6 = 1Š•þ, ¿ÑÙË{uÚuŠ"
2. x˜‡100ê•þ"'X, x <− floor(100*runif(100))"
(1) w«x12130҃"
(2) rx131,35,39҃DŠ0"
(3) w«x¥Ø
11ÒÚ150҃ƒf8"
(4) Ñx¥‡ êu3ƒ"
(5) Ñx¥‡ êu3ƒeI ˜"
(6) ‰xz˜‡ƒ\þ¶i, x1x100"
(7) ¦x²þŠ¿¦z˜‡ƒ~²þŠ
Ú9ƒ²Ú"
l, OŽxl²
(8) rxlŒüS"OŽx10%© ê90%© êƒmål"
3. ½Â˜‡‘ê(3, 4, 2)ê|§Ù1˜ (1neI1)l1m©Û
ê, 1 l2m©óê"w«z˜ 121ƒ"r1(1,1,1),(2,2,2),
(2, 2, 1)҃DŠ""r1˜ \þ100,r1 \þ200"©OOŽ
1˜ Ú1 ²þŠ"
4. é‚5.Y = Xβ + ε,ÑX÷žOŽβSLˆª"ÑOε
SLˆª"
5. rSASUSER.GPAêâ¥SEX, SATM, SATV©OÑ\S¥"OŽØ
Ó5O<ê, ¿OŽz˜|²þSATM©"rù
Cþ|ܤ˜‡
L"r SASUSER.GPA êâÑ\Sêâµ"
6. rŠéx <− floor(100*runif(100))¤)¤•þ˜‡©©‡¥,
êâ‘^˜‚Ú†1©…"ld©‡¥Ö\êâ•þy¥"
7. x´˜‡ݏn•þ,˜ã§S,OŽxݏswÄÚµ
Sx (t) =
s−1
X
xt−i ,
t = s, s + 1, . . . , n
i=0
8. ˜‡AR(1)[¼êµ
xt = a + bxt−1 + εt , t = 1, 2, . . . , n, Var(εt ) = σ 2
¼êëên!a!b!x0 Úσ, "Žžn=100, a=0, b=1, x0 =0, σ=1"
?Í: oÀº
http://www.math.pku.edu.cn/teachers/lidf/
Download