Using R for Numerical Analysis in Science and Engineering
eBook - ePub

Using R for Numerical Analysis in Science and Engineering

Victor A. Bloomfield

Share book
  1. 359 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Using R for Numerical Analysis in Science and Engineering

Victor A. Bloomfield

Book details
Book preview
Table of contents
Citations

About This Book

Instead of presenting the standard theoretical treatments that underlie the various numerical methods used by scientists and engineers, Using R for Numerical Analysis in Science and Engineering shows how to use R and its add-on packages to obtain numerical solutions to the complex mathematical problems commonly faced by scientists and engineers. This practical guide to the capabilities of R demonstrates Monte Carlo, stochastic, deterministic, and other numerical methods through an abundance of worked examples and code, covering the solution of systems of linear algebraic equations and nonlinear equations as well as ordinary differential equations and partial differential equations. It not only shows how to use R's powerful graphic tools to construct the types of plots most useful in scientific and engineering work, but also:



  • Explains how to statistically analyze and fit data to linear and nonlinear models
  • Explores numerical differentiation, integration, and optimization
  • Describes how to find eigenvalues and eigenfunctions
  • Discusses interpolation and curve fitting
  • Considers the analysis of time series

Using R for Numerical Analysis in Science and Engineering provides a solid introduction to the most useful numerical methods for scientific and engineering data analysis using R.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Using R for Numerical Analysis in Science and Engineering an online PDF/ePUB?
Yes, you can access Using R for Numerical Analysis in Science and Engineering by Victor A. Bloomfield in PDF and/or ePUB format, as well as other popular books in Matemáticas & Matemáticas general. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781315360492

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...

Table of contents