Skip to main content
LawSphereX

Learning Statistics with R

Subject: Statistics and Research Methods

Author or source: Danielle Navarro

Updated:

Complete 613-page version 0.6 textbook by Danielle Navarro. Covers research design, R programming, data handling, probability, estimation, hypothesis testing, regression and ANOVA. Original author PDF; software examples reflect this edition.

Contents from the author

Source page 5

Table of Contents Preface ix I Background 1 1 Why do we learn statistics? 3 1.1 On the psychology of statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 The cautionary tale of Simpson’s paradox . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 Statistics in psychology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4 Statistics in everyday life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.5 There’s more to research methods than statistics . . . . . . . . . . . . . . . . . . . . . . 10 2 A brief introduction to research design 11 2.1 Introduction to psychological measurement . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 Scales of measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3 Assessing the reliability of a measurement . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.4 The “role” of variables: predictors and outcomes . . . . . . . . . . . . . . . . . . . . . . 20 2.5 Experimental and non-experimental research . . . . . . . . . . . . . . . . . . . . . . . . 20 2.6 Assessing the validity of a study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.7 Confounds, artifacts and other threats to validity . . . . . . . . . . . . . . . . . . . . . . 25 2.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 II An introduction to R 35 3 Getting started with R 37 3.1 Installing R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.2 Typing commands at the R console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.3 Doing simple calculations with R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.4 Storing a number as a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.5 Using functions to do calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.6 Letting Rstudio help you with your commands . . . . . . . . . . . . . . . . . . . . . . . . 54 3.7 Storing many numbers as a vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3.8 Storing text data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.9 Storing “true or false” data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.10 Indexing vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 3.11 Quitting R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 3.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4 Additional R concepts 73 4.1 Using comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4.2 Installing and loading packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.3 Managing the workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4.4 Navigating the file system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.5 Loading and saving data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4.6 Useful things to know about variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.7 Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 4.8 Data frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 v

Contents from the author

Source page 6

4.9 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 4.10 Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 4.11 Generic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 4.12 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 4.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 III Working with data 111 5 Descriptive statistics 113 5.1 Measures of central tendency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 5.2 Measures of variability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.3 Skew and kurtosis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 5.4 Getting an overall summary of a variable . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5.5 Descriptive statistics separately for each group . . . . . . . . . . . . . . . . . . . . . . . 136 5.6 Standard scores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 5.7 Correlations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 5.8 Handling missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 5.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 6 Drawing graphs 157 6.1 An overview of R graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 6.2 An introduction to plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6.3 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 6.4 Stem and leaf plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 6.5 Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 6.6 Scatterplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 6.7 Bar graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 6.8 Saving image files using R and Rstudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 6.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 7 Pragmatic matters 195 7.1 Tabulating and cross-tabulating data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 7.2 Transforming and recoding a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 7.3 A few more mathematical functions and operations . . . . . . . . . . . . . . . . . . . . . 203 7.4 Extracting a subset of a vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 7.5 Extracting a subset of a data frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 7.6 Sorting, flipping and merging data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 7.7 Reshaping a data frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 7.8 Working with text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 7.9 Reading unusual data files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 7.10 Coercing data from one class to another . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 7.11 Other useful data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 7.12 Miscellaneous topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 7.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 8 Basic programming 253 8.1 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 8.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 8.3 Conditional statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 8.4 Writing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 8.5 Implicit loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 vi

Contents from the author

Source page 7

Section 11 — 10 Summary

Section 12 — 10 Summary

