Using R for Numerical Analysis in Science and Engineering Statistics the R Series
Total Page:16
File Type:pdf, Size:1020Kb
Using R for Numerical Analysis in Science and Engineering Using R for Numerical Analysis Statistics The R Series Instead of presenting the standard theoretical treatments that under- lie the various numerical methods used by scientists and engineers, Using R for Numerical Using R for Numerical Analysis in Science and Engineering shows how to use R and its add-on packages to obtain numerical Analysis in Science 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 and Engineering 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 power- ful graphic tools to construct the types of plots most useful in scien- tific 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 pro- vides a solid introduction to the most useful numerical methods for scientific and engineering data analysis using R. Bloomfield Victor A. Bloomfield K13976 K13976_Cover.indd 1 3/18/14 12:29 PM Using R for Numerical Analysis in Science and Engineering Victor A. Bloomfield University of Minnesota Minneapolis, USA K13976_FM.indd 1 3/24/14 11:19 AM Chapman & Hall/CRC The R Series Series Editors John M. Chambers Torsten Hothorn Department of Statistics Division of Biostatistics Stanford University University of Zurich Stanford, California, USA Switzerland Duncan Temple Lang Hadley Wickham Department of Statistics RStudio University of California, Davis Boston, Massachusetts, USA Davis, California, USA Aims and Scope This book series reflects the recent rapid growth in the development and application of R, the programming language and software environment for statistical computing and graphics. R is now widely used in academic research, education, and industry. It is constantly growing, with new versions of the core software released regularly and more than 5,000 packages available. It is difficult for the documentation to keep pace with the expansion of the software, and this vital book series provides a forum for the publication of books covering many aspects of the development and application of R. The scope of the series is wide, covering three main threads: • Applications of R to specific disciplines such as biology, epidemiology, genetics, engineering, finance, and the social sciences. • Using R for the study of topics of statistical methodology, such as linear and mixed modeling, time series, Bayesian methods, and missing data. • The development of R, including programming, building packages, and graphics. The books will appeal to programmers and developers of R software, as well as applied statisticians and data analysts in many fields. The books will feature detailed worked examples and R code fully integrated into the text, ensuring their usefulness to researchers, practitioners and students. K13976_FM.indd 2 3/24/14 11:19 AM Published Titles Using R for Numerical Analysis in Science and Engineering , Victor A. Bloomfield Event History Analysis with R, Göran Broström Computational Actuarial Science with R, Arthur Charpentier Statistical Computing in C++ and R, Randall L. Eubank and Ana Kupresanin Reproducible Research with R and RStudio, Christopher Gandrud Displaying Time Series, Spatial, and Space-Time Data with R, Oscar Perpiñán Lamigueiro Programming Graphical User Interfaces with R, Michael F. Lawrence and John Verzani Analyzing Baseball Data with R, Max Marchi and Jim Albert Growth Curve Analysis and Visualization Using R, Daniel Mirman R Graphics, Second Edition, Paul Murrell Customer and Business Analytics: Applied Data Mining for Business Decision Making Using R, Daniel S. Putler and Robert E. Krider Implementing Reproducible Research, Victoria Stodden, Friedrich Leisch, and Roger D. Peng Dynamic Documents with R and knitr, Yihui Xie K13976_FM.indd 3 3/24/14 11:19 AM MATLAB® is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® software or related products does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or particular use of the MATLAB® software. CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2014 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Version Date: 20140220 International Standard Book Number-13: 978-1-4398-8449-2 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, includ- ing photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents List of Figures xiii Preface xix 1 Introduction 1 1.1 Obtaining and installing R 1 1.2 Learning R 1 1.3 Learning numerical methods 1 1.4 Finding help 2 1.5 Augmenting R with packages 3 1.6 Learning more about R 5 1.6.1 Books 5 1.6.2 Online resources 5 2 Calculating 7 2.1 Basic operators and functions 7 2.2 Complex numbers 8 2.3 Numerical display, round-off error, and rounding 9 2.4 Assigning variables 11 2.4.1 Listing and removing variables 12 2.5 Relational operators 12 2.6 Vectors 13 2.6.1 Vector elements and indexes 13 2.6.2 Operations with vectors 14 2.6.3 Generating sequences 15 2.6.3.1 Regular sequences 15 2.6.3.2 Repeating values 16 2.6.3.3 Sequences of random numbers 16 2.6.4 Logical vectors 17 2.6.5 Speed of forming large vectors 18 2.6.6 Vector dot product and crossproduct 19 2.7 Matrices 21 2.7.1 Forming matrices 21 2.7.2 Operations on matrices 24 2.7.2.1 Arithmetic operations on matrices 24 2.7.2.2 Matrix multiplication 25 v vi CONTENTS 2.7.2.3 Transpose and determinant 26 2.7.2.4 Matrix crossproduct 26 2.7.2.5 Matrix exponential 27 2.7.2.6 Matrix inverse and solve 27 2.7.2.7 Eigenvalues and eigenvectors 29 2.7.2.8 Singular value decomposition 31 2.7.3 The Matrix package 33 2.7.4 Additional matrix functions and packages 34 2.8 Time and date calculations 34 3 Graphing 37 3.1 Scatter plots 37 3.2 Function plots 39 3.3 Other common plots 40 3.3.1 Bar charts 40 3.3.2 Histograms 42 3.3.3 Box-and-whisker plots 43 3.4 Customizing plots 44 3.4.1 Points and lines 44 3.4.2 Axes, ticks, and par() 44 3.4.3 Overlaying plots with graphic elements 46 3.5 Error bars 48 3.6 Superimposing vectors in a plot 49 3.7 Modifying axes 50 3.7.1 Logarithmic axes 51 3.7.2 Supplementary axes 51 3.7.3 Incomplete axis boxes 52 3.7.4 Broken axes 52 3.8 Adding text and math expressions 54 3.8.1 Making math annotations with expression() 55 3.9 Placing several plots in a figure 56 3.10 Two- and three-dimensional plots 58 3.11 The plotrix package 60 3.11.1 radial.plot and polar.plot 60 3.11.2 Triangle plot 61 3.11.3 Error bars in plotrix 62 3.12 Animation 63 3.13 Additional plotting packages 64 4 Programming and functions 65 4.1 Conditional execution: if and ifelse 65 4.2 Loops 66 4.2.1 for loop 66 4.2.2 Looping with while and repeat 68 4.3 User-defined functions 69 CONTENTS vii 4.4 Debugging 72 4.5 Built-in mathematical functions 73 4.5.1 Bessel functions 73 4.5.2 Beta and gamma functions 74 4.5.3 Binomial coefficients 75 4.6 Special functions of mathematical physics 75 4.6.1 The gsl package 75 4.6.2 Special functions in other packages 75 4.7 Polynomial functions in packages 78 4.7.1 PolynomF package 79 4.7.2 orthopolynom package 83 4.8 Case studies 86 4.8.1 Two-dimensional random walk 86 4.8.2 Eigenvalues