Preface |
|
xiii | |
|
|
xiv | |
|
Obtaining and Installing R and RStudio |
|
|
xvi | |
|
Installing R on a Windows System |
|
|
xvii | |
|
Installing R on a macOS System |
|
|
xvii | |
|
|
xviii | |
|
Installing and Using R Packages |
|
|
xx | |
|
|
xxii | |
|
Optional: Installing LATEX |
|
|
xxiii | |
|
|
xxiii | |
|
|
xxiv | |
|
Typographical Conventions |
|
|
xxv | |
|
|
xxvi | |
|
The Website for the R Companion |
|
|
xxvii | |
|
|
xxviii | |
|
|
xxviii | |
About the Authors |
|
xxx | |
1 Getting Started With R and RStudio |
|
1 | (52) |
|
|
2 | (3) |
|
|
5 | (16) |
|
1.2.1 Interacting With R Through the Console |
|
|
5 | (2) |
|
1.2.2 Editing R Commands in the Console |
|
|
7 | (1) |
|
|
7 | (4) |
|
1.2.4 Vectors and Variables |
|
|
11 | (3) |
|
|
14 | (2) |
|
|
16 | (2) |
|
1.2.7 User-Defined Functions |
|
|
18 | (3) |
|
1.3 Fixing Errors and Getting Help |
|
|
21 | (4) |
|
1.3.1 When Things Go Wrong |
|
|
21 | (2) |
|
1.3.2 Getting Help and Information |
|
|
23 | (2) |
|
1.4 Organizing Your Work in R and RStudio and Making It Reproducible |
|
|
25 | (8) |
|
1.4.1 Using the RStudio Editor With R Script Files |
|
|
25 | (3) |
|
1.4.2 Writing R Markdown Documents |
|
|
28 | (5) |
|
1.5 An Extended Illustration: Duncan's Occupational-Prestige Regression |
|
|
33 | (14) |
|
|
36 | (3) |
|
1.5.2 Regression Analysis |
|
|
39 | (1) |
|
1.5.3 Regression Diagnostics |
|
|
40 | (7) |
|
1.6 R Functions for Basic Statistics |
|
|
47 | (1) |
|
1.7 Generic Functions and Their Methods* |
|
|
47 | (6) |
2 Reading and Manipulating Data |
|
53 | (70) |
|
|
54 | (13) |
|
2.1.1 Accessing Data From a Package |
|
|
54 | (2) |
|
2.1.2 Entering a Data Frame Directly |
|
|
56 | (3) |
|
2.1.3 Reading Data From Plain-Text Files |
|
|
59 | (4) |
|
|
63 | (2) |
|
2.1.5 Exporting or Saving a Data Frame to a File |
|
|
65 | (1) |
|
2.1.6 Reading and Writing Other File Formats |
|
|
66 | (1) |
|
2.2 Other Approaches to Reading and Managing Data Sets in R |
|
|
67 | (2) |
|
2.3 Working With Data Frames |
|
|
69 | (26) |
|
2.3.1 How the R Interpreter Finds Objects |
|
|
69 | (3) |
|
|
72 | (7) |
|
2.3.3 Modifying and Transforming Data |
|
|
79 | (7) |
|
2.3.4 Binding Rows and Columns |
|
|
86 | (1) |
|
2.3.5 Aggregating Data Frames |
|
|
87 | (2) |
|
2.3.6 Merging Data Frames |
|
|
89 | (2) |
|
|
91 | (4) |
|
2.4 Working With Matrices, Arrays, and Lists |
|
|
95 | (12) |
|
|
96 | (1) |
|
|
97 | (1) |
|
|
98 | (1) |
|
|
99 | (8) |
|
|
107 | (3) |
|
|
110 | (7) |
|
2.7 Large Data Sets in R* |
|
|
117 | (5) |
|
2.7.1 How Large Is "Large"? |
|
|
118 | (2) |
|
2.7.2 Reading and Saving Large Data Sets |
|
|
120 | (2) |
|
2.8 Complementary Reading and References |
|
|
122 | (1) |
3 Exploring and Transforming Data |
|
123 | (50) |
|
3.1 Examining Distributions |
|
|
124 | (10) |
|
|
124 | (4) |
|
|
128 | (2) |
|
3.1.3 Quantile-Comparison Plots |
|
|
130 | (3) |
|
|
133 | (1) |
|
3.2 Examining Relationships |
|
|
134 | (11) |
|
|
134 | (7) |
|
|
141 | (3) |
|
3.2.3 More on the plot () Function |
|
|
144 | (1) |
|
3.3 Examining Multivariate Data |
|
|
145 | (3) |
|
3.3.1 Three-Dimensional Plots |
|
|
145 | (1) |
|
3.3.2 Scatterplot Matrices |
|
|
146 | (2) |
|
|
148 | (21) |
|
3.4.1 Logarithms: The Champion of Transformations |
|
|
148 | (6) |
|
3.4.2 Power Transformations |
|
|
154 | (8) |
|
3.4.3 Transformations and Exploratory Data Analysis |
|
|
162 | (5) |
|
3.4.4 Transforming Restricted-Range Variables |
|
|
167 | (1) |
|
3.4.5 Other Transformations |
|
|
168 | (1) |
|
3.5 Point Labeling and Identification |
|
|
169 | (2) |
|
3.5.1 The identify () Function |
|
|
169 | (1) |
|
3.5.2 Automatic Point Labeling |
|
|
170 | (1) |
|
3.6 Scatterplot Smoothing |
|
|
171 | (1) |
|
3.7 Complementary Reading and References |
|
|
172 | (1) |
4 Fitting Linear Models |
|
173 | (70) |
|
|
174 | (2) |
|
4.2 Linear Least-Squares Regression |
|
|
176 | (11) |
|
4.2.1 Simple Linear Regression |
|
|
176 | (7) |
|
4.2.2 Multiple Linear Regression |
|
|
183 | (2) |
|
4.2.3 Standardized Regression Coefficients |
|
|
185 | (2) |
|
4.3 Predictor Effect Plots |
|
|
187 | (3) |
|
4.4 Polynomial Regression and Regression Splines |
|
|
190 | (7) |
|
4.4.1 Polynomial Regression |
|
|
190 | (4) |
|
4.4.2 Regression Splines* |
|
|
194 | (3) |
|
4.5 Factors in Linear Models |
|
|
197 | (10) |
|
4.5.1 A Linear Model With One Factor: One-Way Analysis of Variance |
|
|
201 | (4) |
|
4.5.2 Additive Models With Numeric Predictors and Factors |
|
|
205 | (2) |
|
4.6 Linear Models With Interactions |
|
|
207 | (17) |
|
4.6.1 Interactions Between Numeric Predictors and Factors |
|
|
207 | (6) |
|
4.6.2 Shortcuts for Writing Linear-Model Formulas |
|
|
213 | (1) |
|
|
214 | (8) |
|
4.6.4 Interactions Between Numeric Predictors* |
|
|
222 | (2) |
|
|
224 | (8) |
|
|
224 | (1) |
|
4.7.2 Other Factor Codings |
|
|
224 | (3) |
|
4.7.3 Ordered Factors and Orthogonal-Polynomial Contrasts |
|
|
227 | (3) |
|
4.7.4 User-Specified Contrasts* |
|
|
230 | (1) |
|
4.7.5 Suppressing the Intercept in a Model With Factors* |
|
|
231 | (1) |
|
|
232 | (3) |
|
4.9 The Arguments of the lm () Function |
|
|
235 | (6) |
|
|
235 | (3) |
|
|
238 | (1) |
|
|
238 | (1) |
|
|
239 | (1) |
|
|
239 | (1) |
|
4.9.6 method, model, x, y, qr* |
|
|
240 | (1) |
|
|
240 | (1) |
|
|
240 | (1) |
|
|
240 | (1) |
|
4.10 Complementary Reading and References |
|
|
241 | (2) |
5 Coefficient Standard Errors, Confidence Intervals, and Hypothesis Tests |
|
243 | (28) |
|
5.1 Coefficient Standard Errors |
|
|
244 | (10) |
|
5.1.1 Conventional Standard Errors of Least-Squares Regression Coefficients |
|
|
244 | (2) |
|
5.1.2 Robust Regression Coefficient Standard Errors |
|
|
246 | (2) |
|
5.1.3 Using the Bootstrap to Compute Standard Errors |
|
|
248 | (4) |
|
5.1.4 The Delta Method for Standard Errors of Nonlinear Functions* |
|
|
252 | (2) |
|
|
254 | (4) |
|
5.2.1 Wald Confidence Intervals |
|
|
254 | (1) |
|
5.2.2 Bootstrap Confidence Intervals |
|
|
255 | (1) |
|
5.2.3 Confidence Regions and Data Ellipses* |
|
|
256 | (2) |
|
5.3 Testing Hypotheses About Regression Coefficients |
|
|
258 | (12) |
|
|
258 | (1) |
|
5.3.2 Likelihood-Ratio Tests and the Analysis of Variance |
|
|
259 | (1) |
|
5.3.3 Sequential Analysis of Variance |
|
|
260 | (2) |
|
5.3.4 The Anova () Function |
|
|
262 | (5) |
|
5.3.5 Testing General Linear Hypotheses* |
|
|
267 | (3) |
|
5.4 Complementary Reading and References |
|
|
270 | (1) |
6 Fitting Generalized Linear Models |
|
271 | (64) |
|
6.1 Review of the Structure of GLMs |
|
|
272 | (4) |
|
6.2 The glm () Function in R |
|
|
276 | (1) |
|
6.3 GLMs for Binary Response Data |
|
|
276 | (13) |
|
6.3.1 Example: Women's Labor Force Participation |
|
|
278 | (4) |
|
6.3.2 Example: Volunteering for a Psychological Experiment |
|
|
282 | (1) |
|
6.3.3 Predictor Effect Plots for Logistic Regression |
|
|
283 | (2) |
|
6.3.4 Analysis of Deviance and Hypothesis Tests for Logistic Regression |
|
|
285 | (4) |
|
6.3.5 Fitted and Predicted Values |
|
|
289 | (1) |
|
|
289 | (7) |
|
6.5 Poisson GLMs for Count Data |
|
|
296 | (5) |
|
6.6 Loglinear Models for Contingency Tables |
|
|
301 | (8) |
|
6.6.1 Two-Dimensional Tables |
|
|
301 | (3) |
|
6.6.2 Three-Dimensional Tables |
|
|
304 | (2) |
|
6.6.3 Sampling Plans for Loglinear Models |
|
|
306 | (1) |
|
|
307 | (2) |
|
6.7 Multinomial Response Data |
|
|
309 | (5) |
|
|
314 | (3) |
|
6.9 The Proportional-Odds Model |
|
|
317 | (5) |
|
6.9.1 Testing for Proportional Odds |
|
|
319 | (3) |
|
|
322 | (8) |
|
6.10.1 More on the Anova () Function |
|
|
322 | (1) |
|
|
323 | (2) |
|
6.10.3 Quasi-Likelihood Estimation |
|
|
325 | (1) |
|
6,10.4 Overdispersed Binomial and Poisson Models |
|
|
326 | (4) |
|
|
330 | (2) |
|
|
331 | (1) |
|
6.11.2 start, etastart, mustart |
|
|
331 | (1) |
|
|
331 | (1) |
|
|
332 | (1) |
|
6.11.5 model, method, x, y |
|
|
332 | (1) |
|
6.12 Fitting GLMs by Iterated Weighted Least Squares* |
|
|
332 | (1) |
|
6.13 Complementary Reading and References |
|
|
333 | (2) |
7 Fitting Mixed-Effects Models |
|
335 | (50) |
|
7.1 Background: The Linear Model Revisited |
|
|
336 | (1) |
|
7.1.1 The Linear Model in Matrix Form* |
|
|
336 | (1) |
|
7.2 Linear Mixed-Effects Models |
|
|
336 | (39) |
|
7.2.1 Matrix Form of the Linear Mixed-Effects Model* |
|
|
338 | (1) |
|
7.2.2 An Application to Hierarchical Data |
|
|
339 | (18) |
|
7.2.3 Wald Tests for Linear Mixed-Effects Models |
|
|
357 | (1) |
|
7.2.4 Examining the Random Effects: Computing BLUPs |
|
|
358 | (2) |
|
7.2.5 An Application to Longitudinal Data |
|
|
360 | (11) |
|
7.2.6 Modeling the Errors |
|
|
371 | (2) |
|
7.2.7 Sandwich Standard Errors for Least-Squares Estimates |
|
|
373 | (2) |
|
7.3 Generalized Linear Mixed Models |
|
|
375 | (7) |
|
7.3.1 Matrix Form of the GLMM* |
|
|
376 | (1) |
|
7.3.2 Example: Minneapolis Police Stops |
|
|
377 | (5) |
|
7.4 Complementary Reading |
|
|
382 | (3) |
8 Regression Diagnostics for Linear, Generalized Linear, and Mixed-Effects Models |
|
385 | (52) |
|
|
386 | (2) |
|
8.2 Basic Diagnostic Plots |
|
|
388 | (8) |
|
|
388 | (3) |
|
8.2.2 Marginal-Model Plots |
|
|
391 | (1) |
|
8.2.3 Added-Variable Plots |
|
|
392 | (3) |
|
8.2.4 Marginal-Conditional Plots |
|
|
395 | (1) |
|
|
396 | (9) |
|
8.3.1 Outliers and Studentized Residuals |
|
|
397 | (1) |
|
8.3.2 Leverage: Hat-Values |
|
|
398 | (1) |
|
|
399 | (6) |
|
8.4 Transformations After Fitting a Regression Model |
|
|
405 | (9) |
|
8.4.1 Transforming the Response |
|
|
406 | (4) |
|
8.4.2 Predictor Transformations |
|
|
410 | (4) |
|
8.5 Nonconstant Error Variance |
|
|
414 | (3) |
|
8.5.1 Testing for Nonconstant Error Variance |
|
|
416 | (1) |
|
8.6 Diagnostics for Generalized Linear Models |
|
|
417 | (8) |
|
8.6.1 Residuals and Residual Plots |
|
|
418 | (3) |
|
|
421 | (1) |
|
8.6.3 Graphical Methods: Added-Variable Plots, Component-Plus-Residual Plots, and Effect Plots With Partial Residuals |
|
|
422 | (3) |
|
8.7 Diagnostics for Mixed-Effects Models |
|
|
425 | (4) |
|
8.7.1 Mixed-Model Component-Plus-Residual Plots |
|
|
425 | (3) |
|
8.7.2 Influence Diagnostics for Mixed Models |
|
|
428 | (1) |
|
8.8 Collinearity and Variance Inflation Factors |
|
|
429 | (5) |
|
8.9 Additional Regression Diagnostics |
|
|
434 | (1) |
|
8.10 Complementary Reading and References |
|
|
435 | (2) |
9 Drawing Graphs |
|
437 | (40) |
|
9.1 A General Approach to R Graphics |
|
|
438 | (16) |
|
9.1.1 Defining a Coordinate System: plot () |
|
|
439 | (2) |
|
9.1.2 Graphics Parameters: par () |
|
|
441 | (1) |
|
9.1.3 Adding Graphical Elements: axis (), points (), lines (), text (), et al. |
|
|
442 | (10) |
|
|
452 | (2) |
|
9.2 Putting It Together: Explaining Local Linear Regression |
|
|
454 | (13) |
|
9.2.1 Finer Control Over Plot Layout |
|
|
461 | (6) |
|
9.3 Other R Graphics Packages |
|
|
467 | (9) |
|
9.3.1 The lattice Package |
|
|
467 | (2) |
|
9.3.2 The ggplot2 Package |
|
|
469 | (3) |
|
|
472 | (3) |
|
9.3.4 Other Notable Graphics Packages |
|
|
475 | (1) |
|
9.4 Complementary Reading and References |
|
|
476 | (1) |
10 An Introduction to R Programming |
|
477 | (62) |
|
10.1 Why Learn to Program in R? |
|
|
478 | (1) |
|
10.2 Defining Functions: Preliminary Examples |
|
|
479 | (7) |
|
10.2.1 Lagging a Variable |
|
|
479 | (3) |
|
10.2.2 Creating an Influence Plot |
|
|
482 | (4) |
|
10.3 Working With Matrices* |
|
|
486 | (6) |
|
10.3.1 Basic Matrix Arithmetic |
|
|
486 | (2) |
|
10.3.2 Matrix Inversion and the Solution of Linear Simultaneous Equations |
|
|
488 | (1) |
|
10.3.3 Example: Linear Least-Squares Regression |
|
|
489 | (2) |
|
10.3.4 Eigenvalues and Eigenvectors |
|
|
491 | (1) |
|
10.3.5 Miscellaneous Matrix Computations |
|
|
491 | (1) |
|
10.4 Program Control With Conditionals, Loops, and Recursion |
|
|
492 | (7) |
|
|
492 | (3) |
|
10.4.2 Iteration (Looping) |
|
|
495 | (3) |
|
|
498 | (1) |
|
10.5 Avoiding Loops: apply () and Its Relatives |
|
|
499 | (10) |
|
10.5.1 To Loop or Not to Loop? |
|
|
505 | (4) |
|
10.6 Optimization Problems* |
|
|
509 | (6) |
|
10.6.1 Zero-Inflated Poisson Regression |
|
|
509 | (6) |
|
10.7 Monte-Carlo Simulation* |
|
|
515 | (7) |
|
10.7.1 Testing Regression Models Using Simulation |
|
|
516 | (6) |
|
|
522 | (5) |
|
10.9 Object-Oriented Programming in R* |
|
|
527 | (6) |
|
10.10 Writing Statistical-Modeling Functions in R* |
|
|
533 | (3) |
|
10.11 Organizing Code for R Functions |
|
|
536 | (1) |
|
10.12 Complementary Reading and References |
|
|
537 | (2) |
References |
|
539 | (12) |
Subject Index |
|
551 | (16) |
Data Set Index |
|
567 | (2) |
Package Index |
|
569 | (2) |
Index of Functions and Operators |
|
571 | |