Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Cover Page
Title Page
Copyright Page
Brief Contents
Contents in Detail
Preface
Acknowledgments
Introduction
A Brief History of R
About This Book
Part I: The Language
Part II: Programming
Part III: Statistics and Probability
Part IV: Statistical Testing and Modeling
Part V: Advanced Graphics
For Students
For Instructors
Part I: The Language
Chapter 1: Getting Started
1.1 Obtaining and Installing R from CRAN
1.2 Opening R for the First Time
1.2.1 Console and Editor Panes
1.2.2 Comments
1.2.3 Working Directory
1.2.4 Installing and Loading R Packages
1.2.5 Help Files and Function Documentation
1.2.6 Third-Party Editors
1.3 Saving Work and Exiting R
1.3.1 Workspaces
1.3.2 Scripts
1.4 Conventions
1.4.1 Coding
1.4.2 Math and Equation References
1.4.3 Exercises
Exercise 1.1
Chapter 2: Numerics, Arithmetic, Assignment, and Vectors
2.1 R for Basic Math
2.1.1 Arithmetic
2.1.2 Logarithms and Exponentials
2.1.3 E-Notation
Exercise 2.1
2.2 Assigning Objects
Exercise 2.2
2.3 Vectors
2.3.1 Creating a Vector
2.3.2 Sequences, Repetition, Sorting, and Lengths
Exercise 2.3
2.3.3 Subsetting and Element Extraction
Exercise 2.4
2.3.4 Vector-Oriented Behavior
Exercise 2.5
Chapter 3: Matrices and Arrays
3.1 Defining a Matrix
3.1.1 Filling Direction
3.1.2 Row and Column Bindings
3.1.3 Matrix Dimensions
3.2 Subsetting
3.2.1 Row, Column, and Diagonal Extractions
3.2.2 Omitting and Overwriting
Exercise 3.1
3.3 Matrix Operations and Algebra
3.3.1 Matrix Transpose
3.3.2 Identity Matrix
3.3.3 Scalar Multiple of a Matrix
3.3.4 Matrix Addition and Subtraction
3.3.5 Matrix Multiplication
3.3.6 Matrix Inversion
Exercise 3.2
3.4 Multidimensional Arrays
3.4.1 Definition
3.4.2 Subsets, Extractions, and Replacements
Exercise 3.3
Chapter 4: Non-numeric Values
4.1 Logical Values
4.1.1 TRUE or FALSE?
4.1.2 A Logical Outcome: Relational Operators
Exercise 4.1
4.1.3 Multiple Comparisons: Logical Operators
Exercise 4.2
4.1.4 Logicals Are Numbers!
4.1.5 Logical Subsetting and Extraction
Exercise 4.3
4.2 Characters
4.2.1 Creating a String
4.2.2 Concatenation
4.2.3 Escape Sequences
4.2.4 Substrings and Matching
Exercise 4.4
4.3 Factors
4.3.1 Identifying Categories
4.3.2 Defining and Ordering Levels
4.3.3 Combining and Cutting
Exercise 4.5
Chapter 5: Lists and Data Frames
5.1 Lists of Objects
5.1.1 Definition and Component Access
5.1.2 Naming
5.1.3 Nesting
Exercise 5.1
5.2 Data Frames
5.2.1 Construction
5.2.2 Adding Data Columns and Combining Data Frames
5.2.3 Logical Record Subsets
Exercise 5.2
Chapter 6: Special Values, Classes, and Coercion
6.1 Some Special Values
6.1.1 Infinity
6.1.2 NaN
Exercise 6.1
6.1.3 NA
6.1.4 NULL
Exercise 6.2
6.2 Understanding Types, Classes, and Coercion
6.2.1 Attributes
6.2.2 Object Class
6.2.3 Is-Dot Object-Checking Functions
6.2.4 As-Dot Coercion Functions
Exercise 6.3
Chapter 7: Basic Plotting
7.1 Using plot with Coordinate Vectors
7.2 Graphical Parameters
7.2.1 Automatic Plot Types
7.2.2 Title and Axis Labels
7.2.3 Color
7.2.4 Line and Point Appearances
7.2.5 Plotting Region Limits
7.3 Adding Points, Lines, and Text to an Existing Plot
Exercise 7.1
7.4 The ggplot2 Package
7.4.1 A Quick Plot with qplot
7.4.2 Setting Appearance Constants with Geoms
7.4.3 Aesthetic Mapping with Geoms
Exercise 7.2
Chapter 8: Reading and Writing Files
8.1 R-Ready Data Sets
8.1.1 Built-in Data Sets
8.1.2 Contributed Data Sets
8.2 Reading in External Data Files
8.2.1 The Table Format
8.2.2 Spreadsheet Workbooks
8.2.3 Web-Based Files
8.2.4 Other File Formats
8.3 Writing Out Data Files and Plots
8.3.1 Data Sets
8.3.2 Plots and Graphics Files
8.4 Ad Hoc Object Read/Write Operations
Exercise 8.1
Part II: Programming
Chapter 9: Calling Functions
9.1 Scoping
9.1.1 Environments
9.1.2 Search Path
9.1.3 Reserved and Protected Names
Exercise 9.1
9.2 Argument Matching
9.2.1 Exact
9.2.2 Partial
9.2.3 Positional
9.2.4 Mixed
9.2.5 Dot-Dot-Dot: Use of Ellipses
Exercise 9.2
Chapter 10: Conditions and Loops
10.1 if Statements
10.1.1 Stand-Alone Statement
10.1.2 else Statements
10.1.3 Using ifelse for Element-wise Checks
Exercise 10.1
10.1.4 Nesting and Stacking Statements
10.1.5 The switch Function
Exercise 10.2
10.2 Coding Loops
10.2.1 for Loops
Exercise 10.3
10.2.2 while Loops
Exercise 10.4
10.2.3 Implicit Looping with apply
Exercise 10.5
10.3 Other Control Flow Mechanisms
10.3.1 Declaring break or next
10.3.2 The repeat Statement
Exercise 10.6
Chapter 11: Writing Functions
11.1 The function Command
11.1.1 Function Creation
11.1.2 Using return
Exercise 11.1
11.2 Arguments
11.2.1 Lazy Evaluation
11.2.2 Setting Defaults
11.2.3 Checking for Missing Arguments
11.2.4 Dealing with Ellipses
Exercise 11.2
11.3 Specialized Functions
11.3.1 Helper Functions
11.3.2 Disposable Functions
11.3.3 Recursive Functions
Exercise 11.3
Chapter 12: Exceptions, Timings, and Visibility
12.1 Exception Handling
12.1.1 Formal Notifications: Errors and Warnings
12.1.2 Catching Errors with try Statements
Exercise 12.1
12.2 Progress and Timing
12.2.1 Textual Progress Bars: Are We There Yet?
12.2.2 Measuring Completion Time: How Long Did It Take?
Exercise 12.2
12.3 Masking
12.3.1 Function and Object Distinction
12.3.2 Data Frame Variable Distinction
Part III: Statistics and Probability
Chapter 13: Elementary Statistics
13.1 Describing Raw Data
13.1.1 Numeric Variables
13.1.2 Categorical Variables
13.1.3 Univariate and Multivariate Data
13.1.4 Parameter or Statistic?
Exercise 13.1
13.2 Summary Statistics
13.2.1 Centrality: Mean, Median, Mode
13.2.2 Counts, Percentages, and Proportions
Exercise 13.2
13.2.3 Quantiles, Percentiles, and the Five-Number Summary
13.2.4 Spread: Variance, Standard Deviation, and the Interquartile Range
Exercise 13.3
13.2.5 Covariance and Correlation
13.2.6 Outliers
Exercise 13.4
Chapter 14: Basic Data Visualization
14.1 Barplots and Pie Charts
14.1.1 Building a Barplot
14.1.2 A Quick Pie Chart
14.2 Histograms
14.3 Box-and-Whisker Plots
14.3.1 Stand-Alone Boxplots
14.3.2 Side-by-Side Boxplots
14.4 Scatterplots
14.4.1 Single Plot
14.4.2 Matrix of Plots
Exercise 14.1
Chapter 15: Probability
15.1 What Is a Probability?
15.1.1 Events and Probability
15.1.2 Conditional Probability
15.1.3 Intersection
15.1.4 Union
15.1.5 Complement
Exercise 15.1
15.2 Random Variables and Probability Distributions
15.2.1 Realizations
15.2.2 Discrete Random Variables
15.2.3 Continuous Random Variables
15.2.4 Shape, Skew, and Modality
Exercise 15.2
Chapter 16: Common Probability Distributions
16.1 Common Probability Mass Functions
16.1.1 Bernoulli Distribution
16.1.2 Binomial Distribution
Exercise 16.1
16.1.3 Poisson Distribution
Exercise 16.2
16.1.4 Other Mass Functions
16.2 Common Probability Density Functions
16.2.1 Uniform
Exercise 16.3
16.2.2 Normal
Exercise 16.4
16.2.3 Student’s t-distribution
16.2.4 Exponential
Exercise 16.5
16.2.5 Other Density Functions
Part IV: Statistical Testing and Modeling
Chapter 17: Sampling Distributions and Confidence
17.1 Sampling Distributions
17.1.1 Distribution for a Sample Mean
17.1.2 Distribution for a Sample Proportion
Exercise 17.1
17.1.3 Sampling Distributions for Other Statistics
17.2 Confidence Intervals
17.2.1 An Interval for a Mean
17.2.2 An Interval for a Proportion
17.2.3 Other Intervals
17.2.4 Comments on Interpretation of a CI
Exercise 17.2
Chapter 18: Hypothesis Testing
18.1 Components of a Hypothesis Test
18.1.1 Hypotheses
18.1.2 Test Statistic
18.1.3 p-value
18.1.4 Significance Level
18.1.5 Criticisms of Hypothesis Testing
18.2 Testing Means
18.2.1 Single Mean
Exercise 18.1
18.2.2 Two Means
Exercise 18.2
18.3 Testing Proportions
18.3.1 Single Proportion
18.3.2 Two Proportions
Exercise 18.3
18.4 Testing Categorical Variables
18.4.1 Single Categorical Variable
18.4.2 Two Categorical Variables
Exercise 18.4
18.5 Errors and Power
18.5.1 Hypothesis Test Errors
18.5.2 Type I Errors
18.5.3 Type II Errors
Exercise 18.5
18.5.4 Statistical Power
Exercise 18.6
Chapter 19: Analysis of Variance
19.1 One-Way ANOVA
19.1.1 Hypotheses and Diagnostic Checking
19.1.2 One-Way ANOVA Table Construction
19.1.3 Building ANOVA Tables with the aov Function
Exercise 19.1
19.2 Two-Way ANOVA
19.2.1 A Suite of Hypotheses
19.2.2 Main Effects and Interactions
19.3 Kruskal-Wallis Test
Exercise 19.2
Chapter 20: Simple Linear Regression
20.1 An Example of a Linear Relationship
20.2 General Concepts
20.2.1 Definition of the Model
20.2.2 Estimating the Intercept and Slope Parameters
20.2.3 Fitting Linear Models with lm
20.2.4 Illustrating Residuals
20.3 Statistical Inference
20.3.1 Summarizing the Fitted Model
20.3.2 Regression Coefficient Significance Tests
20.3.3 Coefficient of Determination
20.3.4 Other summary Output
20.4 Prediction
20.4.1 Confidence Interval or Prediction Interval?
20.4.2 Interpreting Intervals
20.4.3 Plotting Intervals
20.4.4 Interpolation vs. Extrapolation
Exercise 20.1
20.5 Understanding Categorical Predictors
20.5.1 Binary Variables: k = 2
20.5.2 Multilevel Variables: k > 2
20.5.3 Changing the Reference Level
20.5.4 Treating Categorical Variables as Numeric
20.5.5 Equivalence with One-Way ANOVA
Exercise 20.2
Chapter 21: Multiple Linear Regression
21.1 Terminology
21.2 Theory
21.2.1 Extending the Simple Model to a Multiple Model
21.2.2 Estimating in Matrix Form
21.2.3 A Basic Example
21.3 Implementing in R and Interpreting
21.3.1 Additional Predictors
21.3.2 Interpreting Marginal Effects
21.3.3 Visualizing the Multiple Linear Model
21.3.4 Finding Confidence Intervals
21.3.5 Omnibus F-Test
21.3.6 Predicting from a Multiple Linear Model
Exercise 21.1
21.4 Transforming Numeric Variables
21.4.1 Polynomial
21.4.2 Logarithmic
21.4.3 Other Transformations
Exercise 21.2
21.5 Interactive Terms
21.5.1 Concept and Motivation
21.5.2 One Categorical, One Continuous
21.5.3 Two Categorical
21.5.4 Two Continuous
21.5.5 Higher-Order Interactions
Exercise 21.3
Chapter 22: Linear Model Selection and Diagnostics
22.1 Goodness-of-Fit vs. Complexity
22.1.1 Principle of Parsimony
22.1.2 General Guidelines
22.2 Model Selection Algorithms
22.2.1 Nested Comparisons: The Partial F-Test
22.2.2 Forward Selection
22.2.3 Backward Selection
22.2.4 Stepwise AIC Selection
Exercise 22.1
22.2.5 Other Selection Algorithms
22.3 Residual Diagnostics
22.3.1 Inspecting and Interpreting Residuals
22.3.2 Assessing Normality
22.3.3 Illustrating Outliers, Leverage, and Influence
22.3.4 Calculating Leverage
22.3.5 Cook’s Distance
22.3.6 Graphically Combining Residuals, Leverage, and Cook’s Distance
Exercise 22.2
22.4 Collinearity
22.4.1 Potential Warning Signs
22.4.2 Correlated Predictors: A Quick Example
Part V: Advanced Graphics
Chapter 23: Advanced Plot Customization
23.1 Handling the Graphics Device
23.1.1 Manually Opening a New Device
23.1.2 Switching Between Devices
23.1.3 Closing a Device
23.1.4 Multiple Plots in One Device
23.2 Plotting Regions and Margins
23.2.1 Default Spacing
23.2.2 Custom Spacing
23.2.3 Clipping
23.3 Point-and-Click Coordinate Interaction
23.3.1 Retrieving Coordinates Silently
23.3.2 Visualizing Selected Coordinates
23.3.3 Ad Hoc Annotation
Exercise 23.1
23.4 Customizing Traditional R Plots
23.4.1 Graphical Parameters for Style and Suppression
23.4.2 Customizing Boxes
23.4.3 Customizing Axes
23.5 Specialized Text and Label Notation
23.5.1 Font
23.5.2 Greek Symbols
23.5.3 Mathematical Expressions
23.6 A Fully Annotated Scatterplot
Exercise 23.2
Chapter 24: Going Further with the Grammar of Graphics
24.1 ggplot or qplot?
24.2 Smoothing and Shading
24.2.1 Adding LOESS Trends
24.2.2 Constructing Smooth Density Estimates
24.3 Multiple Plots and Variable-Mapped Facets
24.3.1 Independent Plots
24.3.2 Facets Mapped to a Categorical Variable
Exercise 24.1
24.4 Interactive Tools in ggvis
Exercise 24.2
Chapter 25: Defining Colors and Plotting in Higher Dimensions
25.1 Representing and Using Color
25.1.1 Red-Green-Blue Hexadecimal Color Codes
25.1.2 Built-in Palettes
25.1.3 Custom Palettes
25.1.4 Using Color Palettes to Index a Continuum
25.1.5 Including a Color Legend
25.1.6 Opacity
25.1.7 RGB Alternatives and Further Functionality
Exercise 25.1
25.2 3D Scatterplots
25.2.1 Basic Syntax
25.2.2 Visual Enhancements
Exercise 25.2
25.3 Preparing a Surface for Plotting
25.3.1 Constructing an Evaluation Grid
25.3.2 Constructing the z-Matrix
25.3.3 Conceptualizing the z-Matrix
25.4 Contour Plots
25.4.1 Drawing Contour Lines
25.4.2 Color-Filled Contours
Exercise 25.3
25.5 Pixel Images
25.5.1 One Grid Point = One Pixel
25.5.2 Surface Truncation and Empty Pixels
Exercise 25.4
25.6 Perspective Plots
25.6.1 Basic Plots and Angle Adjustment
25.6.2 Coloring Facets
25.6.3 Rotating with Loops
Exercise 25.5
Chapter 26: Interactive 3D Plots
26.1 Point Clouds
26.1.1 Basic 3D Cloud
26.1.2 Visual Enhancements and Legends
26.1.3 Adding Further 3D Components
Exercise 26.1
26.2 Bivariate Surfaces
26.2.1 Basic Perspective Surface
26.2.2 Additional Components
26.2.3 Coloring by z Value
26.2.4 Dealing with the Aspect Ratio
Exercise 26.2
26.3 Trivariate Surfaces
26.3.1 Evaluation Coordinates in 3D
26.3.2 Isosurfaces
26.3.3 Example: Nonparametric Trivariate Density
26.4 Handling Parametric Equations
26.4.1 Simple Loci
26.4.2 Mathematical Abstractions
Exercise 26.3
Appendix A: Installing R and Contributed Packages
A.1 Downloading and Installing R
A.2 Using Packages
A.2.1 Base Packages
A.2.2 Recommended Packages
A.2.3 Contributed Packages
A.3 Updating R and Installed Packages
A.4 Using Other Mirrors and Repositories
A.4.1 Switching CRAN Mirror
A.4.2 Other Package Repositories
A.5 Citing and Writing Packages
A.5.1 Citing R and Contributed Packages
A.5.2 Writing Your Own Packages
Appendix B: Working with RStudio
B.1 Basic Layout and Usage
B.1.1 Editor Features and Appearance Options
B.1.2 Customizing Panes
B.2 Auxiliary Tools
B.2.1 Projects
B.2.2 Package Installer and Updater
B.2.3 Support for Debugging
B.2.4 Markup, Document, and Graphics Tools
Reference List
Index
← Prev
Back
Next →
← Prev
Back
Next →