Use R!
Series Editors:
Robert Gentleman Kurt Hornik Giovanni Parmigiani
Use R!
Albert: Bayesian Computation with R
Bivand/Pebesma/Gómez-Rubio: Applied Spatial Data Analysis with R
Cook/Swayne: Interactive and Dynamic Graphics for Data Analysis:
With R and GGobi
Hahne/Huber/Gentleman/Falcon: Bioconductor Case Studies
Paradis: Analysis of Phylogenetics and Evolution with R
Pfaff: Analysis of Integrated and Cointegrated Time Series with R
Ritz/Streibig: Nonlinear Regression with R
Sarkar: Lattice: Multivariate Data Visualization with R
Spector: Data Manipulation with R
Christian Ritz Jens Carl Streibig
Nonlinear Regression with R
123
Jens Carl Streibig
Department of Agriculture and Ecology
(Crop Science)
Faculty of Life Sciences
University of Copenhagen
Hoejbakkegaard Allé 13
DK-2630 Taastrup
Denmark
jcs@life.ku.dk
Kurt Hornik
Department für Statistik und Mathematik
Wirtschaftsuniversität Wien Augasse 2-6
A-1090 Wien
Austria
Christian Ritz
Department of Basic Sciences
and Environment (Statistics)
Faculty of Life Sciences
University of Copenhagen
Thorvaldsensvej 40
DK-1871 Frederiksberg C
Denmark
ritz@life.ku.dk
Series Editors:
Robert Gentleman
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
Seattle, Washington 98109-1024
USA
Giovanni Parmigiani
The Sidney Kimmel Comprehensive Cancer
Center at Johns Hopkins University
550 North Broadway
Baltimore, MD 21205-2011
USA
ISBN: 978-0-387-09615-5
DOI: 10.1007/978-0-387-09616-2
e-ISBN: 978-0-387-09616-2
2008
Springer Science+Business Media, LLC
Library of Congress Control Number: 2008938643
c
All rights reserved. This work may not be translated or copied in whole or in part without the written
permission of the publisher (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY
10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection
with any form of information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed is forbidden.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject
to proprietary rights.
Printed on acid-free paper
springer.com
To Ydun Marie Ritz and in memory of Erik Ritz
Preface
This book is about nonlinear regression analysis with R, in particular, how
to use the function nls() and related functions and methods.
Range of the book
Nonlinear regression may be a confined and narrow topic within statistics.
However, the use of nonlinear regression is seen in many applied sciences,
ranging from biology to engineering to medicine and pharmacology. Therefore,
this book covers a wide range of areas in the examples used. Appendix A lists
the disciplines from which data are used in this book.
What not to expect
This book is not a textbook on nonlinear regression. Basic concepts will be
briefly introduced, but the reader in need of more explanations will have to
consult comprehensive books on nonlinear regression such as Bates and Watts
(1988) or Seber and Wild (1989). Instead, this book may be particularly well-
suited as an accompanying text, explaining in detail how to carry out nonlinear
regression with R. However, we also believe that the book is useful as a stand-
alone, self-study text for the experimenter or researcher who already has some
experience with R and at the same time is familiar with linear regression and
related basic statistical concepts.
Prerequisites
Experience with R at a level corresponding to the first few chapters in Dal-
gaard (2002) should be sufficient: The user should be acquainted with the
basic objects in R such as vectors, data frames, and lists, as well as basic
plotting and statistics functions for making scatter plots, calculating descrip-
tive statistics, and doing linear regression.
VIII
Preface
How to read the book
Chapter 2 is essential for getting started on using nls(). Section 3.2 and
Chapter 4 are at times somewhat technical regarding the use of R, whereas
Section 6.3 is technical on a statistical level. These parts of the book could be
skipped on a first reading.
The R extension package nlrwr is support software for this book, and it
is available at CRAN: http://cran.r-project.org/web/packages/nlrwr/index.html.
All datasets and functions used are available upon loading nlrwr. All code
snippets used in the book are also found in the scripts folder that comes
with the package. This means that all R code snippets shown in this book
can be run once the support package nlrwr has been installed and loaded.
Appendix A provides a list of all datasets used, with a reference to the package
where they are found, and Appendix C lists the main functions used in this
book together with a package reference.
Acknowledgments
We would like to thank Claire della Vedova and Christian Pipper for proof-
reading parts of the book. The first author also wishes to thank the partici-
pants of the short course Non-linear regression with R, held at the Faculty of
Life Sciences, University of Copenhagen, in September 2007, for useful com-
ments and suggestions. We are also grateful to Spencer Graves for his valuable
comments on an almost final version of the book. All remaining breaches or
errors rest solely on the authors.
This volume has benefitted vastly from the many comments and sugges-
tions from the anonymous reviewers of earlier versions. We are also thankful
to John Kimmel for his encouragement and guidance throughout this book
project.
Finally, we would like to thank the R Core Development Team for making
all this happen by developing a great open source project. The book has been
written using LATEX 2εand Sweave, yet another powerful invention in the wake
of the R project.
Copenhagen
July 2008
Christian Ritz
Jens C. Streibig
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . VII
1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1 A stock-recruitment model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Competition between plant biotypes . . . . . . . . . . . . . . . . . . . . . . .
1.3 Grouped dose-response data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
2
3
4
2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.2 Getting started with nls() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
2.2.1 Introducing the data example . . . . . . . . . . . . . . . . . . . . . . .
9
2.2.2 Model fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.2.3 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.4 Making plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.5 Illustrating the estimation . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 Generalised linear models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3
Starting Values and Self-starters . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1 Finding starting values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1.1 Graphical exploration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1.2 Searching a grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2 Using self-starter functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.1 Built-in self-starter functions for nls() . . . . . . . . . . . . . . . 30
3.2.2 Defining a self-starter function for nls() . . . . . . . . . . . . . 31
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4 More on nls() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.1 Arguments and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2 Supplying gradient information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.2.1 Manual supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39