Using R -- Revised January 2011
New in January 2011!
- The alr3 package contains all the data used in the book.
- Most of the functions in older versions of alr3 have been renamed, rewritten, improved, and moved to a different package called car. When you load alr3, car is loaded as well, so all the functions will be available. You don't really need to know which functions are in which package.
- A book published in 2011 called An R Companion to Applied Regression provides many details about the car package, including the functions that were previously part of the alr3 package. However, the primer available on this website and the on-line documentation for the functions will be adequate for many users.
The alr3 package for R
If you are running R, connected to the internet, and have write privileges on your computer's disk, you can install alr3 by typing the following command in the R command window:
> install.packages("alr3", dependencies=TRUE)
Setting dependencies=TRUE
causes car and other packages needed to be downloaded and installed.
You may be prompted to select a location for the download; pick a site close
to your home. After this, the package should install itself. You do not need
to install again unless you upgrade to a newer version of R.
To use the alr3 package you simply enter the library command in R:
> library(alr3)
All other needed packages, including car, are loaded automatically.
The R primer and the alrWeb function
The R primer has been updated to correspond to the new versions of alr3 and car. If you are connected to the internet when using R, the easiest way to get the R primer is to start R, and then enter the following two commands:
> library(alr3)
> alrWeb("primer")
If you prefer, you can download the R primer from here.
The command alrWeb()
opens your browser at this website. The command alrWeb("errata")
opens the Errata sheet for the book.
Chapter and Primer R Scripts
Scripts of R commands that produce nearly all the computations in the book and all the computations discussed in the R primer can be obtained with a command like
> alrWeb(script="chapter1"))
The value of script
can be any of chapter1
,
chapter2
, ... ,
chapter12
,
appendix
,
for the 12 chapters and appendix in the textbook, or primer0
, primer1
primer12
, ... , primer12
, for the corresponding chapters in the R primer including the introductory Chatper 0, or primerappendix
for the appendix to the R primer.
If you prefer,
- Download all the scripts from the book as a zip file
- Download all the scripts from the R primer as a zip file
New Stuff in alr3
- You no longer need to use the
data
function to access a data set from the alr3 package; all data sets are accessible when you use thelibrary
command to load alr3. - The data files are unchanged, except that occasionally row names have been added. The
forbes
data now masks a file of the same name in the MASS package, with slightly different variable names but the same data. Thelongley
data file has been removed, as a file of the same name with the same data is part of the standard R distribution. -
Most of the functions that were previously part of alr3 have been moved to the car package, and almost all of these functions have
different names for consistency within car. Most of the new
functions have the same arguments as the old ones. A notable exception is
the
powerTransform
function which replaces the oldbcpower
function and has completely different arguments; seehelp("powerTransform")
. To get a listing of the functions that have moved, enterhelp("alr3-deprecated")
.
R resources
From www.r-project.org, the home for R, you can get a wealth of information about R, including books and other documentation, access to CRAN (pronounced "see-ran"), which includes the program and many add-on packages, and other interesting stuff. The help files available from the R Help menu are also useful.