Chapter 1
Introduction
1.1 Obtaining and installing R
You can download and install R from the CRAN (Comprehensive R Archive Network) website at http://cran.r-project.org/. Choose the appropriate link for your operating system (Mac OS X, Windows, or Linux), and follow the (not very complicated) directions. Unless you have some special requirements for customization, you should choose the precompiled binary rather than the source code.
As it comes, R has a plain but serviceable interface. It can be run from the command line or from a set of windows (console, graphics, help, etc.) on MacOS X or Windows. A neater, more streamlinedâbut perhaps less flexibleâintegrated development interface can be had by installing the freeware RStudio from http://www.rstudio.org//ide.
1.2 Learning R
The next several chapters of this book are intended to provide a basic introduction to R. The basic manual for learning R is the online An Introduction to R, found at http://cran.r-project.org/ â Documentation. The section Learning more about R at the end of this chapter lists numerous books and online resources.
1.3 Learning numerical methods
This book tries to lightly sketch the basic ideas of the various numerical methods used, but does not attempt to present their theoretical background or details. Currently the standard reference on numerical methods in science and engineering is Numerical Recipes by Press et al. (2007), and there are many other worthwhile books devoted to the field and the various topics within it. Readers are encouraged to consult such references, and/or have recourse to various online sources. A Google search on a given topic will typically lead to a useful Wikipedia article, several sets of university-level lecture notes, and often treatments based on MATLABÂŽ or Mathematica. Such online resources may be much more accessible than standard printed references, especially for readers without convenient access to specialized research library collections.
1.4 Finding help
If you know the name of an R object, such as a function, and want to know what it does, what its various arguments (including defaults) are, and what values it returns, with examples, typehelp (function.name) or ?function.name. For example, ?solve tells us that âThis generic function solves the equation a%*% x = b for x, where b can be either a vector or a matrix.â As one example, it gives inversion of a Hilbert matrix:
hilbert <- function(n) {i <- 1:n; 1 / outer(i - 1, i, "+")} h8 <- hilbert(8); h8 sh8 <- solve(h8) round(sh8 %*% h8, 3) (Donât worry if you donât understand the code at this time. We will discuss R programming beginning in Chapter 4.)
Often, you may need to be reminded of the name of a function. A very useful âcheat sheetâ listing many of the more common R functions is âR Reference Cardâ by Tom Short, available at http://cran.r-project.org/doc/contrib/Short-refcard.pdf.
If you think that an object or function may be available, and can guess part of its name, try apropos(). For example, if youâre interested in spectral analysis, apropos(spec) gives
[1] "plot.spec" "plot.spec.coherency" "plot.spec.phase" "spec.ar" [5] "spec.pgramââ "spec.taper" "spectrum"
However, this does not turn up Special, which yields special mathematical functions related to the beta and gamma functions. apropos() allows searches using regular expressions; enter ?apropos to see some examples.
help.search() âallows for searching the help system for documentation matching a given character string in the (file) name, alias, title, concept or keyword entries (or any combination thereof), using either fuzzy matching or regular expression matching.â Note that the character string must be in quotes. For example, help.search("spectral") turns up five topics, with descriptions:
eigen: Spectral Decomposition of a Matrix
plot.spec: Plotting Spectral Densities
spec.ar: Estimate Spectral Density of a Time Series from AR Fit
spec.pgram: Estimate Spectral Density of a Time Series by a Smoothed Periodogram
spectrum: Spectral Density Estimation
Clicking on any of these topics brings up its help page.
Using regular expressions, help.search(^spec) brings up those help pages containing information about topics whose title, alias, or concept contain words that begin with âspecâ: Special, specific, spectral, specification, etc.
help.start() opens your web browser to Râs online documentation. The manual âAn Introduction to Râ is the standard online reference to the language. Click on âSearch Engine & Keywordsâ to search for keywords, function and data names, and concepts, and also for words or phrases within help-page titles. A list of keywords arranged by topics (Basics; Graphics; MASS (the book); Mathematics; Programming, Input/Output, and Miscellaneous; and Statistics) is provided to help target the search.
R has a large and helpful online community, of which you can ask questions if you canât find answers through your own efforts. A very large database (nearly 2700 pages as of the end of 2013) of topics is maintained at http://r.789695.n4.nabble.com/. Searching this database can provide leads to existing resources, or show how others have solved puzzling problems.
Two sites for doing Google-type searching of the R language are http://www.dangoldstein.com/search_r.html and http://www.rseek.org/.
If all else fails, you can ask your own questions by going to http://www.r-project.org/ > Mailing Lists. The third item down is R-help. (The first two are R-announce, âfor major announcements about the development of R and the availability of new codeâ and R-packages, âfor announcements ... on the availability of new or enhanced contributed packages.â) The posting guide gives important advice about âhow to ask good questions that prompt useful answers.â Follow that advice to avoid grumpy responses from the experts.
A somewhat haphazard but occasionally enlightening way to learn about various aspects of R is to look at R-bloggers (http://www.r-bloggers.com/), which collects âdaily news and tutorials about R, contributed by over 450 bloggers.â
1.5 Augmenting R with packages
R is to a large extent an environment for packages that perform specialized tasks. The R distribution itself installs a number of packages, some of which are âjust thereâ and need not be loaded explicitly. These include base, graphics, stats, utils, splines, datasets, and several others. Some other packages are ârecommended,â and are included in all binary distributions of R. Most pertinent for our purposes among these are Matrix (which we will discuss in Chapters 2 and 5), cluster (functions for cluster analysis), and nlme (for nonlinear mixed-effects models). These must be loaded with the library("package-name") or require("package-name") function. (library and require can generally be used interchangeably, although require is intended for use within other functions and the two will give different messages if the package is not available. See ?library for details of their usage.)
Much of the real power of R comes from contributed packages (over 5000 as of the end of 2013) that can be downloaded from CRAN and installed in your local copy of R using the command install.packages("package-name"). (Mac OS X and Windows users can also install packages via the R menu system.) The packages can then be loaded with require(package-name) or library(package-name). Packages are in many ways analogous to the add-ons for other mathematical languages, but are generally free and open source. We will describe and use a number of such packages in this book, including packages for ordinary and partial differential equations, orthogonal polynomials, root-finding, optimization, and more.
A package may contain datasets, functions written in R, and dynamically loaded libraries of C or Fortran code. To find what packages are currently installed in R on your computer, type library(). The datasets in some packages can be of use as examples in learning about statistical analysis of data, as we will do in Chapter 10. To get summary help about a package you have installed in R, type library(help = "package.name") or help(package = "package.name"). Navigating to individual packages in the CRAN archive will give access to their reference manuals and (sometimes) vignettes, as downloadable pdf files.
It is often difficult to find a particular function in R, if itâs been implemented in one of the many packages. The Task Views page, accessible from the CRAN home page, groups packages according to the tasks that they help to facilitate. For example, the ChemPhys task view refers to packages useful in chemometrics and chemical physics that carry out such tasks as linear and nonlinear regression models, curve resolution, differential equations, optimization, cellular automata, etc. The NumericalMathematics, DifferentialEquations, Optimization, and TimeSeries task views are particularly pertinent to the material in this book.
Perhaps the best resource for searching the help pages of contributed packages to find particular functions is the findFn function in the sos package. Its documentation states âThe sos package provides a means to quickly and flexibly search the help pages of contributed packages, finding functions and datasets in seconds or minutes that could not be found in hours or days by any other means we know.â
The R community site http://www.inside-r.org/ enables you to search for the packages that contain the keyword(s) of interest, and then to browse the help files of those packages. A similar function is served by the community site crantastic! (http://crantastic.org/), which also provides information about new and upgraded packages, and allows reviews by users.
RSiteSearch("keyword") at the R prompt opens a web-based interface to search functions, contributed packages, and R-help postings. For example, typing RSiteSearch("orthogonal polynomials") yielded 194 documents matching the query within function, package vignette, and task view targets.
If you rely on certain packages, and want to check whether theyâve been updated, a bit of code written by Karthik Ram will give you a list of changes.
installed = installed.packages() available = available.packages() ia = merge(installed, available, by="Package") [,c ("Package", "Version.x", "Version.y")] updates = ia[as.character(ia$Version.x) != as.character(ia$Version.y),] updates To install every available update, enter update.packages.
One must be cautious when using contributed packages, however, since they are generally less broadly usedâhe...