人大经济论坛下载系统

经济学计量与统计 工商管理与财会 金融投资学 其他
返回首页
当前位置: 主页 > 图书 > 计量与统计 >

statistics with R(07版)

文件格式:Pdf 可复制性:可复制 TAG标签: Statistics 点击次数: 更新时间:2009-09-18 15:21
介绍

Contents
Warning xx
1 Introduction to R 1
1.1 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Statistics vs Signal processing . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 GUI ?or lack thereof . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.3 Speed issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.4 Memory issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.5 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.6 Freedom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.7 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Installing R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.2 MacOS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.3 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.4 Linux: Installing R on Mandriva . . . . . . . . . . . . . . . . . . . 4
1.2.5 Linux: Installing R on Gentoo . . . . . . . . . . . . . . . . . . . . 4
1.2.6 Linux: installing R from the sources . . . . . . . . . . . . . . . . . 5
1.3 R: Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 First steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.2 RTFM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.3 CRAN Task Views . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.4 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.5 More technical documents . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.6 The pictures in the manual . . . . . . . . . . . . . . . . . . . . . . 13
1.4 Graphical interface ?R for non-statisticians and non-programmers . . . . 18
1.4.1 ESS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.2 Windows-specific stuff . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.3 R Commander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4.4 Zelig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.4.5 SciViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.4.6 JGR (pronounce ?jaguar? . . . . . . . . . . . . . . . . . . . . . . 22
1.5 R: Some elementary functions . . . . . . . . . . . . . . . . . . . . . . . . . 22
2 Programming in R 26
2.1 The R language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.1.1 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.1.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.3 How to get the code of a function? . . . . . . . . . . . . . . . . . . 28
2.1.4 Functions with side effects . . . . . . . . . . . . . . . . . . . . . . . 31
2.1.5 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.1.6 Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.1.7 Object Oriented Programming . . . . . . . . . . . . . . . . . . . . 34
2.2 Data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.2.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.2.2 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.2.3 Ordered factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2.4 Missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.2.5 Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.2.6 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
i
CONTENTS ii
2.2.7 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.2.8 Matrices and arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.2.9 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.2.10 The contents of those complex objects: str, unclass, deparse . . . . 50
2.2.11 Operations on vectors or arrays . . . . . . . . . . . . . . . . . . . . 53
2.2.12 Operations on vectors and arrays (continued) . . . . . . . . . . . . 54
2.2.13 Aggregate functions: by, aggregate . . . . . . . . . . . . . . . . . . 55
2.2.14 Operations of vectors and arrays: useful commands . . . . . . . . . 57
2.2.15 ?Unlooping?exercises . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.2.16 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.2.17 Time and Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
2.2.18 Miscellanies: match . . . . . . . . . . . . . . . . . . . . . . . . . . 70
2.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.3.1 Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.3.2 The printf way: print, cat, str . . . . . . . . . . . . . . . . . . . . . 71
2.3.3 Step-by-step execution: debug . . . . . . . . . . . . . . . . . . . . 71
2.3.4 Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.3.5 The calling stack: traceback, dump.frames, sys.call . . . . . . . . . 72
2.3.6 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.3.7 Test-Driven Development (TDD): RUnit . . . . . . . . . . . . . . . 74
2.3.8 Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.4 Object Oriented Programming: S3 Classes . . . . . . . . . . . . . . . . . . 75
2.4.1 Introduction: more complex types . . . . . . . . . . . . . . . . . . 75
2.4.2 Introduction: the ?plot?method . . . . . . . . . . . . . . . . . . . 78
2.4.3 Class of an object . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.4.4 Other methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
2.4.5 Creating your own classes with the usual methods . . . . . . . . . 83
2.4.6 Overloadable function . . . . . . . . . . . . . . . . . . . . . . . . . 84
2.4.7 All the methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
2.4.8 All the classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
2.4.9 Writing your own classes: toy example . . . . . . . . . . . . . . . . 86
2.4.10 More complex example . . . . . . . . . . . . . . . . . . . . . . . . . 87
2.4.11 Problems with S4 classes . . . . . . . . . . . . . . . . . . . . . . . 90
2.5 Object Oriented Programming: S3 Classes . . . . . . . . . . . . . . . . . . 90
2.5.1 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
2.6 Data storage, Data import, Data export . . . . . . . . . . . . . . . . . . . 91
2.6.1 read.table and co. . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
2.6.2 Importing data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
2.6.3 Large CSV files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
2.6.4 Excel files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
2.6.5 Diverting the output . . . . . . . . . . . . . . . . . . . . . . . . . . 97
2.6.6 .Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
2.6.7 source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
2.6.8 Out-of-memory computations . . . . . . . . . . . . . . . . . . . . . 97
2.6.9 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
2.6.10 SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
2.6.11 SQLite ?TODO: merge this section with the previous... . . . . . . 99
2.6.12 SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
2.6.13 ETL (Extraction, Transformation, Loading) . . . . . . . . . . . . . 105
2.6.14 Uses of databases: OLTP, OLAP, Data Warehouse (DW) . . . . . 112
2.6.15 Types of database management systems . . . . . . . . . . . . . . . 113
2.6.16 Hot topics in databases . . . . . . . . . . . . . . . . . . . . . . . . 113
2.6.17 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.6.18 MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.6.19 ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.6.20 PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
2.7 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
2.7.1 More Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
2.7.2 Writing your own packages . . . . . . . . . . . . . . . . . . . . . . 115
2.7.3 Vocabulary: package, library, bundle . . . . . . . . . . . . . . . . . 116
2.7.4 Example: package.skeleton() . . . . . . . . . . . . . . . . . . . . . 116
CONTENTS iii
2.7.5 example, demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.7.6 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.7.7 R, Sweave and Lyx . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.7.8 Vignettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.8 Other languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.8.1 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
2.8.2 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
2.8.3 Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
2.8.4 PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
2.8.5 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
2.9 (Graphical) User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
2.9.1 Tcl/Tk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
2.9.2 Why do we need a GUI? . . . . . . . . . . . . . . . . . . . . . . . . 123
2.9.3 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
2.9.4 Idiosyncrasies of the link between R and Tcl . . . . . . . . . . . . 123
2.9.5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
2.9.6 RCommander: a complete R GUI in Tk . . . . . . . . . . . . . . . 127
2.9.7 Other examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
2.9.8 Rgl: interactive 3D graphics . . . . . . . . . . . . . . . . . . . . . . 127
2.9.9 Other widget libraries: RGtk2 . . . . . . . . . . . . . . . . . . . . 127
2.10 Web interface: Rpad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
2.10.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
2.10.2 Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
2.10.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
2.10.4 Conlusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
2.11 Web programming: RZope . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
2.12 Web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
2.13 Clusters, parallel programming . . . . . . . . . . . . . . . . . . . . . . . . 132
2.13.1 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
2.13.2 BLAS (Basic Linear Algebra System) . . . . . . . . . . . . . . . . 132
2.13.3 PVM (Parallel Virtual Machine), MPI (Message Passing Interface) 133
2.13.4 Beowulf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
2.13.5 SSI (Single System Image) . . . . . . . . . . . . . . . . . . . . . . . 133
2.13.6 OpenMosix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
2.13.7 pR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
2.13.8 RScaLaPack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.13.9 Snow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.13.10 nws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.13.11 Rmpi, Rpvm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.13.12RSprng (Scalable Parallel Random Number Generator) . . . . . . 134
2.13.13 Aspect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.14 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.14.1 Functions: numerical integration and derivation . . . . . . . . . . . 134
2.14.2 Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
2.14.3 Sparse matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
2.14.4 IEEE arithmetics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
2.15 Numerical optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
2.15.1 Newton-Raphson . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
2.15.2 Nelder-Mead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
2.15.3 Linear programming: Simplex . . . . . . . . . . . . . . . . . . . . . 136
2.15.4 MIP (Mixed Integer Programming) and Branch and Bound . . . . 137
2.15.5 Interior Point (IP) Methods . . . . . . . . . . . . . . . . . . . . . . 137
2.15.6 Quadratic programming . . . . . . . . . . . . . . . . . . . . . . . . 137
2.15.7 Sequential Quadratic Programming (SQP) . . . . . . . . . . . . . . 137
2.15.8 Interior Point (IP) methods . . . . . . . . . . . . . . . . . . . . . 137
2.15.9 EM (Expectation Maximization) . . . . . . . . . . . . . . . . . . . 138
2.15.10Optimizing noisy functions . . . . . . . . . . . . . . . . . . . . . . 138
2.15.11Dynamic programming . . . . . . . . . . . . . . . . . . . . . . . . . 138
2.16 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
2.16.1 Memory woes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
2.16.2 Displaying numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 139
CONTENTS iv
2.17 Dirty Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
2.17.1 Catching errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
2.17.2 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
2.17.3 Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
2.17.4 Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
2.17.5 Launching R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
2.17.6 #! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
2.17.7 Other external programs . . . . . . . . . . . . . . . . . . . . . . . . 143
2.17.8 deparse() and substitute() . . . . . . . . . . . . . . . . . . . . . . . 143
2.17.9 get() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
2.17.10Environments: ls(), rm(), search() . . . . . . . . . . . . . . . . . . 144
2.17.11 stop(), warning() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
2.17.12 parse(), expression() . . . . . . . . . . . . . . . . . . . . . . . . . . 146
3 From Data to Graphics 147
3.1 Sample data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
3.1.1 An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
3.1.2 Other examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
3.1.3 All the examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
3.1.4 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
3.2 Quantitative univariate data . . . . . . . . . . . . . . . . . . . . . . . . . . 150
3.2.1 A few numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
3.2.2 Mean and standard deviation: L1-L2 space . . . . . . . . . . . . . 152
3.2.3 Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
3.2.4 Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
3.2.5 L-moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
3.2.6 Droves of numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
3.2.7 Stripchart (scatterplot) . . . . . . . . . . . . . . . . . . . . . . . . 163
3.2.8 Cumulated frequencies . . . . . . . . . . . . . . . . . . . . . . . . . 166
3.2.9 Box-and-whiskers plot . . . . . . . . . . . . . . . . . . . . . . . . . 168
3.2.10 Histogram and density . . . . . . . . . . . . . . . . . . . . . . . . . 173
3.2.11 Symetry plot (seldom used) . . . . . . . . . . . . . . . . . . . . . . 176
3.2.12 Quantile-Quantile plot (important) . . . . . . . . . . . . . . . . . . 189
3.2.13 Detrended probability plot . . . . . . . . . . . . . . . . . . . . . . 201
3.2.14 Gini concentration . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
3.3 Ordered univariate data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
3.4 Qualitative univariate variables . . . . . . . . . . . . . . . . . . . . . . . . 207
3.4.1 Different ways of presenting those data: data frames and contingency
tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
3.4.2 Column plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
3.4.3 Bar plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
3.4.4 Pareto Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
3.4.5 Pie chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
3.4.6 Polar coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
3.4.7 2-dimensional barplot (treemap) . . . . . . . . . . . . . . . . . . . 216
3.4.8 Beyond mosaic plots: Treemaps, Region Trees and TempleMVV
plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
3.4.9 Dotchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
3.5 Quantitative bivariate data . . . . . . . . . . . . . . . . . . . . . . . . . . 222
3.5.1 Point cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
3.5.2 With a periodic variable . . . . . . . . . . . . . . . . . . . . . . . . 225
3.5.3 Beyond polar coordinates: conformal mappings . . . . . . . . . . . 228
3.5.4 Lattice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
3.5.5 Facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
3.5.6 Convex hull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
3.5.7 Ellipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
3.5.8 2-dimensional density estimation . . . . . . . . . . . . . . . . . . . 234
3.5.9 Mean per fractile . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
3.6 Qualitative/quantitative bivariate data . . . . . . . . . . . . . . . . . . . . 242
3.6.1 Box-and-whiskers plots (boxplots) . . . . . . . . . . . . . . . . . . 242
3.6.2 Violin plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
CONTENTS v
3.6.3 Box-percentile plots . . . . . . . . . . . . . . . . . . . . . . . . . . 245
3.6.4 Highest Density Region (HDR) . . . . . . . . . . . . . . . . . . . . 248
3.6.5 Parallel scatterplot . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
3.6.6 Colored scatterplot . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
3.6.7 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
3.6.8 Lattice (treillis) plots . . . . . . . . . . . . . . . . . . . . . . . . . 253
3.6.9 R squared . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
3.7 Qualitative bivariate data . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
3.7.1 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
3.7.2 Row-profiles and column-profiles. . . . . . . . . . . . . . . . . . . 258
3.7.3 Several coloured curves . . . . . . . . . . . . . . . . . . . . . . . . 262
3.7.4 Fourfoldplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
3.8 Three variables and more . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
3.8.1 Bubble chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
3.8.2 Line chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
3.8.3 Point clouds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
3.8.4 Scagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
3.8.5 Data patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
3.8.6 dotplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
3.8.7 Star plots (or radar plots) . . . . . . . . . . . . . . . . . . . . . . . 283
3.8.8 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
3.8.9 Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
3.8.10 image() versus image.plot() . . . . . . . . . . . . . . . . . . . . . . 291
3.8.11 Colour palette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
3.8.12 Rgl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
3.8.13 XGobi: animations, interactions . . . . . . . . . . . . . . . . . . . 295
3.8.14 Parallel plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
3.8.15 Chernoff faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
3.8.16 Barycentric coordinates (ternary plot) . . . . . . . . . . . . . . . . 302
3.8.17 Barycentric coordinates, in higher dimensions . . . . . . . . . . . . 305
3.8.18 Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
3.9 Multivariate data, with some qualitative variables . . . . . . . . . . . . . . 306
3.9.1 One qualitative variable and two quantitative variables . . . . . . . 306
3.9.2 One or two qualitative variables and several quantitative variables 308
3.9.3 fourfoldplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
3.9.4 Several qualitative variables: box-and-whiskers plots . . . . . . . . 308
3.9.5 Several qualitative variables: the ?matplot?function . . . . . . . 309
3.10 Fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
3.10.1 Decorations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
3.10.2 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
3.11 TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
3.11.1 Dense pixel displays . . . . . . . . . . . . . . . . . . . . . . . . . . 314
3.11.2 Hiearchical data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
3.11.3 Visualizing complex functions . . . . . . . . . . . . . . . . . . . . . 315
4 Customizing graphics 316
4.1 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
4.1.1 Basic commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
4.1.2 Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
4.1.3 Mathematical symbols . . . . . . . . . . . . . . . . . . . . . . . . . 330
4.1.4 Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
4.1.5 Splitting the screen . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
4.1.6 Overlaying graphical elements . . . . . . . . . . . . . . . . . . . . . 340
4.1.7 par . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
4.1.8 User interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
4.1.9 Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
4.2 LATEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
4.2.1 xtable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
4.2.2 Sweave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
4.3 Lattice (Treillis) plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
4.3.1 Lattice (slicing) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
CONTENTS vi
4.3.2 xyplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
4.3.3 barchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
4.3.4 dotplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
4.3.5 bwplot, histogram, densityplot and stripplot . . . . . . . . . . . . . 360
4.3.6 Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
4.3.7 Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
4.3.8 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
4.3.9 Banking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
4.3.10 Commented code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
5 Factorial methods: Around Principal Component Analysis (PCA) 370
5.1 Principal Component Analysis (PCA) . . . . . . . . . . . . . . . . . . . . 370
5.1.1 Introducing PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
5.1.2 Principal Component Analysis: details . . . . . . . . . . . . . . . . 376
5.1.3 Normalized and non-mormalized PCA . . . . . . . . . . . . . . . . 377
5.1.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
5.1.5 PCA and linear algebra: Singular Value Decomposition (SVD) . . 381
5.1.6 Rank PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
5.1.7 Non linear PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
5.1.8 Projection Pursuit . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
5.1.9 Robust PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
5.1.10 Spherical PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
5.1.11 Grand tour, guided tour . . . . . . . . . . . . . . . . . . . . . . . . 384
5.1.12 Independant Component Analysis (ICA) . . . . . . . . . . . . . . . 384
5.1.13 Factor analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
5.1.14 Factor models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
5.1.15 Functional Principal Component Analysis (fPCA) . . . . . . . . . 389
5.1.16 Varimax rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
5.1.17 Tensor approximation . . . . . . . . . . . . . . . . . . . . . . . . . 391
5.2 Distance-based methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
5.2.1 PCO (Distance Analysis) . . . . . . . . . . . . . . . . . . . . . . . 394
5.2.2 MDS (Multi-Dimensional Scaling) . . . . . . . . . . . . . . . . . . 394
5.2.3 xgvis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
5.2.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
5.2.5 Minimum Spanning Tree (MST) . . . . . . . . . . . . . . . . . . . 397
5.2.6 Minimum Spanning Tree (MST) . . . . . . . . . . . . . . . . . . . 403
5.2.7 Isomap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
5.3 SOM (Self-Organizing Maps) . . . . . . . . . . . . . . . . . . . . . . . . . 412
5.3.1 Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
5.3.2 Remark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
5.3.3 Remark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
5.3.4 Remark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
5.3.5 Remark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
5.3.6 Graphical representation . . . . . . . . . . . . . . . . . . . . . . . . 413
5.3.7 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
5.3.8 Other graphical representation . . . . . . . . . . . . . . . . . . . . 416
5.3.9 Other graphical representation . . . . . . . . . . . . . . . . . . . . 417
5.3.10 Graphical representation . . . . . . . . . . . . . . . . . . . . . . . . 417
5.3.11 Other graphical representation . . . . . . . . . . . . . . . . . . . . 418
5.3.12 Application areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
5.3.13 Supervised Learning (Feature map) . . . . . . . . . . . . . . . . . . 418
5.3.14 Size of the SOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
5.3.15 Geometric interpretation of SOMs . . . . . . . . . . . . . . . . . . 419
5.4 Simple Correspondance Analysis (CA) . . . . . . . . . . . . . . . . . . . . 419
5.4.1 Around Principal Component Analysis . . . . . . . . . . . . . . . . 419
5.4.2 Simple correspondances . . . . . . . . . . . . . . . . . . . . . . . . 419
5.4.3 Example: using correspondance analysis to reorder a table . . . . . 423
5.4.4 Simple Correspondance Analysis: Details . . . . . . . . . . . . . . 425
5.4.5 Detrended Correspondence Analysis (DCA) . . . . . . . . . . . . . 427
5.4.6 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
5.5 Multiple Correspondance Analysis . . . . . . . . . . . . . . . . . . . . . . 427
CONTENTS vii
5.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
5.5.2 Multiple Correspondance Analysis: Details . . . . . . . . . . . . . 429
5.5.3 Mixing quantitative and qualitative variables (it does not work) . . 433
5.5.4 Mixing qualitative and quantitative variables (a solution) . . . . . 437
5.5.5 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
5.6 Log-linear model (Poisson Regression) . . . . . . . . . . . . . . . . . . . . 439
5.7 Discriminant Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
5.7.1 Discriminant Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 440
5.7.2 Quadratic discriminant analysis . . . . . . . . . . . . . . . . . . . . 443
5.7.3 Bayesian Discriminant Factor Analysis . . . . . . . . . . . . . . . . 443
5.8 Canonical analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
5.8.1 Canonical Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
5.8.2 Co-inertia analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
5.8.3 Partial Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . 444
5.9 Kernel methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
5.9.1 Principal Component Analysis and kernel methods . . . . . . . . . 444
5.9.2 kernlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
5.10 Neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
5.10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
5.10.2 Perceptron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
5.10.3 Linear filter neuron . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
5.10.4 Neural networks: TO SORT . . . . . . . . . . . . . . . . . . . . . . 452
5.10.5 Neural networks and dimension reduction . . . . . . . . . . . . . . 454
5.10.6 Neural networks and dimension reduction: examples . . . . . . . . 457
5.11 Dimension reduction: TODO: Rewrite/Remove this section . . . . . . . . 457
5.11.1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
5.11.2 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
5.11.3 Other idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
5.11.4 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
5.11.5 Other idea (bad) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
5.11.6 Traveling Salesman Problem (TSP) . . . . . . . . . . . . . . . . . . 471
5.11.7 TSP: descent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
5.11.8 TSP: Simulated annealing . . . . . . . . . . . . . . . . . . . . . . . 475
5.11.9 Exterior programs: heuristics and exact algorithms . . . . . . . . . 476
5.11.10 Looking for an open path . . . . . . . . . . . . . . . . . . . . . . . 478
5.11.11 With our data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
5.11.12Yet another idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
5.11.13TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
5.12 TODO: to sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
5.12.1 Latent class analysis (LCA) . . . . . . . . . . . . . . . . . . . . . . 487
5.12.2 Complements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
5.12.3 Caveat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
5.12.4 Various . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
6 Clustering 488
6.1 Non-hierarchical clustering (k-means) . . . . . . . . . . . . . . . . . . . . 488
6.2 Hierarchical Classification (dendogram) . . . . . . . . . . . . . . . . . . . 493
6.2.1 Various shapes of trees . . . . . . . . . . . . . . . . . . . . . . . . . 495
6.2.2 Distance between points . . . . . . . . . . . . . . . . . . . . . . . . 498
6.2.3 Mahalanobis distance . . . . . . . . . . . . . . . . . . . . . . . . . 499
6.2.4 Tracking Error distance . . . . . . . . . . . . . . . . . . . . . . . . 500
6.2.5 Distance between clusters . . . . . . . . . . . . . . . . . . . . . . . 501
6.2.6 A few applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
6.3 Comparing those two methods . . . . . . . . . . . . . . . . . . . . . . . . 502
6.4 Density estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
6.5 Other packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
7 Probability Distributions 513
7.1 The zoo of discrete probability distributions . . . . . . . . . . . . . . . . . 513
7.1.1 Bernoulli distribution . . . . . . . . . . . . . . . . . . . . . . . . . 513
7.1.2 Uniform discrete distribution . . . . . . . . . . . . . . . . . . . . . 515
CONTENTS viii
7.1.3 Binomial distribution . . . . . . . . . . . . . . . . . . . . . . . . . 515
7.1.4 Hypergeometric distribution . . . . . . . . . . . . . . . . . . . . . . 517
7.1.5 Poisson distribution . . . . . . . . . . . . . . . . . . . . . . . . . . 519
7.1.6 Geometric distribution . . . . . . . . . . . . . . . . . . . . . . . . . 521
7.1.7 Negative binomial distribution . . . . . . . . . . . . . . . . . . . . 523
7.1.8 Multinomial distribution . . . . . . . . . . . . . . . . . . . . . . . . 525
7.2 The zoo of continuous probability distributions . . . . . . . . . . . . . . . 525
7.2.1 Uniform continuous distribution . . . . . . . . . . . . . . . . . . . 525
7.2.2 Expenential distribution . . . . . . . . . . . . . . . . . . . . . . . . 525
7.2.3 Gaussian distribution . . . . . . . . . . . . . . . . . . . . . . . . . 526
7.2.4 Chi2 distribution with one degree of freedom . . . . . . . . . . . . 530
7.2.5 Chi2 distribution with n degrees of freedom . . . . . . . . . . . . . 531
7.2.6 Student?s T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
7.2.7 Fisher?s F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
7.2.8 Lognormal law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
7.2.9 Cauchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
7.2.10 Fat-tailed (or heavy-tailed) distributions . . . . . . . . . . . . . . . 535
7.2.11 Mixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
7.2.12 Quantile mixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
7.2.13 Edgeworth expansion . . . . . . . . . . . . . . . . . . . . . . . . . 539
7.2.14 Stable distributions . . . . . . . . . . . . . . . . . . . . . . . . . . 539
7.2.15 Levy distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
7.2.16 Weibull distribution . . . . . . . . . . . . . . . . . . . . . . . . . . 540
7.2.17 Gamma Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . 541
7.2.18 Beta distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
7.2.19 Dirichlet distribution . . . . . . . . . . . . . . . . . . . . . . . . . 546
7.2.20 Exponential distributions . . . . . . . . . . . . . . . . . . . . . . . 546
7.2.21 Pathological distributions . . . . . . . . . . . . . . . . . . . . . . . 546
7.2.22 Multivariate gaussian distribution . . . . . . . . . . . . . . . . . . 547
7.3 Fitting a distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
7.4 Extreme value theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
7.4.1 Evd (Extreme Value Distribution) . . . . . . . . . . . . . . . . . . 549
7.4.2 Extreme Value Theory (EVT) . . . . . . . . . . . . . . . . . . . . 549
7.4.3 EVT and risk management . . . . . . . . . . . . . . . . . . . . . . 552
7.4.4 EVT: GEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
7.4.5 EVT: Conditionnal Excess Distribution Function (GPD) . . . . . . 554
7.5 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
7.5.1 Copulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
7.5.2 Other distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
8 Estimators and Statistical Tests 561
8.1 Introduction to statistical tests: TODO: REWRITE THIS SECTION . . 561
8.1.1 H0 (null hypothesis) and H1 (alternative hypothesis) . . . . . . . . 562
8.1.2 Type I error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
8.1.3 p-value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
8.1.4 Type II error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
8.1.5 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
8.1.6 Simple hypothesis . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
8.1.7 Composite hypothesis . . . . . . . . . . . . . . . . . . . . . . . . . 566
8.1.8 Confidence Interval . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
8.1.9 UMP (Uniform Most Powerful) tests . . . . . . . . . . . . . . . . . 567
8.1.10 Non parametric test . . . . . . . . . . . . . . . . . . . . . . . . . . 568
8.1.11 Robustness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
8.1.12 Resistance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
8.1.13 Pearson residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
8.1.14 Outlier detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
8.1.15 Breaking point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
8.1.16 TODO: A few robust estimators . . . . . . . . . . . . . . . . . . . 569
8.1.17 Three means of performing statistical tests . . . . . . . . . . . . . 569
8.1.18 Criticism of statistical tests . . . . . . . . . . . . . . . . . . . . . . 569
8.1.19 Decision Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
CONTENTS ix
8.2 The Zoo of statistical tests: Parametric Tests . . . . . . . . . . . . . . . . 570
8.2.1 Statistical tests under R . . . . . . . . . . . . . . . . . . . . . . . . 570
8.2.2 Reading a test result . . . . . . . . . . . . . . . . . . . . . . . . . . 570
8.2.3 The zoo of parametric tests . . . . . . . . . . . . . . . . . . . . . . 575
8.2.4 WARNING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
8.2.5 Student?s T test . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
8.2.6 Student?s T test: robustness . . . . . . . . . . . . . . . . . . . . . . 577
8.2.7 Z test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
8.2.8 Student T test: comparing the mean of two samples. . . . . . . . . 582
8.2.9 Robustness of Student?s 2-sample T test . . . . . . . . . . . . . . . 584
8.2.10 Several means of comparing means . . . . . . . . . . . . . . . . . . 585
8.2.11 The Chi? and variance computations . . . . . . . . . . . . . . . . 587
8.2.12 Fisher distribution (F test) and comparison of the variance of two
samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
8.3 The Zoo of Statistical Tests: discrete variables and the Chi? test . . . . 590
8.3.1 Binomial test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
8.3.2 Mock binomial test (not important) . . . . . . . . . . . . . . . . . 591
8.3.3 Another binomial test . . . . . . . . . . . . . . . . . . . . . . . . . 592
8.3.4 Chi? test (very important) . . . . . . . . . . . . . . . . . . . . . . 592
8.3.5 Independance Chi? . . . . . . . . . . . . . . . . . . . . . . . . . . 594
8.3.6 Fisher test: independance of two qualitative variables . . . . . . . 595
8.4 The Zoo of Statistical Tests: non-parametric tests . . . . . . . . . . . . . 597
8.4.1 Sign test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
8.4.2 Wilcoxon?s U test: comparing two ?means?. . . . . . . . . . . . . 599
8.4.3 Kolmogorov-Smirnov Test (comparing two distributions) . . . . . . 601
8.4.4 Shapiro?Wilk test . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
8.4.5 Other non-parametric tests . . . . . . . . . . . . . . . . . . . . . . 602
8.5 Estimators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
8.5.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
8.5.2 Modelling a statistical experiment . . . . . . . . . . . . . . . . . . 603
8.5.3 Some vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
8.5.4 Estimator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
8.5.5 Unbiased estimator . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
8.5.6 Maximum Likelihood Estimators (MLE) . . . . . . . . . . . . . . . 608
8.5.7 log-likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
8.5.8 Fisher information . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
8.5.9 Likelihood Ratio (LR) test . . . . . . . . . . . . . . . . . . . . . . 611
8.5.10 MLE: mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
8.5.11 Bayesian methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
8.5.12 Bayesian statistics and quantum mechanics . . . . . . . . . . . . . 614
8.5.13 Bayesian Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
8.5.14 Fuzzy logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
8.5.15 Influence curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
8.5.16 Method of Moments Estimation (MME) . . . . . . . . . . . . . . . 616
8.5.17 L-moments, TL-moments . . . . . . . . . . . . . . . . . . . . . . . 617
8.5.18 GMM (Generalized Method of Moments) . . . . . . . . . . . . . . 617
8.5.19 Cramer-Rao inequality . . . . . . . . . . . . . . . . . . . . . . . . . 619
8.5.20 Sufficient statistic . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
8.5.21 BUE (Best Unbiaised Estimators, aka UMVUE, Uniformly Minimum
Variance Unbiased Estimator) . . . . . . . . . . . . . . . . . 619
8.6 TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
8.6.1 Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
8.6.2 Entropy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
8.6.3 Relative entropy (Kullback-Leibler distance) . . . . . . . . . . . . 620
8.6.4 Maximum Likelihood Methods and Statistical Tests . . . . . . . . 620
9 Regression 621
9.1 Model behind the regression . . . . . . . . . . . . . . . . . . . . . . . . . . 621
9.1.1 Model and assumptions . . . . . . . . . . . . . . . . . . . . . . . . 621
9.1.2 Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
CONTENTS x
9.1.3 Two-Stage Least Squares (2SLS) and Instrumental Variables (IV)
TODO: Put this somewhere else... . . . . . . . . . . . . . . . . . . 621
9.1.4 Structural Equation Modeling (SEM) TODO: Put this elsewhere . 623
9.1.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
9.1.6 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
9.2 A few DIY regression lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
9.2.1 Brown?Mood Line . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
9.2.2 Another line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
9.2.3 The median line . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
9.2.4 Another median line . . . . . . . . . . . . . . . . . . . . . . . . . . 626
9.3 Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
9.3.1 Correlation Coefficient . . . . . . . . . . . . . . . . . . . . . . . . . 627
9.3.2 Example: beta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
9.3.3 Accuracy of the correlation . . . . . . . . . . . . . . . . . . . . . . 632
9.3.4 Testing the correlation . . . . . . . . . . . . . . . . . . . . . . . . . 634
9.3.5 Spearman correlation . . . . . . . . . . . . . . . . . . . . . . . . . 635
9.3.6 Kendall?s tau . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
9.3.7 Plotting correlation matrices . . . . . . . . . . . . . . . . . . . . . 637
9.3.8 Correlation and missing values . . . . . . . . . . . . . . . . . . . . 639
9.3.9 Random Matrix Theory (RMT) . . . . . . . . . . . . . . . . . . . . 641
9.3.10 Correlation beyond gaussian distributions: copulas . . . . . . . . . 643
9.4 Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
9.4.1 Least squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
9.4.2 Geometric Interpretation . . . . . . . . . . . . . . . . . . . . . . . 649
9.4.3 The Gauss?Markov Theorem . . . . . . . . . . . . . . . . . . . . . 649
9.5 Regression with R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
9.5.1 Computations with R ?reading the result . . . . . . . . . . . . . . 649
9.6 Regression: general definition . . . . . . . . . . . . . . . . . . . . . . . . . 652
9.7 Regression asymetry and PCA (Principal Component Analysis) . . . . . . 652
9.8 TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
9.9 Other regressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
9.10 TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
9.10.1 nlme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
9.11 TODO: TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
9.11.1 Regression Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
9.12 Preparing the data: transformations, missing values, outliers . . . . . . . 660
9.13 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
9.14 Missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
9.14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
9.14.2 Decision trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
9.14.3 Taxonomy of decision trees . . . . . . . . . . . . . . . . . . . . . . 668
9.14.4 Logistic regression . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
9.14.5 naclus, naplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
9.14.6 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
9.14.7 Other functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
9.15 Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
9.16 TODO: TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
9.16.1 ACE (Alternating Conditionnal Expectation) . . . . . . . . . . . . 673
9.16.2 AVAS (Additivity and Variance Selection) . . . . . . . . . . . . . . 674
9.17 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
9.18 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
9.18.1 Let us first look at the variables one at a time . . . . . . . . . . . 676
9.18.2 TODO: go on... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
9.18.3 Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
9.18.4 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
10 Other regressions 679
10.1 Polynomials: curvilinear regression . . . . . . . . . . . . . . . . . . . . . . 679
10.1.1 Orthogonal polynomials . . . . . . . . . . . . . . . . . . . . . . . . 680
10.1.2 Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
10.1.3 Regression in other bases . . . . . . . . . . . . . . . . . . . . . . . 688
CONTENTS xi
10.1.4 Regression in other bases . . . . . . . . . . . . . . . . . . . . . . . 688
10.2 Non linear regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
10.2.1 Robust non-linear least squares . . . . . . . . . . . . . . . . . . . . 700
10.2.2 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
10.3 Local regressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
10.3.1 Broken line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
10.3.2 Segmented regression . . . . . . . . . . . . . . . . . . . . . . . . . . 702
10.3.3 Other broken line: lowess . . . . . . . . . . . . . . . . . . . . . . . 704
10.3.4 Moving Average (MA), Moving quartiles . . . . . . . . . . . . . . . 705
10.3.5 Weighted Local Least Squares: loess . . . . . . . . . . . . . . . . . 707
10.4 Variants of the Least Squares Method . . . . . . . . . . . . . . . . . . . . 711
10.4.1 L1 Regression (aka LAR (Least Absolute Residuals) or LAD (Least
Absolute Deviation)) . . . . . . . . . . . . . . . . . . . . . . . . . . 711
10.4.2 M-estimators, Huber Regression . . . . . . . . . . . . . . . . . . . 712
10.4.3 Least Trimmed Squares (LTS) . . . . . . . . . . . . . . . . . . . . 714
10.4.4 Generalized Least Squares (GLS) . . . . . . . . . . . . . . . . . . . 715
10.4.5 Generalized Least Squares . . . . . . . . . . . . . . . . . . . . . . . 715
10.4.6 Weighted Least Squares (WLS) . . . . . . . . . . . . . . . . . . . . 716
10.4.7 Iteratively Reweighted Least Squares (IRLS) . . . . . . . . . . . . 717
10.4.8 Quantile regression . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
10.4.9 Comparing those regressions . . . . . . . . . . . . . . . . . . . . . 722
10.4.10 Other robust or resistant linear regressions . . . . . . . . . . . . . 722
10.5 Penalized regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
10.5.1 Regularized Regression (PCR: Principal Component Regression) . 726
10.5.2 Partial Least Squares (PLS) . . . . . . . . . . . . . . . . . . . . . . 727
10.5.3 Penalized Least Squares . . . . . . . . . . . . . . . . . . . . . . . . 727
10.5.4 Ridge regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
10.5.5 Lasso . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
10.5.6 Comparing some of these regressions . . . . . . . . . . . . . . . . . 738
10.5.7 Penalized MLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
11 Regression Problems ?and their Solutions 743
11.1 Tests and confidence intervals . . . . . . . . . . . . . . . . . . . . . . . . . 743
11.1.1 Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
11.1.2 Confidence intervals and prediction intervals . . . . . . . . . . . . 746
11.1.3 Test on a pair of variables (ellipses) . . . . . . . . . . . . . . . . . 748
11.1.4 The dangers of multiple tests . . . . . . . . . . . . . . . . . . . . . 751
11.1.5 Regression and sums of squares . . . . . . . . . . . . . . . . . . . . 751
11.1.6 Reading the results of a regression . . . . . . . . . . . . . . . . . . 752
11.1.7 Comparing two models . . . . . . . . . . . . . . . . . . . . . . . . . 753
11.1.8 Anova and regression . . . . . . . . . . . . . . . . . . . . . . . . . . 754
11.2 Partial residual plots, added variable plots . . . . . . . . . . . . . . . . . . 755
11.3 Some plots to explore a regression . . . . . . . . . . . . . . . . . . . . . . 755
11.3.1 Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
11.3.2 Residuals and noise . . . . . . . . . . . . . . . . . . . . . . . . . . 755
11.3.3 Studentized (or standardized) residuals . . . . . . . . . . . . . . . 755
11.3.4 Jackknife (or studentized) residuals . . . . . . . . . . . . . . . . . . 756
11.3.5 Plotting the residuals . . . . . . . . . . . . . . . . . . . . . . . . . 756
11.3.6 Partial regression plot (or added variable plot) . . . . . . . . . . . 760
11.3.7 Partial residual plots . . . . . . . . . . . . . . . . . . . . . . . . . . 761
11.4 Overfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
11.4.1 Overfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
11.4.2 Sample too small . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
11.4.3 Too many variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
11.5 Underfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
11.5.1 Underfit (curvilinearity) . . . . . . . . . . . . . . . . . . . . . . . . 765
11.5.2 Structural changes: TODO . . . . . . . . . . . . . . . . . . . . . . 768
11.6 Influential points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
11.6.1 Influential observations . . . . . . . . . . . . . . . . . . . . . . . . 769
11.7 Influential clusters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
11.7.1 Clusters of outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
CONTENTS xii
11.8 Non gaussian residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
11.8.1 Non-gaussian residuals . . . . . . . . . . . . . . . . . . . . . . . . . 782
11.9 Heteroskedasticity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
11.9.1 Heteroscedasticity . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
11.9.2 lmtest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
11.10Correlated errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
11.10.1 Correlated errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
11.11Unidentifiability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
11.11.1Multicolinearity (unidentifiability) . . . . . . . . . . . . . . . . . . 808
11.12Missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
11.13Extrapolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
11.14Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
11.14.1Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
11.14.2 Measurement errors . . . . . . . . . . . . . . . . . . . . . . . . . . 815
11.15The curse of dimension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
11.15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
11.15.2Variable selection and non-supervised classification . . . . . . . . . 816
11.15.3TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
11.15.4Variables selection and regression . . . . . . . . . . . . . . . . . . . 817
11.15.5 General idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
11.15.6Example, by hand . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
11.15.7Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
11.15.8 Stepwise regression is BAD . . . . . . . . . . . . . . . . . . . . . . 826
11.15.9 Stepwise regression and Bayesian Model Averaging (BMA) . . . . 826
11.15.10Model selection and the Vapnik-Chervonenkis dimension . . . . . . 829
11.15.11Genetic algorithms and non-linear model selection . . . . . . . . . 831
11.15.12Dimension reduction . . . . . . . . . . . . . . . . . . . . . . . . . . 832
11.15.13SVM (Support Vector Machines) . . . . . . . . . . . . . . . . . . . 833
11.15.14TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
11.15.15GAM (Generatized Additive Model) . . . . . . . . . . . . . . . . . 837
11.15.16Classification and Regression Trees (CART (TM)) . . . . . . . . . 841
11.15.17PRIM (Patient Rule Induction Method, aka Bump Hunting) . . . 842
11.15.18Bagging (bootstrap aggregation) . . . . . . . . . . . . . . . . . . . 843
11.15.19Boosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
11.15.20Ensemble methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
11.15.21Random Forest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
11.15.22Outlier detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
11.15.23Non supervised learning . . . . . . . . . . . . . . . . . . . . . . . . 847
11.15.24Supervised learning . . . . . . . . . . . . . . . . . . . . . . . . . . 847
11.15.25Neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
11.15.26Bayesian Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
11.15.27MARS (Multivariate Adaptative Regression Splines) . . . . . . . . 848
11.15.28HME (Hierarchical Mixture of Experts) . . . . . . . . . . . . . . . 851
11.15.29MART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
11.15.30TODO: TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
11.15.31Stacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
11.15.32Bumping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
11.16Wide problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
11.16.1 Supervised Principal component Analysis (SPCA) . . . . . . . . . 851
12 Generalized Linear Models: logistic regression, Poisson regression,
etc. 854
12.1 Example: a classification problem . . . . . . . . . . . . . . . . . . . . . . . 854
12.1.1 Using regression in a classification problem . . . . . . . . . . . . . 854
12.1.2 Curvilinear regression and classification . . . . . . . . . . . . . . . 855
12.1.3 Nearest neighbours . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
12.1.4 Technical note: kD trees, nearest neighbours and local regression . 858
12.1.5 Comparing those methods . . . . . . . . . . . . . . . . . . . . . . . 860
12.1.6 Other examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
12.1.7 TODO: lda, qda . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
12.1.8 Mixture discriminant analysis . . . . . . . . . . . . . . . . . . . . . 862
CONTENTS xiii
12.1.9 Flexible discriminant analysis . . . . . . . . . . . . . . . . . . . . . 862
12.2 Naive Bayes classifyer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
12.2.1 Toy example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
12.2.2 The Bayes formula . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
12.2.3 Several predictiva variables, non-binary variables . . . . . . . . . . 863
12.2.4 Hypotheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
12.2.5 Implementation in R . . . . . . . . . . . . . . . . . . . . . . . . . . 864
12.2.6 Implemetation in R . . . . . . . . . . . . . . . . . . . . . . . . . . 865
12.2.7 Precision and recall rates . . . . . . . . . . . . . . . . . . . . . . . 865
12.2.8 ROC Curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
12.2.9 Practical example: spam filtering . . . . . . . . . . . . . . . . . . . 865
12.2.10 Other naive Bayes classifier . . . . . . . . . . . . . . . . . . . . . . 866
12.2.11 Other Bayes classifiers . . . . . . . . . . . . . . . . . . . . . . . . . 866
12.2.12 Other (unsorted) examples . . . . . . . . . . . . . . . . . . . . . . 866
12.3 Discriminant Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866
12.4 Logistic Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867
12.4.1 Generalized Linear Model . . . . . . . . . . . . . . . . . . . . . . . 867
12.4.2 Reading the result . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
12.4.3 Residuals and plots . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
12.4.4 Deviance, residuals, AIC . . . . . . . . . . . . . . . . . . . . . . . . 877
12.4.5 Model comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
12.4.6 TODO: to sort, to rewrite . . . . . . . . . . . . . . . . . . . . . . . 879
12.5 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
12.6 Variants of logistic regression . . . . . . . . . . . . . . . . . . . . . . . . . 889
12.6.1 Log-linear model (Poisson regression) . . . . . . . . . . . . . . . . 889
12.6.2 If the qualitative variable to predict has more than two values (first
attempt) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
12.6.3 Ordinal Logistic Regression . . . . . . . . . . . . . . . . . . . . . . 892
12.6.4 Multilogistic regression (aka Multinomial regression) . . . . . . . . 895
13 Analysis of Variance (Anova) 896
13.1 Regression with qualitative predictive variables . . . . . . . . . . . . . . . 896
13.1.1 Binary variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896
13.1.2 Two predictive variables, one qualitative (binary), another quantitative
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
13.1.3 Two qualitative (binary) predictive variables . . . . . . . . . . . . 900
13.1.4 Two qualitative (binary) predictive variables: interactions . . . . . 900
13.1.5 Qualitative variable with more that two values . . . . . . . . . . . 901
13.1.6 Contrasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903
13.2 ANalysis Of VAriance (Anova) . . . . . . . . . . . . . . . . . . . . . . . . 906
13.2.1 Anova: comparing more than two means . . . . . . . . . . . . . . . 906
13.2.2 R?, adjusted R? . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
13.2.3 Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
13.2.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912
13.2.5 Comparing models . . . . . . . . . . . . . . . . . . . . . . . . . . . 912
13.2.6 Likelihood ratio test . . . . . . . . . . . . . . . . . . . . . . . . . . 913
13.3 ANOVA vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
13.3.1 Simple anova, one-factor anova, one-way anova . . . . . . . . . . . 913
13.3.2 Double anova, two-factor anova, two-way anova . . . . . . . . . . . 914
13.3.3 Interaction plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
13.3.4 Repeated measure anova . . . . . . . . . . . . . . . . . . . . . . . . 916
13.3.5 Cross-factor anova . . . . . . . . . . . . . . . . . . . . . . . . . . . 916
13.3.6 Hierarchical anova . . . . . . . . . . . . . . . . . . . . . . . . . . . 916
13.3.7 Within subject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
13.3.8 Across subjets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
13.3.9 Experience design . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
13.3.10Between-subject design . . . . . . . . . . . . . . . . . . . . . . . . 919
13.3.11 In-subject design . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
13.3.12 Split-splot design . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
13.3.13 Hierarchical design . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
13.3.14Between-subject factor . . . . . . . . . . . . . . . . . . . . . . . . . 919
CONTENTS xiv
13.3.15Avova within . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
13.3.16 Carry-over effects . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
13.3.17 Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
13.3.18 Fixed effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
13.3.19Random effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
13.3.20Random effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
13.3.21 Within effects, within-subject effects . . . . . . . . . . . . . . . . . 922
13.3.22 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
13.3.23Repeated measures . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
13.3.24 Split-splot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
13.3.25 Hierarchical design . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
13.3.26 Hierarchical design (2) . . . . . . . . . . . . . . . . . . . . . . . . . 925
13.3.27 The ?Error?argument in the ?aov?function . . . . . . . . . . . . 925
13.3.28TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
13.3.29Anova within and error terms . . . . . . . . . . . . . . . . . . . . . 926
13.3.30Mixed models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
13.3.31 Fixed effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
13.3.32Random effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
13.3.33Mixed model and linear model . . . . . . . . . . . . . . . . . . . . 930
13.3.34Mixed models and Generalized Least Squares (GLS) . . . . . . . . 930
13.4 Mixed models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
13.4.1 Hierarchical Models . . . . . . . . . . . . . . . . . . . . . . . . . . 930
13.4.2 Other motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
13.4.3 more complex models . . . . . . . . . . . . . . . . . . . . . . . . . 934
13.4.4 The ecological fallacy . . . . . . . . . . . . . . . . . . . . . . . . . 934
13.4.5 Levels are not predictive variables . . . . . . . . . . . . . . . . . . 935
13.4.6 Regressions galore . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
13.4.7 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
13.4.8 TODO: TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
14 Mixed Models 937
14.1 Mixed models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
14.1.1 Generalized Least Squares (GLS) . . . . . . . . . . . . . . . . . . . 937
14.1.2 GLS systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
14.1.3 Mixed Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
14.1.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
14.1.5 Which effects are random? . . . . . . . . . . . . . . . . . . . . . . 941
14.1.6 Mixed models in R . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
14.1.7 General procedure to choose a mixed model . . . . . . . . . . . . . 943
14.1.8 Other examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
14.1.9 Example: nested groupings (Hierarchical Models) . . . . . . . . . . 949
14.1.10Example: crossed groupings . . . . . . . . . . . . . . . . . . . . . . 949
14.1.11About non-linear mixed effects . . . . . . . . . . . . . . . . . . . . 949
14.1.12Mixed models and bayesian statistics . . . . . . . . . . . . . . . . . 950
14.1.13Penalized splines, mixed models and bayesian statistics . . . . . . 950
14.2 Longitudinal data, Panel data . . . . . . . . . . . . . . . . . . . . . . . . . 953
14.2.1 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
14.3 Bayesian Networks, Graphical Models, etc. . . . . . . . . . . . . . . . . . 955
14.4 Mixed models, hierarchical models . . . . . . . . . . . . . . . . . . . . . . 955
14.4.1 TODO: write this part . . . . . . . . . . . . . . . . . . . . . . . . . 956
14.5 From hierarchical models to bayesian networks . . . . . . . . . . . . . . . 959
14.5.1 Hierarchical models . . . . . . . . . . . . . . . . . . . . . . . . . . 959
14.5.2 Bayesian networks: qualitative variables . . . . . . . . . . . . . . . 960
14.5.3 Bayesian methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
14.6 The dangers of Anova . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
14.6.1 The model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
14.6.2 The order of the terms . . . . . . . . . . . . . . . . . . . . . . . . . 961
14.6.3 Non parametric anova . . . . . . . . . . . . . . . . . . . . . . . . . 961
14.6.4 Post-hoc tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
14.7 TODO: TO SORT ?Anova examples . . . . . . . . . . . . . . . . . . . . 967
14.7.1 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
CONTENTS xv
14.7.2 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
14.7.3 1-way anova: example . . . . . . . . . . . . . . . . . . . . . . . . . 967
14.7.4 ??? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
14.7.5 Mixed models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
14.7.6 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
14.7.7 TODO: TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
14.7.8 Two-way anova example . . . . . . . . . . . . . . . . . . . . . . . . 975
14.7.9 Manova (Multivariate Analysis of Variance) . . . . . . . . . . . . . 975
14.7.10TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
15 Time series 977
15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
15.1.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
15.1.2 Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
15.1.3 The main problem of time series analysis . . . . . . . . . . . . . . 983
15.1.4 Autocorrelation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
15.1.5 White noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
15.1.6 Diagnostics: is this white noise? . . . . . . . . . . . . . . . . . . . 987
15.1.7 tsdiag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
15.2 Simple time series models . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
15.2.1 The classical model . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
15.2.2 First attempt: regression . . . . . . . . . . . . . . . . . . . . . . . 992
15.2.3 Other attempt (apparently a bad idea) . . . . . . . . . . . . . . . . 995
15.2.4 Other attempt (better than the previous) . . . . . . . . . . . . . . 996
15.2.5 Same idea, with splines . . . . . . . . . . . . . . . . . . . . . . . . 998
15.2.6 Finding the trend (or removing the seasonal component): Moving
Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
15.2.7 MA: Filtering and smoothing . . . . . . . . . . . . . . . . . . . . . 998
15.2.8 Applications of the Moving Average . . . . . . . . . . . . . . . . . 999
15.2.9 Exponential Moving average . . . . . . . . . . . . . . . . . . . . . . 999
15.2.10 Other moving quantities . . . . . . . . . . . . . . . . . . . . . . . . 1001
15.2.11 Finding the trend: Fourrier Transform . . . . . . . . . . . . . . . . 1002
15.2.12 Finding the trend: differentiation . . . . . . . . . . . . . . . . . . . 1002
15.2.13 Local regression: loess . . . . . . . . . . . . . . . . . . . . . . . . . 1002
15.2.14 Holt-Winters filtering . . . . . . . . . . . . . . . . . . . . . . . . . 1003
15.2.15 Structural models . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
15.2.16Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010
15.2.17Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011
15.3 ARIMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
15.3.1 ACF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
15.3.2 Correlogram, variogram . . . . . . . . . . . . . . . . . . . . . . . . 1020
15.3.3 MA (Moving Average models) . . . . . . . . . . . . . . . . . . . . 1021
15.3.4 AR (Auto-Regressive models) . . . . . . . . . . . . . . . . . . . . . 1023
15.3.5 PACF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
15.3.6 Yule-Walker Equations . . . . . . . . . . . . . . . . . . . . . . . . . 1025
15.3.7 Stationarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
15.3.8 Ergodicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026
15.3.9 AR and stationarity . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
15.3.10MA and invertibility . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
15.3.11 Unit root tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029
15.3.12ARMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029
15.3.13Overfitting an ARMA process . . . . . . . . . . . . . . . . . . . . . 1030
15.3.14How to get to a stationary process . . . . . . . . . . . . . . . . . . 1030
15.3.15ARIMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
15.3.16SARIMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038
15.3.17 The Box and Jenkins method . . . . . . . . . . . . . . . . . . . . . 1040
15.3.18Sample ARMA processes and their ACF and PACF . . . . . . . . 1046
15.3.19 Brute force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
15.3.20 Long-term memory, fractional integration . . . . . . . . . . . . . . 1053
15.4 Validating a model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
15.5 Spectral Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
CONTENTS xvi
15.5.1 Periodogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
15.5.2 Harmonics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
15.5.3 Periodogram (continued) . . . . . . . . . . . . . . . . . . . . . . . 1056
15.5.4 Spectrum and Autocorrelation . . . . . . . . . . . . . . . . . . . . 1059
15.5.5 Linear filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
15.5.6 Smoothing the raw periodogram . . . . . . . . . . . . . . . . . . . 1059
15.5.7 Fourier transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
15.5.8 Time domain, frequency domain . . . . . . . . . . . . . . . . . . . 1060
15.5.9 Reading the results of a DFT . . . . . . . . . . . . . . . . . . . . . 1060
15.5.10FFT: Details of the algorithm . . . . . . . . . . . . . . . . . . . . . 1065
15.5.11Applications of the FFT . . . . . . . . . . . . . . . . . . . . . . . . 1065
15.5.12Applications of the FFT: detailed examples . . . . . . . . . . . . . 1065
15.5.13FFT and time series . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
15.5.14FFT and sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
15.6 Wavelets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070
15.6.1 General idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070
15.6.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071
15.6.3 Gory technical details . . . . . . . . . . . . . . . . . . . . . . . . . 1072
15.6.4 Statistical applications of wavelets . . . . . . . . . . . . . . . . . . 1075
15.6.5 Wavelets in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
15.6.6 More about wavelets . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
15.7 Digital Signal Processing (DSP) . . . . . . . . . . . . . . . . . . . . . . . . 1076
15.7.1 Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
15.7.2 Hilbert transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
15.8 Modeling volatility: GARCH models (Generalized AutoRegressive Conditionnal
Heteroscedasticity) . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
15.8.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
15.8.2 Volatility clustering . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
15.8.3 Volatility clustering and runs test . . . . . . . . . . . . . . . . . . . 1077
15.8.4 Other examples, in finance (Stochastic Differential Equations) . . . 1080
15.8.5 ARCH model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084
15.8.6 Generalizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087
15.8.7 Regression with GARCH residuals . . . . . . . . . . . . . . . . . . 1088
15.8.8 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088
15.8.9 Implied volatility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
15.9 Multivariate time series . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
15.9.1 VAR models (Vector Auto-Regressive) . . . . . . . . . . . . . . . . 1094
15.9.2 Granger causality . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
15.9.3 Risk models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
15.9.4 Dynamic principal component analysis . . . . . . . . . . . . . . . . 1096
15.9.5 Cointegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
15.9.6 Panel data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
15.10State-Space Models and Kalman Filtering . . . . . . . . . . . . . . . . . . 1096
15.10.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
15.10.2 Other motivation: the notion of volatility in finance . . . . . . . . 1096
15.10.3 Other, more formal, example . . . . . . . . . . . . . . . . . . . . . 1097
15.10.4Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097
15.10.5 Scope of State Space Models . . . . . . . . . . . . . . . . . . . . . 1097
15.10.6 Kalman filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1098
15.10.7 1-dimensional Kalman filter . . . . . . . . . . . . . . . . . . . . . . 1100
15.10.8Multi-dimensional Kalman filter: the dse1 package . . . . . . . . . 1100
15.10.9Adaptive least squares . . . . . . . . . . . . . . . . . . . . . . . . . 1100
15.10.10Extended Kalman filter . . . . . . . . . . . . . . . . . . . . . . . . 1100
15.10.11Particle Kalman filter . . . . . . . . . . . . . . . . . . . . . . . . . 1100
15.10.12TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
15.11Non-linear time series and chaos . . . . . . . . . . . . . . . . . . . . . . . 1101
15.11.1Power laws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
15.11.2Beta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1104
15.11.3 The colours of noise . . . . . . . . . . . . . . . . . . . . . . . . . . 1105
15.11.4 Alpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106
15.11.5H (Hurst exponent) . . . . . . . . . . . . . . . . . . . . . . . . . . 1106
CONTENTS xvii
15.11.6Fractionnal brownian motion . . . . . . . . . . . . . . . . . . . . . 1107
15.11.7 Generalized Hurst exponent . . . . . . . . . . . . . . . . . . . . . . 1107
15.11.8 Other plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1108
15.11.9Recurrence plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1110
15.11.10Phase plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115
15.11.11Correlation dimension . . . . . . . . . . . . . . . . . . . . . . . . . 1117
15.11.12Zoom, Fish-eye . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117
15.12Other times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120
15.12.1 Irregular time series . . . . . . . . . . . . . . . . . . . . . . . . . . 1120
15.12.2 Alternate times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120
15.12.3Continuous time, stochastic differential equations . . . . . . . . . . 1122
15.13Discrete-valued time series: Markov chains and beyond . . . . . . . . . . . 1122
15.13.1Example, definition . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
15.13.2 Stationnary distribution . . . . . . . . . . . . . . . . . . . . . . . . 1122
15.13.3 Irreducibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
15.13.4Aperiodicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
15.13.5 Ergodicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
15.13.6Reversible Markov Chain . . . . . . . . . . . . . . . . . . . . . . . 1123
15.14Variants of Markov chains . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
15.14.1Markov chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
15.14.2HMM (Hidden Markov Chains) . . . . . . . . . . . . . . . . . . . . 1133
15.14.3HMM emitting continuous variables . . . . . . . . . . . . . . . . . 1133
15.14.4Markov Decision Processes (MDP) . . . . . . . . . . . . . . . . . . 1133
15.14.5TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133
15.14.6Markov chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133
15.14.7 Hidden Markov chains . . . . . . . . . . . . . . . . . . . . . . . . . 1134
15.15Untackled subjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1134
15.15.1TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136
15.16TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136
15.16.1Examples of time series (TO DELETE?) . . . . . . . . . . . . . . . 1136
15.16.2Examples (TO DELETE?) . . . . . . . . . . . . . . . . . . . . . . 1143
15.16.3Nyquist Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
15.16.4Phase space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
15.16.5Phase space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
15.16.6 Other packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1153
16 Miscellaneous 1154
16.1 Survival analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1154
16.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1155
16.2.1 Definition and Examples . . . . . . . . . . . . . . . . . . . . . . . . 1155
16.2.2 Survival function, Hazard function . . . . . . . . . . . . . . . . . . 1156
16.3 Some Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156
16.3.1 Exponential distribution . . . . . . . . . . . . . . . . . . . . . . . . 1156
16.3.2 Weibull Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . 1157
16.3.3 Gamma Distribution . . . . . . . . . . . . . . . . . . . . . . . . . 1158
16.3.4 Log-normal distribution . . . . . . . . . . . . . . . . . . . . . . . . 1158
16.3.5 Other models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159
16.4 Kaplan-Meier estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159
16.4.1 product-limit Kaplan?Meier estimator of the survival function . . 1159
16.4.2 Survival analysis with R . . . . . . . . . . . . . . . . . . . . . . . . 1160
16.4.3 Mantel-Cox Log-rank test . . . . . . . . . . . . . . . . . . . . . . . 1166
16.5 Discrete time models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167
16.6 Parametric regression models . . . . . . . . . . . . . . . . . . . . . . . . . 1167
16.7 Proportional Hazard (PH) models . . . . . . . . . . . . . . . . . . . . . . 1174
16.8 Accelerated Failure Time (AFT) models . . . . . . . . . . . . . . . . . . . 1175
16.9 Other models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
16.9.1 PCE model (Piecewise Constant Exponential) . . . . . . . . . . . . 1175
16.10Cox Proportional Hazard model . . . . . . . . . . . . . . . . . . . . . . . . 1175
16.11TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
16.11.1TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
16.11.2 Interval-censored data . . . . . . . . . . . . . . . . . . . . . . . . . 1176
CONTENTS xviii
16.11.3Multiple events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176
16.11.4Frailty models (unobserved heterogeneity) . . . . . . . . . . . . . . 1176
16.11.5TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176
16.12Spatial data and GIS (Geographical Information Systems) . . . . . . . . . 1177
16.12.1 Deformed maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177
16.12.2 Spacial processes and graphs . . . . . . . . . . . . . . . . . . . . . 1177
16.12.3Sample data: galaxy locations . . . . . . . . . . . . . . . . . . . . . 1179
16.12.4Voronoi tessallations . . . . . . . . . . . . . . . . . . . . . . . . . . 1180
16.12.5Penrose tilings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
16.13Bootstrap and simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
16.14Simulations: Parametric bootstrap . . . . . . . . . . . . . . . . . . . . . . 1185
16.14.1Monte Carlo Method . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
16.14.2 Distribution of a statistic . . . . . . . . . . . . . . . . . . . . . . . 1186
16.14.3 Accuracy of the forecasts . . . . . . . . . . . . . . . . . . . . . . . 1190
16.14.4Comparing two methods, choice of a parameter (ridge regression) . 1191
16.14.5 Estimating the bias of an estimator . . . . . . . . . . . . . . . . . 1193
16.14.6Parametric bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . 1198
16.15Non-parametric bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . 1198
16.15.1 Cross-validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1198
16.15.2 Jack-knife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1199
16.15.3 Jack-knife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200
16.15.4Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200
16.15.5Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1203
16.15.6Confidence intervals . . . . . . . . . . . . . . . . . . . . . . . . . . 1204
16.15.7 The bootstrap is biased . . . . . . . . . . . . . . . . . . . . . . . . 1205
16.15.8Out-of-the-bag bootstrap . . . . . . . . . . . . . . . . . . . . . . . 1205
16.15.9 .632 bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205
16.16Other examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206
16.16.1Bootstrap and linear regression . . . . . . . . . . . . . . . . . . . . 1206
16.16.2Bootstrap and time series . . . . . . . . . . . . . . . . . . . . . . . 1207
16.17TODO: delete what follows? . . . . . . . . . . . . . . . . . . . . . . . . . . 1207
16.17.1 Cross-validation and bootstrap . . . . . . . . . . . . . . . . . . . . 1207
16.17.2 Jackknife: correcting the bias . . . . . . . . . . . . . . . . . . . . . 1208
16.17.3Bootstrap (resampling) . . . . . . . . . . . . . . . . . . . . . . . . 1208
16.17.4 Jackknife-after-Bootstrap Plots . . . . . . . . . . . . . . . . . . . . 1209
16.17.5Why should the bootstrap work? . . . . . . . . . . . . . . . . . . . 1210
16.17.6Out-of-the-bag bootstrap and bootstrap .632 . . . . . . . . . . . . 1210
16.17.7Bootstrap and estimator comparison . . . . . . . . . . . . . . . . . 1211
16.17.8TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
16.17.9TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
16.17.10TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
16.17.11TODO: TO SORT . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
16.17.12Jackknife . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214
16.18TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214
16.19TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214
16.19.1Compromise bias-varance . . . . . . . . . . . . . . . . . . . . . . . 1214
16.20MCMC: Monte Carlo simulations with Markov Chains . . . . . . . . . . . 1216
16.20.1 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1216
16.20.2 Structure of this part . . . . . . . . . . . . . . . . . . . . . . . . . 1216
16.20.3 Motivations (1): Random sampling . . . . . . . . . . . . . . . . . . 1216
16.20.4 Motivation (2): integration . . . . . . . . . . . . . . . . . . . . . . 1217
16.20.5 The Metropolis-Hastings algorithm . . . . . . . . . . . . . . . . . . 1218
16.20.6Variant: Random Walk Metropolis . . . . . . . . . . . . . . . . . 1219
16.20.7Variant: Independance sampler . . . . . . . . . . . . . . . . . . . 1219
16.20.8Variant: one coordinate at a time . . . . . . . . . . . . . . . . . . . 1219
16.20.9Variant: Gibbs Sampling . . . . . . . . . . . . . . . . . . . . . . . 1219
16.20.10Interlude: a simple application of the Gibbs sampler . . . . . . . . 1221
16.20.11Application of the Gibbs sampler . . . . . . . . . . . . . . . . . . . 1221
16.20.12Another application of the Gibbs sampler . . . . . . . . . . . . . . 1222
16.20.13TODO: Gibbs and EM . . . . . . . . . . . . . . . . . . . . . . . . . 1222
16.20.14TODO: Gibbs, MCMC, Bugs (OpenBuGS, JAGS, Hydra) . . . . . 1222
CONTENTS xix
16.20.15Variant: Metropolis-Hastings-Green . . . . . . . . . . . . . . . . . 1222
16.20.16Problems with the Metropolis?Hastings algorithm . . . . . . . . . 1222
16.20.17Application> Integral computation . . . . . . . . . . . . . . . . . . 1223
16.20.18Simulated Annealing . . . . . . . . . . . . . . . . . . . . . . . . . . 1227
16.20.19MCMC and MLE . . . . . . . . . . . . . . . . . . . . . . . . . . . 1227
16.20.20Ergodicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1227
16.20.21TODO: TO SORT Importance Sampling . . . . . . . . . . . . . . 1227
16.21Bayesian methods and MCMC . . . . . . . . . . . . . . . . . . . . . . . . 1228
16.21.1BUGS, WinBUGS, OpenBUGS . . . . . . . . . . . . . . . . . . . . 1228
16.21.2JAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1230
16.21.3 The BUGS language . . . . . . . . . . . . . . . . . . . . . . . . . . 1231
16.21.4Example: classical regression . . . . . . . . . . . . . . . . . . . . . 1232
16.21.5Example: logistic regression . . . . . . . . . . . . . . . . . . . . . . 1232
16.21.6Example: mixture . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
16.21.7Example: Mixed effects model . . . . . . . . . . . . . . . . . . . . 1233
16.21.8Example: changepoint problem . . . . . . . . . . . . . . . . . . . . 1233
16.21.9Example: model selection . . . . . . . . . . . . . . . . . . . . . . . 1234
16.21.10Jags and coda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234
16.21.11Hydra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237
16.22Graph theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237
16.22.1 Installing RGraphviz . . . . . . . . . . . . . . . . . . . . . . . . . . 1238
16.22.2Miscellanies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1238
16.23Linguistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1242
17 Applications 1243
17.1 Finance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243
17.2 Genetics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243
17.3 Image Analysis: Mathematical morphology . . . . . . . . . . . . . . . . . 1243
17.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243
17.3.2 TODO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244

下载地址
顶一下
(6)
85.7%
踩一下
(1)
14.3%
------分隔线----------------------------