Tải bản đầy đủ (.pdf) (225 trang)

IT training r and MATLAB hiebeler 2015 06 23

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (12.1 MB, 225 trang )

The R Series

R and MATLAB®

David E. Hiebeler



R and MATLAB®


Chapman & Hall/CRC
The R Series
Series Editors
John M. Chambers
Department of Statistics
Stanford University
Stanford, California, USA

Torsten Hothorn
Division of Biostatistics
University of Zurich
Switzerland

Duncan Temple Lang
Department of Statistics
University of California, Davis
Davis, California, USA

Hadley Wickham
RStudio


Boston, Massachusetts, USA

Aims and Scope
This book series reflects the recent rapid growth in the development and application
of R, the programming language and software environment for statistical computing
and graphics. R is now widely used in academic research, education, and industry.
It is constantly growing, with new versions of the core software released regularly
and more than 6,000 packages available. It is difficult for the documentation to
keep pace with the expansion of the software, and this vital book series provides a
forum for the publication of books covering many aspects of the development and
application of R.
The scope of the series is wide, covering three main threads:
• Applications of R to specific disciplines such as biology, epidemiology,
genetics, engineering, finance, and the social sciences.
• Using R for the study of topics of statistical methodology, such as linear and
mixed modeling, time series, Bayesian methods, and missing data.
• The development of R, including programming, building packages, and
graphics.
The books will appeal to programmers and developers of R software, as well as
applied statisticians and data analysts in many fields. The books will feature
detailed worked examples and R code fully integrated into the text, ensuring their
usefulness to researchers, practitioners and students.


Published Titles
Stated Preference Methods Using R, Hideo Aizaki, Tomoaki Nakatani,
and Kazuo Sato
Using R for Numerical Analysis in Science and Engineering, Victor A. Bloomfield
Event History Analysis with R, Göran Broström
Computational Actuarial Science with R, Arthur Charpentier

Statistical Computing in C++ and R, Randall L. Eubank and Ana Kupresanin
Reproducible Research with R and RStudio, Second Edition, Christopher Gandrud
R and MATLAB®David E. Hiebeler
Nonparametric Statistical Methods Using R, John Kloke and Joseph McKean
Displaying Time Series, Spatial, and Space-Time Data with R,
Oscar Perpiñán Lamigueiro
Programming Graphical User Interfaces with R, Michael F. Lawrence
and John Verzani
Analyzing Sensory Data with R, Sébastien Lê and Theirry Worch
Parallel Computing for Data Science: With Examples in R, C++ and CUDA,
Norman Matloff
Analyzing Baseball Data with R, Max Marchi and Jim Albert
Growth Curve Analysis and Visualization Using R, Daniel Mirman
R Graphics, Second Edition, Paul Murrell
Data Science in R: A Case Studies Approach to Computational Reasoning and
Problem Solving, Deborah Nolan and Duncan Temple Lang
Multiple Factor Analysis by Example Using R, Jérôme Pagès
Customer and Business Analytics: Applied Data Mining for Business Decision
Making Using R, Daniel S. Putler and Robert E. Krider
Implementing Reproducible Research, Victoria Stodden, Friedrich Leisch,
and Roger D. Peng
Graphical Data Analysis with R, Antony Unwin
Using R for Introductory Statistics, Second Edition, John Verzani
Advanced R, Hadley Wickham
Dynamic Documents with R and knitr, Second Edition, Yihui Xie



R and MATLAB®


David E. Hiebeler
University of Maine
Orono, USA


MATLAB® is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks does not warrant the
accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® software or related products
does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or particular
use of the MATLAB® software.

CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2015 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20150406
International Standard Book Number-13: 978-1-4665-6839-6 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://
www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For
organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at

and the CRC Press Web site at