8.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 IV Statistical theory 269 9 Introduction to probability 275 9.1 How are probability and statistics different? . . . . . . . . . . . . . . . . . . . . . . . . . 276 9.2 What does probability mean? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 9.3 Basic probability theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 9.4 The binomial distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 9.5 The normal distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 9.6 Other useful distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 9.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 10 Estimating unknown quantities from a sample 301 10.1 Samples, populations and sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 10.2 The law of large numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 10.3 Sampling distributions and the central limit theorem . . . . . . . . . . . . . . . . . . . . 309 10.4 Estimating population parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 10.5 Estimating a confidence interval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 10.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 11 Hypothesis testing 327 11.1 A menagerie of hypotheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 11.2 Two types of errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 11.3 Test statistics and sampling distributions . . . . . . . . . . . . . . . . . . . . . . . . . . 332 11.4 Making decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 11.5 The p value of a test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 11.6 Reporting the results of a hypothesis test . . . . . . . . . . . . . . . . . . . . . . . . . . 338 11.7 Running the hypothesis test in practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 11.8 Effect size, sample size and power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 11.9 Some issues to consider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 11.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 V Statistical tools 349 12 Categorical data analysis 351 12.1 The χ2 goodness-of-fit test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 12.2 The χ2 test of independence (or association) . . . . . . . . . . . . . . . . . . . . . . . . . 363 12.3 The continuity correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 12.4 Effect size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 12.5 Assumptions of the test(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 12.6 The most typical way to do chi-square tests in R . . . . . . . . . . . . . . . . . . . . . . . 371 12.7 The Fisher exact test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 12.8 The McNemar test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 12.9 What’s the difference between McNemar and independence? . . . . . . . . . . . . . . . . 377 12.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 vii

Contents from the author

Source page 8

Section 13 — 10 Testing non-normal data with Wilcoxon tests

Section 14 — 10 Removing the normality assumption

Section 15 — 10 Model selection

Section 16 — 10 Factorial ANOVA 3: unbalanced designs

13 Comparing two means 379 13.1 The one-sample z-test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 13.2 The one-sample t-test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 13.3 The independent samples t-test (Student test) . . . . . . . . . . . . . . . . . . . . . . . . 389 13.4 The independent samples t-test (Welch test) . . . . . . . . . . . . . . . . . . . . . . . . . 398 13.5 The paired-samples t-test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 13.6 One sided tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 13.7 Using the t.test() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 13.8 Effect size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412 13.9 Checking the normality of a sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 13.10 Testing non-normal data with Wilcoxon tests . . . . . . . . . . . . . . . . . . . . . . . . 420 13.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 14 Comparing several means (one-way ANOVA) 425 14.1 An illustrative data set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 14.2 How ANOVA works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 14.3 Running an ANOVA in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 14.4 Effect size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 14.5 Multiple comparisons and post hoc tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 14.6 Assumptions of one-way ANOVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 14.7 Checking the homogeneity of variance assumption . . . . . . . . . . . . . . . . . . . . . 447 14.8 Removing the homogeneity of variance assumption . . . . . . . . . . . . . . . . . . . . . 449 14.9 Checking the normality assumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 14.10 Removing the normality assumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 14.11 On the relationship between ANOVA and the Student t test . . . . . . . . . . . . . . . . 453 14.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 15 Linear regression 457 15.1 What is a linear regression model? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 15.2 Estimating a linear regression model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 15.3 Multiple linear regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 15.4 Quantifying the fit of the regression model . . . . . . . . . . . . . . . . . . . . . . . . . . 464 15.5 Hypothesis tests for regression models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 15.6 Testing the significance of a correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 15.7 Regarding regression coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 15.8 Assumptions of regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 15.9 Model checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 15.10 Model selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 15.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 16 Factorial ANOVA 497 16.1 Factorial ANOVA 1: balanced designs, no interactions . . . . . . . . . . . . . . . . . . . 497 16.2 Factorial ANOVA 2: balanced designs, interactions allowed . . . . . . . . . . . . . . . . . 506 16.3 Effect size, estimated means, and confidence intervals . . . . . . . . . . . . . . . . . . . . 513 16.4 Assumption checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 16.5 The F test as a model comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 16.6 ANOVA as a linear model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 16.7 Different ways to specify contrasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532 16.8 Post hoc tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 16.9 The method of planned comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 16.10 Factorial ANOVA 3: unbalanced designs . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 16.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 viii