Intro-to-R_2006-34.pdf

(4395 KB) Pobierz
CSIRO Mathematical and Information Sciences
An Introduction to R:
Software for Statistical Modelling & Computing
Course Materials and Exercises
Petra Kuhnert and Bill Venables
CSIRO Mathematical and Information Sciences
Cleveland, Australia
c CSIRO Australia, 2005
c CSIRO Australia 2005
All rights are reserved. Permission to reproduce individual copies of this document for
personal use is granted. Redistribution in any other form is prohibited.
The information contained in this document is based on a number of technical, circum-
stantial or otherwise specified assumptions and parameters. The user must make its own
analysis and assessment of the suitability of the information or material contained in or
generated from the use of the document. To the extent permitted by law. CSIRO excludes
all liability to any party for any expenses, losses, damages and costs arising directly or
indirectly from using this document.
Contents
An Elementary Introduction to R
11
Whirlwind Tour of R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Example 1: The Whiteside Insulation Data . . . . . . . . . . . . . . . . . . . . 13
Example 2: Cars and Fuel Economy . . . . . . . . . . . . . . . . . . . . . . . 15
Example 3: Images of Volcanic Activity . . . . . . . . . . . . . . . . . . . . . 19
Example 4: Coastline and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . 19
R and the Tinn-R Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
R and the Tinn-R Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Obtaining R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
R Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
R Reference Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
How Does R Work and How Do I Work with it? . . . . . . . . . . . . . . . . 23
Installing and Loading R Packages . . . . . . . . . . . . . . . . . . . . . . . . 25
Customisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The
Rprofile.site
file: An Example . . . . . . . . . . . . . . . . . . . . . 27
What Editor can I use with R . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The Tinn-R Editor: A Demonstration . . . . . . . . . . . . . . . . . . . . . . . 28
The R Language: Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
The R Language: Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The R Language: Missing, Indefinite and Infinite Values . . . . . . . . . . . . 37
Distributions and Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
R Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3
4
Data Objects in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Creating Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Creating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Manipulating Data: An Example . . . . . . . . . . . . . . . . . . . . . . . . . 51
Accessing Elements of a Vector or Matrix . . . . . . . . . . . . . . . . . . . . 53
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Example: Cars93 Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Graphics: An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Anatomy of a Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Overview of Graphics Functions . . . . . . . . . . . . . . . . . . . . . . . . . 64
Displaying Univariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Working with Time Series Objects . . . . . . . . . . . . . . . . . . . . . . . . . 76
Displaying Bivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Labelling and Documenting Plots . . . . . . . . . . . . . . . . . . . . . . . . . 85
Displaying Higher Dimensional Data . . . . . . . . . . . . . . . . . . . . . . 86
Manipulating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
with, subset
and
transform
Functions . . . . . . . . . . . . . . . . . . . . 102
Vectorised Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Classical Linear Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Statistical Models in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Model Formulae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Generic Functions for Inference . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Example: The Janka Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Example: Iowa Wheat Yield Data . . . . . . . . . . . . . . . . . . . . . . . . . 117
A Flexible Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
c CSIRO Australia, 2005
Course Materials and Exercises
5
Example: Petroleum Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Non-Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Example: Stormer Viscometer Data . . . . . . . . . . . . . . . . . . . . . . . . 129
Overview and Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Fitting the Model and Looking at the Results . . . . . . . . . . . . . . . . . . 130
Self Starting Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Example: Muscle Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Final Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Generalized Linear Modelling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Example: Budworm Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Example: Low Birth Weight . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
GLM Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
The Negative Binomial Distribution . . . . . . . . . . . . . . . . . . . . . . . 157
Multinomial Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Example: Copenhagen Housing Data . . . . . . . . . . . . . . . . . . . . . . 163
Proportional Odds Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Generalized Additive Models: An Introduction . . . . . . . . . . . . . . . . . . . . 169
Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Example: The Iowa Wheat Yield Data . . . . . . . . . . . . . . . . . . . . . . 170
Example: Rock Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Advanced Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Lattice Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Example: Whiteside Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Changing Trellis Parameters & Adding Keys . . . . . . . . . . . . . . . . . . 183
Example: Stormer Viscometer Data . . . . . . . . . . . . . . . . . . . . . . . . 185
Adding Fitted Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Output Over Several Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Example: Volcanos in New Zealand . . . . . . . . . . . . . . . . . . . . . . . 191
c CSIRO Australia, 2005
Course Materials and Exercises
Zgłoś jeśli naruszono regulamin