<li><aname="toc-Introduction-and-preliminaries-1"href="#Introduction-and-preliminaries">1 Introduction and preliminaries</a>
<ulclass="no-bullet">
<li><aname="toc-The-R-environment-1"href="#The-R-environment">1.1 The R environment</a></li>
<li><aname="toc-Related-software-and-documentation-1"href="#Related-software-and-documentation">1.2 Related software and documentation</a></li>
<li><aname="toc-R-and-statistics-1"href="#R-and-statistics">1.3 R and statistics</a></li>
<li><aname="toc-R-and-the-window-system-1"href="#R-and-the-window-system">1.4 R and the window system</a></li>
<li><aname="toc-Using-R-interactively-1"href="#Using-R-interactively">1.5 Using R interactively</a></li>
<li><aname="toc-An-introductory-session"href="#An-introductory-session">1.6 An introductory session</a></li>
<li><aname="toc-Getting-help-with-functions-and-features"href="#Getting-help">1.7 Getting help with functions and features</a></li>
<li><aname="toc-R-commands_002c-case-sensitivity_002c-etc_002e"href="#R-commands_003b-case-sensitivity-etc">1.8 R commands, case sensitivity, etc.</a></li>
<li><aname="toc-Recall-and-correction-of-previous-commands-1"href="#Recall-and-correction-of-previous-commands">1.9 Recall and correction of previous commands</a></li>
<li><aname="toc-Executing-commands-from-or-diverting-output-to-a-file-1"href="#Executing-commands-from-or-diverting-output-to-a-file">1.10 Executing commands from or diverting output to a file</a></li>
<li><aname="toc-Data-permanency-and-removing-objects-1"href="#Data-permanency-and-removing-objects">1.11 Data permanency and removing objects</a></li>
</ul></li>
<li><aname="toc-Simple-manipulations_003b-numbers-and-vectors"href="#Simple-manipulations-numbers-and-vectors">2 Simple manipulations; numbers and vectors</a>
<ulclass="no-bullet">
<li><aname="toc-Vectors-and-assignment-1"href="#Vectors-and-assignment">2.1 Vectors and assignment</a></li>
<li><aname="toc-Character-vectors-1"href="#Character-vectors">2.6 Character vectors</a></li>
<li><aname="toc-Index-vectors_003b-selecting-and-modifying-subsets-of-a-data-set"href="#Index-vectors">2.7 Index vectors; selecting and modifying subsets of a data set</a></li>
<li><aname="toc-Other-types-of-objects-1"href="#Other-types-of-objects">2.8 Other types of objects</a></li>
</ul></li>
<li><aname="toc-Objects_002c-their-modes-and-attributes"href="#Objects">3 Objects, their modes and attributes</a>
<ulclass="no-bullet">
<li><aname="toc-Intrinsic-attributes_003a-mode-and-length"href="#The-intrinsic-attributes-mode-and-length">3.1 Intrinsic attributes: mode and length</a></li>
<li><aname="toc-Changing-the-length-of-an-object-1"href="#Changing-the-length-of-an-object">3.2 Changing the length of an object</a></li>
<li><aname="toc-Getting-and-setting-attributes-1"href="#Getting-and-setting-attributes">3.3 Getting and setting attributes</a></li>
<li><aname="toc-The-class-of-an-object-1"href="#The-class-of-an-object">3.4 The class of an object</a></li>
</ul></li>
<li><aname="toc-Ordered-and-unordered-factors"href="#Factors">4 Ordered and unordered factors</a>
<ulclass="no-bullet">
<li><aname="toc-A-specific-example"href="#A-specific-example">4.1 A specific example</a></li>
<li><aname="toc-The-function-tapply_0028_0029-and-ragged-arrays-1"href="#The-function-tapply_0028_0029-and-ragged-arrays">4.2 The function <code>tapply()</code> and ragged arrays</a></li>
<li><aname="toc-Array-indexing_002e-Subsections-of-an-array"href="#Array-indexing">5.2 Array indexing. Subsections of an array</a></li>
<li><aname="toc-Index-matrices-1"href="#Index-matrices">5.3 Index matrices</a></li>
<li><aname="toc-The-array_0028_0029-function-1"href="#The-array_0028_0029-function">5.4 The <code>array()</code> function</a>
<ulclass="no-bullet">
<li><aname="toc-Mixed-vector-and-array-arithmetic_002e-The-recycling-rule"href="#The-recycling-rule">5.4.1 Mixed vector and array arithmetic. The recycling rule</a></li>
</ul></li>
<li><aname="toc-The-outer-product-of-two-arrays-1"href="#The-outer-product-of-two-arrays">5.5 The outer product of two arrays</a></li>
<li><aname="toc-Generalized-transpose-of-an-array-1"href="#Generalized-transpose-of-an-array">5.6 Generalized transpose of an array</a></li>
<li><aname="toc-Linear-equations-and-inversion-1"href="#Linear-equations-and-inversion">5.7.2 Linear equations and inversion</a></li>
<li><aname="toc-Eigenvalues-and-eigenvectors-1"href="#Eigenvalues-and-eigenvectors">5.7.3 Eigenvalues and eigenvectors</a></li>
<li><aname="toc-Singular-value-decomposition-and-determinants-1"href="#Singular-value-decomposition-and-determinants">5.7.4 Singular value decomposition and determinants</a></li>
<li><aname="toc-Least-squares-fitting-and-the-QR-decomposition-1"href="#Least-squares-fitting-and-the-QR-decomposition">5.7.5 Least squares fitting and the QR decomposition</a></li>
</ul></li>
<li><aname="toc-Forming-partitioned-matrices_002c-cbind_0028_0029-and-rbind_0028_0029"href="#Forming-partitioned-matrices">5.8 Forming partitioned matrices, <code>cbind()</code> and <code>rbind()</code></a></li>
<li><aname="toc-The-concatenation-function_002c-c_0028_0029_002c-with-arrays"href="#The-concatenation-function-c_0028_0029-with-arrays">5.9 The concatenation function, <code>c()</code>, with arrays</a></li>
<li><aname="toc-Frequency-tables-from-factors-1"href="#Frequency-tables-from-factors">5.10 Frequency tables from factors</a></li>
</ul></li>
<li><aname="toc-Lists-and-data-frames-1"href="#Lists-and-data-frames">6 Lists and data frames</a>
<li><aname="toc-Data-frames-1"href="#Data-frames">6.3 Data frames</a>
<ulclass="no-bullet">
<li><aname="toc-Making-data-frames-1"href="#Making-data-frames">6.3.1 Making data frames</a></li>
<li><aname="toc-attach_0028_0029-and-detach_0028_0029-1"href="#attach_0028_0029-and-detach_0028_0029">6.3.2 <code>attach()</code> and <code>detach()</code></a></li>
<li><aname="toc-Working-with-data-frames-1"href="#Working-with-data-frames">6.3.3 Working with data frames</a></li>
<li><aname="toc-Managing-the-search-path-1"href="#Managing-the-search-path">6.3.5 Managing the search path</a></li>
</ul></li>
</ul></li>
<li><aname="toc-Reading-data-from-files-1"href="#Reading-data-from-files">7 Reading data from files</a>
<ulclass="no-bullet">
<li><aname="toc-The-read_002etable_0028_0029-function-1"href="#The-read_002etable_0028_0029-function">7.1 The <code>read.table()</code> function</a></li>
<li><aname="toc-The-scan_0028_0029-function-1"href="#The-scan_0028_0029-function">7.2 The <code>scan()</code> function</a></li>
<li><aname="toc-Probability-distributions-1"href="#Probability-distributions">8 Probability distributions</a>
<ulclass="no-bullet">
<li><aname="toc-R-as-a-set-of-statistical-tables-1"href="#R-as-a-set-of-statistical-tables">8.1 R as a set of statistical tables</a></li>
<li><aname="toc-Examining-the-distribution-of-a-set-of-data-1"href="#Examining-the-distribution-of-a-set-of-data">8.2 Examining the distribution of a set of data</a></li>
<li><aname="toc-One_002d-and-two_002dsample-tests-1"href="#One_002d-and-two_002dsample-tests">8.3 One- and two-sample tests</a></li>
</ul></li>
<li><aname="toc-Grouping_002c-loops-and-conditional-execution"href="#Loops-and-conditional-execution">9 Grouping, loops and conditional execution</a>
<li><aname="toc-Defining-new-binary-operators-1"href="#Defining-new-binary-operators">10.2 Defining new binary operators</a></li>
<li><aname="toc-Named-arguments-and-defaults-1"href="#Named-arguments-and-defaults">10.3 Named arguments and defaults</a></li>
<li><aname="toc-The-_2026-argument"href="#The-three-dots-argument">10.4 The ‘<samp>…</samp>’ argument</a></li>
<li><aname="toc-Assignments-within-functions"href="#Assignment-within-functions">10.5 Assignments within functions</a></li>
<li><aname="toc-More-advanced-examples-1"href="#More-advanced-examples">10.6 More advanced examples</a>
<ulclass="no-bullet">
<li><aname="toc-Efficiency-factors-in-block-designs-1"href="#Efficiency-factors-in-block-designs">10.6.1 Efficiency factors in block designs</a></li>
<li><aname="toc-Dropping-all-names-in-a-printed-array-1"href="#Dropping-all-names-in-a-printed-array">10.6.2 Dropping all names in a printed array</a></li>
<li><aname="toc-Linear-models-1"href="#Linear-models">11.2 Linear models</a></li>
<li><aname="toc-Generic-functions-for-extracting-model-information-1"href="#Generic-functions-for-extracting-model-information">11.3 Generic functions for extracting model information</a></li>
<li><aname="toc-Analysis-of-variance-and-model-comparison-1"href="#Analysis-of-variance-and-model-comparison">11.4 Analysis of variance and model comparison</a>
<li><aname="toc-The-glm_0028_0029-function-1"href="#The-glm_0028_0029-function">11.6.2 The <code>glm()</code> function</a></li>
</ul></li>
<li><aname="toc-Nonlinear-least-squares-and-maximum-likelihood-models-1"href="#Nonlinear-least-squares-and-maximum-likelihood-models">11.7 Nonlinear least squares and maximum likelihood models</a>
<ulclass="no-bullet">
<li><aname="toc-Least-squares-1"href="#Least-squares">11.7.1 Least squares</a></li>
<li><aname="toc-Maximum-likelihood-1"href="#Maximum-likelihood">11.7.2 Maximum likelihood</a></li>
</ul></li>
<li><aname="toc-Some-non_002dstandard-models-1"href="#Some-non_002dstandard-models">11.8 Some non-standard models</a></li>
<li><aname="toc-Function-and-variable-index-1"href="#Function-and-variable-index">Appendix D Function and variable index</a></li>
<li><aname="toc-Concept-index-1"href="#Concept-index">Appendix E Concept index</a></li>
<li><aname="toc-References-1"href="#References">Appendix F References</a></li>
</ul>
</div>
<aname="Top"></a>
<divclass="header">
<p>
Next: <ahref="#Preface"accesskey="n"rel="next">Preface</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="An-Introduction-to-R"></a>
<h1class="top">An Introduction to R</h1>
<p>This is an introduction to R (“GNU S”), a language and environment for
statistical computing and graphics. R is similar to the
award-winning<aname="DOCF1"href="#FOOT1"><sup>1</sup></a> S
system, which was developed at Bell Laboratories by John Chambers et al.
It provides a wide variety of statistical and graphical techniques
(linear and nonlinear modelling, statistical tests, time series
analysis, classification, clustering, ...).
</p>
<p>This manual provides information on data types, programming elements,
<tr><tdalign="left"valign="top">•<ahref="#Introduction-and-preliminaries"accesskey="2">Introduction and preliminaries</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Simple-manipulations-numbers-and-vectors"accesskey="3">Simple manipulations numbers and vectors</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Arrays-and-matrices"accesskey="6">Arrays and matrices</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Lists-and-data-frames"accesskey="7">Lists and data frames</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Reading-data-from-files"accesskey="8">Reading data from files</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Loops-and-conditional-execution">Loops and conditional execution</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Writing-your-own-functions">Writing your own functions</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Statistical-models-in-R">Statistical models in R</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Function-and-variable-index">Function and variable index</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Introduction-and-preliminaries"accesskey="1">Introduction and preliminaries</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="Introduction-and-preliminaries"></a>
<divclass="header">
<p>
Next: <ahref="#Simple-manipulations-numbers-and-vectors"accesskey="n"rel="next">Simple manipulations numbers and vectors</a>, Previous: <ahref="#Preface"accesskey="p"rel="prev">Preface</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Introduction-and-preliminaries-1"></a>
<h2class="chapter">1 Introduction and preliminaries</h2>
<tr><tdalign="left"valign="top">•<ahref="#The-R-environment"accesskey="1">The R environment</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Related-software-and-documentation"accesskey="2">Related software and documentation</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#R-and-statistics"accesskey="3">R and statistics</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#R-and-the-window-system"accesskey="4">R and the window system</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Using-R-interactively"accesskey="5">Using R interactively</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#R-commands_003b-case-sensitivity-etc"accesskey="7">R commands; case sensitivity etc</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Recall-and-correction-of-previous-commands"accesskey="8">Recall and correction of previous commands</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Executing-commands-from-or-diverting-output-to-a-file"accesskey="9">Executing commands from or diverting output to a file</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Data-permanency-and-removing-objects">Data permanency and removing objects</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="The-R-environment"></a>
<divclass="header">
<p>
Next: <ahref="#Related-software-and-documentation"accesskey="n"rel="next">Related software and documentation</a>, Previous: <ahref="#Introduction-and-preliminaries"accesskey="p"rel="prev">Introduction and preliminaries</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="The-R-environment-1"></a>
<h3class="section">1.1 The R environment</h3>
<p>R is an integrated suite of software facilities for data
manipulation, calculation and graphical display. Among other things it
has
</p>
<ul>
<li> an effective data handling and storage facility,
</li><li> a suite of operators for calculations on arrays, in particular matrices,
</li><li> a large, coherent, integrated collection of intermediate tools for data
analysis,
</li><li> graphical facilities for data analysis and display either directly at
the computer or on hardcopy, and
</li><li> a well developed, simple and effective programming language (called ‘S’)
which includes conditionals, loops, user defined recursive functions and
input and output facilities. (Indeed most of the system supplied
functions are themselves written in the S language.)
</li></ul>
<p>The term “environment” is intended to characterize it as a fully
planned and coherent system, rather than an incremental accretion of
very specific and inflexible tools, as is frequently the case with other
data analysis software.
</p>
<p>R is very much a vehicle for newly developing methods of interactive
data analysis. It has developed rapidly, and has been extended by a
large collection of <em>packages</em>. However, most programs written in
R are essentially ephemeral, written for a single piece of data
analysis.
</p>
<hr>
<aname="Related-software-and-documentation"></a>
<divclass="header">
<p>
Next: <ahref="#R-and-statistics"accesskey="n"rel="next">R and statistics</a>, Previous: <ahref="#The-R-environment"accesskey="p"rel="prev">The R environment</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<h3class="section">1.2 Related software and documentation</h3>
<p>R can be regarded as an implementation of the S language which
was developed at Bell Laboratories by Rick Becker, John Chambers and
Allan Wilks, and also forms the basis of the <small>S-PLUS</small> systems.
</p>
<p>The evolution of the S language is characterized by four books by
John Chambers and coauthors. For R, the basic reference is <em>The
New S Language: A Programming Environment for Data Analysis and
Graphics</em> by Richard A. Becker, John M. Chambers and Allan R.
Wilks. The new features of the 1991 release of S
are covered in <em>Statistical Models in S</em> edited by John M.
Chambers and Trevor J. Hastie. The formal methods and classes of the
<strong>methods</strong> package are based on those described in <em>Programming
with Data</em> by John M. Chambers. See <ahref="#References">References</a>, for precise
references.
</p>
<p>There are now a number of books which describe how to use R for data
analysis and statistics, and documentation for S/<small>S-PLUS</small> can
typically be used with R, keeping the differences between the S
implementations in mind. See <ahref="R-FAQ.html#What-documentation-exists-for-R_003f">What documentation exists for R?</a> in <cite>The R statistical system FAQ</cite>.
</p>
<hr>
<aname="R-and-statistics"></a>
<divclass="header">
<p>
Next: <ahref="#R-and-the-window-system"accesskey="n"rel="next">R and the window system</a>, Previous: <ahref="#Related-software-and-documentation"accesskey="p"rel="prev">Related software and documentation</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="R-and-statistics-1"></a>
<h3class="section">1.3 R and statistics</h3>
<aname="index-Packages"></a>
<p>Our introduction to the R environment did not mention
<em>statistics</em>, yet many people use R as a statistics system. We
prefer to think of it of an environment within which many classical and
modern statistical techniques have been implemented. A few of these are
built into the base R environment, but many are supplied as
<em>packages</em>. There are about 25 packages supplied with R (called
“standard” and “recommended” packages) and many more are available
through the <acronym>CRAN</acronym> family of Internet sites (via
<ahref="https://CRAN.R-project.org">https://CRAN.R-project.org</a>) and elsewhere. More details on
packages are given later (see <ahref="#Packages">Packages</a>).
</p>
<p>Most classical statistics and much of the latest methodology is
available for use with R, but users may need to be prepared to do a
little work to find it.
</p>
<p>There is an important difference in philosophy between S (and hence
R) and the other main statistical systems. In S a statistical
analysis is normally done as a series of steps, with intermediate
results being stored in objects. Thus whereas SAS and SPSS will give
copious output from a regression or discriminant analysis, R will
give minimal output and store the results in a fit object for subsequent
interrogation by further R functions.
</p>
<hr>
<aname="R-and-the-window-system"></a>
<divclass="header">
<p>
Next: <ahref="#Using-R-interactively"accesskey="n"rel="next">Using R interactively</a>, Previous: <ahref="#R-and-statistics"accesskey="p"rel="prev">R and statistics</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="R-and-the-window-system-1"></a>
<h3class="section">1.4 R and the window system</h3>
<p>The most convenient way to use R is at a graphics workstation running
a windowing system. This guide is aimed at users who have this
facility. In particular we will occasionally refer to the use of R
on an X window system although the vast bulk of what is said applies
generally to any implementation of the R environment.
</p>
<p>Most users will find it necessary to interact directly with the
operating system on their computer from time to time. In this guide, we
mainly discuss interaction with the operating system on UNIX machines.
If you are running R under Windows or OS X you will need to make
some small adjustments.
</p>
<p>Setting up a workstation to take full advantage of the customizable
features of R is a straightforward if somewhat tedious procedure, and
will not be considered further here. Users in difficulty should seek
local expert help.
</p>
<hr>
<aname="Using-R-interactively"></a>
<divclass="header">
<p>
Next: <ahref="#Getting-help"accesskey="n"rel="next">Getting help</a>, Previous: <ahref="#R-and-the-window-system"accesskey="p"rel="prev">R and the window system</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Using-R-interactively-1"></a>
<h3class="section">1.5 Using R interactively</h3>
<p>When you use the R program it issues a prompt when it expects input
commands. The default prompt is ‘<samp><code>></code></samp>’, which on UNIX might be
the same as the shell prompt, and so it may appear that nothing is
happening. However, as we shall see, it is easy to change to a
different R prompt if you wish. We will assume that the UNIX shell
prompt is ‘<samp><code>$</code></samp>’.
</p>
<p>In using R under UNIX the suggested procedure for the first occasion
is as follows:
</p>
<ol>
<li> Create a separate sub-directory, say <samp>work</samp>, to hold data files on
which you will use R for this problem. This will be the working
directory whenever you use R for this particular problem.
<divclass="example">
<preclass="example">$ mkdir work
$ cd work
</pre></div>
</li><li> Start the R program with the command
<divclass="example">
<preclass="example">$ R
</pre></div>
</li><li> At this point R commands may be issued (see later).
</li><li> To quit the R program the command is
<divclass="example">
<preclass="example">> q()
</pre></div>
<p>At this point you will be asked whether you want to save the data from
your R session. On some systems this will bring up a dialog box, and
on others you will receive a text prompt to which you can respond
<kbd>yes</kbd>, <kbd>no</kbd> or <kbd>cancel</kbd> (a single letter abbreviation will
do) to save the data before quitting, quit without saving, or return to
the R session. Data which is saved will be available in future R
sessions.
</p>
</li></ol>
<p>Further R sessions are simple.
</p>
<ol>
<li> Make <samp>work</samp> the working directory and start the program as before:
<divclass="example">
<preclass="example">$ cd work
$ R
</pre></div>
</li><li> Use the R program, terminating with the <code>q()</code> command at the end
of the session.
</li></ol>
<p>To use R under Windows the procedure to
follow is basically the same. Create a folder as the working directory,
and set that in the <samp>Start In</samp> field in your R shortcut.
Then launch R by double clicking on the icon.
</p>
<aname="An-introductory-session"></a>
<h3class="section">1.6 An introductory session</h3>
<p>Readers wishing to get a feel for R at a computer before proceeding
are strongly advised to work through the introductory session
given in <ahref="#A-sample-session">A sample session</a>.
</p>
<hr>
<aname="Getting-help"></a>
<divclass="header">
<p>
Next: <ahref="#R-commands_003b-case-sensitivity-etc"accesskey="n"rel="next">R commands; case sensitivity etc</a>, Previous: <ahref="#Using-R-interactively"accesskey="p"rel="prev">Using R interactively</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Next: <ahref="#Recall-and-correction-of-previous-commands"accesskey="n"rel="next">Recall and correction of previous commands</a>, Previous: <ahref="#Getting-help"accesskey="p"rel="prev">Getting help</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Next: <ahref="#Executing-commands-from-or-diverting-output-to-a-file"accesskey="n"rel="next">Executing commands from or diverting output to a file</a>, Previous: <ahref="#R-commands_003b-case-sensitivity-etc"accesskey="p"rel="prev">R commands; case sensitivity etc</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Next: <ahref="#Data-permanency-and-removing-objects"accesskey="n"rel="next">Data permanency and removing objects</a>, Previous: <ahref="#Recall-and-correction-of-previous-commands"accesskey="p"rel="prev">Recall and correction of previous commands</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Previous: <ahref="#Executing-commands-from-or-diverting-output-to-a-file"accesskey="p"rel="prev">Executing commands from or diverting output to a file</a>, Up: <ahref="#Introduction-and-preliminaries"accesskey="u"rel="up">Introduction and preliminaries</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<tr><tdalign="left"valign="top">•<ahref="#Vectors-and-assignment"accesskey="1">Vectors and assignment</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Other-types-of-objects"accesskey="8">Other types of objects</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="Vectors-and-assignment"></a>
<divclass="header">
<p>
Next: <ahref="#Vector-arithmetic"accesskey="n"rel="next">Vector arithmetic</a>, Previous: <ahref="#Simple-manipulations-numbers-and-vectors"accesskey="p"rel="prev">Simple manipulations numbers and vectors</a>, Up: <ahref="#Simple-manipulations-numbers-and-vectors"accesskey="u"rel="up">Simple manipulations numbers and vectors</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Vectors-and-assignment-1"></a>
<h3class="section">2.1 Vectors and assignment</h3>
<p>R operates on named <em>data structures</em>. The simplest such
structure is the numeric <em>vector</em>, which is a single entity
consisting of an ordered collection of numbers. To set up a vector
named <code>x</code>, say, consisting of five numbers, namely 10.4, 5.6, 3.1,
6.4 and 21.7, use the R command
</p>
<divclass="example">
<preclass="example">> x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
</pre></div>
<aname="index-c"></a>
<aname="index-vector"></a>
<p>This is an <em>assignment</em> statement using the <em>function</em>
<code>c()</code> which in this context can take an arbitrary number of vector
<em>arguments</em> and whose value is a vector got by concatenating its
arguments end to end.<aname="DOCF7"href="#FOOT7"><sup>7</sup></a>
</p>
<p>A number occurring by itself in an expression is taken as a vector of
length one.
</p>
<p>Notice that the assignment operator (‘<samp><code><-</code></samp>’), which consists
of the two characters ‘<samp><code><</code></samp>’ (“less than”) and
‘<samp><code>-</code></samp>’ (“minus”) occurring strictly side-by-side and it
‘points’ to the object receiving the value of the expression.
In most contexts the ‘<samp><code>=</code></samp>’ operator can be used as an alternative.
<aname="index-Assignment"></a>
</p>
<p>Assignment can also be made using the function <code>assign()</code>. An
equivalent way of making the same assignment as above is with:
<tr><tdalign="left"valign="top">•<ahref="#The-intrinsic-attributes-mode-and-length"accesskey="1">The intrinsic attributes mode and length</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Changing-the-length-of-an-object"accesskey="2">Changing the length of an object</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Getting-and-setting-attributes"accesskey="3">Getting and setting attributes</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#The-class-of-an-object"accesskey="4">The class of an object</a>:</td><td> </td><tdalign="left"valign="top">
Next: <ahref="#Changing-the-length-of-an-object"accesskey="n"rel="next">Changing the length of an object</a>, Previous: <ahref="#Objects"accesskey="p"rel="prev">Objects</a>, Up: <ahref="#Objects"accesskey="u"rel="up">Objects</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>makes it an object of length 5 consisting of just the former components
with even index. (The old indices are not retained, of course.) We can
then retain just the first three values by
</p>
<divclass="example">
<preclass="example">> length(alpha) <- 3
</pre></div>
<p>and vectors can be extended (by missing values) in the same way.
</p>
<hr>
<aname="Getting-and-setting-attributes"></a>
<divclass="header">
<p>
Next: <ahref="#The-class-of-an-object"accesskey="n"rel="next">The class of an object</a>, Previous: <ahref="#Changing-the-length-of-an-object"accesskey="p"rel="prev">Changing the length of an object</a>, Up: <ahref="#Objects"accesskey="u"rel="up">Objects</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Getting-and-setting-attributes-1"></a>
<h3class="section">3.3 Getting and setting attributes</h3>
<aname="index-attr"></a>
<aname="index-attributes"></a>
<p>The function <code>attributes(<var>object</var>)</code>
<aname="index-attributes-1"></a>
returns a list of all the non-intrinsic attributes currently defined for
that object. The function <code>attr(<var>object</var>, <var>name</var>)</code>
<aname="index-attr-1"></a>
can be used to select a specific attribute. These functions are rarely
used, except in rather special circumstances when some new attribute is
being created for some particular purpose, for example to associate a
creation date or an operator with an R object. The concept, however,
is very important.
</p>
<p>Some care should be exercised when assigning or deleting attributes
since they are an integral part of the object system used in R.
</p>
<p>When it is used on the left hand side of an assignment it can be used
either to associate a new attribute with <code><var>object</var></code> or to
<p>allows R to treat <code>z</code> as if it were a 10-by-10 matrix.
</p>
<hr>
<aname="The-class-of-an-object"></a>
<divclass="header">
<p>
Previous: <ahref="#Getting-and-setting-attributes"accesskey="p"rel="prev">Getting and setting attributes</a>, Up: <ahref="#Objects"accesskey="u"rel="up">Objects</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="The-class-of-an-object-1"></a>
<h3class="section">3.4 The class of an object</h3>
<aname="index-Classes"></a>
<p>All objects in R have a <em>class</em>, reported by the function
<code>class</code>. For simple vectors this is just the mode, for example
<code>"numeric"</code>, <code>"logical"</code>, <code>"character"</code> or <code>"list"</code>,
but <code>"matrix"</code>, <code>"array"</code>, <code>"factor"</code> and
<code>"data.frame"</code> are other possible values.
</p>
<p>A special attribute known as the <em>class</em> of the object is used to
allow for an object-oriented style<aname="DOCF13"href="#FOOT13"><sup>13</sup></a> of
programming in R. For example if an object has class
<code>"data.frame"</code>, it will be printed in a certain way, the
<code>plot()</code> function will display it graphically in a certain way, and
other so-called generic functions such as <code>summary()</code> will react to
it as an argument in a way sensitive to its class.
</p>
<p>To remove temporarily the effects of class, use the function
<code>unclass()</code>.
<aname="index-unclass"></a>
For example if <code>winter</code> has the class <code>"data.frame"</code> then
</p>
<divclass="example">
<preclass="example">> winter
</pre></div>
<p>will print it in data frame form, which is rather like a matrix, whereas
</p>
<divclass="example">
<preclass="example">> unclass(winter)
</pre></div>
<p>will print it as an ordinary list. Only in rather special situations do
you need to use this facility, but one is when you are learning to come
to terms with the idea of class and generic functions.
</p>
<p>Generic functions and classes will be discussed further in <ahref="#Object-orientation">Object orientation</a>, but only briefly.
</p>
<hr>
<aname="Factors"></a>
<divclass="header">
<p>
Next: <ahref="#Arrays-and-matrices"accesskey="n"rel="next">Arrays and matrices</a>, Previous: <ahref="#Objects"accesskey="p"rel="prev">Objects</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Ordered-and-unordered-factors"></a>
<h2class="chapter">4 Ordered and unordered factors</h2>
<aname="index-Factors"></a>
<aname="index-Ordered-factors"></a>
<p>A <em>factor</em> is a vector object used to specify a discrete
classification (grouping) of the components of other vectors of the same length.
R provides both <em>ordered</em> and <em>unordered</em> factors.
While the “real” application of factors is with model formulae
(see <ahref="#Contrasts">Contrasts</a>), we here look at a specific example.
</p>
<aname="A-specific-example"></a>
<h3class="section">4.1 A specific example</h3>
<p>Suppose, for example, we have a sample of 30 tax accountants from all
the states and territories of Australia<aname="DOCF14"href="#FOOT14"><sup>14</sup></a>
and their individual state of origin is specified by a character vector
<tr><tdalign="left"valign="top">•<ahref="#The-function-tapply_0028_0029-and-ragged-arrays"accesskey="1">The function tapply() and ragged arrays</a>:</td><td> </td><tdalign="left"valign="top">
<p>(Writing functions will be considered later in <ahref="#Writing-your-own-functions">Writing your own functions</a>, and in this case was unnecessary as R also has a builtin
function <code>sd()</code>.)
<aname="index-sd"></a>
<aname="index-var-1"></a>
After this assignment, the standard errors are calculated by
<p>As an exercise you may care to find the usual 95% confidence limits for
the state mean incomes. To do this you could use <code>tapply()</code> once
more with the <code>length()</code> function to find the sample sizes, and the
<code>qt()</code> function to find the percentage points of the appropriate
<em>t</em>-distributions. (You could also investigate R’s facilities
for <em>t</em>-tests.)
</p>
<p>The function <code>tapply()</code> can also be used to handle more complicated
indexing of a vector by multiple categories. For example, we might wish
to split the tax accountants by both state and sex. However in this
simple instance (just one factor) what happens can be thought of as
follows. The values in the vector are collected into groups
corresponding to the distinct entries in the factor. The function is
then applied to each of these groups individually. The value is a
vector of function results, labelled by the <code>levels</code> attribute of
the factor.
</p>
<p>The combination of a vector and a labelling factor is an example of what
is sometimes called a <em>ragged array</em>, since the subclass sizes are
possibly irregular. When the subclass sizes are all the same the
indexing may be done implicitly and much more efficiently, as we see in
the next section.
</p>
<hr>
<aname="Ordered-factors"></a>
<divclass="header">
<p>
Previous: <ahref="#The-function-tapply_0028_0029-and-ragged-arrays"accesskey="p"rel="prev">The function tapply() and ragged arrays</a>, Up: <ahref="#Factors"accesskey="u"rel="up">Factors</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Ordered-factors-1"></a>
<h3class="section">4.3 Ordered factors</h3>
<aname="index-ordered"></a>
<p>The levels of factors are stored in alphabetical order, or in the order
they were specified to <code>factor</code> if they were specified explicitly.
</p>
<p>Sometimes the levels will have a natural ordering that we want to record
and want our statistical analysis to make use of. The <code>ordered()</code>
<aname="index-ordered-1"></a>
function creates such ordered factors but is otherwise identical to
<code>factor</code>. For most purposes the only difference between ordered
and unordered factors is that the former are printed showing the
ordering of the levels, but the contrasts generated for them in fitting
linear models are different.
</p>
<hr>
<aname="Arrays-and-matrices"></a>
<divclass="header">
<p>
Next: <ahref="#Lists-and-data-frames"accesskey="n"rel="next">Lists and data frames</a>, Previous: <ahref="#Factors"accesskey="p"rel="prev">Factors</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<tr><tdalign="left"valign="top">•<ahref="#The-outer-product-of-two-arrays"accesskey="5">The outer product of two arrays</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Generalized-transpose-of-an-array"accesskey="6">Generalized transpose of an array</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#The-concatenation-function-c_0028_0029-with-arrays"accesskey="9">The concatenation function c() with arrays</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Frequency-tables-from-factors">Frequency tables from factors</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="Arrays"></a>
<divclass="header">
<p>
Next: <ahref="#Array-indexing"accesskey="n"rel="next">Array indexing</a>, Previous: <ahref="#Arrays-and-matrices"accesskey="p"rel="prev">Arrays and matrices</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Arrays-1"></a>
<h3class="section">5.1 Arrays</h3>
<aname="index-Arrays"></a>
<aname="index-Matrices"></a>
<p>An array can be considered as a multiply subscripted collection of data
entries, for example numeric. R allows simple facilities for
creating and handling arrays, and in particular the special case of
matrices.
</p>
<p>A dimension vector is a vector of non-negative integers. If its length is
<em>k</em> then the array is <em>k</em>-dimensional, e.g. a matrix is a
<em>2</em>-dimensional array. The dimensions are indexed from one up to
the values given in the dimension vector.
</p>
<p>A vector can be used by R as an array only if it has a dimension
vector as its <em>dim</em> attribute. Suppose, for example, <code>z</code> is a
vector of 1500 elements. The assignment
</p>
<divclass="example">
<preclass="example">> dim(z) <- c(3,5,100)
</pre></div>
<aname="index-dim"></a>
<p>gives it the <em>dim</em> attribute that allows it to be treated as a
<em>3</em> by <em>5</em> by <em>100</em> array.
</p>
<p>Other functions such as <code>matrix()</code> and <code>array()</code> are available
for simpler and more natural looking assignments, as we shall see in
Next: <ahref="#The-array_0028_0029-function"accesskey="n"rel="next">The array() function</a>, Previous: <ahref="#Array-indexing"accesskey="p"rel="prev">Array indexing</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Index-matrices-1"></a>
<h3class="section">5.3 Index matrices</h3>
<p>As well as an index vector in any subscript position, a matrix may be
used with a single <em>index matrix</em> in order either to assign a vector
of quantities to an irregular collection of elements in the array, or to
extract an irregular collection as a vector.
</p>
<p>A matrix example makes the process clear. In the case of a doubly
indexed array, an index matrix may be given consisting of two columns
and as many rows as desired. The entries in the index matrix are the
row and column indices for the doubly indexed array. Suppose for
example we have a <em>4</em> by <em>5</em> array <code>X</code> and we wish to do
the following:
</p>
<ul>
<li> Extract elements <code>X[1,3]</code>, <code>X[2,2]</code> and <code>X[3,1]</code> as a
vector structure, and
</li><li> Replace these entries in the array <code>X</code> by zeroes.
</li></ul>
<p>In this case we need a <em>3</em> by <em>2</em> subscript array, as in the
following example.
</p>
<divclass="example">
<preclass="example">> x <- array(1:20, dim=c(4,5)) # <spanclass="roman">Generate a 4 by 5 array.</span>
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> i <- array(c(1:3,3:1), dim=c(3,2))
> i # <spanclass="roman"><code>i</code> is a 3 by 2 index array.</span>
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 1
> x[i] # <spanclass="roman">Extract those elements</span>
[1] 9 6 3
> x[i] <- 0 # <spanclass="roman">Replace those elements by zeros.</span>
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 0 13 17
[2,] 2 0 10 14 18
[3,] 0 7 11 15 19
[4,] 4 8 12 16 20
>
</pre></div>
<p>Negative indices are not allowed in index matrices. <code>NA</code> and zero
values are allowed: rows in the index matrix containing a zero are
ignored, and rows containing an <code>NA</code> produce an <code>NA</code> in the
result.
</p>
<p>As a less trivial example, suppose we wish to generate an (unreduced)
design matrix for a block design defined by factors <code>blocks</code>
(<code>b</code> levels) and <code>varieties</code> (<code>v</code> levels). Further
suppose there are <code>n</code> plots in the experiment. We could proceed as
follows:
</p>
<divclass="example">
<preclass="example">> Xb <- matrix(0, n, b)
> Xv <- matrix(0, n, v)
> ib <- cbind(1:n, blocks)
> iv <- cbind(1:n, varieties)
> Xb[ib] <- 1
> Xv[iv] <- 1
> X <- cbind(Xb, Xv)
</pre></div>
<p>To construct the incidence matrix, <code>N</code> say, we could use
</p>
<divclass="example">
<preclass="example">> N <- crossprod(Xb, Xv)
</pre></div>
<aname="index-crossprod"></a>
<p>However a simpler direct way of producing this matrix is to use
<code>table()</code>:
<aname="index-table"></a>
</p>
<divclass="example">
<preclass="example">> N <- table(blocks, varieties)
</pre></div>
<p>Index matrices must be numerical: any other form of matrix (e.g. a
logical or character matrix) supplied as a matrix is treated as an
indexing vector.
</p>
<hr>
<aname="The-array_0028_0029-function"></a>
<divclass="header">
<p>
Next: <ahref="#The-outer-product-of-two-arrays"accesskey="n"rel="next">The outer product of two arrays</a>, Previous: <ahref="#Index-matrices"accesskey="p"rel="prev">Index matrices</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="The-array_0028_0029-function-1"></a>
<h3class="section">5.4 The <code>array()</code> function</h3>
<aname="index-array"></a>
<p>As well as giving a vector structure a <code>dim</code> attribute, arrays can
be constructed from vectors by the <code>array</code> function, which has the
form
</p>
<divclass="example">
<preclass="example">> Z <- array(<var>data_vector</var>, <var>dim_vector</var>)
</pre></div>
<p>For example, if the vector <code>h</code> contains 24 or fewer, numbers then
the command
</p>
<divclass="example">
<preclass="example">> Z <- array(h, dim=c(3,4,2))
</pre></div>
<p>would use <code>h</code> to set up <em>3</em> by <em>4</em> by <em>2</em> array in
<code>Z</code>. If the size of <code>h</code> is exactly 24 the result is the same as
</p>
<divclass="example">
<preclass="example">> Z <- h ; dim(Z) <- c(3,4,2)
</pre></div>
<p>However if <code>h</code> is shorter than 24, its values are recycled from the
beginning again to make it up to size 24 (see <ahref="#The-recycling-rule">The recycling rule</a>)
but <code>dim(h) <- c(3,4,2)</code> would signal an error about mismatching
length.
As an extreme but common example
</p>
<divclass="example">
<preclass="example">> Z <- array(0, c(3,4,2))
</pre></div>
<p>makes <code>Z</code> an array of all zeros.
</p>
<p>At this point <code>dim(Z)</code> stands for the dimension vector
<code>c(3,4,2)</code>, and <code>Z[1:24]</code> stands for the data vector as it was
in <code>h</code>, and <code>Z[]</code> with an empty subscript or <code>Z</code> with no
subscript stands for the entire array as an array.
</p>
<p>Arrays may be used in arithmetic expressions and the result is an array
formed by element-by-element operations on the data vector. The
<code>dim</code> attributes of operands generally need to be the same, and
this becomes the dimension vector of the result. So if <code>A</code>,
<code>B</code> and <code>C</code> are all similar arrays, then
</p>
<divclass="example">
<preclass="example">> D <- 2*A*B + C + 1
</pre></div>
<p>makes <code>D</code> a similar array with its data vector being the result of
the given element-by-element operations. However the precise rule
concerning mixed array and vector calculations has to be considered a
<h4class="subsection">5.4.1 Mixed vector and array arithmetic. The recycling rule</h4>
<aname="index-Recycling-rule-1"></a>
<p>The precise rule affecting element by element mixed calculations with
vectors and arrays is somewhat quirky and hard to find in the
references. From experience we have found the following to be a reliable
guide.
</p>
<ul>
<li> The expression is scanned from left to right.
</li><li> Any short vector operands are extended by recycling their values until
they match the size of any other operands.
</li><li> As long as short vectors and arrays <em>only</em> are encountered, the
arrays must all have the same <code>dim</code> attribute or an error results.
</li><li> Any vector operand longer than a matrix or array operand generates an error.
</li><li> If array structures are present and no error or coercion to vector has
been precipitated, the result is an array structure with the common
<code>dim</code> attribute of its array operands.
</li></ul>
<hr>
<aname="The-outer-product-of-two-arrays"></a>
<divclass="header">
<p>
Next: <ahref="#Generalized-transpose-of-an-array"accesskey="n"rel="next">Generalized transpose of an array</a>, Previous: <ahref="#The-array_0028_0029-function"accesskey="p"rel="prev">The array() function</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="The-outer-product-of-two-arrays-1"></a>
<h3class="section">5.5 The outer product of two arrays</h3>
<aname="index-Outer-products-of-arrays"></a>
<p>An important operation on arrays is the <em>outer product</em>. If
<code>a</code> and <code>b</code> are two numeric arrays, their outer product is an
array whose dimension vector is obtained by concatenating their two
dimension vectors (order is important), and whose data vector is got by
forming all possible products of elements of the data vector of <code>a</code>
with those of <code>b</code>. The outer product is formed by the special
operator <code>%o%</code>:
<aname="index-_0025o_0025"></a>
</p>
<divclass="example">
<preclass="example">> ab <- a %o% b
</pre></div>
<p>An alternative is
</p>
<divclass="example">
<preclass="example">> ab <- outer(a, b, "*")
</pre></div>
<aname="index-outer"></a>
<p>The multiplication function can be replaced by an arbitrary function of
two variables. For example if we wished to evaluate the function
f(x; y) = cos(y)/(1 + x^2)
over a regular grid of values with <em>x</em>- and <em>y</em>-coordinates
defined by the R vectors <code>x</code> and <code>y</code> respectively, we could
proceed as follows:
</p>
<divclass="example">
<preclass="example">> f <- function(x, y) cos(y)/(1 + x^2)
> z <- outer(x, y, f)
</pre></div>
<p>In particular the outer product of two ordinary vectors is a doubly
subscripted array (that is a matrix, of rank at most 1). Notice that
the outer product operator is of course non-commutative. Defining your
own R functions will be considered further in <ahref="#Writing-your-own-functions">Writing your own functions</a>.
<p>Notice the coercion of the <code>names</code> attribute of the frequency table
to numeric in order to recover the range of the determinant values. The
“obvious” way of doing this problem with <code>for</code> loops, to be
discussed in <ahref="#Loops-and-conditional-execution">Loops and conditional execution</a>, is so inefficient as
to be impractical.
</p>
<p>It is also perhaps surprising that about 1 in 20 such matrices is
singular.
</p>
<hr>
<aname="Generalized-transpose-of-an-array"></a>
<divclass="header">
<p>
Next: <ahref="#Matrix-facilities"accesskey="n"rel="next">Matrix facilities</a>, Previous: <ahref="#The-outer-product-of-two-arrays"accesskey="p"rel="prev">The outer product of two arrays</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Generalized-transpose-of-an-array-1"></a>
<h3class="section">5.6 Generalized transpose of an array</h3>
may be used to permute an array, <code>a</code>. The argument <code>perm</code>
must be a permutation of the integers <em>{1, …, k}</em>, where
<em>k</em> is the number of subscripts in <code>a</code>. The result of the
function is an array of the same size as <code>a</code> but with old dimension
given by <code>perm[j]</code> becoming the new <code>j</code>-th dimension. The
easiest way to think of this operation is as a generalization of
transposition for matrices. Indeed if <code>A</code> is a matrix, (that is, a
doubly subscripted array) then <code>B</code> given by
</p>
<divclass="example">
<preclass="example">> B <- aperm(A, c(2,1))
</pre></div>
<p>is just the transpose of <code>A</code>. For this special case a simpler
function <code>t()</code>
<aname="index-t"></a>
is available, so we could have used <code>B <- t(A)</code>.
</p>
<hr>
<aname="Matrix-facilities"></a>
<divclass="header">
<p>
Next: <ahref="#Forming-partitioned-matrices"accesskey="n"rel="next">Forming partitioned matrices</a>, Previous: <ahref="#Generalized-transpose-of-an-array"accesskey="p"rel="prev">Generalized transpose of an array</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Matrix-facilities-1"></a>
<h3class="section">5.7 Matrix facilities</h3>
<p>As noted above, a matrix is just an array with two subscripts. However
it is such an important special case it needs a separate discussion.
R contains many operators and functions that are available only for
matrices. For example <code>t(X)</code> is the matrix transpose function, as
noted above. The functions <code>nrow(A)</code> and <code>ncol(A)</code> give the
number of rows and columns in the matrix <code>A</code> respectively.
<tr><tdalign="left"valign="top">•<ahref="#Linear-equations-and-inversion"accesskey="2">Linear equations and inversion</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Eigenvalues-and-eigenvectors"accesskey="3">Eigenvalues and eigenvectors</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Singular-value-decomposition-and-determinants"accesskey="4">Singular value decomposition and determinants</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Least-squares-fitting-and-the-QR-decomposition"accesskey="5">Least squares fitting and the QR decomposition</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="Multiplication"></a>
<divclass="header">
<p>
Next: <ahref="#Linear-equations-and-inversion"accesskey="n"rel="next">Linear equations and inversion</a>, Previous: <ahref="#Matrix-facilities"accesskey="p"rel="prev">Matrix facilities</a>, Up: <ahref="#Matrix-facilities"accesskey="u"rel="up">Matrix facilities</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>The operator <code>%*%</code> is used for matrix multiplication.
<aname="index-_0025_002a_0025"></a>
An <em>n</em> by <em>1</em> or <em>1</em> by <em>n</em> matrix may of course be
used as an <em>n</em>-vector if in the context such is appropriate.
Conversely, vectors which occur in matrix multiplication expressions are
automatically promoted either to row or column vectors, whichever is
multiplicatively coherent, if possible, (although this is not always
unambiguously possible, as we see later).
</p>
<p>If, for example, <code>A</code> and <code>B</code> are square matrices of the same
size, then
</p>
<divclass="example">
<preclass="example">> A * B
</pre></div>
<p>is the matrix of element by element products and
</p>
<divclass="example">
<preclass="example">> A %*% B
</pre></div>
<p>is the matrix product. If <code>x</code> is a vector, then
</p>
<divclass="example">
<preclass="example">> x %*% A %*% x
</pre></div>
<p>is a quadratic form.<aname="DOCF16"href="#FOOT16"><sup>16</sup></a>
</p>
<aname="index-crossprod-1"></a>
<p>The function <code>crossprod()</code> forms “crossproducts”, meaning that
<code>crossprod(X, y)</code> is the same as <code>t(X) %*% y</code> but the
operation is more efficient. If the second argument to
<code>crossprod()</code> is omitted it is taken to be the same as the first.
</p>
<aname="index-diag"></a>
<p>The meaning of <code>diag()</code> depends on its argument. <code>diag(v)</code>,
where <code>v</code> is a vector, gives a diagonal matrix with elements of the
vector as the diagonal entries. On the other hand <code>diag(M)</code>, where
<code>M</code> is a matrix, gives the vector of main diagonal entries of
<code>M</code>. This is the same convention as that used for <code>diag()</code> in
<small>MATLAB</small>. Also, somewhat confusingly, if <code>k</code> is a single
numeric value then <code>diag(k)</code> is the <code>k</code> by <code>k</code> identity
matrix!
</p>
<hr>
<aname="Linear-equations-and-inversion"></a>
<divclass="header">
<p>
Next: <ahref="#Eigenvalues-and-eigenvectors"accesskey="n"rel="next">Eigenvalues and eigenvectors</a>, Previous: <ahref="#Multiplication"accesskey="p"rel="prev">Multiplication</a>, Up: <ahref="#Matrix-facilities"accesskey="u"rel="up">Matrix facilities</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Linear-equations-and-inversion-1"></a>
<h4class="subsection">5.7.2 Linear equations and inversion</h4>
<aname="index-Linear-equations"></a>
<aname="index-solve"></a>
<p>Solving linear equations is the inverse of matrix multiplication.
When after
</p>
<divclass="example">
<preclass="example">> b <- A %*% x
</pre></div>
<p>only <code>A</code> and <code>b</code> are given, the vector <code>x</code> is the
solution of that linear equation system. In R,
</p>
<divclass="example">
<preclass="example">> solve(A,b)
</pre></div>
<p>solves the system, returning <code>x</code> (up to some accuracy loss).
Note that in linear algebra, formally
<code>x = A^{-1} %*% b</code>
where
<code>A^{-1}</code> denotes the <em>inverse</em> of
<code>A</code>, which can be computed by
</p>
<divclass="example">
<preclass="example">solve(A)
</pre></div>
<p>but rarely is needed. Numerically, it is both inefficient and
potentially unstable to compute <code>x <- solve(A) %*% b</code> instead of
<code>solve(A,b)</code>.
</p>
<p>The quadratic form <code>x %*% A^{-1} %*%
x</code> which is used in multivariate computations, should be computed by
something like<aname="DOCF17"href="#FOOT17"><sup>17</sup></a><code>x %*% solve(A,x)</code>, rather
than computing the inverse of <code>A</code>.
</p>
<hr>
<aname="Eigenvalues-and-eigenvectors"></a>
<divclass="header">
<p>
Next: <ahref="#Singular-value-decomposition-and-determinants"accesskey="n"rel="next">Singular value decomposition and determinants</a>, Previous: <ahref="#Linear-equations-and-inversion"accesskey="p"rel="prev">Linear equations and inversion</a>, Up: <ahref="#Matrix-facilities"accesskey="u"rel="up">Matrix facilities</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Eigenvalues-and-eigenvectors-1"></a>
<h4class="subsection">5.7.3 Eigenvalues and eigenvectors</h4>
<aname="index-Eigenvalues-and-eigenvectors"></a>
<aname="index-eigen"></a>
<p>The function <code>eigen(Sm)</code> calculates the eigenvalues and
eigenvectors of a symmetric matrix <code>Sm</code>. The result of this
function is a list of two components named <code>values</code> and
<code>vectors</code>. The assignment
</p>
<divclass="example">
<preclass="example">> ev <- eigen(Sm)
</pre></div>
<p>will assign this list to <code>ev</code>. Then <code>ev$val</code> is the vector of
eigenvalues of <code>Sm</code> and <code>ev$vec</code> is the matrix of
corresponding eigenvectors. Had we only needed the eigenvalues we could
Next: <ahref="#Least-squares-fitting-and-the-QR-decomposition"accesskey="n"rel="next">Least squares fitting and the QR decomposition</a>, Previous: <ahref="#Eigenvalues-and-eigenvectors"accesskey="p"rel="prev">Eigenvalues and eigenvectors</a>, Up: <ahref="#Matrix-facilities"accesskey="u"rel="up">Matrix facilities</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Previous: <ahref="#Singular-value-decomposition-and-determinants"accesskey="p"rel="prev">Singular value decomposition and determinants</a>, Up: <ahref="#Matrix-facilities"accesskey="u"rel="up">Matrix facilities</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<h4class="subsection">5.7.5 Least squares fitting and the QR decomposition</h4>
<aname="index-Least-squares-fitting"></a>
<aname="index-QR-decomposition"></a>
<p>The function <code>lsfit()</code> returns a list giving results of a least
squares fitting procedure. An assignment such as
</p>
<divclass="example">
<preclass="example">> ans <- lsfit(X, y)
</pre></div>
<aname="index-lsfit"></a>
<p>gives the results of a least squares fit where <code>y</code> is the vector of
observations and <code>X</code> is the design matrix. See the help facility
for more details, and also for the follow-up function <code>ls.diag()</code>
for, among other things, regression diagnostics. Note that a grand mean
term is automatically included and need not be included explicitly as a
column of <code>X</code>. Further note that you almost always will prefer
using <code>lm(.)</code> (see <ahref="#Linear-models">Linear models</a>) to <code>lsfit()</code> for
regression modelling.
</p>
<aname="index-qr"></a>
<p>Another closely related function is <code>qr()</code> and its allies.
Consider the following assignments
</p>
<divclass="example">
<preclass="example">> Xplus <- qr(X)
> b <- qr.coef(Xplus, y)
> fit <- qr.fitted(Xplus, y)
> res <- qr.resid(Xplus, y)
</pre></div>
<p>These compute the orthogonal projection of <code>y</code> onto the range of
<code>X</code> in <code>fit</code>, the projection onto the orthogonal complement in
<code>res</code> and the coefficient vector for the projection in <code>b</code>,
that is, <code>b</code> is essentially the result of the <small>MATLAB</small>
‘backslash’ operator.
</p>
<p>It is not assumed that <code>X</code> has full column rank. Redundancies will
be discovered and removed as they are found.
</p>
<p>This alternative is the older, low-level way to perform least squares
calculations. Although still useful in some contexts, it would now
generally be replaced by the statistical models features, as will be
discussed in <ahref="#Statistical-models-in-R">Statistical models in R</a>.
</p>
<hr>
<aname="Forming-partitioned-matrices"></a>
<divclass="header">
<p>
Next: <ahref="#The-concatenation-function-c_0028_0029-with-arrays"accesskey="n"rel="next">The concatenation function c() with arrays</a>, Previous: <ahref="#Matrix-facilities"accesskey="p"rel="prev">Matrix facilities</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<h3class="section">5.9 The concatenation function, <code>c()</code>, with arrays</h3>
<p>It should be noted that whereas <code>cbind()</code> and <code>rbind()</code> are
concatenation functions that respect <code>dim</code> attributes, the basic
<code>c()</code> function does not, but rather clears numeric objects of all
<code>dim</code> and <code>dimnames</code> attributes. This is occasionally useful
in its own right.
</p>
<p>The official way to coerce an array back to a simple vector object is to
use <code>as.vector()</code>
</p>
<divclass="example">
<preclass="example">> vec <- as.vector(X)
</pre></div>
<aname="index-as_002evector"></a>
<p>However a similar result can be achieved by using <code>c()</code> with just
one argument, simply for this side-effect:
</p>
<divclass="example">
<preclass="example">> vec <- c(X)
</pre></div>
<aname="index-c-2"></a>
<p>There are slight differences between the two, but ultimately the choice
between them is largely a matter of style (with the former being
preferable).
</p>
<hr>
<aname="Frequency-tables-from-factors"></a>
<divclass="header">
<p>
Previous: <ahref="#The-concatenation-function-c_0028_0029-with-arrays"accesskey="p"rel="prev">The concatenation function c() with arrays</a>, Up: <ahref="#Arrays-and-matrices"accesskey="u"rel="up">Arrays and matrices</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Frequency-tables-from-factors-1"></a>
<h3class="section">5.10 Frequency tables from factors</h3>
<aname="index-Tabulation"></a>
<p>Recall that a factor defines a partition into groups. Similarly a pair
of factors defines a two way cross classification, and so on.
<aname="index-table-1"></a>
The function <code>table()</code> allows frequency tables to be calculated
from equal length factors. If there are <em>k</em> factor arguments,
the result is a <em>k</em>-way array of frequencies.
</p>
<p>Suppose, for example, that <code>statef</code> is a factor giving the state
code for each entry in a data vector. The assignment
<p>Then to calculate a two-way table of frequencies:
</p>
<divclass="example">
<preclass="example">> table(incomef,statef)
statef
incomef act nsw nt qld sa tas vic wa
(35,45] 1 1 0 1 0 0 1 0
(45,55] 1 1 1 1 2 0 1 3
(55,65] 0 3 1 3 2 2 2 1
(65,75] 0 1 0 0 0 0 1 0
</pre></div>
<p>Extension to higher-way frequency tables is immediate.
</p>
<hr>
<aname="Lists-and-data-frames"></a>
<divclass="header">
<p>
Next: <ahref="#Reading-data-from-files"accesskey="n"rel="next">Reading data from files</a>, Previous: <ahref="#Arrays-and-matrices"accesskey="p"rel="prev">Arrays and matrices</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<tr><tdalign="left"valign="top">•<ahref="#Constructing-and-modifying-lists"accesskey="2">Constructing and modifying lists</a>:</td><td> </td><tdalign="left"valign="top">
Next: <ahref="#Constructing-and-modifying-lists"accesskey="n"rel="next">Constructing and modifying lists</a>, Previous: <ahref="#Lists-and-data-frames"accesskey="p"rel="prev">Lists and data frames</a>, Up: <ahref="#Lists-and-data-frames"accesskey="u"rel="up">Lists and data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Lists-1"></a>
<h3class="section">6.1 Lists</h3>
<aname="index-Lists"></a>
<p>An R <em>list</em> is an object consisting of an ordered collection of
objects known as its <em>components</em>.
</p>
<p>There is no particular need for the components to be of the same mode or
type, and, for example, a list could consist of a numeric vector, a
logical value, a matrix, a complex vector, a character array, a
function, and so on. Here is a simple example of how to make a list:
<p>This is a very useful convention as it makes it easier to get the right
component if you forget the number.
</p>
<p>So in the simple example given above:
</p>
<p><code>Lst$name</code> is the same as <code>Lst[[1]]</code> and is the string
<code>"Fred"</code>,
</p>
<p><code>Lst$wife</code> is the same as <code>Lst[[2]]</code> and is the string
<code>"Mary"</code>,
</p>
<p><code>Lst$child.ages[1]</code> is the same as <code>Lst[[4]][1]</code> and is the
number <code>4</code>.
</p>
<p>Additionally, one can also use the names of the list components in
double square brackets, i.e., <code>Lst[["name"]]</code> is the same as
<code>Lst$name</code>. This is especially useful, when the name of the
component to be extracted is stored in another variable as in
</p>
<divclass="example">
<preclass="example">> x <- "name"; Lst[[x]]
</pre></div>
<p>It is very important to distinguish <code>Lst[[1]]</code> from <code>Lst[1]</code>.
‘<samp><code>[[<var>…</var>]]</code></samp>’ is the operator used to select a single
element, whereas ‘<samp><code>[<var>…</var>]</code></samp>’ is a general subscripting
operator. Thus the former is the <em>first object in the list</em>
<code>Lst</code>, and if it is a named list the name is <em>not</em> included.
The latter is a <em>sublist of the list <code>Lst</code> consisting of the
first entry only. If it is a named list, the names are transferred to
the sublist.</em>
</p>
<p>The names of components may be abbreviated down to the minimum number of
letters needed to identify them uniquely. Thus <code>Lst$coefficients</code>
may be minimally specified as <code>Lst$coe</code> and <code>Lst$covariance</code>
as <code>Lst$cov</code>.
</p>
<p>The vector of names is in fact simply an attribute of the list like any
other and may be handled as such. Other structures besides lists may,
of course, similarly be given a <em>names</em> attribute also.
</p>
<hr>
<aname="Constructing-and-modifying-lists"></a>
<divclass="header">
<p>
Next: <ahref="#Data-frames"accesskey="n"rel="next">Data frames</a>, Previous: <ahref="#Lists"accesskey="p"rel="prev">Lists</a>, Up: <ahref="#Lists-and-data-frames"accesskey="u"rel="up">Lists and data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Constructing-and-modifying-lists-1"></a>
<h3class="section">6.2 Constructing and modifying lists</h3>
<p>New lists may be formed from existing objects by the function
Previous: <ahref="#Constructing-and-modifying-lists"accesskey="p"rel="prev">Constructing and modifying lists</a>, Up: <ahref="#Constructing-and-modifying-lists"accesskey="u"rel="up">Constructing and modifying lists</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>Recall that with vector objects as arguments the concatenation function
similarly joined together all arguments into a single vector structure.
In this case all other attributes, such as <code>dim</code> attributes, are
discarded.
</p>
<hr>
<aname="Data-frames"></a>
<divclass="header">
<p>
Previous: <ahref="#Constructing-and-modifying-lists"accesskey="p"rel="prev">Constructing and modifying lists</a>, Up: <ahref="#Lists-and-data-frames"accesskey="u"rel="up">Lists and data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Data-frames-1"></a>
<h3class="section">6.3 Data frames</h3>
<aname="index-Data-frames"></a>
<p>A <em>data frame</em> is a list with class <code>"data.frame"</code>. There are
restrictions on lists that may be made into data frames, namely
</p>
<ul>
<li> The components must be vectors (numeric, character, or logical),
factors, numeric matrices, lists, or other data frames.
</li><li> Matrices, lists, and data frames provide as many variables to the new
data frame as they have columns, elements, or variables, respectively.
</li><li> Numeric vectors, logicals and factors are included as is, and by
default<aname="DOCF18"href="#FOOT18"><sup>18</sup></a> character vectors are coerced to be
factors, whose levels are the unique values appearing in the vector.
</li><li> Vector structures appearing as variables of the data frame must all have
the <em>same length</em>, and matrix structures must all have the same
<em>row size</em>.
</li></ul>
<p>A data frame may for many purposes be regarded as a matrix with columns
possibly of differing modes and attributes. It may be displayed in
matrix form, and its rows and columns extracted using matrix indexing
<tr><tdalign="left"valign="top">•<ahref="#Making-data-frames"accesskey="1">Making data frames</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#attach_0028_0029-and-detach_0028_0029"accesskey="2">attach() and detach()</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Working-with-data-frames"accesskey="3">Working with data frames</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Managing-the-search-path"accesskey="5">Managing the search path</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="Making-data-frames"></a>
<divclass="header">
<p>
Next: <ahref="#attach_0028_0029-and-detach_0028_0029"accesskey="n"rel="next">attach() and detach()</a>, Previous: <ahref="#Data-frames"accesskey="p"rel="prev">Data frames</a>, Up: <ahref="#Data-frames"accesskey="u"rel="up">Data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Making-data-frames-1"></a>
<h4class="subsection">6.3.1 Making data frames</h4>
<p>Objects satisfying the restrictions placed on the columns (components)
of a data frame may be used to form one using the function
Next: <ahref="#Working-with-data-frames"accesskey="n"rel="next">Working with data frames</a>, Previous: <ahref="#Making-data-frames"accesskey="p"rel="prev">Making data frames</a>, Up: <ahref="#Data-frames"accesskey="u"rel="up">Data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<h4class="subsection">6.3.2 <code>attach()</code> and <code>detach()</code></h4>
<aname="index-attach"></a>
<aname="index-detach"></a>
<p>The <code>$</code> notation, such as <code>accountants$home</code>, for list
components is not always very convenient. A useful facility would be
somehow to make the components of a list or data frame temporarily
visible as variables under their component name, without the need to
quote the list name explicitly each time.
</p>
<p>The <code>attach()</code> function takes a ‘database’ such as a list or data
frame as its argument. Thus suppose <code>lentils</code> is a
data frame with three variables <code>lentils$u</code>, <code>lentils$v</code>,
<code>lentils$w</code>. The attach
</p>
<divclass="example">
<preclass="example">> attach(lentils)
</pre></div>
<p>places the data frame in the search path at position 2<!-- /@w -->, and provided
there are no variables <code>u</code>, <code>v</code> or <code>w</code> in position 1<!-- /@w -->,
<code>u</code>, <code>v</code> and <code>w</code> are available as variables from the data
frame in their own right. At this point an assignment such as
</p>
<divclass="example">
<preclass="example">> u <- v+w
</pre></div>
<p>does not replace the component <code>u</code> of the data frame, but rather
masks it with another variable <code>u</code> in the working directory at
position 1<!-- /@w --> on the search path. To make a permanent change to the
data frame itself, the simplest way is to resort once again to the
<code>$</code> notation:
</p>
<divclass="example">
<preclass="example">> lentils$u <- v+w
</pre></div>
<p>However the new value of component <code>u</code> is not visible until the
data frame is detached and attached again.
</p>
<p>To detach a data frame, use the function
</p>
<divclass="example">
<preclass="example">> detach()
</pre></div>
<p>More precisely, this statement detaches from the search path the entity
currently at position 2<!-- /@w -->. Thus in the present context the variables
<code>u</code>, <code>v</code> and <code>w</code> would be no longer visible, except under
the list notation as <code>lentils$u</code> and so on. Entities at positions
greater than 2 on the search path can be detached by giving their number
to <code>detach</code>, but it is much safer to always use a name, for example
by <code>detach(lentils)</code> or <code>detach("lentils")</code>
</p>
<blockquote>
<p><b>Note:</b> In R lists and data frames can only be attached at position 2 or
above, and what is attached is a <em>copy</em> of the original object.
You can alter the attached values <em>via</em><code>assign</code>, but the
original list or data frame is unchanged.
</p></blockquote>
<hr>
<aname="Working-with-data-frames"></a>
<divclass="header">
<p>
Next: <ahref="#Attaching-arbitrary-lists"accesskey="n"rel="next">Attaching arbitrary lists</a>, Previous: <ahref="#attach_0028_0029-and-detach_0028_0029"accesskey="p"rel="prev">attach() and detach()</a>, Up: <ahref="#Data-frames"accesskey="u"rel="up">Data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Working-with-data-frames-1"></a>
<h4class="subsection">6.3.3 Working with data frames</h4>
<p>A useful convention that allows you to work with many different problems
comfortably together in the same working directory is
</p>
<ul>
<li> gather together all variables for any well defined and separate problem
in a data frame under a suitably informative name;
</li><li> when working with a problem attach the appropriate data frame at
position 2<!-- /@w -->, and use the working directory at level 1<!-- /@w --> for
operational quantities and temporary variables;
</li><li> before leaving a problem, add any variables you wish to keep for future
reference to the data frame using the <code>$</code> form of assignment, and
then <code>detach()</code>;
</li><li> finally remove all unwanted variables from the working directory and
keep it as clean of left-over temporary variables as possible.
</li></ul>
<p>In this way it is quite simple to work with many problems in the same
directory, all of which have variables named <code>x</code>, <code>y</code> and
<code>z</code>, for example.
</p>
<hr>
<aname="Attaching-arbitrary-lists"></a>
<divclass="header">
<p>
Next: <ahref="#Managing-the-search-path"accesskey="n"rel="next">Managing the search path</a>, Previous: <ahref="#Working-with-data-frames"accesskey="p"rel="prev">Working with data frames</a>, Up: <ahref="#Data-frames"accesskey="u"rel="up">Data frames</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Next: <ahref="#Probability-distributions"accesskey="n"rel="next">Probability distributions</a>, Previous: <ahref="#Lists-and-data-frames"accesskey="p"rel="prev">Lists and data frames</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Reading-data-from-files-1"></a>
<h2class="chapter">7 Reading data from files</h2>
<aname="index-Reading-data-from-files"></a>
<p>Large data objects will usually be read as values from external files
rather than entered during an R session at the keyboard. R input
facilities are simple and their requirements are fairly strict and even
rather inflexible. There is a clear presumption by the designers of
R that you will be able to modify your input files using other tools,
such as file editors or Perl<aname="DOCF20"href="#FOOT20"><sup>20</sup></a> to fit in with the
requirements of R. Generally this is very simple.
</p>
<p>If variables are to be held mainly in data frames, as we strongly
suggest they should be, an entire data frame can be read directly with
the <code>read.table()</code> function. There is also a more primitive input
function, <code>scan()</code>, that can be called directly.
</p>
<p>For more details on importing data into R and also exporting data,
Next: <ahref="#The-scan_0028_0029-function"accesskey="n"rel="next">The scan() function</a>, Previous: <ahref="#Reading-data-from-files"accesskey="p"rel="prev">Reading data from files</a>, Up: <ahref="#Reading-data-from-files"accesskey="u"rel="up">Reading data from files</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>If you wish to access the variables separately they may either be
re-assigned to variables in the working frame:
</p>
<divclass="example">
<preclass="example">> label <- inp$id; x <- inp$x; y <- inp$y
</pre></div>
<p>or the list may be attached at position 2<!-- /@w --> of the search path
(see <ahref="#Attaching-arbitrary-lists">Attaching arbitrary lists</a>).
</p>
<p>If the second argument is a single value and not a list, a single vector
is read in, all components of which must be of the same mode as the
dummy value.
</p>
<divclass="example">
<preclass="example">> X <- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE)
</pre></div>
<p>There are more elaborate input facilities available and these are
detailed in the manuals.
</p>
<hr>
<aname="Accessing-builtin-datasets"></a>
<divclass="header">
<p>
Next: <ahref="#Editing-data"accesskey="n"rel="next">Editing data</a>, Previous: <ahref="#The-scan_0028_0029-function"accesskey="p"rel="prev">The scan() function</a>, Up: <ahref="#Reading-data-from-files"accesskey="u"rel="up">Reading data from files</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>If a package has been attached by <code>library</code>, its datasets are
automatically included in the search.
</p>
<p>User-contributed packages can be a rich source of datasets.
</p>
<hr>
<aname="Editing-data"></a>
<divclass="header">
<p>
Previous: <ahref="#Accessing-builtin-datasets"accesskey="p"rel="prev">Accessing builtin datasets</a>, Up: <ahref="#Reading-data-from-files"accesskey="u"rel="up">Reading data from files</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Editing-data-1"></a>
<h3class="section">7.4 Editing data</h3>
<aname="index-edit"></a>
<p>When invoked on a data frame or matrix, <code>edit</code> brings up a separate
spreadsheet-like environment for editing. This is useful for making
small changes once a data set has been read. The command
</p>
<divclass="example">
<preclass="example">> xnew <- edit(xold)
</pre></div>
<p>will allow you to edit your data set <code>xold</code>, and on completion the
changed object is assigned to <code>xnew</code>. If you want to alter the
original dataset <code>xold</code>, the simplest way is to use
<code>fix(xold)</code>, which is equivalent to <code>xold <- edit(xold)</code>.
<p>to enter new data via the spreadsheet interface.
</p>
<hr>
<aname="Probability-distributions"></a>
<divclass="header">
<p>
Next: <ahref="#Loops-and-conditional-execution"accesskey="n"rel="next">Loops and conditional execution</a>, Previous: <ahref="#Reading-data-from-files"accesskey="p"rel="prev">Reading data from files</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Probability-distributions-1"></a>
<h2class="chapter">8 Probability distributions</h2>
<tr><tdalign="left"valign="top">•<ahref="#R-as-a-set-of-statistical-tables"accesskey="1">R as a set of statistical tables</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Examining-the-distribution-of-a-set-of-data"accesskey="2">Examining the distribution of a set of data</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#One_002d-and-two_002dsample-tests"accesskey="3">One- and two-sample tests</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="R-as-a-set-of-statistical-tables"></a>
<divclass="header">
<p>
Next: <ahref="#Examining-the-distribution-of-a-set-of-data"accesskey="n"rel="next">Examining the distribution of a set of data</a>, Previous: <ahref="#Probability-distributions"accesskey="p"rel="prev">Probability distributions</a>, Up: <ahref="#Probability-distributions"accesskey="u"rel="up">Probability distributions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="R-as-a-set-of-statistical-tables-1"></a>
<h3class="section">8.1 R as a set of statistical tables</h3>
<p>One convenient use of R is to provide a comprehensive set of
statistical tables. Functions are provided to evaluate the cumulative
distribution function P(X <= x),
the probability density function and the quantile function (given
<em>q</em>, the smallest <em>x</em> such that P(X <= x) > q),
Next: <ahref="#One_002d-and-two_002dsample-tests"accesskey="n"rel="next">One- and two-sample tests</a>, Previous: <ahref="#R-as-a-set-of-statistical-tables"accesskey="p"rel="prev">R as a set of statistical tables</a>, Up: <ahref="#Probability-distributions"accesskey="u"rel="up">Probability distributions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>Quantile-quantile (Q-Q) plots can help us examine this more carefully.
<aname="index-Quantile_002dquantile-plots"></a>
<aname="index-qqnorm"></a>
<aname="index-qqline"></a>
</p>
<divclass="example">
<preclass="example">par(pty="s") # arrange for a square figure region
qqnorm(long); qqline(long)
</pre></div>
<p>which shows a reasonable fit but a shorter right tail than one would
expect from a normal distribution. Let us compare this with some
simulated data from a <em>t</em> distribution
</p>
<imgsrc="images/QQ.png"alt="images/QQ">
<divclass="example">
<preclass="example">x <- rt(250, df = 5)
qqnorm(x); qqline(x)
</pre></div>
<p>which will usually (if it is a random sample) show longer tails than
expected for a normal. We can make a Q-Q plot against the generating
distribution by
</p>
<divclass="example">
<preclass="example">qqplot(qt(ppoints(250), df = 5), x, xlab = "Q-Q plot for t dsn")
qqline(x)
</pre></div>
<p>Finally, we might want a more formal test of agreement with normality
(or not). R provides the Shapiro-Wilk test
<aname="index-Shapiro_002dWilk-test"></a>
<aname="index-shapiro_002etest"></a>
</p>
<divclass="example">
<preclass="example">> shapiro.test(long)
Shapiro-Wilk normality test
data: long
W = 0.9793, p-value = 0.01052
</pre></div>
<p>and the Kolmogorov-Smirnov test
<aname="index-Kolmogorov_002dSmirnov-test"></a>
<aname="index-ks_002etest"></a>
</p>
<divclass="example">
<preclass="example">> ks.test(long, "pnorm", mean = mean(long), sd = sqrt(var(long)))
One-sample Kolmogorov-Smirnov test
data: long
D = 0.0661, p-value = 0.4284
alternative hypothesis: two.sided
</pre></div>
<p>(Note that the distribution theory is not valid here as we
have estimated the parameters of the normal distribution from the same
sample.)
</p>
<hr>
<aname="One_002d-and-two_002dsample-tests"></a>
<divclass="header">
<p>
Previous: <ahref="#Examining-the-distribution-of-a-set-of-data"accesskey="p"rel="prev">Examining the distribution of a set of data</a>, Up: <ahref="#Probability-distributions"accesskey="u"rel="up">Probability distributions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="One_002d-and-two_002dsample-tests-1"></a>
<h3class="section">8.3 One- and two-sample tests</h3>
<p>will show the two empirical CDFs, and <code>qqplot</code> will perform a Q-Q
plot of the two samples. The Kolmogorov-Smirnov test is of the maximal
vertical distance between the two ecdf’s, assuming a common continuous
distribution:
</p>
<divclass="example">
<preclass="example">> ks.test(A, B)
Two-sample Kolmogorov-Smirnov test
data: A and B
D = 0.5962, p-value = 0.05919
alternative hypothesis: two-sided
Warning message:
cannot compute correct p-values with ties in: ks.test(A, B)
</pre></div>
<hr>
<aname="Loops-and-conditional-execution"></a>
<divclass="header">
<p>
Next: <ahref="#Writing-your-own-functions"accesskey="n"rel="next">Writing your own functions</a>, Previous: <ahref="#Probability-distributions"accesskey="p"rel="prev">Probability distributions</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<h4class="subsection">9.2.2 Repetitive execution: <code>for</code> loops, <code>repeat</code> and <code>while</code></h4>
<aname="index-for"></a>
<p>There is also a <code>for</code> loop construction which has the form
</p>
<divclass="example">
<preclass="example">> for (<code><var>name</var></code> in <var>expr_1</var>) <var>expr_2</var>
</pre></div>
<p>where <code><var>name</var></code> is the loop variable. <var>expr_1</var> is a
vector expression, (often a sequence like <code>1:20</code>), and
<var>expr_2</var> is often a grouped expression with its sub-expressions
written in terms of the dummy <em>name</em>. <var>expr_2</var> is repeatedly
evaluated as <var>name</var> ranges through the values in the vector result
of <var>expr_1</var>.
</p>
<p>As an example, suppose <code>ind</code> is a vector of class indicators and we
wish to produce separate plots of <code>y</code> versus <code>x</code> within
classes. One possibility here is to use <code>coplot()</code>,<aname="DOCF21"href="#FOOT21"><sup>21</sup></a>
which will produce an array of plots corresponding to each level of the
factor. Another way to do this, now putting all plots on the one
display, is as follows:
</p>
<divclass="example">
<preclass="example">> xc <- split(x, ind)
> yc <- split(y, ind)
> for (i in 1:length(yc)) {
plot(xc[[i]], yc[[i]])
abline(lsfit(xc[[i]], yc[[i]]))
}
</pre></div>
<aname="index-split"></a>
<p>(Note the function <code>split()</code> which produces a list of vectors
obtained by splitting a larger vector according to the classes specified
by a factor. This is a useful function, mostly used in connection
with boxplots. See the <code>help</code> facility for further details.)
</p>
<blockquote>
<p><strong>Warning</strong>: <code>for()</code> loops are used in R code much less
often than in compiled languages. Code that takes a ‘whole object’ view
is likely to be both clearer and faster in R.
</p></blockquote>
<p>Other looping facilities include the
</p>
<divclass="example">
<preclass="example">> repeat <var>expr</var>
</pre></div>
<aname="index-repeat"></a>
<p>statement and the
</p>
<divclass="example">
<preclass="example">> while (<var>condition</var>) <var>expr</var>
</pre></div>
<aname="index-while"></a>
<p>statement.
</p>
<p>The <code>break</code> statement can be used to terminate any loop, possibly
abnormally. This is the only way to terminate <code>repeat</code> loops.
<aname="index-break"></a>
</p>
<p>The <code>next</code> statement can be used to discontinue one particular
cycle and skip to the “next”.
<aname="index-next"></a>
</p>
<p>Control statements are most often used in connection with
<em>functions</em> which are discussed in <ahref="#Writing-your-own-functions">Writing your own functions</a>, and where more examples will emerge.
</p>
<hr>
<aname="Writing-your-own-functions"></a>
<divclass="header">
<p>
Next: <ahref="#Statistical-models-in-R"accesskey="n"rel="next">Statistical models in R</a>, Previous: <ahref="#Loops-and-conditional-execution"accesskey="p"rel="prev">Loops and conditional execution</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Writing-your-own-functions-1"></a>
<h2class="chapter">10 Writing your own functions</h2>
<aname="index-Writing-functions"></a>
<p>As we have seen informally along the way, the R language allows the
user to create objects of mode <em>function</em>. These are true R
functions that are stored in a special internal form and may be used in
further expressions and so on. In the process, the language gains
enormously in power, convenience and elegance, and learning to write
useful functions is one of the main ways to make your use of R
comfortable and productive.
</p>
<p>It should be emphasized that most of the functions supplied as part of
the R system, such as <code>mean()</code>, <code>var()</code>,
<code>postscript()</code> and so on, are themselves written in R and thus
do not differ materially from user written functions.
</p>
<p>A function is defined by an assignment of the form
<tr><tdalign="left"valign="top">•<ahref="#Defining-new-binary-operators"accesskey="2">Defining new binary operators</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Named-arguments-and-defaults"accesskey="3">Named arguments and defaults</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#The-three-dots-argument"accesskey="4">The three dots argument</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Assignment-within-functions"accesskey="5">Assignment within functions</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Customizing-the-environment"accesskey="8">Customizing the environment</a>:</td><td> </td><tdalign="left"valign="top">
Next: <ahref="#Defining-new-binary-operators"accesskey="n"rel="next">Defining new binary operators</a>, Previous: <ahref="#Writing-your-own-functions"accesskey="p"rel="prev">Writing your own functions</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Simple-examples-1"></a>
<h3class="section">10.1 Simple examples</h3>
<p>As a first example, consider a function to calculate the two sample
<em>t</em>-statistic, showing “all the steps”. This is an artificial
example, of course, since there are other, simpler ways of achieving the
<p>The classical R function <code>lsfit()</code> does this job quite well, and
more<aname="DOCF22"href="#FOOT22"><sup>22</sup></a>. It in turn uses the functions <code>qr()</code> and <code>qr.coef()</code>
in the slightly counterintuitive way above to do this part of the
calculation. Hence there is probably some value in having just this
part isolated in a simple to use function if it is going to be in
frequent use. If so, we may wish to make it a matrix binary operator
for even more convenient use.
</p>
<hr>
<aname="Defining-new-binary-operators"></a>
<divclass="header">
<p>
Next: <ahref="#Named-arguments-and-defaults"accesskey="n"rel="next">Named arguments and defaults</a>, Previous: <ahref="#Simple-examples"accesskey="p"rel="prev">Simple examples</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Defining-new-binary-operators-1"></a>
<h3class="section">10.2 Defining new binary operators</h3>
<aname="index-Binary-operators"></a>
<p>Had we given the <code>bslash()</code> function a different name, namely one of
the form
</p>
<divclass="example">
<preclass="example">%<var>anything</var>%
</pre></div>
<p>it could have been used as a <em>binary operator</em> in expressions
rather than in function form. Suppose, for example, we choose <code>!</code>
for the internal character. The function definition would then start as
<p>(Note the use of quote marks.) The function could then be used as
<code>X %!% y</code>. (The backslash symbol itself is not a convenient choice
as it presents special problems in this context.)
</p>
<p>The matrix multiplication operator, <code>%*%</code>, and the outer product
matrix operator <code>%o%</code> are other examples of binary operators
defined in this way.
</p>
<hr>
<aname="Named-arguments-and-defaults"></a>
<divclass="header">
<p>
Next: <ahref="#The-three-dots-argument"accesskey="n"rel="next">The three dots argument</a>, Previous: <ahref="#Defining-new-binary-operators"accesskey="p"rel="prev">Defining new binary operators</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Named-arguments-and-defaults-1"></a>
<h3class="section">10.3 Named arguments and defaults</h3>
<aname="index-Named-arguments"></a>
<aname="index-Default-values"></a>
<p>As first noted in <ahref="#Generating-regular-sequences">Generating regular sequences</a>, if arguments to
called functions are given in the “<code><var>name</var>=<var>object</var></code>”
form, they may be given in any order. Furthermore the argument sequence
may begin in the unnamed, positional form, and specify named arguments
after the positional arguments.
</p>
<p>Thus if there is a function <code>fun1</code> defined by
<p>which is now equivalent to the three cases above, or as
</p>
<divclass="example">
<preclass="example">> ans <- fun1(d, df, limit=10)
</pre></div>
<p>which changes one of the defaults.
</p>
<p>It is important to note that defaults may be arbitrary expressions, even
involving other arguments to the same function; they are not restricted
to be constants as in our simple example here.
</p>
<hr>
<aname="The-three-dots-argument"></a>
<divclass="header">
<p>
Next: <ahref="#Assignment-within-functions"accesskey="n"rel="next">Assignment within functions</a>, Previous: <ahref="#Named-arguments-and-defaults"accesskey="p"rel="prev">Named arguments and defaults</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="The-_2026-argument"></a>
<h3class="section">10.4 The ‘<samp>…</samp>’ argument</h3>
<p>Another frequent requirement is to allow one function to pass on
argument settings to another. For example many graphics functions use
the function <code>par()</code> and functions like <code>plot()</code> allow the
user to pass on graphical parameters to <code>par()</code> to control the
graphical output. (See <ahref="#The-par_0028_0029-function">The par() function</a>, for more details on the
<code>par()</code> function.) This can be done by including an extra
argument, literally ‘<samp>…</samp>’, of the function, which may then be
<p>Less frequently, a function will need to refer to components of
‘<samp>…</samp>’. The expression <code>list(...)</code> evaluates all such
arguments and returns them in a named list, while <code>..1</code>,
<code>..2</code>, etc. evaluate them one at a time, with ‘<samp>..n</samp>’
returning the n’th unmatched argument.
</p>
<hr>
<aname="Assignment-within-functions"></a>
<divclass="header">
<p>
Next: <ahref="#More-advanced-examples"accesskey="n"rel="next">More advanced examples</a>, Previous: <ahref="#The-three-dots-argument"accesskey="p"rel="prev">The three dots argument</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Assignments-within-functions"></a>
<h3class="section">10.5 Assignments within functions</h3>
<p>Note that <em>any ordinary assignments done within the function are
local and temporary and are lost after exit from the function</em>. Thus
the assignment <code>X <- qr(X)</code> does not affect the value of the
argument in the calling program.
</p>
<p>To understand completely the rules governing the scope of R assignments
the reader needs to be familiar with the notion of an evaluation
<em>frame</em>. This is a somewhat advanced, though hardly difficult,
topic and is not covered further here.
</p>
<p>If global and permanent assignments are intended within a function, then
either the “superassignment” operator, <code><<-</code> or the function
<code>assign()</code> can be used. See the <code>help</code> document for details.
<small>S-PLUS</small> users should be aware that <code><<-</code> has different semantics
in R. These are discussed further in <ahref="#Scope">Scope</a>.
</p>
<hr>
<aname="More-advanced-examples"></a>
<divclass="header">
<p>
Next: <ahref="#Scope"accesskey="n"rel="next">Scope</a>, Previous: <ahref="#Assignment-within-functions"accesskey="p"rel="prev">Assignment within functions</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="More-advanced-examples-1"></a>
<h3class="section">10.6 More advanced examples</h3>
<tr><tdalign="left"valign="top">•<ahref="#Efficiency-factors-in-block-designs"accesskey="1">Efficiency factors in block designs</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Dropping-all-names-in-a-printed-array"accesskey="2">Dropping all names in a printed array</a>:</td><td> </td><tdalign="left"valign="top">
Next: <ahref="#Dropping-all-names-in-a-printed-array"accesskey="n"rel="next">Dropping all names in a printed array</a>, Previous: <ahref="#More-advanced-examples"accesskey="p"rel="prev">More advanced examples</a>, Up: <ahref="#More-advanced-examples"accesskey="u"rel="up">More advanced examples</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
## <spanclass="roman">Remove all dimension names from an array for compact printing.</span>
d <- list()
l <- 0
for(i in dim(a)) {
d[[l <- l + 1]] <- rep("", i)
}
dimnames(a) <- d
a
}
</pre></div>
<p>With this function defined, an array may be printed in close format
using
</p>
<divclass="example">
<preclass="example">> no.dimnames(X)
</pre></div>
<p>This is particularly useful for large integer arrays, where patterns are
the real interest rather than the values.
</p>
<hr>
<aname="Recursive-numerical-integration"></a>
<divclass="header">
<p>
Previous: <ahref="#Dropping-all-names-in-a-printed-array"accesskey="p"rel="prev">Dropping all names in a printed array</a>, Up: <ahref="#More-advanced-examples"accesskey="u"rel="up">More advanced examples</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Next: <ahref="#Customizing-the-environment"accesskey="n"rel="next">Customizing the environment</a>, Previous: <ahref="#More-advanced-examples"accesskey="p"rel="prev">More advanced examples</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Scope-1"></a>
<h3class="section">10.7 Scope</h3>
<aname="index-Scope"></a>
<p>The discussion in this section is somewhat more technical than in other
parts of this document. However, it details one of the major differences
between <small>S-PLUS</small> and R.
</p>
<p>The symbols which occur in the body of a function can be divided into
three classes; formal parameters, local variables and free variables.
The formal parameters of a function are those occurring in the argument
list of the function. Their values are determined by the process of
<em>binding</em> the actual function arguments to the formal parameters.
Local variables are those whose values are determined by the evaluation
of expressions in the body of the functions. Variables which are not
formal parameters or local variables are called free variables. Free
variables become local variables if they are assigned to. Consider the
following function definition.
</p>
<divclass="example">
<preclass="example">f <- function(x) {
y <- 2*x
print(x)
print(y)
print(z)
}
</pre></div>
<p>In this function, <code>x</code> is a formal parameter, <code>y</code> is a local
variable and <code>z</code> is a free variable.
</p>
<p>In R the free variable bindings are resolved by first looking in the
environment in which the function was created. This is called
<em>lexical scope</em>. First we define a function called <code>cube</code>.
</p>
<divclass="example">
<preclass="example">cube <- function(n) {
sq <- function() n*n
n*sq()
}
</pre></div>
<p>The variable <code>n</code> in the function <code>sq</code> is not an argument to that
function. Therefore it is a free variable and the scoping rules must be
used to ascertain the value that is to be associated with it. Under static
scope (<small>S-PLUS</small>) the value is that associated with a global variable named
<code>n</code>. Under lexical scope (R) it is the parameter to the function
<code>cube</code> since that is the active binding for the variable <code>n</code> at
the time the function <code>sq</code> was defined. The difference between
evaluation in R and evaluation in <small>S-PLUS</small> is that <small>S-PLUS</small> looks for a
global variable called <code>n</code> while R first looks for a variable
called <code>n</code> in the environment created when <code>cube</code> was invoked.
</p>
<divclass="example">
<preclass="example">## <spanclass="roman">first evaluation in S</span>
S> cube(2)
Error in sq(): Object "n" not found
Dumped
S> n <- 3
S> cube(2)
[1] 18
## <spanclass="roman">then the same function evaluated in R</span>
R> cube(2)
[1] 8
</pre></div>
<p>Lexical scope can also be used to give functions <em>mutable state</em>.
In the following example we show how R can be used to mimic a bank
account. A functioning bank account needs to have a balance or total, a
function for making withdrawals, a function for making deposits and a
function for stating the current balance. We achieve this by creating
the three functions within <code>account</code> and then returning a list
containing them. When <code>account</code> is invoked it takes a numerical
argument <code>total</code> and returns a list containing the three functions.
Because these functions are defined in an environment which contains
<code>total</code>, they will have access to its value.
</p>
<p>The special assignment operator, <code><<-</code>,
<aname="index-_003c_003c_002d"></a>
is used to change the value associated with <code>total</code>. This operator
looks back in enclosing environments for an environment that contains
the symbol <code>total</code> and when it finds such an environment it
replaces the value, in that environment, with the value of right hand
side. If the global or top-level environment is reached without finding
the symbol <code>total</code> then that variable is created and assigned to
there. For most users <code><<-</code> creates a global variable and assigns
the value of the right hand side to it<aname="DOCF23"href="#FOOT23"><sup>23</sup></a>. Only when <code><<-</code> has
been used in a function that was returned as the value of another
function will the special behavior described here occur.
Next: <ahref="#Object-orientation"accesskey="n"rel="next">Object orientation</a>, Previous: <ahref="#Scope"accesskey="p"rel="prev">Scope</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Customizing-the-environment-1"></a>
<h3class="section">10.8 Customizing the environment</h3>
<aname="index-Customizing-the-environment"></a>
<p>Users can customize their environment in several different ways. There
is a site initialization file and every directory can have its own
special initialization file. Finally, the special functions
<code>.First</code> and <code>.Last</code> can be used.
</p>
<p>The location of the site initialization file is taken from the value of
the <code>R_PROFILE</code> environment variable. If that variable is unset,
the file <samp>Rprofile.site</samp> in the R home subdirectory <samp>etc</samp> is
used. This file should contain the commands that you want to execute
every time R is started under your system. A second, personal,
profile file named <samp>.Rprofile</samp><aname="DOCF24"href="#FOOT24"><sup>24</sup></a> can be placed in any directory. If R is invoked in that
directory then that file will be sourced. This file gives individual
users control over their workspace and allows for different startup
procedures in different working directories. If no <samp>.Rprofile</samp>
file is found in the startup directory, then R looks for a
<samp>.Rprofile</samp> file in the user’s home directory and uses that (if it
exists). If the environment variable <code>R_PROFILE_USER</code> is set, the
file it points to is used instead of the <samp>.Rprofile</samp> files.
</p>
<p>Any function named <code>.First()</code> in either of the two profile files or
in the <samp>.RData</samp> image has a special status. It is automatically
performed at the beginning of an R session and may be used to
initialize the environment. For example, the definition in the example
below alters the prompt to <code>$</code> and sets up various other useful
things that can then be taken for granted in the rest of the session.
</p>
<p>Thus, the sequence in which files are executed is, <samp>Rprofile.site</samp>,
the user profile, <samp>.RData</samp> and then <code>.First()</code>. A definition
in later files will mask definitions in earlier files.
library(MASS) # <spanclass="roman">attach a package</span>
}
</pre></div>
<aname="index-_002eFirst"></a>
<p>Similarly a function <code>.Last()</code>, if defined, is (normally) executed
at the very end of the session. An example is given below.
</p>
<divclass="example">
<preclass="example">> .Last <- function() {
graphics.off() # <spanclass="roman">a small safety measure.</span>
cat(paste(date(),"\nAdios\n")) # <spanclass="roman">Is it time for lunch?</span>
}
</pre></div>
<aname="index-_002eLast"></a>
<hr>
<aname="Object-orientation"></a>
<divclass="header">
<p>
Previous: <ahref="#Customizing-the-environment"accesskey="p"rel="prev">Customizing the environment</a>, Up: <ahref="#Writing-your-own-functions"accesskey="u"rel="up">Writing your own functions</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>A function named <code><var>gen</var>.<var>cl</var></code> will be invoked by the
generic <code><var>gen</var></code> for class <code><var>cl</var></code>, so do not name
functions in this style unless they are intended to be methods.
</p>
<p>The reader is referred to the <em>R Language Definition</em> for a more
complete discussion of this mechanism.
</p>
<hr>
<aname="Statistical-models-in-R"></a>
<divclass="header">
<p>
Next: <ahref="#Graphics"accesskey="n"rel="next">Graphics</a>, Previous: <ahref="#Writing-your-own-functions"accesskey="p"rel="prev">Writing your own functions</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Statistical-models-in-R-1"></a>
<h2class="chapter">11 Statistical models in R</h2>
<aname="index-Statistical-models"></a>
<p>This section presumes the reader has some familiarity with statistical
methodology, in particular with regression analysis and the analysis of
variance. Later we make some rather more ambitious presumptions, namely
that something is known about generalized linear models and nonlinear
regression.
</p>
<p>The requirements for fitting statistical models are sufficiently well
defined to make it possible to construct general tools that apply in a
broad spectrum of problems.
</p>
<p>R provides an interlocking suite of facilities that make fitting
statistical models very simple. As we mention in the introduction, the
basic output is minimal, and one needs to ask for the details by calling
<tr><tdalign="left"valign="top">•<ahref="#Formulae-for-statistical-models"accesskey="1">Formulae for statistical models</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Generic-functions-for-extracting-model-information"accesskey="3">Generic functions for extracting model information</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Analysis-of-variance-and-model-comparison"accesskey="4">Analysis of variance and model comparison</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Generalized-linear-models"accesskey="6">Generalized linear models</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Nonlinear-least-squares-and-maximum-likelihood-models"accesskey="7">Nonlinear least squares and maximum likelihood models</a>:</td><td> </td><tdalign="left"valign="top">
Previous: <ahref="#Formulae-for-statistical-models"accesskey="p"rel="prev">Formulae for statistical models</a>, Up: <ahref="#Formulae-for-statistical-models"accesskey="u"rel="up">Formulae for statistical models</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Contrasts-1"></a>
<h4class="subsection">11.1.1 Contrasts</h4>
<aname="index-Contrasts"></a>
<p>We need at least some idea how the model formulae specify the columns of
the model matrix. This is easy if we have continuous variables, as each
provides one column of the model matrix (and the intercept will provide
a column of ones if included in the model).
</p>
<aname="index-Factors-1"></a>
<aname="index-Ordered-factors-1"></a>
<p>What about a <em>k</em>-level factor <code>A</code>? The answer differs for
unordered and ordered factors. For <em>unordered</em> factors <em>k -
1</em> columns are generated for the indicators of the second, …,
<em>k</em>th levels of the factor. (Thus the implicit parameterization is
to contrast the response at each level with that at the first.) For
<em>ordered</em> factors the <em>k - 1</em> columns are the orthogonal
polynomials on <em>1, …, k</em>, omitting the constant term.
</p>
<p>Although the answer is already complicated, it is not the whole story.
First, if the intercept is omitted in a model that contains a factor
term, the first such term is encoded into <em>k</em> columns giving the
indicators for all the levels. Second, the whole behavior can be
changed by the <code>options</code> setting for <code>contrasts</code>. The default
<p>This is a deliberate difference, as treatment contrasts (R’s default)
are thought easier for newcomers to interpret.
</p>
<p>We have still not finished, as the contrast scheme to be used can be set
for each term in the model using the functions <code>contrasts</code> and
<code>C</code>.
<aname="index-contrasts"></a>
<aname="index-C"></a>
</p>
<p>We have not yet considered interaction terms: these generate the
products of the columns introduced for their component terms.
</p>
<p>Although the details are complicated, model formulae in R will
normally generate the models that an expert statistician would expect,
provided that marginality is preserved. Fitting, for example, a model
with an interaction but not the corresponding main effects will in
general lead to surprising results, and is for experts only.
</p>
<hr>
<aname="Linear-models"></a>
<divclass="header">
<p>
Next: <ahref="#Generic-functions-for-extracting-model-information"accesskey="n"rel="next">Generic functions for extracting model information</a>, Previous: <ahref="#Formulae-for-statistical-models"accesskey="p"rel="prev">Formulae for statistical models</a>, Up: <ahref="#Statistical-models-in-R"accesskey="u"rel="up">Statistical models in R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Linear-models-1"></a>
<h3class="section">11.2 Linear models</h3>
<aname="index-Linear-models"></a>
<p>The basic function for fitting ordinary multiple models is <code>lm()</code>,
and a streamlined version of the call is as follows:
<aname="index-lm"></a>
</p>
<divclass="example">
<preclass="example">><var>fitted.model</var><- lm(<var>formula</var>, data = <var>data.frame</var>)
Next: <ahref="#Analysis-of-variance-and-model-comparison"accesskey="n"rel="next">Analysis of variance and model comparison</a>, Previous: <ahref="#Linear-models"accesskey="p"rel="prev">Linear models</a>, Up: <ahref="#Statistical-models-in-R"accesskey="u"rel="up">Statistical models in R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Next: <ahref="#Updating-fitted-models"accesskey="n"rel="next">Updating fitted models</a>, Previous: <ahref="#Generic-functions-for-extracting-model-information"accesskey="p"rel="prev">Generic functions for extracting model information</a>, Up: <ahref="#Statistical-models-in-R"accesskey="u"rel="up">Statistical models in R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
Previous: <ahref="#Analysis-of-variance-and-model-comparison"accesskey="p"rel="prev">Analysis of variance and model comparison</a>, Up: <ahref="#Analysis-of-variance-and-model-comparison"accesskey="u"rel="up">Analysis of variance and model comparison</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="ANOVA-tables-1"></a>
<h4class="subsection">11.4.1 ANOVA tables</h4>
<p>Note also that the analysis of variance table (or tables) are for a
sequence of fitted models. The sums of squares shown are the decrease
in the residual sums of squares resulting from an inclusion of
<em>that term</em> in the model at <em>that place</em> in the sequence.
Hence only for orthogonal experiments will the order of inclusion be
inconsequential.
</p>
<p>For multistratum experiments the procedure is first to project the
response onto the error strata, again in sequence, and to fit the mean
model to each projection. For further details, see Chambers & Hastie
(1992).
</p>
<p>A more flexible alternative to the default full ANOVA table is to
compare two or more models directly using the <code>anova()</code> function.
<p>The display is then an ANOVA table showing the differences between the
fitted models when fitted in sequence. The fitted models being compared
would usually be an hierarchical sequence, of course. This does not
give different information to the default, but rather makes it easier to
comprehend and control.
</p>
<hr>
<aname="Updating-fitted-models"></a>
<divclass="header">
<p>
Next: <ahref="#Generalized-linear-models"accesskey="n"rel="next">Generalized linear models</a>, Previous: <ahref="#Analysis-of-variance-and-model-comparison"accesskey="p"rel="prev">Analysis of variance and model comparison</a>, Up: <ahref="#Statistical-models-in-R"accesskey="u"rel="up">Statistical models in R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<p>would fit a five variate multiple regression with variables (presumably)
from the data frame <code>production</code>, fit an additional model including
a sixth regressor variable, and fit a variant on the model where the
response had a square root transform applied.
</p>
<p>Note especially that if the <code>data=</code> argument is specified on the
original call to the model fitting function, this information is passed on
through the fitted model object to <code>update()</code> and its allies.
</p>
<p>The name ‘<samp>.</samp>’ can also be used in other contexts, but with slightly
different meaning. For example
</p>
<divclass="example">
<preclass="example">> fmfull <- lm(y ~ . , data = production)
</pre></div>
<p>would fit a model with response <code>y</code> and regressor variables
<em>all other variables in the data frame <code>production</code></em>.
</p>
<p>Other functions for exploring incremental sequences of models are
<code>add1()</code>, <code>drop1()</code> and <code>step()</code>.
<aname="index-add1"></a>
<aname="index-drop1"></a>
<aname="index-step-1"></a>
The names of these give a good clue to their purpose, but for full
details see the on-line help.
</p>
<hr>
<aname="Generalized-linear-models"></a>
<divclass="header">
<p>
Next: <ahref="#Nonlinear-least-squares-and-maximum-likelihood-models"accesskey="n"rel="next">Nonlinear least squares and maximum likelihood models</a>, Previous: <ahref="#Updating-fitted-models"accesskey="p"rel="prev">Updating fitted models</a>, Up: <ahref="#Statistical-models-in-R"accesskey="u"rel="up">Statistical models in R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Generalized-linear-models-1"></a>
<h3class="section">11.6 Generalized linear models</h3>
<aname="index-Generalized-linear-models"></a>
<p>Generalized linear modeling is a development of linear models to
accommodate both non-normal response distributions and transformations
to linearity in a clean and straightforward way. A generalized linear
model may be described in terms of the following sequence of
assumptions:
</p>
<ul>
<li> There is a response, <em>y</em>, of interest and stimulus variables
x_1, x_2, …,
whose values influence the distribution of the response.
</li><li> The stimulus variables influence the distribution of <em>y</em> through
<em>a single linear function, only</em>. This linear function is called
the <em>linear predictor</em>, and is usually written
Next: <ahref="#The-glm_0028_0029-function"accesskey="n"rel="next">The glm() function</a>, Previous: <ahref="#Generalized-linear-models"accesskey="p"rel="prev">Generalized linear models</a>, Up: <ahref="#Generalized-linear-models"accesskey="u"rel="up">Generalized linear models</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Families-1"></a>
<h4class="subsection">11.6.1 Families</h4>
<aname="index-Families"></a>
<p>The class of generalized linear models handled by facilities supplied in
R includes <em>gaussian</em>, <em>binomial</em>, <em>poisson</em>,
<em>inverse gaussian</em> and <em>gamma</em> response distributions and also
<em>quasi-likelihood</em> models where the response distribution is not
explicitly specified. In the latter case the <em>variance function</em>
must be specified as a function of the mean, but in other cases this
function is implied by the response distribution.
</p>
<p>Each response distribution admits a variety of link functions to connect
the mean with the linear predictor. Those automatically available are
<p>The combination of a response distribution, a link function and various
other pieces of information that are needed to carry out the modeling
exercise is called the <em>family</em> of the generalized linear model.
</p>
<hr>
<aname="The-glm_0028_0029-function"></a>
<divclass="header">
<p>
Previous: <ahref="#Families"accesskey="p"rel="prev">Families</a>, Up: <ahref="#Generalized-linear-models"accesskey="u"rel="up">Generalized linear models</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="The-glm_0028_0029-function-1"></a>
<h4class="subsection">11.6.2 The <code>glm()</code> function</h4>
<aname="index-glm"></a>
<p>Since the distribution of the response depends on the stimulus variables
through a single linear function <em>only</em>, the same mechanism as was
used for linear models can still be used to specify the linear part of a
generalized model. The family has to be specified in a different way.
</p>
<p>The R function to fit a generalized linear model is <code>glm()</code>
Next: <ahref="#Maximum-likelihood"accesskey="n"rel="next">Maximum likelihood</a>, Previous: <ahref="#Nonlinear-least-squares-and-maximum-likelihood-models"accesskey="p"rel="prev">Nonlinear least squares and maximum likelihood models</a>, Up: <ahref="#Nonlinear-least-squares-and-maximum-likelihood-models"accesskey="u"rel="up">Nonlinear least squares and maximum likelihood models</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Least-squares-1"></a>
<h4class="subsection">11.7.1 Least squares</h4>
<p>One way to fit a nonlinear model is by minimizing the sum of the squared
errors (SSE) or residuals. This method makes sense if the observed
errors could have plausibly arisen from a normal distribution.
</p>
<p>Here is an example from Bates & Watts (1988), page 51. The data are:
Residual standard error: 10.93 on 10 degrees of freedom
Correlation of Parameter Estimates:
Vm
K 0.7651
</pre></div>
<hr>
<aname="Maximum-likelihood"></a>
<divclass="header">
<p>
Previous: <ahref="#Least-squares"accesskey="p"rel="prev">Least squares</a>, Up: <ahref="#Nonlinear-least-squares-and-maximum-likelihood-models"accesskey="u"rel="up">Nonlinear least squares and maximum likelihood models</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Maximum-likelihood-1"></a>
<h4class="subsection">11.7.2 Maximum likelihood</h4>
<aname="index-Maximum-likelihood"></a>
<p>Maximum likelihood is a method of nonlinear model fitting that applies
even if the errors are not normal. The method finds the parameter values
which maximize the log likelihood, or equivalently which minimize the
negative log-likelihood. Here is an example from Dobson (1990), pp.
108–111. This example fits a logistic model to dose-response data,
which clearly could also be fit by <code>glm()</code>. The data are:
</p>
<divclass="example">
<preclass="example">> x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113,
<p>A 95% confidence interval would be the parameter estimate +/-
1.96 SE.
</p>
<hr>
<aname="Some-non_002dstandard-models"></a>
<divclass="header">
<p>
Previous: <ahref="#Nonlinear-least-squares-and-maximum-likelihood-models"accesskey="p"rel="prev">Nonlinear least squares and maximum likelihood models</a>, Up: <ahref="#Statistical-models-in-R"accesskey="u"rel="up">Statistical models in R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Some-non_002dstandard-models-1"></a>
<h3class="section">11.8 Some non-standard models</h3>
<p>We conclude this chapter with just a brief mention of some of the other
facilities available in R for special regression and data analysis
problems.
</p>
<ul>
<li><aname="index-Mixed-models"></a>
<strong>Mixed models.</strong> The recommended <ahref="https://CRAN.R-project.org/package=nlme"><strong>nlme</strong></a> package provides
functions <code>lme()</code> and <code>nlme()</code>
<aname="index-lme"></a>
<aname="index-nlme"></a>
for linear and non-linear mixed-effects models, that is linear and
non-linear regressions in which some of the coefficients correspond to
random effects. These functions make heavy use of formulae to specify
<strong>Tree-based models.</strong> Rather than seek an explicit global linear
model for prediction or interpretation, tree-based models seek to
bifurcate the data, recursively, at critical points of the determining
variables in order to partition the data ultimately into groups that are
as homogeneous as possible within, and as heterogeneous as possible
between. The results often lead to insights that other data analysis
methods tend not to yield.
<p>Models are again specified in the ordinary linear model form. The model
fitting function is <code>tree()</code>,
<aname="index-tree"></a>
but many other generic functions such as <code>plot()</code> and <code>text()</code>
are well adapted to displaying the results of a tree-based model fit in
a graphical way.
</p>
<p>Tree models are available in R <em>via</em> the user-contributed
packages <ahref="https://CRAN.R-project.org/package=rpart"><strong>rpart</strong></a> and <ahref="https://CRAN.R-project.org/package=tree"><strong>tree</strong></a>.
</p>
</li></ul>
<hr>
<aname="Graphics"></a>
<divclass="header">
<p>
Next: <ahref="#Packages"accesskey="n"rel="next">Packages</a>, Previous: <ahref="#Statistical-models-in-R"accesskey="p"rel="prev">Statistical models in R</a>, Up: <ahref="#Top"accesskey="u"rel="up">Top</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Graphical-procedures"></a>
<h2class="chapter">12 Graphical procedures</h2>
<p>Graphical facilities are an important and extremely versatile component
of the R environment. It is possible to use the facilities to
display a wide variety of statistical graphs and also to build entirely
new types of graph.
</p>
<p>The graphics facilities can be used in both interactive and batch modes,
but in most cases, interactive use is more productive. Interactive use
is also easy because at startup time R initiates a graphics
<em>device driver</em> which opens a special <em>graphics window</em> for
the display of interactive graphics. Although this is done
automatically, it may useful to know that the command used is
<code>X11()</code> under UNIX, <code>windows()</code> under Windows and
<code>quartz()</code> under OS X. A new device can always be opened by
<code>dev.new()</code>.
</p>
<p>Once the device driver is running, R plotting commands can be used to
produce a variety of graphical displays and to create entirely new kinds
of display.
</p>
<p>Plotting commands are divided into three basic groups:
</p>
<ul>
<li><strong>High-level</strong> plotting functions create a new plot on the graphics
device, possibly with axes, labels, titles and so on.
</li><li><strong>Low-level</strong> plotting functions add more information to an
existing plot, such as extra points, lines and labels.
</li><li><strong>Interactive</strong> graphics functions allow you interactively add
information to, or extract information from, an existing plot, using a
pointing device such as a mouse.
</li></ul>
<p>In addition, R maintains a list of <em>graphical parameters</em> which
can be manipulated to customize your plots.
</p>
<p>This manual only describes what are known as ‘base’ graphics. A
separate graphics sub-system in package <strong>grid</strong> coexists with base –
it is more powerful but harder to use. There is a recommended package
<ahref="https://CRAN.R-project.org/package=lattice"><strong>lattice</strong></a> which builds on <strong>grid</strong> and provides ways to produce
multi-panel plots akin to those in the <em>Trellis</em> system in S.
<tr><tdalign="left"valign="top">•<ahref="#Interacting-with-graphics"accesskey="3">Interacting with graphics</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Arguments-to-high_002dlevel-plotting-functions"accesskey="4">Arguments to high-level plotting functions</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Arguments-to-graphics-functions"accesskey="2">Arguments to graphics functions</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="The-par_0028_0029-function"></a>
<divclass="header">
<p>
Next: <ahref="#Arguments-to-graphics-functions"accesskey="n"rel="next">Arguments to graphics functions</a>, Previous: <ahref="#Using-graphics-parameters"accesskey="p"rel="prev">Using graphics parameters</a>, Up: <ahref="#Using-graphics-parameters"accesskey="u"rel="up">Using graphics parameters</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
<tr><tdalign="left"valign="top">•<ahref="#Axes-and-tick-marks"accesskey="2">Axes and tick marks</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#PostScript-diagrams-for-typeset-documents"accesskey="1">PostScript diagrams for typeset documents</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Contributed-packages-and-CRAN"accesskey="2">Contributed packages and CRAN</a>:</td><td> </td><tdalign="left"valign="top">
contains a list of CRAN packages current at the time of release, but the
collection of available packages changes very frequently.
</p>
<hr>
<aname="Namespaces"></a>
<divclass="header">
<p>
Previous: <ahref="#Contributed-packages-and-CRAN"accesskey="p"rel="prev">Contributed packages and CRAN</a>, Up: <ahref="#Packages"accesskey="u"rel="up">Packages</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Namespaces-1"></a>
<h3class="section">13.3 Namespaces</h3>
<aname="index-Namespace"></a>
<aname="index-_003a_003a"></a>
<aname="index-_003a_003a_003a"></a>
<p>All packages have <em>namespaces</em>, and have since R 2.14.0.
Namespaces do three things: they allow the package writer to hide
functions and data that are meant only for internal use, they prevent
functions from breaking when a user (or other package writer) picks a
name that clashes with one in the package, and they provide a way to
refer to an object within a particular package.
</p>
<p>For example, <code>t()</code> is the transpose function in R, but users
might define their own function named <code>t</code>. Namespaces prevent
the user’s definition from taking precedence, and breaking every
function that tries to transpose a matrix.
</p>
<p>There are two operators that work with namespaces. The double-colon
operator <code>::</code> selects definitions from a particular namespace.
In the example above, the transpose function will always be available
as <code>base::t</code>, because it is defined in the <code>base</code> package.
Only functions that are exported from the package can be retrieved in
this way.
</p>
<p>The triple-colon operator <code>:::</code> may be seen in a few places in R
code: it acts like the double-colon operator but also allows access to
hidden objects. Users are more likely to use the <code>getAnywhere()</code>
function, which searches multiple packages.
</p>
<p>Packages are often inter-dependent, and loading one may cause others to
be automatically loaded. The colon operators described above will also
cause automatic loading of the associated package. When packages with
namespaces are loaded automatically they are not added to the search
<tr><tdalign="left"valign="top">•<ahref="#Files-and-directories"accesskey="1">Files and directories</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Compression-and-Archives"accesskey="4">Compression and Archives</a>:</td><td> </td><tdalign="left"valign="top">
<tr><tdalign="left"valign="top">•<ahref="#Invoking-R-from-the-command-line"accesskey="1">Invoking R from the command line</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Invoking-R-under-Windows"accesskey="2">Invoking R under Windows</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Invoking-R-under-OS-X"accesskey="3">Invoking R under OS X</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
<tr><tdalign="left"valign="top">•<ahref="#Scripting-with-R"accesskey="4">Scripting with R</a>:</td><td> </td><tdalign="left"valign="top">
</td></tr>
</table>
<hr>
<aname="Invoking-R-from-the-command-line"></a>
<divclass="header">
<p>
Next: <ahref="#Invoking-R-under-Windows"accesskey="n"rel="next">Invoking R under Windows</a>, Previous: <ahref="#Invoking-R"accesskey="p"rel="prev">Invoking R</a>, Up: <ahref="#Invoking-R"accesskey="u"rel="up">Invoking R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Invoking-R-from-the-command-line-1"></a>
<h3class="appendixsec">B.1 Invoking R from the command line</h3>
<p>When working at a command line on UNIX or Windows, the command ‘<samp>R</samp>’
can be used both for starting the main R program in the form
<p>for any other executable <code><var>cmd</var></code> on the path or given by an
absolute filepath: this is useful to have the same environment as R
or the specific commands run under, for example to run <code>ldd</code> or
<code>pdflatex</code>. Under Windows <var>cmd</var> can be an executable or a
batch file, or if it has extension <code>.sh</code> or <code>.pl</code> the
appropriate interpreter (if available) is called to run it.
</p>
<hr>
<aname="Invoking-R-under-Windows"></a>
<divclass="header">
<p>
Next: <ahref="#Invoking-R-under-OS-X"accesskey="n"rel="next">Invoking R under OS X</a>, Previous: <ahref="#Invoking-R-from-the-command-line"accesskey="p"rel="prev">Invoking R from the command line</a>, Up: <ahref="#Invoking-R"accesskey="u"rel="up">Invoking R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Invoking-R-under-Windows-1"></a>
<h3class="appendixsec">B.2 Invoking R under Windows</h3>
<p>There are two ways to run R under Windows. Within a terminal window
(e.g. <code>cmd.exe</code> or a more capable shell), the methods described in
the previous section may be used, invoking by <code>R.exe</code> or more
directly by <code>Rterm.exe</code>. For interactive use, there is a
console-based GUI (<code>Rgui.exe</code>).
</p>
<p>The startup procedure under Windows is very similar to that under
UNIX, but references to the ‘home directory’ need to be clarified, as
this is not always defined on Windows. If the environment variable
<code>R_USER</code> is defined, that gives the home directory. Next, if the
environment variable <code>HOME</code> is defined, that gives the home
directory. After those two user-controllable settings, R tries to
find system defined home directories. It first tries to use the
Windows "personal" directory (typically <code>C:\Documents and
Settings\username\My Documents</code> in Windows XP). If that fails, and
environment variables <code>HOMEDRIVE</code> and <code>HOMEPATH</code> are defined
(and they normally are) these define the home directory. Failing all
those, the home directory is taken to be the starting directory.
</p>
<p>You need to ensure that either the environment variables <code>TMPDIR</code>,
<code>TMP</code> and <code>TEMP</code> are either unset or one of them points to a
valid place to create temporary files and directories.
</p>
<p>Environment variables can be supplied as ‘<samp><var>name</var>=<var>value</var></samp>’
pairs on the command line.
</p>
<p>If there is an argument ending <samp>.RData</samp> (in any case) it is
interpreted as the path to the workspace to be restored: it implies
<samp>--restore</samp> and sets the working directory to the parent of the
named file. (This mechanism is used for drag-and-drop and file
association with <code>RGui.exe</code>, but also works for <code>Rterm.exe</code>.
If the named file does not exist it sets the working directory
if the parent directory exists.)
</p>
<p>The following additional command-line options are available when
invoking <code>RGui.exe</code>.
</p>
<dlcompact="compact">
<dt><samp>--mdi</samp></dt>
<dt><samp>--sdi</samp></dt>
<dt><samp>--no-mdi</samp></dt>
<dd><p>Control whether <code>Rgui</code> will operate as an MDI program
(with multiple child windows within one main window) or an SDI application
(with multiple top-level windows for the console, graphics and pager). The
command-line setting overrides the setting in the user’s <samp>Rconsole</samp> file.
</p>
</dd>
<dt><samp>--debug</samp></dt>
<dd><p>Enable the “Break to debugger” menu item in <code>Rgui</code>, and trigger
a break to the debugger during command line processing.
</p></dd>
</dl>
<p>Under Windows with <code>R CMD</code> you may also specify your own
<samp>.bat</samp>, <samp>.exe</samp>, <samp>.sh</samp> or <samp>.pl</samp> file. It will be run
under the appropriate interpreter (Perl for <samp>.pl</samp>) with several
environment variables set appropriately, including <code>R_HOME</code>,
<code>R_OSTYPE</code>, <code>PATH</code>, <code>BSTINPUTS</code> and <code>TEXINPUTS</code>. For
example, if you already have <samp>latex.exe</samp> on your path, then
</p>
<divclass="example">
<preclass="example">R CMD latex.exe mydoc
</pre></div>
<p>will run LaTeX on <samp>mydoc.tex</samp>, with the path to R’s
<samp>share/texmf</samp> macros appended to <code>TEXINPUTS</code>. (Unfortunately,
this does not help with the MiKTeX build of LaTeX, but
<code>R CMD texify mydoc</code> will work in that case.)
</p>
<hr>
<aname="Invoking-R-under-OS-X"></a>
<divclass="header">
<p>
Next: <ahref="#Scripting-with-R"accesskey="n"rel="next">Scripting with R</a>, Previous: <ahref="#Invoking-R-under-Windows"accesskey="p"rel="prev">Invoking R under Windows</a>, Up: <ahref="#Invoking-R"accesskey="u"rel="up">Invoking R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Invoking-R-under-OS-X-1"></a>
<h3class="appendixsec">B.3 Invoking R under OS X</h3>
<p>There are two ways to run R under OS X. Within a <code>Terminal.app</code>
window by invoking <code>R</code>, the methods described in the first
subsection apply. There is also console-based GUI (<code>R.app</code>) that by
default is installed in the <code>Applications</code> folder on your
system. It is a standard double-clickable OS X application.
</p>
<p>The startup procedure under OS X is very similar to that under UNIX, but
<code>R.app</code> does not make use of command-line arguments. The ‘home
directory’ is the one inside the R.framework, but the startup and
current working directory are set as the user’s home directory unless a
different startup directory is given in the Preferences window
accessible from within the GUI.
</p>
<hr>
<aname="Scripting-with-R"></a>
<divclass="header">
<p>
Previous: <ahref="#Invoking-R-under-OS-X"accesskey="p"rel="prev">Invoking R under OS X</a>, Up: <ahref="#Invoking-R"accesskey="u"rel="up">Invoking R</a> [<ahref="#SEC_Contents"title="Table of contents"rel="contents">Contents</a>][<ahref="#Function-and-variable-index"title="Index"rel="index">Index</a>]</p>
</div>
<aname="Scripting-with-R-1"></a>
<h3class="appendixsec">B.4 Scripting with R</h3>
<p>If you just want to run a file <samp>foo.R</samp> of R commands, the
recommended way is to use <code>R CMD BATCH foo.R</code>. If you want to
run this in the background or as a batch job use OS-specific facilities
to do so: for example in most shells on Unix-alike OSes <code>R CMD
BATCH foo.R &</code> runs a background job.
</p>
<p>You can pass parameters to scripts via additional arguments on the
command line: for example (where the exact quoting needed will depend on
the shell in use)
</p>
<divclass="example">
<preclass="example">R CMD BATCH "--args arg1 arg2" foo.R &
</pre></div>
<p>will pass arguments to a script which can be retrieved as a character
vector by
</p>
<divclass="example">
<preclass="example">args <- commandArgs(TRUE)
</pre></div>
<p>This is made simpler by the alternative front-end <code>Rscript</code>,
which can be invoked by
</p>
<divclass="example">
<preclass="example">Rscript foo.R arg1 arg2
</pre></div>
<p>and this can also be used to write executable script files like (at
least on Unix-alikes, and in some Windows shells)
</p>
<divclass="example">
<preclass="example">#! /path/to/Rscript
args <- commandArgs(TRUE)
...
q(status=<exit status code>)
</pre></div>
<p>If this is entered into a text file <samp>runfoo</samp> and this is made
executable (by <code>chmod 755 runfoo</code>), it can be invoked for
different arguments by
</p>
<divclass="example">
<preclass="example">runfoo arg1 arg2
</pre></div>
<p>For further options see <code>help("Rscript")</code>. This writes R
output to <samp>stdout</samp> and <samp>stderr</samp>, and this can be redirected in
the usual way for the shell running the command.
</p>
<p>If you do not wish to hardcode the path to <code>Rscript</code> but have it
in your path (which is normally the case for an installed R except on
Windows, but e.g. OS X users may need to add <samp>/usr/local/bin</samp>
to their path), use
</p>
<divclass="example">
<preclass="example">#! /usr/bin/env Rscript
...
</pre></div>
<p>At least in Bourne and bash shells, the <code>#!</code> mechanism does
<strong>not</strong> allow extra arguments like
<code>#! /usr/bin/env Rscript --vanilla</code>.
</p>
<p>One thing to consider is what <code>stdin()</code> refers to. It is
<tr><td></td><tdvalign="top"><ahref="#index-_0025o_0025"><code>%o%</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-outer-product-of-two-arrays">The outer product of two arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-_002eFirst"><code>.First</code></a>:</td><td> </td><tdvalign="top"><ahref="#Customizing-the-environment">Customizing the environment</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-_002eLast"><code>.Last</code></a>:</td><td> </td><tdvalign="top"><ahref="#Customizing-the-environment">Customizing the environment</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-_007e"><code>~</code></a>:</td><td> </td><tdvalign="top"><ahref="#Formulae-for-statistical-models">Formulae for statistical models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-anova"><code>anova</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-aov"><code>aov</code></a>:</td><td> </td><tdvalign="top"><ahref="#Analysis-of-variance-and-model-comparison">Analysis of variance and model comparison</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-aperm"><code>aperm</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generalized-transpose-of-an-array">Generalized transpose of an array</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-as_002edata_002eframe"><code>as.data.frame</code></a>:</td><td> </td><tdvalign="top"><ahref="#Making-data-frames">Making data frames</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-as_002evector"><code>as.vector</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-concatenation-function-c_0028_0029-with-arrays">The concatenation function c() with arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-attach"><code>attach</code></a>:</td><td> </td><tdvalign="top"><ahref="#attach_0028_0029-and-detach_0028_0029">attach() and detach()</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-attr"><code>attr</code></a>:</td><td> </td><tdvalign="top"><ahref="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-attr-1"><code>attr</code></a>:</td><td> </td><tdvalign="top"><ahref="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-attributes"><code>attributes</code></a>:</td><td> </td><tdvalign="top"><ahref="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-attributes-1"><code>attributes</code></a>:</td><td> </td><tdvalign="top"><ahref="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-boxplot"><code>boxplot</code></a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-c"><code>c</code></a>:</td><td> </td><tdvalign="top"><ahref="#Vectors-and-assignment">Vectors and assignment</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-c-2"><code>c</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-concatenation-function-c_0028_0029-with-arrays">The concatenation function c() with arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-coef"><code>coef</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-coefficients"><code>coefficients</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-cut"><code>cut</code></a>:</td><td> </td><tdvalign="top"><ahref="#Frequency-tables-from-factors">Frequency tables from factors</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-data_002eframe"><code>data.frame</code></a>:</td><td> </td><tdvalign="top"><ahref="#Making-data-frames">Making data frames</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-density"><code>density</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-det"><code>det</code></a>:</td><td> </td><tdvalign="top"><ahref="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-detach"><code>detach</code></a>:</td><td> </td><tdvalign="top"><ahref="#attach_0028_0029-and-detach_0028_0029">attach() and detach()</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-determinant"><code>determinant</code></a>:</td><td> </td><tdvalign="top"><ahref="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-deviance"><code>deviance</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-ecdf"><code>ecdf</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-eigen"><code>eigen</code></a>:</td><td> </td><tdvalign="top"><ahref="#Eigenvalues-and-eigenvectors">Eigenvalues and eigenvectors</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Error"><code>Error</code></a>:</td><td> </td><tdvalign="top"><ahref="#Analysis-of-variance-and-model-comparison">Analysis of variance and model comparison</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-fivenum"><code>fivenum</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-formula"><code>formula</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-function"><code>function</code></a>:</td><td> </td><tdvalign="top"><ahref="#Writing-your-own-functions">Writing your own functions</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-hist"><code>hist</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-identify"><code>identify</code></a>:</td><td> </td><tdvalign="top"><ahref="#Interacting-with-graphics">Interacting with graphics</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-ks_002etest"><code>ks.test</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-locator"><code>locator</code></a>:</td><td> </td><tdvalign="top"><ahref="#Interacting-with-graphics">Interacting with graphics</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-lsfit"><code>lsfit</code></a>:</td><td> </td><tdvalign="top"><ahref="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-nlm"><code>nlm</code></a>:</td><td> </td><tdvalign="top"><ahref="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-nlminb"><code>nlminb</code></a>:</td><td> </td><tdvalign="top"><ahref="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-optim"><code>optim</code></a>:</td><td> </td><tdvalign="top"><ahref="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-outer"><code>outer</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-outer-product-of-two-arrays">The outer product of two arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-plot"><code>plot</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-predict"><code>predict</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-print"><code>print</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-qqline"><code>qqline</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-qqnorm"><code>qqnorm</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-qr"><code>qr</code></a>:</td><td> </td><tdvalign="top"><ahref="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-resid"><code>resid</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-residuals"><code>residuals</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-rm"><code>rm</code></a>:</td><td> </td><tdvalign="top"><ahref="#Data-permanency-and-removing-objects">Data permanency and removing objects</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-sd"><code>sd</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-function-tapply_0028_0029-and-ragged-arrays">The function tapply() and ragged arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-search"><code>search</code></a>:</td><td> </td><tdvalign="top"><ahref="#Managing-the-search-path">Managing the search path</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-shapiro_002etest"><code>shapiro.test</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-sink"><code>sink</code></a>:</td><td> </td><tdvalign="top"><ahref="#Executing-commands-from-or-diverting-output-to-a-file">Executing commands from or diverting output to a file</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-solve"><code>solve</code></a>:</td><td> </td><tdvalign="top"><ahref="#Linear-equations-and-inversion">Linear equations and inversion</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-source"><code>source</code></a>:</td><td> </td><tdvalign="top"><ahref="#Executing-commands-from-or-diverting-output-to-a-file">Executing commands from or diverting output to a file</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-stem"><code>stem</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-step"><code>step</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-summary"><code>summary</code></a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-summary-1"><code>summary</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-svd"><code>svd</code></a>:</td><td> </td><tdvalign="top"><ahref="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-t"><code>t</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generalized-transpose-of-an-array">Generalized transpose of an array</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-t_002etest"><code>t.test</code></a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-table-1"><code>table</code></a>:</td><td> </td><tdvalign="top"><ahref="#Frequency-tables-from-factors">Frequency tables from factors</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-tapply"><code>tapply</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-function-tapply_0028_0029-and-ragged-arrays">The function tapply() and ragged arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-unclass"><code>unclass</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-class-of-an-object">The class of an object</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-var-1"><code>var</code></a>:</td><td> </td><tdvalign="top"><ahref="#The-function-tapply_0028_0029-and-ragged-arrays">The function tapply() and ragged arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-var_002etest"><code>var.test</code></a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-vcov"><code>vcov</code></a>:</td><td> </td><tdvalign="top"><ahref="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-vector"><code>vector</code></a>:</td><td> </td><tdvalign="top"><ahref="#Vectors-and-assignment">Vectors and assignment</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-wilcox_002etest"><code>wilcox.test</code></a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Analysis-of-variance">Analysis of variance</a>:</td><td> </td><tdvalign="top"><ahref="#Analysis-of-variance-and-model-comparison">Analysis of variance and model comparison</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Arithmetic-functions-and-operators">Arithmetic functions and operators</a>:</td><td> </td><tdvalign="top"><ahref="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Assignment">Assignment</a>:</td><td> </td><tdvalign="top"><ahref="#Vectors-and-assignment">Vectors and assignment</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Binary-operators">Binary operators</a>:</td><td> </td><tdvalign="top"><ahref="#Defining-new-binary-operators">Defining new binary operators</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Box-plots">Box plots</a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Classes">Classes</a>:</td><td> </td><tdvalign="top"><ahref="#The-class-of-an-object">The class of an object</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-CRAN">CRAN</a>:</td><td> </td><tdvalign="top"><ahref="#Contributed-packages-and-CRAN">Contributed packages and CRAN</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Customizing-the-environment">Customizing the environment</a>:</td><td> </td><tdvalign="top"><ahref="#Customizing-the-environment">Customizing the environment</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Default-values">Default values</a>:</td><td> </td><tdvalign="top"><ahref="#Named-arguments-and-defaults">Named arguments and defaults</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Density-estimation">Density estimation</a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Determinants">Determinants</a>:</td><td> </td><tdvalign="top"><ahref="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Diverting-input-and-output">Diverting input and output</a>:</td><td> </td><tdvalign="top"><ahref="#Executing-commands-from-or-diverting-output-to-a-file">Executing commands from or diverting output to a file</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Eigenvalues-and-eigenvectors">Eigenvalues and eigenvectors</a>:</td><td> </td><tdvalign="top"><ahref="#Eigenvalues-and-eigenvectors">Eigenvalues and eigenvectors</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Empirical-CDFs">Empirical CDFs</a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Formulae">Formulae</a>:</td><td> </td><tdvalign="top"><ahref="#Formulae-for-statistical-models">Formulae for statistical models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Generalized-linear-models">Generalized linear models</a>:</td><td> </td><tdvalign="top"><ahref="#Generalized-linear-models">Generalized linear models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Generalized-transpose-of-an-array">Generalized transpose of an array</a>:</td><td> </td><tdvalign="top"><ahref="#Generalized-transpose-of-an-array">Generalized transpose of an array</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Indexing-of-and-by-arrays">Indexing of and by arrays</a>:</td><td> </td><tdvalign="top"><ahref="#Array-indexing">Array indexing</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Kolmogorov_002dSmirnov-test">Kolmogorov-Smirnov test</a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Least-squares-fitting">Least squares fitting</a>:</td><td> </td><tdvalign="top"><ahref="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Linear-equations">Linear equations</a>:</td><td> </td><tdvalign="top"><ahref="#Linear-equations-and-inversion">Linear equations and inversion</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Loops-and-conditional-execution">Loops and conditional execution</a>:</td><td> </td><tdvalign="top"><ahref="#Loops-and-conditional-execution">Loops and conditional execution</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Named-arguments">Named arguments</a>:</td><td> </td><tdvalign="top"><ahref="#Named-arguments-and-defaults">Named arguments and defaults</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Nonlinear-least-squares">Nonlinear least squares</a>:</td><td> </td><tdvalign="top"><ahref="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-One_002d-and-two_002dsample-tests">One- and two-sample tests</a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Outer-products-of-arrays">Outer products of arrays</a>:</td><td> </td><tdvalign="top"><ahref="#The-outer-product-of-two-arrays">The outer product of two arrays</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Packages">Packages</a>:</td><td> </td><tdvalign="top"><ahref="#R-and-statistics">R and statistics</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-QR-decomposition">QR decomposition</a>:</td><td> </td><tdvalign="top"><ahref="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Quantile_002dquantile-plots">Quantile-quantile plots</a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Reading-data-from-files">Reading data from files</a>:</td><td> </td><tdvalign="top"><ahref="#Reading-data-from-files">Reading data from files</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Search-path">Search path</a>:</td><td> </td><tdvalign="top"><ahref="#Managing-the-search-path">Managing the search path</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Shapiro_002dWilk-test">Shapiro-Wilk test</a>:</td><td> </td><tdvalign="top"><ahref="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Singular-value-decomposition">Singular value decomposition</a>:</td><td> </td><tdvalign="top"><ahref="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Statistical-models">Statistical models</a>:</td><td> </td><tdvalign="top"><ahref="#Statistical-models-in-R">Statistical models in R</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Student_0027s-t-test">Student’s <em>t</em> test</a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Tabulation">Tabulation</a>:</td><td> </td><tdvalign="top"><ahref="#Frequency-tables-from-factors">Frequency tables from factors</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Vectors">Vectors</a>:</td><td> </td><tdvalign="top"><ahref="#Simple-manipulations-numbers-and-vectors">Simple manipulations numbers and vectors</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Wilcoxon-test">Wilcoxon test</a>:</td><td> </td><tdvalign="top"><ahref="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Workspace">Workspace</a>:</td><td> </td><tdvalign="top"><ahref="#Data-permanency-and-removing-objects">Data permanency and removing objects</a></td></tr>
<tr><td></td><tdvalign="top"><ahref="#index-Writing-functions">Writing functions</a>:</td><td> </td><tdvalign="top"><ahref="#Writing-your-own-functions">Writing your own functions</a></td></tr>
<tr><tdcolspan="4"><hr></td></tr>
</table>
<tablesummary=""><tr><thvalign="top">Jump to: </th><td><aclass="summary-letter"href="#Concept-index_cp_letter-A"><b>A</b></a>