Using the R Environment for Statistical Computing An example with the Mercer & Hall wheat yield dataset
Contents
1 Introduction 1
2 R basics 1
2.1 Leaving R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Loading and examining a data set 7
3.1 Reading a CSV file into an R object . . . . . . . . . . . . . . . 7
3.2 Examining a dataset . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 Saving a dataset in R format . . . . . . . . . . . . . . . . . . . 11
3.4 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Exploratory graphics 12
4.1 Univariate exploratory graphics . . . . . . . . . . . . . . . . . . 13
4.1.1 Enhancing the histogram* . . . . . . . . . . . . . . . . . 14
4.1.2 Kernel density* . . . . . . . . . . . . . . . . . . . . . . . 15
4.1.3 Another histogram enhancement: colour-coding rela-
tive frequency* . . . . . . . . . . . . . . . . . . . . . . . 17
4.2 Bivariate exploratory graphics . . . . . . . . . . . . . . . . . . . 18
4.3 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5 Descriptive statistics 22
5.1 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
6 Editing a data frame 25
6.1 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7 Introduction to modelling 27
8 Univariate modelling 27
8.1 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
9 Bivariate modelling: continuous variables 33
9.1 Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
9.2 Univariate linear regression . . . . . . . . . . . . . . . . . . . . 40
9.2.1 Fitting a regression line . . . . . . . . . . . . . . . . . . 41
9.2.2 Regression diagnostics . . . . . . . . . . . . . . . . . . . 44
9.3 Structural Analysis* . . . . . . . . . . . . . . . . . . . . . . . . 49
9.4 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
10 Bivariate modelling: continuous vs. classified variables 57
10.1 Exploratory data analysis . . . . . . . . . . . . . . . . . . . . . 59
10.2 Two-sample t-test . . . . . . . . . . . . . . . . . . . . . . . . . . 61
10.3 One-way ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . 62
10.4 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
11 Multivariate modelling 64
11.1 Additive model: parallel regression . . . . . . . . . . . . . . . . 66
11.2 Comparing models . . . . . . . . . . . . . . . . . . . . . . . . . 67
11.3 Interaction model . . . . . . . . . . . . . . . . . . . . . . . . . . 69
11.4 Regression diagnostics . . . . . . . . . . . . . . . . . . . . . . . 71
11.5 Analysis of covariance: a nested model . . . . . . . . . . . . . . 75
11.6 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
12 Spatial analysis 79
12.1 Geographic visualisation . . . . . . . . . . . . . . . . . . . . . . 79
12.2 Setting up a co¨ ordinate system . . . . . . . . . . . . . . . . . . 83
12.3 Loading add-in packages . . . . . . . . . . . . . . . . . . . . . . 84
12.4 Creating a spatially-explicit object . . . . . . . . . . . . . . . . 85
12.5 More geographic visualisation . . . . . . . . . . . . . . . . . . . 86
12.6 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
13 Spatial structure 88
13.1 Spatial structure: trend . . . . . . . . . . . . . . . . . . . . . . . 88
13.2 Spatial structure: local . . . . . . . . . . . . . . . . . . . . . . . 91
13.3 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
14 Spatial structure of field halves 94
14.1 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
15 The effect of plot size 97
15.1 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
16 Wrapup 108
References 109
Index of R concepts 112
A Example Data Set 114
B Colours 115