21,99 €
Simplify stats and learn how to graph, analyze, and interpret data the easy way
Statistical Analysis with R For Dummies makes stats approachable by combining clear explanations with practical applications. You'll learn how to download and use R and RStudio—two free, open-source tools—to learn statistics concepts, create graphs, test hypotheses, and draw meaningful conclusions. Get started by learning the basics of statistics and R, calculate descriptive statistics, and use inferential statistics to test hypotheses. Then, visualize it all with graphs and charts. This Dummies guide is your well-marked path to sailing through statistics.
This is the perfect introduction to R for students, professionals, and the stat-curious.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 508
Veröffentlichungsjahr: 2025
Cover
Table of Contents
Title Page
Copyright
Introduction
About This Book
Similarity with This Other For Dummies Book
What You Can Safely Skip
Foolish Assumptions
How This Book Is Organized
Icons Used in This Book
Where to Go from Here
Part 1: Getting Started with Statistical Analysis with R
Chapter 1: Data, Statistics, and Decisions
The Statistical (and Related) Notions You Just Have to Know
Inferential Statistics: Testing Hypotheses
Chapter 2: R: What It Does and How It Does It
Downloading R and RStudio
A Session with R
R Functions
User-Defined Functions
Comments
R Structures
Packages
More Packages
R Formulas
Reading and Writing
Part 2: Describing Data
Chapter 3: Getting Graphic
Finding Patterns
Base R Graphics
Graduating to ggplot2
Wrapping Up
Chapter 4: Finding Your Center
Means: The Lure of Averages
The Average in R: mean()
Medians: Caught in the Middle
The Median in R: median()
Statistics à la Mode
The Mode in R
Chapter 5: Deviating from the Average
Measuring Variation
Back to the Roots: Standard Deviation
Standard Deviation in R
Conditions, Conditions, Conditions …
Chapter 6: Meeting Standards and Standings
Catching Some Z’s
Standard Scores in R
Where Do You Stand?
Summarizing
Chapter 7: Summarizing It All
How Many?
The High and the Low
Living in the Moments
Tuning in the Frequency
Summarizing a Data Frame
Chapter 8: What’s Normal?
Hitting the Curve
Working with Normal Distributions
A Distinguished Member of the Family
Part 3: Drawing Conclusions from Data
Chapter 9: The Confidence Game: Estimation
Understanding Sampling Distributions
An EXTREMELY Important Idea: The Central Limit Theorem
Confidence: It Has Its Limits!
Fit to a t
Chapter 10: One-Sample Hypothesis Testing
Hypotheses, Tests, and Errors
Hypothesis Tests and Sampling Distributions
Catching Some Z’s Again
Z Testing in R
t for One
t Testing in R
Working with t-Distributions
Visualizing t-Distributions
Testing a Variance
Working with Chi-Square Distributions
Visualizing Chi-Square Distributions
Chapter 11: Two-Sample Hypothesis Testing
Hypotheses Built for Two
Sampling Distributions Revisited
t for Two
Like Peas in a Pod: Equal Variances
t-Testing in R
A Matched Set: Hypothesis Testing for Paired Samples
Paired Sample t-Testing in R
Testing Two Variances
Working with
F
-Distributions
Visualizing
F
-Distributions
Chapter 12: Testing More than Two Samples
Testing More than Two
ANOVA in R
Another Kind of Hypothesis, Another Kind of Test
Getting Trendy
Trend Analysis in R
Chapter 13: More Complicated Testing
Cracking the Combinations
Two-Way ANOVA in R
Two Kinds of Variables … at Once
After the Analysis
Multivariate Analysis of Variance
Chapter 14: Regression: Linear, Multiple, and the General Linear Model
The Plot of Scatter
Graphing Lines
Regression: What a Line!
Linear Regression in R
Juggling Many Relationships at Once: Multiple Regression
ANOVA: Another Look
Analysis of Covariance: The Final Component of the GLM
But Wait — There's More
Chapter 15: Correlation: The Rise and Fall of Relationships
Scatterplots Again
Understanding Correlation
Correlation and Regression
Testing Hypotheses About Correlation
Correlation in R
Multiple Correlation
Partial Correlation
Partial Correlation in R
Semipartial Correlation
Semipartial Correlation in R
Chapter 16: Curvilinear Regression: When Relationships Get Complicated
What Is a Logarithm?
What Is e?
Power Regression
Exponential Regression
Logarithmic Regression
Polynomial Regression: A Higher Power
Which Model Should You Use?
Part 4: Working with Probability
Chapter 17: Introducing Probability
What Is Probability?
Compound Events
Conditional Probability
Large Sample Spaces
R Functions for Counting Rules
Random Variables: Discrete and Continuous
Probability Distributions and Density Functions
The Binomial Distribution
The Binomial and Negative Binomial in R
Hypothesis Testing with the Binomial Distribution
More on Hypothesis Testing: R versus Tradition
Chapter 18: Introducing Modeling
Modeling a Distribution
A Simulating Discussion
Chapter 19: Probability Meets Regression: Logistic Regression
Getting the Data
Doing the Analysis
Visualizing the Results
Part 5: The Part of Tens
Chapter 20: Ten Tips for Excel Émigrés
Defining a Vector in R Is Like Naming a Range in Excel
Operating On Vectors Is Like Operating On Named Ranges
Sometimes Statistical Functions Work the Same Way …
… and Sometimes They Don’t
Contrast: Excel and R Work with Different Data Formats
Distribution Functions Are (Somewhat) Similar
A Data Frame Is (Something) Like a Multicolumn Named Range
The
sapply()
Function Is Like Dragging
Using
data_edit()
Is (Almost) Like Editing a Spreadsheet
Use the Clipboard to Import a Table from Excel into R
Chapter 21: Ten Valuable Online R Resources
Websites for R Users
Online Books and Documentation
Index
About the Author
Connect with Dummies
End User License Agreement
Cover
Table of Contents
Title Page
Copyright
Begin Reading
Index
About the Author
i
ii
1
2
3
4
5
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
403
404
405
406
407
408
409
410
411
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
Statistical Analysis with R™ For Dummies®, 2nd Edition
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2025 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
Media and software compilation copyright © 2025 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
Published simultaneously in Canada.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
The manufacturer’s authorized representative according to the EU General Product Safety Regulation is Wiley-VCH GmbH, Boschstr. 12, 69469 Weinheim, Germany, e-mail: [email protected].
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number is available from the publisher.
ISBN: 978-1-394-34306-5 (pbk); 978-1-394-34308-9 (ebk); 978-1-394-34307-2 (ebk)
So, you’re holding a statistics book. In my humble (and absolutely biased) opinion, it’s not just another statistics book. It’s also not just another R book. I say this for two reasons.
First, many statistics books teach you the concepts but don’t give you an easy way to apply them. That often leads to a lack of understanding. Because R is ready-made for statistics, it’s a tool for applying (and learning) statistics concepts.
Second, let’s look at it from the opposite direction: Before I tell you about one of R’s features, I give you the statistical foundation it’s based on. That way, you understand that feature when you use it — and you use it more effectively.
I didn’t want to write a book that only covers the details of R and introduces some clever coding techniques. Some of that is necessary, of course, in any book that shows you how to use a software tool like R. My goal was to go way beyond that.
Neither did I want to write a statistics “cookbook” — when-faced-with-problem-category-#152-use-statistical-procedure-#346. My goal was to go way beyond that, too.
Bottom line: This book isn’t just about statistics or just about R — it’s firmly at the intersection of the two. In the proper context, R can be a useful tool for teaching and learning statistics, and I’ve tried to supply the proper context.
Although the field of statistics proceeds in a logical way, I’ve organized this book so that you can open it up in any chapter and start reading. The idea is for you to find the information you’re looking for in a hurry and use it immediately — whether it’s a statistical concept or an R-related one.
On the other hand, reading from cover to cover is okay if you’re so inclined. If you’re a statistics newbie and you have to use R to analyze data, I recommend that you begin at the beginning.
You might be aware that I’ve written another book: Statistical Analysis with Excel For Dummies (Wiley). This is not a shameless plug for that book. (I do that elsewhere.)
I’m just letting you know that the sections in this book that explain statistical concepts are much like the corresponding sections in the other book. I use (mostly) the same examples and, in many cases, the same words. I’ve developed that material during decades of teaching statistics and found it to be quite effective. (Reviewers seem to like it, too.) Also, if you happen to have read the other book and you’re transitioning to R, the common material might just help you make the switch.
And, you know: If it ain’t broke… .
Any reference book throws a lot of information at you, and this one is no exception. I intended for it all to be useful, but I didn’t aim it all at the same level. So if you’re not deeply into the subject matter, you can avoid paragraphs marked with the Technical Stuff icon.
As you read, you’ll run into sidebars. They provide information that elaborates on a topic, but they’re not part of the main path. If you’re in a hurry, you can breeze past them.
I’m assuming this much about you:
You know how to work with Windows or the Mac.
I don’t describe the details of pointing, clicking, selecting, and other actions
.
You’re able to install R and RStudio (I show you how in
Chapter
2
) and follow along with the examples.
I use the Windows version of RStudio, but you should have no problem if you’re working on a Mac.
I’ve organized this book into five parts and two appendixes, which you can find on this book’s companion website at www.dummies.com/go/statisticalanalysiswithrfd. (The website also includes a copy of all the sample code I use in this book in a downloadable format.)
In Part 1, I provide a general introduction to statistics and to R. I discuss important statistical concepts and describe useful R techniques. If it’s been a long time since your last course in statistics or if you’ve never even had a statistics course, start with Part 1. If you have never worked with R, definitely start with Part 1.
Part of working with statistics is to summarize data in meaningful ways. In Part 2, you find out how to do that. Most people know about averages and how to compute them. But that’s not the whole story. In Part 2, I tell you about additional statistics that fill in the gaps, and I show you how to use R to work with those statistics. I also introduce R graphics in this part.
Part 3 addresses the fundamental aim of statistical analysis: to go beyond the data and help you make decisions. Usually, the data are measurements of a sample taken from a large population. The goal is to use these data to figure out what’s going on in the population.
This opens a wide range of questions: What does an average mean? What does the difference between two averages mean? Are two things associated? These are only a few of the questions I address in Part 3, and I discuss the R functions that help you answer them.
Probability is the basis for statistical analysis and decision-making. In Part 4, I tell you all about it. I show you how to apply probability, particularly in the area of modeling. New in this edition is a chapter on a statistical technique called logistic regression that marries a method from Part 3 with probability. R provides a rich set of capabilities that deal with probability, and here’s where you find them.
Part 5 has two chapters. In the first, I give Excel users ten tips for moving to R. In the second, I cover ten valuable R-related resources you can find online.
This online appendix continues what I start in Part 4. The material is a bit on the esoteric side, so I’ve stashed it in an appendix.
Non-parametric statistics are based on concepts that differ somewhat from most of the rest of this book. In this appendix, you learn these concepts and see how to use R to apply them.
Icons appear all over For Dummies books, and this one is no exception. Each one is a little picture in the margin that lets you know something special about the paragraph it sits next to.
This icon points out a hint or a shortcut that can help you in your work (and perhaps make you a finer, kinder, and more insightful human being).
This one points out timeless wisdom to take with you on your continuing quest for statistics knowledge.
Pay attention to the information accompanied by this icon. It’s a reminder to avoid something that might gum up the works for you.
As I mention in the earlier section “What You Can Safely Skip,” this icon indicates material you can blow past if it’s just too technical. (I’ve kept this to a minimum.)
You can start reading this book anywhere, but here are a couple of hints. Want to learn the foundations of statistics? Turn the page. Introduce yourself to R? That’s Chapter 2. Want to start with graphics? Hit Chapter 3. For anything else, find it in the table of contents or the index and go for it.
In addition to what you’re reading right now, this product comes with a free access-anywhere Cheat Sheet that presents a selected list of R functions and describes what they do. To get this Cheat Sheet, visit www.dummies.com and type Statistical Analysis with R For Dummies Cheat Sheet in the search box.
Part 1
IN THIS PART …
Find out about R’s statistical capabilities
Explore how to work with populations and samples
Test your hypotheses
Understand errors in decision-making
Determine independent and dependent variables
Chapter 1
IN THIS CHAPTER
Introducing statistical concepts
Generalizing from samples to populations
Getting into probability
Testing hypotheses
Two types of error
Statistics? That’s all about crunching numbers into arcane-looking formulas, right? Not really. Statistics, first and foremost, is about decision-making. Some number-crunching is involved, of course, but the primary goal is to use numbers to make decisions. Statisticians look at data and wonder what the numbers are saying. What kinds of trends are in the data? What kinds of predictions are possible? What conclusions can you make?
To make sense of data and answer these questions, statisticians have developed a wide variety of analytical tools.
About the number-crunching part: If you had to do it via pencil-and-paper (or with the aid of a pocket calculator), you’d soon grow discouraged with the amount of computation involved and the errors that might creep in. Software like R helps you crunch the data and compute the numbers. As a bonus, R can also help you comprehend statistical concepts.
Developed specifically for statistical analysis, R is a computer language that implements many of the analytical tools statisticians have developed for decision-making. I wrote this book to show you how to use these tools in your work.
The analytical tools that R provides are based on statistical concepts I help you explore in the remainder of this chapter. As you’ll see, these concepts are based on common sense.
If you watch TV on election night, you know that one exciting occurrence that takes place before the main event is the prediction of the outcome immediately after the polls close (and before all the votes are counted). How is it that pundits almost always get it right?
The idea is to talk to a sample of voters right after they vote. If they’re truthful about how they marked their ballots, and if the sample is representative of the population of voters, analysts can use the sample data to draw conclusions about the population.
That, in a nutshell, is what statistics is all about — using the data from samples to draw conclusions about populations.
Here’s another example. Imagine that your job is to find the average height of 10-year-old children in the United States. Because you probably wouldn’t have the time or the resources to measure every child, you’d measure the heights of a representative sample. Then you’d average those heights and use that average as the estimate of the population average.
Estimating the population average is one kind of inference that statisticians make from sample data. I discuss inference in more detail in the upcoming section “Inferential Statistics: Testing Hypotheses.”
Here’s some important terminology: Properties of a population (like the population average) are called parameters, and properties of a sample (like the sample average) are called statistics. If your only concern is the sample properties (like the heights of the children in your sample), the statistics you calculate are descriptive. If you’re concerned about estimating the population properties, your statistics are inferential.
Now for an important convention about notation: Statisticians use Greek letters (μ, σ, ρ) to stand for parameters, and English letters (, s, r) to stand for statistics. Figure 1-1 summarizes the relationship between populations and samples, and between parameters and statistics.
FIGURE 1-1: The relationship between populations, samples, parameters, and statistics.
A variable is something that can take on more than one value — like your age, the value of the dollar against other currencies, or the number of games your favorite sports team wins. Something that can have only one value is a constant. Scientists tell us that the speed of light is a constant, and we use the constant π to calculate the area of a circle.
Statisticians work with independent variables and dependent variables. In any study or experiment, you’ll find both kinds. Statisticians assess the relationship between them.
Imagine a computerized training method designed to increase a person’s IQ. How would a researcher find out whether this method does what it’s supposed to do? First, that person would randomly assign a sample of people to one of two groups. One group would receive the training method, and the other would complete another kind of computer-based activity — like reading text on a website. Before and after each group completes its activities, the researcher measures each person’s IQ. What happens next? I discuss that topic in the upcoming section “Inferential Statistics: Testing Hypotheses.”
For now, understand that the independent variable here is Type of Activity. The two possible values of this variable are IQ Training and Reading Text. The dependent variable is the change in IQ from Before to After.
A dependent variable is what a researcher measures. In an experiment, an independent variable is what a researcher manipulates. In other contexts, a researcher can’t manipulate an independent variable. Instead, they note naturally occurring values of the independent variable and how they affect a dependent variable.
In general, the objective is to find out whether changes in an independent variable are associated with changes in a dependent variable.
In the examples that appear throughout this book, I show you how to use R to calculate characteristics of groups of scores, or to compare groups of scores. Whenever I show you a group of scores, I’m talking about the values of a dependent variable.
When you do statistical work, you can run into four kinds of data. And when you work with a variable, the way you work with it depends on what kind of data it is. The first kind is nominal data. If a set of numbers happens to be nominal data, the numbers are labels — their values don’t signify anything. On a sports team, the jersey numbers are nominal. They just identify the players.
The next kind is ordinal data. In this data type, the numbers are more than just labels. As the name ordinal might tell you, the order of the numbers is important. If I ask you to rank ten foods from the one you like best (1) to the one you like least (10), we’d have a set of ordinal data.
But the difference between your third-favorite food and your fourth-favorite food might not be the same as the difference between your ninth-favorite and your tenth-favorite. So this type of data lacks equal intervals and equal differences.
Interval data gives us equal differences. The Fahrenheit scale of temperature is a good example. The difference between 30o and 40o is the same as the difference between 90o and 100o. So each degree is an interval.
People are sometimes surprised to find out that on the Fahrenheit scale, a temperature of 80o is not twice as hot as 40o. For ratio statements (“twice as much as,” “half as much as”) to make sense, zero has to mean the complete absence of the thing you’re measuring. A temperature of 0o F doesn’t mean the complete absence of heat — it’s just an arbitrary point on the Fahrenheit scale. (The same holds true for Celsius.)
The fourth kind of data, ratio, provides a meaningful zero point. On the Kelvin scale of temperature, zero means “absolute zero,” where all molecular motion (the basis of heat) stops. So 200o Kelvin is twice as hot as 100o Kelvin. Another example is length. Eight inches is twice as long as 4 inches. Zero inches means “a complete absence of length.”
An independent variable or a dependent variable can be either nominal, ordinal, interval, or ratio. The analytical tools you use depend on the type of data you work with.
When statisticians make decisions, they use probability to express their confidence about those decisions. They can never be absolutely certain about what they decide. They can only tell you how probable their conclusions are.
What do I mean by probability? Mathematicians and philosophers might give you complex definitions. In my experience, however, the best way to understand probability is in terms of examples.
Here’s a simple example: If you toss a coin, what’s the probability that it turns up heads? If the coin is fair, you might figure that you have a 50-50 chance of heads and a 50-50 chance of tails. And you’d be right. In terms of the kinds of numbers associated with probability, that’s ½.
Think about rolling a fair die (one member of a pair of dice). What’s the probability that you roll a 4? Well, a die has six faces and one of them is 4, so that’s ⅙. Still another example: Select 1 card at random from a standard deck of 52 cards. What’s the probability that it’s a diamond? A deck of cards has four suits, so that’s ¼.
These examples tell you that if you want to know the probability that an event occurs, count how many ways that event can happen and divide by the total number of events that can happen. In the first two examples (heads, 4), the event you’re interested in happens in only one way. For the coin, you divide 1 by 2. For the die, you divide 1 by 6. In the third example (diamond), the event can happen in 1 of 13 ways (ace through king), so you divide 13 by 52 (to get ¼).
Now for a slightly more complicated example. Toss a coin and roll a die at the same time. What’s the probability of tails and a 4? Think about all the possible events that can happen when you toss a coin and roll a die at the same time. You could have tails and 1 through 6, or heads and 1 through 6. That adds up to 12 possibilities. The tails-and-4 combination can happen only one way. So the probability is .
In general, the formula for the probability that a particular event occurs is
At the beginning of this section, I say that statisticians express their confidence about their conclusions in terms of probability, which is why I brought all this up in the first place. This line of thinking leads to conditional probability — the probability that an event occurs given that some other event occurs. Suppose that I roll a die, look at it (so that you don’t see it), and tell you that I rolled an odd number. What’s the probability that I’ve rolled a 5? Ordinarily, the probability of a 5 is ⅙, but “I rolled an odd number” narrows it down. That piece of information eliminates the three even numbers (2, 4, 6) as possibilities. Only the three odd numbers (1, 3, 5) are possible, so the probability is ⅓.
What’s the big deal about conditional probability? What role does it play in statistical analysis? Read on.
Before any statistician begins a study, they draw up a tentative explanation — a hypothesis that tells why the data might come out a certain way. After gathering all the data, the statistician has to decide whether to reject the hypothesis.
That decision is the answer to a conditional probability question — what’s the probability of obtaining the data, given that this hypothesis is correct? Statisticians have tools that calculate the probability. If the probability turns out to be low, the statistician rejects the hypothesis.
Back to coin-tossing for an example: Imagine that you’re interested in whether a particular coin is fair — whether it has an equal chance of heads or tails on any toss. Let’s start with “The coin is fair” as the hypothesis.
To test the hypothesis, you’d toss the coin a number of times — let’s say 100. These 100 tosses are the sample data. If the coin is fair (as per the hypothesis), you’d expect 50 heads and 50 tails.
If it’s 99 heads and 1 tail, you’d surely reject the fair-coin hypothesis: The conditional probability of 99 heads and 1 tail given a fair coin is very low. Of course, the coin could still be fair, and you could, quite by chance, get a 99-1 split, right? Sure. You never really know. You have to gather the sample data (the 100-toss results) and then decide. Your decision might be right, or it might not.
Juries make these types of decisions. In the United States, the starting hypothesis is that the defendant is not guilty (“innocent until proven guilty”). Think of the evidence as data. Jury members consider the evidence and answer a conditional probability question: What’s the probability of the evidence, given that the defendant is not guilty? Their answer determines the verdict.
Think again about that coin-tossing study I just mentioned. The sample data are the results from the 100 tosses. I said that we can start with the hypothesis that the coin is fair. This starting point is called the null hypothesis. The statistical notation for the null hypothesis is H0. According to this hypothesis, any heads-tails split in the data is consistent with a fair coin. Think of it as the idea that nothing in the sample data is out of the ordinary.
An alternative hypothesis is possible — that the coin isn’t a fair one and it’s loaded to produce an unequal number of heads and tails. This hypothesis says that any heads-tails split is consistent with an unfair coin. This alternative hypothesis is called, believe it or not, the alternative hypothesis. The statistical notation for the alternative hypothesis is H1.
Now toss the coin 100 times and note the number of heads and tails. If the results are something like 90 heads and 10 tails, it’s a good idea to reject H0. If the results are around 50 heads and 50 tails, don’t reject H0.
Similar ideas apply to the IQ example I gave earlier. One sample receives the computer-based IQ training method, and the other participates in a different computer-based activity — like reading text on a website. Before and after each group completes its activities, the researcher measures each person’s IQ. The null hypothesis, H0, is that one group’s improvement isn’t different from the other. If the improvements are greater with the IQ training than with the other activity — so much greater that it’s unlikely that the two aren’t different from one another — reject H0. If they’re not, don’t reject H0.
Notice that I did not say “accept H0.” The way the logic works, you never accept a hypothesis. You either reject H0 or don’t reject H0. In a jury trial, the verdict is either “guilty” (reject the null hypothesis of “not guilty”) or “not guilty” (don’t reject H0). “Innocent” (acceptance of the null hypothesis) is not a possible verdict.
Notice also that in the coin-tossing example, I said “around 50 heads and 50 tails.” What does around mean? Also, I said that if it’s 90-10, reject H0. What about 85-15? 80-20? 70-30? Exactly how much different from 50-50 does the split have to be for you to reject H0? In the IQ training example, how much greater does the IQ improvement have to be to reject H0?
I won’t answer these questions now. Statisticians have formulated decision rules for situations like this, and I’ll help you explore those rules throughout the book.
Whenever you evaluate data and decide to reject H0 or to not reject H0, you can never be absolutely sure. You never really know the “true” state of the world. In the coin-tossing example, that means you can’t be certain whether the coin is fair. All you can do is make a decision based on the sample data. If you want to know for sure about the coin, you have to have the data for the entire population of tosses — which means you have to keep tossing the coin until the end of time.
Because you’re never certain about your decisions, you can make an error either way you decide. As I mention earlier, the coin could be fair, and you just happen to get 99 heads in 100 tosses. That’s not likely, and that’s why you reject H0 if that happens. It’s also possible that the coin is biased, yet you just happen to toss 50 heads in 100 tosses. Again, that’s not likely and you don’t reject H0 in that case.
Although those errors aren’t likely, they’re possible. They lurk in every study that involves inferential statistics. Statisticians have named them Type I errors and Type II errors.
If you reject H0 and you shouldn’t, that’s a Type I error. In the coin example, that’s rejecting the hypothesis that the coin is fair when in reality it’s a fair coin.
If you don’t reject H0 and you should have, that’s a Type II error. It happens when you don’t reject the hypothesis that the coin is fair and in reality it’s biased.
How do you know whether you’ve made either type of error? You don’t — at least not right after you make the decision to reject or not reject H0. (If it’s possible to know, you wouldn’t make the error in the first place!) All you can do is gather more data and see whether the additional data is consistent with your decision.
If you think of H0 as a tendency to maintain the status quo and not interpret anything as being out of the ordinary (no matter how it looks), a Type II error means you’ve missed out on something big. In fact, some iconic mistakes are Type II errors.
Here’s what I mean. On New Year’s Day in 1962, a rock group consisting of three guitarists and a drummer auditioned in the London studio of a major recording company. Legend has it that the recording executives didn’t like what they heard, didn’t like what they saw, and believed that guitar groups were on their way out. Although the musicians played their hearts out, the group failed the audition.
Who was that group? The Beatles!
And that’s a Type II error.
Chapter 2
IN THIS CHAPTER
Getting R and RStudio
Working with RStudio
Learning R functions
Learning R structures
Working with packages
Forming R formulas
Reading and writing files
R is a computer language — it’s a tool for doing the computation and number-crunching that set the stage for statistical analysis and decision-making. An important aspect of statistical analysis is to present the results in a comprehensible way. For this reason, graphics is a major component of R.
Ross Ihaka and Robert Gentleman developed R in the 1990s at the University of Auckland, New Zealand. Supported by the Foundation for Statistical Computing, R is one of the most popular computer languages.
RStudio is an open-source integrated development environment (IDE) for creating and running R code. It’s available in versions for Windows, Mac, and Linux. Although you don’t need an IDE in order to work with R, RStudio makes life much easier.
To download both R and RStudio, visit https://posit.co/download/rstudio-desktop . This page is on the website of Posit, the company that provides RStudio. Helpfully, this page gives you links to install R as well as RStudio — and, yes, you do have to install R first. For both installations, be sure to follow the appropriate instructions for your type of computer.
After the RStudio installation is finished, click the RStudio icon to open the window shown in Figure 2-1.
FIGURE 2-1: RStudio, immediately after you install it and click on its icon.
I’m working in Windows 11 with R version 4.4.2 and RStudio 2024.09.1 Build 394. By the time you read this chapter, newer versions of R and RStudio might be available.
Every version of R is associated with an enigmatic phrase. For version 4.4.2, as Figure 2-1 shows, it’s “Pile of Leaves.” For an earlier version, it was “Great Pumpkin.” It turns out that the origin of all these phrases is the Peanuts comic strip.
The large Console pane on the left runs R code. One way to run R code is to type it directly into the Console pane and then press Enter (or press Return on a Mac). I show you another way in a moment. This pane also has a Terminal tab (which provides a command-line interface) and a tab labeled Background Jobs.
The other two panes provide helpful information as you work with R. The upper right pane has tabs labeled Environment, History, Connections, and Tutorial. The Environment tab keeps track of the things you create (which R calls objects) as you work with R. The History tab tracks R code that you enter.
Get used to the word object. Everything in R is an object.
The Connections tab shows connections to local or remote databases. The Tutorial tab, as its name suggests, runs tutorials for RStudio. These tutorials depend on additional items you have to install.
The Files, Plots, Packages, Help, Viewer, and Presentation tabs are in the pane in the lower right. The Files tab shows files you create. The Plots tab holds graphs you create from your data. The Packages tab shows add-ons (called packages) you downloaded as part of the R installation. Bear in mind that downloaded doesn’t mean “ready to use.” To use a package’s capabilities, one more step is necessary and — believe me — you’ll want to use packages.
Figure 2-2 shows the Packages tab. The packages are in either the User library (which you can see in the figure) or the System library (which you have to scroll down to). I discuss packages later in this chapter.
FIGURE 2-2: The RStudio Packages tab.
The Help tab, shown in Figure 2-3, provides links to a wealth of information about R and RStudio. (Check out the Posit Cheat Sheets.) The Viewer tab displays local web content. The Presentation tab shows HTML slides.
FIGURE 2-3: The RStudio Help tab.
To tap into the full power of RStudio as an IDE, click the icon in the upper right corner of the Console pane. That changes the appearance of RStudio so that it looks like Figure 2-4.
The top of the Console pane relocates to the lower left. The new pane in the upper left corner is the Scripts pane. You type and edit code in the Scripts pane and press Ctrl+Enter (or Control+Return on a Mac) and then the code executes in the Console pane.
In the Scripts pane, you can also choose Code ⇒ Run Selected Line(s), but that’s the 19th item on the drop-down menu.
FIGURE 2-4: RStudio, after you click the icon in the upper right corner of the Console pane.
Before you start working, choose File ⇒ Save As from the main menu and then save your first session as My First R Session. This relabels the tab in the Scripts pane with the name of the file and adds the .R extension. This also causes the filename (along with the .R extension) to appear on the Files tab.
What exactly does R save, and where does R save it? What R saves is called the workspace, which is the environment you're working in. R saves the workspace in the working directory. To get the path to your working directory, type getwd() after the right-pointing arrowhead in the Console pane and press Enter (or Return on a Mac), and then R returns the path.
The right-pointing arrowhead at the beginning of the line is just a prompt for you. It serves no other purpose.
My working directory looks like this:
> getwd()[1] "C:/Users/jschm/OneDrive/Documents"
Note which way the slashes are slanted. They’re opposite to what you typically see in Windows file paths. This is because R uses \ as an escape character, meaning that whatever follows the \ means something different from what it usually means. For example, \t in R means “Tab key.
You can also write a Windows file path in R as
C:\\Users\\<User Name>\\Documents
If you like, you can change the working directory:
> setwd(<file path>)
Another way to change the working directory is to choose Session ⇒ Set Working Directory ⇒ Choose Directory from the main menu.
And now for some R! In the Scripts pane, type x <- c(3,4,5)