To my parents,
for encouraging me as I got started
with that first Apple ][+.



Contents

List of Figures

xiii

List of Tables

xv

Preface

xvii

About the Author


xxi

1 Installing and Running R and MATLAB
1.1 Obtaining and installing . . . . . . . . .
1.2 Commands for getting help . . . . . . .
1.3 Demos . . . . . . . . . . . . . . . . . . .
1.4 Quitting . . . . . . . . . . . . . . . . . .
1.5 Additional resources . . . . . . . . . . .
2 Getting Started: Variables and
2.1 Variable names . . . . . . .
2.2 Assignment statements . . .
2.3 Basic computations . . . . .
2.4 Formatting of output . . . .
2.5 Other computations . . . .
2.6 Complex numbers . . . . . .
2.7 Strange variable names in R
2.8 Data types . . . . . . . . . .
2.8.1 R . . . . . . . . . . .
2.8.2 MATLAB . . . . . .

Basic
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .

. . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

1
1
4
6
6
6

Computations
. . . . . . . . . .
. . . . . . . . . .

. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

9
9
10
11
14
15
16
16
17

17
18

. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
rows/columns .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

19
19
19
22
25
27
31
33
33
33
35
37
39

.
.
.
.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.

3 Matrices and Vectors
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Creating vectors . . . . . . . . . . . . . . . . . . . . .
3.3 Working with vectors . . . . . . . . . . . . . . . . . . .
3.4 Creating matrices . . . . . . . . . . . . . . . . . . . . .
3.5 Working with matrices . . . . . . . . . . . . . . . . . .
3.6 Reshaping matrices, and higher-dimensional arrays . .
3.7 Sparse matrices . . . . . . . . . . . . . . . . . . . . . .
3.8 Names with vectors and matrices/arrays . . . . . . . .
3.8.1 R: names for vector/matrix elements and matrix
3.8.2 R data frames . . . . . . . . . . . . . . . . . . .
3.8.3 MATLAB structs . . . . . . . . . . . . . . . . .
3.9 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . .

ix


x

Contents

4 Matrix/Vector Calculations and Functions
4.1 Applying a function to rows or columns of a matrix .
4.2 Applying a function to all elements of a matrix . . .
4.3 Linear algebra calculations with vectors and matrices
4.4 Statistical calculations . . . . . . . . . . . . . . . . .
4.5 Vectorized logical tests . . . . . . . . . . . . . . . . .
4.6 Other calculations . . . . . . . . . . . . . . . . . . .

5 Lists
5.1
5.2
5.3
5.4

5.5

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

41
41
42
43
46

49
50

and Cell Arrays
Creating lists and cell arrays . . . . . . . . . . . . . . .
Using lists and cell arrays . . . . . . . . . . . . . . . . .
Applying functions to all elements of lists and cell arrays
Converting other data types to lists and cell arrays . . .
5.4.1 All values in a numeric vector or matrix . . . . .
5.4.2 Matrix, by columns or rows . . . . . . . . . . . .
Converting lists and cell arrays to other data types . . .
5.5.1 Set of vectors to a single vector . . . . . . . . . .
5.5.2 Set of vectors to matrix . . . . . . . . . . . . . . .
5.5.3 Set of sets to matrix . . . . . . . . . . . . . . . .
5.5.4 Set of strings to a set of numeric vectors . . . . .

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

55
55
56
58
60
60
61
61
61
62
63
63

6 Flow Control

6.1 Conditional (“if”) statements
6.2 “If/else” statements . . . . .
6.3 “for” loops . . . . . . . . . . .
6.4 “while” loops . . . . . . . . .
6.5 Breaking out of loops . . . . .
6.6 “switch” statements . . . . .
6.7 “ifelse” statements in R . . .

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

65
65
67
69
71
72
73
76

7 Running Code from Files: Scripts
7.1 Current working directory . . . . . . . . . .
7.2 The MATLAB search path . . . . . . . . .
7.3 Executing code from a file . . . . . . . . . .
7.4 Creating a new script document in the editor
7.5 Comments in script files . . . . . . . . . . .
7.6 Executing code from the editor window . .
7.7 Summary of differences . . . . . . . . . . . .

. . . .
. . . .
. . . .
. . .
. . . .

. . . .
. . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

77
77
78
78
79
80
81
82

8 Writing Your Own Functions
8.1 R . . . . . . . . . . . . . . . . . . . . . .
8.1.1 Writing functions . . . . . . . . .
8.1.2 Calling functions . . . . . . . . .

8.1.3 Environments and variable scope
8.1.4 Static variables . . . . . . . . . .
8.1.5 Variable arguments . . . . . . . .
8.2 MATLAB . . . . . . . . . . . . . . . . .
8.2.1 Inline and anonymous functions .
8.2.2 Writing functions . . . . . . . . .
8.2.3 Calling functions . . . . . . . . .
8.2.4 Environments and variable scope

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

83
83
83

85
85
87
88
89
89
89
91
93

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.


Contents

xi
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

94
95
96
96

9 Probability and Random Numbers
9.1 Basic random values, permutations, and samples . .
9.2 Random number seed . . . . . . . . . . . . . . . . .
9.3 Random variates from probability distributions . . .
9.4 PDFs, CDFs, and inverse CDFs . . . . . . . . . . . .

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

99
99

101
102
104

10 Graphics
10.1 Creating, selecting, and closing figure windows
10.1.1 Creating windows . . . . . . . . . . . .
10.1.2 Listing and selecting windows . . . . .
10.1.3 Closing windows . . . . . . . . . . . . .
10.2 Basic 2-D scatterplots . . . . . . . . . . . . .
10.3 Adding additional plots to a figure . . . . . .
10.4 Axis ranges . . . . . . . . . . . . . . . . . . .
10.5 Logarithmic axis scales . . . . . . . . . . . . .
10.6 Background grid . . . . . . . . . . . . . . . .
10.7 Plotting multiple data sets simultaneously . .
10.8 Axis labels and figure titles . . . . . . . . . .
10.9 Adding text to figures . . . . . . . . . . . . .
10.10 Greek letters and mathematical symbols . . .
10.11 Arrows . . . . . . . . . . . . . . . . . . . . . .
10.12 Figure legends . . . . . . . . . . . . . . . . . .
10.13 Size and font adjustments . . . . . . . . . . .
10.14 Two y axes . . . . . . . . . . . . . . . . . . .
10.15 Plotting functions . . . . . . . . . . . . . . . .
10.16 Image plots and contours . . . . . . . . . . .
10.17 Colormaps . . . . . . . . . . . . . . . . . . . .
10.18 3-D plotting . . . . . . . . . . . . . . . . . . .
10.19 Multiple subplots in one figure . . . . . . . .
10.20 Saving figures . . . . . . . . . . . . . . . . . .
10.21 Other types of plots . . . . . . . . . . . . . .
10.22 Final notes about graphics . . . . . . . . . . .

11 Numerical Computing
11.1 Root-finding . . . . . . . . . . . . . .
11.1.1 Something to watch out for . .
11.2 Univariate optimization . . . . . . .
11.3 Multivariate optimization . . . . . .
11.4 Numerical integration . . . . . . . .
11.5 Curve fitting . . . . . . . . . . . . .
11.5.1 Piecewise linear interpolation
11.5.2 Polynomial fitting . . . . . . .
11.5.3 Splines . . . . . . . . . . . . .
11.6 Differential equations . . . . . . . . .

8.3

8.2.5 Static variables . . . . . . .
8.2.6 Variable arguments . . . . .
8.2.7 Function handles . . . . . .
Summary of main differences . . .

.
.
.
.

.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

107
107
107
108
108
108

111
111
112
112
112
114
114
115
116
117
119
120
121
122
126
126
128
131
133
135

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

137
137
138
138
139
140
140
140
141
142
142



xii

Contents

12 File Input and Output
12.1 Opening files . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.2 Reading a table of numbers . . . . . . . . . . . . . . . . . . .
12.2.1 Subsets of a data file . . . . . . . . . . . . . . . . . . .
12.3 Reading numeric data with a different comment character . .
12.4 Reading numbers from a file where different lines have varying
values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.5 Reading numbers and strings . . . . . . . . . . . . . . . . . .
12.6 Reading the raw character data in, a line at a time . . . . . .
12.7 Writing a table of numbers . . . . . . . . . . . . . . . . . . .
12.8 Writing a set of strings . . . . . . . . . . . . . . . . . . . . . .
12.9 Saving and loading variables in binary format . . . . . . . . .
12.10 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.11 URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.12 Excel files . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
numbers of
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

13 Miscellaneous
13.1 Working with variables . . . . . . . . . . .
13.2 Character strings . . . . . . . . . . . . . .
13.3 Reading user input . . . . . . . . . . . . .
13.4 Recording a copy of commands and output
13.5 Date calculations . . . . . . . . . . . . . .
13.6 Miscellaneous . . . . . . . . . . . . . . . .
13.7 Debugging . . . . . . . . . . . . . . . . . .
13.8 Startup and shutdown sequences . . . . .
13.9 Add-ons: packages and toolboxes . . . . .
13.10 Object-oriented programming . . . . . . .
13.11 Other interfaces . . . . . . . . . . . . . . .
13.12 Efficiency/performance . . . . . . . . . . .

. . . .
. . . .
. . . .
. . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .

. . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

165
165
166
168
169
169
170
171
172
173
174
174
175

14 Calling C
14.1 R . . . . . . . . . . . . . . . . . . . .
14.1.1 Example and overview . . . .
14.1.2 Printing, warnings, and errors
14.1.3 Random numbers . . . . . . .
14.1.4 More advanced features . . . .
14.2 MATLAB . . . . . . . . . . . . . . .
14.2.1 Example and overview . . . .
14.2.2 Printing, warnings, and errors
14.2.3 Random numbers . . . . . . .

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

177

178
178
181
182
185
188
188
192
192

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

147
147
148
151
152
153
156
159
159
160
161
162
163
163

Bibliography

199

Index of R commands, variables, and symbols


201

Index of MATLAB commands, variables, and symbols

207


List of Figures

1.1
1.2
1.3

The R graphical user interface in Mac OS-X. . . . . . . . . . . . . . . . . .
The RStudio graphical user interface in Mac OS-X. . . . . . . . . . . . . .
The MATLAB graphical user interface in Mac OS-X. . . . . . . . . . . . .

10.1
10.2
10.3
10.4
10.5
10.6
10.7

A plot of matrix data . .
Figures with two y axes .
Image plots in R . . . . .
Image plots in MATLAB

Surface plots of a function
Subfigures in R . . . . . .
Subfigures in MATLAB .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

2
3
4
113
121
123
125
127
130
132

xiii




List of Tables

2.1
2.2
2.3

Basic computations which are identical in R and MATLAB . . . . . . . . .
Basic computations which are different in R and MATLAB . . . . . . . . .
Basic functions for working with complex numbers . . . . . . . . . . . . . .

13
14
16

3.1

Comparison operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

6.1

Scalar logical operators

65

. . . . . . . . . . . . . . . . . . . . . . . . . . . . .


10.1 Possible values for the type parameter to the R plot command . . .
10.2 Values for the pch (“Plotting CHaracter”) parameter to the R plot
command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.3 Values for the lty (“Line TYpe”) parameter to the R plot command
10.4 Available colors, plotting symbols/markers, and line styles for basic
MATLAB plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14.1
14.2
14.3
14.4
14.5

Timings for permuting 2 × 105 values in C and R . . . . . . .
Functions for generating random values in C code called from
Timings of a birth-death process in C and R . . . . . . . . . .
Timings for permuting 2 × 105 values in C and MATLAB . .
Timings of a birth-death process in C and MATLAB . . . . .

.
R
.
.
.

.
.
.
.
.


.
.
.
.
.

.
.
.
.
.

. . . .

109

. . . .
. . . .

109
110

. . . .

110

.
.
.

.
.

181
183
185
191
197

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

xv




Preface

Who this book is for
I have interacted with many people over the past several years after making my “MATLAB/R Reference” available on my Web site. Based on those conversations, there is a large
population of people out there who have used MATLAB 1 for some period of time, but
who now find themselves working with biologists, statisticians, or some other professionals
who speak R rather than MATLAB. There is a complementary group of people who use R,
who now find themselves trying to work and share code with colleagues who use MATLAB.
The intended reader is someone who already knows one package, and for whatever reasons,
now needs or wants to learn the other.
This book grew out of the MATLAB/R Reference document mentioned above. That document is a concise reference summary of many of the key topics presented in this book, and
continues to be available on my Web site, at />
My own experience learning R
I fall into the first category of users described above. I used MATLAB for many years,
primarily for prototyping my research simulations (which I would then rewrite in C for
faster performance) and for data visualization and graphics. I also regularly taught a course
on “Modeling and Simulation” in which I used MATLAB as the software platform. The
course covers various biological models, including stochastic spatial models. I found more
and more biologists signing up for the class over the years, and some of them started asking
if they could use R because they had already started learning to use it in their statistics
classes. I was somewhat tired of learning new programming languages/environments, and
was not particularly eager to learn another one.2 But I reluctantly decided to look into R.
At first I was very frustrated with some of the differences that struck me right away — for
example, when you edit a file defining a new R function, you cannot just type its name to
call the function, but instead must “source” it first. Typing a matrix into R is certainly
more tedious than doing so in MATLAB.
However, over time, I came to appreciate the power and flexibility of many features of
R. The fact that it is available for free on Windows, Mac OS-X, and Linux certainly has
1 MATLAB and Simulink are registered trademarks of The MathWorks, Inc. For product information,

please contact: The MathWorks, Inc., 3 Apple Hill Drive, Natick, MA 01760-2098 USA; Tel: 508-647-7000;
Fax: 508-647-7001; E-mail: ; Web: www.mathworks.com.
2 The main programming languages I had used over the years at that point were Applesoft BASIC, Apple
][+ machine language, Pascal, Modula-2, Forth, C, Lisp, APL, SNOBOL, PostScript, Java, Perl, MATLAB,
and a bit of Python. Plus various libraries/environments and programming-like things such as SunView,
X11, csh scripting, LATEX, and HTML. That list is certainly smaller than that of many computer scientists,
but long enough for me.

xvii


xviii

Preface

made it easier for the many students who work on research with me or take my courses to
obtain a copy for their own computers.
There are still some times when I miss the more concise/simple way of expressing things
in MATLAB (in particular, how much more convenient it is to type in a matrix), but I have
now come to enjoy the time I spend working in R.

Formatting conventions and terminology
R and MATLAB commands, i.e., things you could actually type at the command prompt,
are generally formatted using a non-bold typewriter font, for example x = sqrt(7). A
bold font is used when referring to the name of a function, file, variable, or keyword, for
example the sqrt function, the variable foo, or for loops.
R and MATLAB are referred to in the book as “platforms,” rather than the perhaps
more natural “software packages,” because the word “package” is a loaded word with specific
meaning in R.
In some parts of the book, R and MATLAB code are placed side by side. This is done

for brief commands and concepts which do not need much explanation. In other parts, R
material is presented, followed by MATLAB material, with some differences between the
platforms emphasized.

Commands vs. GUI
There are two main approaches to working with software platforms like MATLAB and R:
doing things primarily with commands, and doing things primarily via menus through a
graphical user interface (GUI). I will admit that I am old-school, and started using computers before they had graphical user-interfaces. Personally, I can type commands much
more quickly than I can click my way to equivalent commands via menus, so I prefer the
command-line approach over the GUI approach.
As one justification for my preference, using commands to achieve goals is usually a bit
more portable and easier to share with others than using menus. The placements of various
items in menus is sometimes different on different operating systems such as Mac OS-X vs.
Microsoft Windows, so if you are trying to explain to someone how to accomplish something,
and they have a different operating system than you (or even simply a different version of the
software), you may have some trouble. Admittedly, some commands may also differ between
operating systems or between versions of the software. But for the most part, if you write
an R or MATLAB script on one operating system, it will work identically on others, and can
be usefully shared with other people more easily than a description of which menu items to
select and which buttons to push. The configuration and details of user-interfaces also tend
to change more frequently than the commands in packages like MATLAB and R; if you
tell someone how to do something using commands, it is likely to work on a wider variety
of versions of the software. I therefore focus more on using commands than on clicking on
menu items in this book, for those tasks where both approaches can be used.


Preface

xix


What this book is not
This book is not intended to be a comprehensive introduction or overview of either the R
or MATLAB platforms. I should warn you up front that this book will not teach you what
many may consider the “best” way to do things in R or MATLAB, according to the deeper
philosophy of either platform. My goal, based on my experience, is to try and show how to
do things in either platform which is most similar to the ways they are done in the other,
to make the transition from one platform to the other as quick and painless as possible for
you. If you want to delve into the deeper ways of thinking behind using either platform,
this reference can help get you started, but you should follow up with some of the other
plentiful resources available.
This book is also not intended to steer you toward one platform or the other. Quite
often, the choice of software platform to use depends on your context, i.e., your employer
and colleagues. If you truly have a wide-open choice, it is difficult to say which platform
is best for you. My personal feeling is that many things are easier to do in MATLAB, but
can be done more flexibly in R, at the expense of being more complicated. That is just a
general impression, and there are of course many counterexamples. Another thing to note
is that The MathWorks, Inc. (developer of MATLAB) has been quite aggressive recently
about improving the performance of various types of MATLAB code; many of my MATLAB
computations run many times faster in newer versions of MATLAB than they used to. If
you are at a large company or academic institution, you likely have access to a site license
to use MATLAB. If you are looking for routines to perform specialized statistical tests, R
has a vast and quickly growing library of packages to fit that need. R is of course available
to download free of charge, although MATLAB can be evaluated in a free trial, and there
is very affordable pricing for students or for personal use.

Acknowledgments
Thanks to my editor, John Kimmel, for being far too patient with me as I worked on this
book.
Bill Halteman helped me learn R, first by going through my MATLAB simulation lab
exercises and determining it would not be too difficult to implement them all in R. He then

patiently answered my many questions about R (and pretty much everything else under
the sun) for the following several years. Many people have also sent me suggestions and
corrections for my MATLAB/R Reference that this book was originally based on. Those
contributors include Juan David Ospina Arango, Berry Boessenkool, Robert Bryce, Thomas
Clerc, Alan Cobo-Lewis, Richard Cotton, Stephen Eglen, Andreas Handel, Niels Richard
Hansen, Luke Hartigan, Roger Jeurissen, David Khabie-Zeitoune, Seungyeon Kim, Michael
Kiparsky, Isaac Michaud, Andy Moody, Ben Morin, Lee Pang, Manas A. Pathak, Rachel
Rier, Rune Schjellerup Philosof, Rachel Rier, William Simpson, David Winsemius, Corey
Yanofsky, and Jian Ye.
Finally, like most authors, I must thank my family and apologize to them for all of the
times I chose to spend with drafts of this book rather than with them.



About the Author

David E. Hiebeler is an Associate Professor in the Department of Mathematics & Statistics at the University of Maine. He was previously a Visiting Lecturer at Cornell University.
He has degrees from Rensselaer Polytechnic Institute, Harvard University, and a Ph.D. in
applied mathematics from Cornell University.
He previously worked at the Center for Nonlinear Studies, the Theoretical Division, and
the Advanced Computing Lab at Los Alamos National Laboratory, the Santa Fe Institute,
and Thinking Machines Corporation. He began programming at age 12 on an Apple ][+
computer. His research involves mathematical and computational stochastic spatial models
in population ecology and epidemiology. He also dabbles in iOS (iPhone/iPad) development,
primarily for K–12 outreach.

xxi




1
Installing and Running R and MATLAB

1.1

Obtaining and installing

R
R can be downloaded for free from The R Project Web site at .
Follow the link to CRAN (Comprehensive R Archive Network) to download R. You must
first choose a CRAN mirror, i.e., one of the many Web sites around the world that keep
copies of the R software. It is probably best to choose one that is geographically close to
you.
Once you have chosen a mirror, you can choose which operating system (Linux, Mac
OS-X, or Windows) you use. Depending on your operating system, you may then have
further choices to make, such as which distribution of Linux or which version of OS-X you
use. Note that the source code for R is also freely available for download from the same site.
When you actually run R on Windows or Mac OS-X, you will see a graphical user interface like the one shown in Figure 1.1. The interface primarily consists of the R Console
or Command window, where you can interactively type commands for R to interpret. The
standard command prompt in R is “>,” which means R is waiting for you to type a command. You can try it out with some simple computations; for example, type√7*8 (and then
press Enter) to do a simple multiplication, or sqrt(exp(3)) to compute e3 . When you
run R on Linux in a terminal, you just get the command prompt, without the various menus
in the interface for OS-X and Windows.
You may wish to use a much more comprehensive, friendlier interface, especially if you
are coming to R from MATLAB. A very popular integrated development environment for
R, which has many of the same useful feature’s as MATLAB’s interface, is called RStudio;
it is available from . RStudio’s interface is shown in Figure 1.2.
If you are trying to ease your transition from MATLAB to R, you may seriously want to
consider installing and loading the pracma package, as it implements many functions which
behave nearly equivalently with MATLAB routines; see Section 13.9 for information about

installing and loading packages.

MATLAB
MATLAB is available from The MathWorks, Inc. (). Pricing and licensing information is available under their “Products & Services” link. There is a
very inexpensive option for students, and also special pricing for home and educational use.
It is also possible to obtain a trial version of the software. When you purchase MATLAB,
you will receive information for how to download and activate your copy.
MATLAB is available for all three popular operating systems (Linux, Mac OS-X, and
Windows).
When you run MATLAB, you will see a graphical user interface like the one shown in
Figure 1.3. The large region in the middle of the user interface is the Command Window,
1


×