Download First40YearsofRSP.pdf and more Exams Biology in PDF only on Docsity!
Applied Statistics with R
David Dalpiaz
- 1 Introduction
- 1.1 About This Book
- 1.2 Conventions
- 1.3 Acknowledgements
- 1.4 License
- 2 Introduction to R
- 2.1 Getting Started
- 2.2 Basic Calculations
- 2.3 Getting Help
- 2.4 Installing Packages
- 3 Data and Programming
- 3.1 Data Types
- 3.2 Data Structures
- 3.2.1 Vectors
- 3.2.2 Vectorization
- 3.2.3 Logical Operators
- 3.2.4 More Vectorization
- 3.2.5 Matrices
- 3.2.6 Lists
- 3.2.7 Data Frames
- 3.3 Programming Basics
- 3.3.1 Control Flow 4 CONTENTS
- 3.3.2 Functions
- 4 Summarizing Data
- 4.1 Summary Statistics
- 4.2 Plotting
- 4.2.1 Histograms
- 4.2.2 Barplots
- 4.2.3 Boxplots
- 4.2.4 Scatterplots
- 5 Probability and Statistics in R
- 5.1 Probability in R
- 5.2 Hypothesis Tests in R
- 5.2.1 One Sample t-Test: Review
- 5.2.2 One Sample t-Test: Example
- 5.2.3 Two Sample t-Test: Review
- 5.2.4 Two Sample t-Test: Example
- 5.3 Simulation
- 5.3.1 Paired Differences
- 5.3.2 Distribution of a Sample Mean
- 6 R Resources
- 6.1 Beginner Tutorials and References
- 6.2 Intermediate References
- 6.3 Advanced References
- 6.4 Quick Comparisons to Other Languages
- 6.5 RStudio and RMarkdown Videos
- 6.6 RMarkdown Template
- CONTENTS
- 7 Simple Linear Regression
- 7.1 Modeling
- 7.1.1 Simple Linear Regression Model
- 7.2 Least Squares Approach
- 7.2.1 Making Predictions
- 7.2.2 Residuals
- 7.2.3 Variance Estimation
- 7.3 Decomposition of Variation
- 7.3.1 Coefficient of Determination
- 7.4 The lm Function
- 7.5 Maximum Likelihood Estimation (MLE) Approach
- 7.6 Simulating SLR
- 7.7 History
- 7.8 R Markdown
- 8 Inference for Simple Linear Regression
- 8.1 Gauss–Markov Theorem
- 8.2 Sampling Distributions
- 8.2.1 Simulating Sampling Distributions
- 8.3 Standard Errors
- 8.4 Confidence Intervals for Slope and Intercept
- 8.5 Hypothesis Tests
- 8.6 cars Example
- 8.6.1 Tests in R
- 8.6.2 Significance of Regression, t-Test
- 8.6.3 Confidence Intervals in R
- 8.7 Confidence Interval for Mean Response
- 8.8 Prediction Interval for New Observations
- 8.9 Confidence and Prediction Bands
- 8.10 Significance of Regression, F-Test
- 8.11 R Markdown
- 9 Multiple Linear Regression 6 CONTENTS
- 9.1 Matrix Approach to Regression
- 9.2 Sampling Distribution
- 9.2.1 Single Parameter Tests
- 9.2.2 Confidence Intervals
- 9.2.3 Confidence Intervals for Mean Response
- 9.2.4 Prediction Intervals
- 9.3 Significance of Regression
- 9.4 Nested Models
- 9.5 Simulation
- 9.6 R Markdown
- 10 Model Building
- 10.1 Family, Form, and Fit
- 10.1.1 Fit
- 10.1.2 Form
- 10.1.3 Family
- 10.1.4 Assumed Model, Fitted Model
- 10.2 Explanation versus Prediction
- 10.2.1 Explanation
- 10.2.2 Prediction
- 10.3 Summary
- 10.4 R Markdown
- 11 Categorical Predictors and Interactions
- 11.1 Dummy Variables
- 11.2 Interactions
- 11.3 Factor Variables
- 11.3.1 Factors with More Than Two Levels
- 11.4 Parameterization
- 11.5 Building Larger Models
- 11.6 R Markdown
- CONTENTS
- 12 Analysis of Variance
- 12.1 Experiments
- 12.2 Two-Sample t-Test
- 12.3 One-Way ANOVA
- 12.3.1 Factor Variables
- 12.3.2 Some Simulation
- 12.3.3 Power
- 12.4 Post Hoc Testing
- 12.5 Two-Way ANOVA
- 12.6 R Markdown
- 13 Model Diagnostics
- 13.1 Model Assumptions
- 13.2 Checking Assumptions
- 13.2.1 Fitted versus Residuals Plot
- 13.2.2 Breusch-Pagan Test
- 13.2.3 Histograms
- 13.2.4 Q-Q Plots
- 13.2.5 Shapiro-Wilk Test
- 13.3 Unusual Observations
- 13.3.1 Leverage
- 13.3.2 Outliers
- 13.3.3 Influence
- 13.4 Data Analysis Examples
- 13.4.1 Good Diagnostics
- 13.4.2 Suspect Diagnostics
- 13.5 R Markdown
- 14 Transformations 8 CONTENTS
- 14.1 Response Transformation
- 14.1.1 Variance Stabilizing Transformations
- 14.1.2 Box-Cox Transformations
- 14.2 Predictor Transformation
- 14.2.1 Polynomials
- 14.2.2 A Quadratic Model
- 14.2.3 Overfitting and Extrapolation
- 14.2.4 Comparing Polynomial Models
- 14.2.5 poly() Function and Orthogonal Polynomials
- 14.2.6 Inhibit Function
- 14.2.7 Data Example
- 14.3 R Markdown
- 15 Collinearity
- 15.1 Exact Collinearity
- 15.2 Collinearity
- 15.2.1 Variance Inflation Factor.
- 15.3 Simulation
- 15.4 R Markdown
- 16 Variable Selection and Model Building
- 16.1 Quality Criterion
- 16.1.1 Akaike Information Criterion
- 16.1.2 Bayesian Information Criterion
- 16.1.3 Adjusted R-Squared
- 16.1.4 Cross-Validated RMSE
- 16.2 Selection Procedures
- 16.2.1 Backward Search
- 16.2.2 Forward Search
- 16.2.3 Stepwise Search
- 16.2.4 Exhaustive Search
- CONTENTS
- 16.3 Higher Order Terms
- 16.4 Explanation versus Prediction
- 16.4.1 Explanation
- 16.4.2 Prediction
- 16.5 R Markdown
- 17 Logistic Regression
- 17.1 Generalized Linear Models
- 17.2 Binary Response
- 17.2.1 Fitting Logistic Regression
- 17.2.2 Fitting Issues
- 17.2.3 Simulation Examples
- 17.3 Working with Logistic Regression
- 17.3.1 Testing with GLMs
- 17.3.2 Wald Test
- 17.3.3 Likelihood-Ratio Test
- 17.3.4 SAheart Example
- 17.3.5 Confidence Intervals
- 17.3.6 Confidence Intervals for Mean Response
- 17.3.7 Formula Syntax
- 17.3.8 Deviance
- 17.4 Classification
- 17.4.1 spam Example
- 17.4.2 Evaluating Classifiers
- 17.5 R Markdown
- 18 Beyond
- 18.1 What’s Next
- 18.2 RStudio
- 18.3 Tidy Data
- 18.4 Visualization
- 18.5 Web Applications
- 18.6 Experimental Design 10 CONTENTS
- 18.7 Machine Learning
- 18.8 Time Series
- 18.9 Bayesianism
- 18.10High Performance Computing
- 18.11Further R Resources
- 19 Appendix
Chapter 1
Introduction
Welcome to Applied Statistics with R!
1.1 About This Book
This book was originally (and currently) designed for use with STAT 420 , Methods of Applied Statistics , at the University of Illinois at Urbana-Champaign. It may certainly be used elsewhere, but any references to “this course” in this book specifically refer to STAT 420.
This book is under active development. When possible, it would be best to always access the text online to be sure you are using the most up-to-date version. Also, the html version provides additional features such as changing text size, font, and colors. If you are in need of a local copy, a pdf version is continuously maintained, however, because a pdf uses pages, the formatting may not be as functional. (In other words, the author needs to go back and spend some time working on the pdf formatting.)
Since this book is under active development you may encounter errors ranging from typos, to broken code, to poorly explained topics. If you do, please let us know! Simply send an email and we will make the changes as soon as possible. (dalpiaz2 AT illinois DOT edu) Or, if you know RMarkdown and are famil- iar with GitHub, make a pull request and fix an issue yourself! This process is partially automated by the edit button in the top-left corner of the html version. If your suggestion or fix becomes part of the book, you will be added to the list at the end of this chapter. We’ll also link to your GitHub account, or personal website upon request.
This text uses MathJax to render mathematical notation for the web. Occa- sionally, but rarely, a JavaScript error will prevent MathJax from rendering
1.3. ACKNOWLEDGEMENTS 13
- James Balamuta - Current graduate student at the University of Illinois at Urbana- Champaign. Provided the initial push to write this book by intro- ducing the author to the bookdown package in R. Also a frequent contributor via GitHub.
Your name could be here! Suggest an edit! Correct a typo! If you submit a correction and would like to be listed below, please provide your name as you would like it to appear, as well as a link to a GitHub, LinkedIn, or personal website.
- Daniel McQuillan
- Mason Rubenstein
- Yuhang Wang
- Zhao Liu
- Jinfeng Xiao
- Somu Palaniappan
- Michael Hung-Yiu Chan
- Eloise Rosen
- Kiomars Nassiri
- Jeff Gerlach
- Brandon Ching
- Ray Fix
- Tyler Kim
- Yeongho Kim
- Elmar Langholz
- Thai Duy Cuong Nguyen
- Junyoung Kim
- Sezgin Kucukcoban
- Tony Ma
- Radu Manolescu
- Dileep Pasumarthi
- Sihun Wang
- Joseph Wilson
- Yingkui Lin
- Andy Siddall
- Nishant Balepur
- Durga Krovi
- Raj Krishnan
- Ed Pureza
- Siddharth Singh
- Schillaci Mcinnis
- Ivan Valdes Castillo
- Tony Mu
- Salman Yousaf
14 CHAPTER 1. INTRODUCTION
- Yutaro Nishiyama
- Regina Sahani Goonetilleke
- Paul Zuradzki
- Will Tsai
- Ellen Veomett
1.4 License
Figure 1.1: This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License.
16 CHAPTER 2. INTRODUCTION TO R
data, but we will also perform programming exercises that help illustrate certain concepts.
RStudio has a large number of useful keyboard shortcuts. A list of these can be found using a keyboard shortcut – the keyboard shortcut to rule them all:
- On Windows: Alt + Shift + K
- On Mac: Option + Shift + K
The RStudio team has developed a number of “cheatsheets” for working with both R and RStudio. This particular cheatsheet for “Base” R will summarize many of the concepts in this document. (“Base” R is a name used to differentiate the practice of using built-in R functions, as opposed to using functions from outside packages, in particular, those from the tidyverse. More on this later.)
When programming, it is often a good practice to follow a style guide. (Where do spaces go? Tabs or spaces? Underscores or CamelCase when naming variables?) No style guide is “correct” but it helps to be aware of what others do. The more important thing is to be consistent within your own code.
- Hadley Wickham Style Guide from Advanced R
- Google Style Guide
For this course, our main deviation from these two guides is the use of = in place of <-. (More on that later.)
2.2 Basic Calculations
To get started, we’ll use R like a simple calculator.
Addition, Subtraction, Multiplication and Division
Math R Result 3 + 2 3 + 2 5 3 − 2 3 - 2 1 3 ⋅ 2 3 * 2 6 3/2 3 / 2 1.
Exponents
2.3. GETTING HELP 17
Math R Result 32 3 ^ 2 9 2 (−3)^ 2 ^ (-3) 0. (^100) √ 1/2^ 100 ^ (1 / 2) 10 100 sqrt(100) 10
Mathematical Constants
Math R Result 𝜋 pi 3. 𝑒 exp(1) 2.
Logarithms
Note that we will use ln and log interchangeably to mean the natural logarithm. There is no ln() in R, instead it uses log() to mean the natural logarithm.
Math R Result log(𝑒) log(exp(1)) 1 log 10 (1000) log10(1000) 3 log 2 (8) log2(8) 3 log 4 (16) log(16, base = 4) 2
Trigonometry
Math R Result sin(𝜋/2) sin(pi / 2) 1 cos(0) cos(0) 1
2.3 Getting Help
In using R as a calculator, we have seen a number of functions: sqrt(), exp(), log() and sin(). To get documentation about a function in R, simply put a question mark in front of the function name and RStudio will display the documentation, for example:
2.4. INSTALLING PACKAGES 19
library (ggplot2)
Once you close R, all the packages are closed and put back on the imaginary shelf. The next time you open R, you do not have to install the package again, but you do have to load any packages you intend to use by invoking library().
20 CHAPTER 2. INTRODUCTION TO R