Graphical Models With R
Graphical Models With R
STATISTICS WITH R
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the valid-
ity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or uti-
lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy-
ing, microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.
For permission to photocopy or use material electronically from this work, please access [Link] (http://
[Link]/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For
organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
[Link]
and the CRC Press Web site at
[Link]
Preface
The Book
Probability and Statistics with R is a work born of the love of statistics and the
advancements that have been made in the field as more powerful computers can be used to
perform calculations and simulations that were only dreamed of by those who came before.
The S language and its derivative, R, have made the practice of statistics available to anyone
with the time and inclination to do so.
Teachers will enjoy the real-world examples and the thoroughly worked out derivations.
Those wanting to use this book as a reference work will appreciate the extensive treat-
ments on data analysis using appropriate techniques, both parametric and nonparametric.
Students who are visual learners will appreciate the detailed graphics and clear captions,
while the hands-on learners will be pleased with the abundant problems and solutions.
(A solutions manual should be available from Taylor & Francis.) It is our hope that
practitioners of statistics at every level will welcome the features of this book and that
it will become a valuable addition to their statistics libraries.
The Purpose
Our primary intention when we undertook this project was to introduce R as a teaching
statistical package, rather than just a program for researchers. As much as possible, we
have made a great effort to link the statistical contents with the procedures used by R to
show consistency to undergraduate students. The reader who uses S-PLUS will also find
this text useful, as S-PLUS commands are included with those for R in the vast majority of
the examples.
This book is intended to be practical, readable, and clear. It gives the reader real-world
examples of how S can be used to solve problems in every topic covered including, but
not limited to, general probability in both the univariate and multivariate cases, sampling
distributions and point estimation, confidence intervals, hypothesis testing, experimental
design, and regression. Most of the problems are taken from genuine data sets rather than
created out of thin air. Next, it is unusually thorough in its treatment of virtually every
topic, covering both the traditional methods to solve problems as well as many nonparamet-
ric techniques. Third, the figures used to explain difficult topics are exceptionally detailed.
v
vi
Finally, the derivations of difficult equations are worked out thoroughly rather than being
left as exercises. These features, and many others, will make this book beneficial to any
reader interested in applying the S language to the world of statistics.
The Program
The S language includes both R and S-PLUS. “R can be regarded as an implementation of
the S language which was developed at Bell Laboratories by Rick Becker, John Chambers,
and Allan Wilks, and also forms the basis of the S-PLUS systems.”
([Link]
The current R is the result of a collaborative effort with contributions from all over
the world. R was initially written by Robert Gentleman and Ross Ihaka of the Statistics
Department of the University of Auckland. Since mid-1997 there has been a core group
with write access to the R source ([Link] “Contributors”on the
sidebar).
Not only is R an outstanding statistical package, but it is offered free of charge and can
be downloaded from [Link] The authors are greatly indebted to the
giants of statistics and programming on whose shoulders we have stood to see what we will
show the readers of this text.
The Content
The core of the material covered in this text has been used in undergraduate courses at
the Public University of Navarre for the last ten years. It has been used to teach engineering
(agricultural, industrial, and telecommunications) and economics majors. Some of the
material in this book has also been used to teach graduate students studying agriculture,
biology, engineering, and medicine.
The book starts with a brief introduction to S that includes syntax, structures, and
functions. It is designed to provide an overview of how to use both R and S-PLUS so that
even a neophyte will be able to solve the problems by the end of the chapter.
Chapter 2, entitled “Exploring Data,”covers important graphical and numerical descrip-
tive methods. This chapter could be used to teach a first course in statistics.
The next three chapters deal with probability and random variables in a generally classical
presentation that includes many examples and an extensive collection of problems to practice
all that has been learned.
Chapter 6 presents some important statistics and their sampling distributions. Solving
the exercises will give any reader confidence that the difficult topics covered in this chapter
are understood.
The next four chapters encompass point estimation, confidence intervals, hypothesis test-
ing, and a wide range of nonparametric methods including goodness-of-fit tests, categorical
data analysis, nonparametric bootstrapping, and permutation tests.
Chapter 11 provides an introduction to experimental design using fixed and random
effects models as well as the randomized block design and the two-factor factorial design.
The book ends with a chapter on simple and multiple regression analysis. The procedures
from this chapter are used to solve three interesting case studies based on real data.
The Fonts
Knowing several typographical conventions will help the reader in understanding the
material presented in this text. R code is displayed in a monospaced font with the > symbol
in front of commands that are entered at the R prompt.
vii
> x<-0.28354
> round(x,2)
[1] 0.28
The same font is used for data sets and functions, though functions are followed by ().
For example, the PASWR package but the round() function would be shown. Throughout
the text, a is found at the end of solutions to examples. In the index, page numbers
in BOLD are where the primary occurrences of topics are found, while those in ITALICS
indicate the pages where a problem about a topic or using a given data set can be located.
The Web
This text is supported at [Link] arnholta/PASWR on the Internet.
The website has up-to-date errata, chapter scripts, and a copy of the PASWR package (which
is also on CRAN) available for download.
Acknowledgments
We gratefully acknowledge the invaluable help provided by Susie Arnholt. Her willingness
to apply her expertise in LATEX and knowledge of English grammar to the production of
this book is appreciated beyond words.
Several people were instrumental in improving the overall readability of this text. The
recommendations made by Phil Spector, the Applications Manager and Software Consultant
for the Statistical Computing Facility in the Department of Statistics at the University of
California at Berkeley, who reviewed this text for Taylor & Francis, were used in improving
much of the original R code as well as decreasing the inevitable typographical error rate.
Tomás Goicoa, a member of the Spatial Statistics Research Group at the Public University
of Navarre, was of great help in preparing and checking exercises. Celes Alexander, an
Appalachian State University graduate student, graciously read the entire text and found
several typos. Any remaining typos or errors are entirely the fault of the authors.
Thanks to our editor at Taylor & Francis, David Grubbs, for embracing and encouraging
our project. Many thanks the Statistics and Operations Research Department at Public
University of Navarre and to the Department of Mathematical Sciences at Appalachian
State University for the support they gave us in our writing of this text.
The “You choose, you decide” initiative sponsored by Caja Navarra also provided funding
for in-person collaborations. Thanks to the Universidad Nacional de Educación a Distancia,
in particular the Centro Asociado de Pamplona, for allowing us to present this project under
their auspices.
Special thanks to José Luis Iriarte, the former Vicerector of International Relations of
the Public University of Navarre, and to T. Marvin Williamsen, the former Associate Vice
Chancellor for International Programs at Appalachian State University. These men were
instrumental in gaining funding and support for several in-person collaborations including a
year-long visit at the Public University of Navarre for the third author and two multi-week
visits for the first two authors to Appalachian State University.
Finally, to the geniuses of this age who first conceived of the idea of an excellent open
source software for statistics and those who reared the idea to adulthood, our gratitude
is immeasurable. May the lighthouse of your brilliance guide travelers on the ocean of
statistics for decades to come. Thank you, R Core Team.
Contents
1 A Brief Introduction to S 1
1.1 The Basics of S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Using S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4.1 S Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4.2 Mathematical Operations . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.4 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.5 Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
[Link] Using scan() . . . . . . . . . . . . . . . . . . . . . . . . 7
[Link] Using [Link]() . . . . . . . . . . . . . . . . . . . . 8
[Link] Using write() . . . . . . . . . . . . . . . . . . . . . . . 8
[Link] Using dump() and source() . . . . . . . . . . . . . . . 9
1.4.6 Logical Operators and Missing Values . . . . . . . . . . . . . . . . 9
1.4.7 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.8 Vector and Matrix Operations . . . . . . . . . . . . . . . . . . . . 14
1.4.9 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.4.10 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.4.11 Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.4.12 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.4.13 Functions Operating on Factors and Lists . . . . . . . . . . . . . . 19
1.5 Probability Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.6 Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.7 Programming Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.9 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2 Exploring Data 29
2.1 What Is Statistics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3 Displaying Qualitative Data . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3.1 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3.2 Barplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.3.3 Dot Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3.4 Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.4 Displaying Quantitative Data . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.4.1 Stem-and-Leaf Plots . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.4.2 Strip Charts (R Only) . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.4.3 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.5 Summary Measures of Location . . . . . . . . . . . . . . . . . . . . . . . . 39
2.5.1 The Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.5.2 The Median . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
ix
x
2.5.3 Quantiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.5.4 Hinges and Five-Number Summary . . . . . . . . . . . . . . . . . 44
2.5.5 Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.6 Summary Measures of Spread . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.6.1 Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.6.2 Interquartile Range . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.6.3 Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.7 Bivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.7.1 Two-Way Contingency Tables . . . . . . . . . . . . . . . . . . . . 49
2.7.2 Graphical Representations of Two-Way Contingency Tables . . . . 51
2.7.3 Comparing Samples . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.7.4 Relationships between Two Numeric Variables . . . . . . . . . . . 56
2.7.5 Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.7.6 Sorting a Data Frame by One or More of Its Columns . . . . . . . 59
2.7.7 Fitting Lines to Bivariate Data . . . . . . . . . . . . . . . . . . . . 60
2.8 Multivariate Data (Lattice and Trellis Graphs) . . . . . . . . . . . . . . . 65
2.8.1 Arranging Several Graphs on a Single Page . . . . . . . . . . . . . 67
2.8.2 Panel Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.9 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
12 Regression 563
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
12.2 Simple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
12.3 Multiple Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . 565
12.4 Ordinary Least Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
12.5 Properties of the Fitted Regression Line . . . . . . . . . . . . . . . . . . . 570
12.6 Using Matrix Notation with Ordinary Least Squares . . . . . . . . . . . . 571
12.7 The Method of Maximum Likelihood . . . . . . . . . . . . . . . . . . . . . 576
12.8 The Sampling Distribution of β̂ . . . . . . . . . . . . . . . . . . . . . . . . 577
12.9 ANOVA Approach to Regression . . . . . . . . . . . . . . . . . . . . . . . 580
12.9.1 ANOVA with Simple Linear Regression . . . . . . . . . . . . . . . 581
12.9.2 ANOVA with Multiple Linear Regression . . . . . . . . . . . . . . 584
12.9.3 Coefficient of Determination . . . . . . . . . . . . . . . . . . . . . 586
12.9.4 Extra Sum of Squares . . . . . . . . . . . . . . . . . . . . . . . . . 587
[Link] Tests on a Single Parameter . . . . . . . . . . . . . . . 589
[Link] Tests on Subsets of the Regression Parameters . . . . . 591
12.10 General Linear Hypothesis . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
12.11 Model Selection and Validation . . . . . . . . . . . . . . . . . . . . . . . . 597
12.11.1 Testing-Based Procedures . . . . . . . . . . . . . . . . . . . . . . . 597
[Link] Backward Elimination . . . . . . . . . . . . . . . . . . . 597
[Link] Forward Selection . . . . . . . . . . . . . . . . . . . . . 597
[Link] Stepwise Regression . . . . . . . . . . . . . . . . . . . . 598
[Link] Criterion-Based Procedures . . . . . . . . . . . . . . . . 598
[Link] Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 606
12.11.2 Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
[Link] Checking Error Assumptions . . . . . . . . . . . . . . . 607
[Link].1 Assessing Normality and Constant Variance 608
[Link].2 Testing Autocorrelation . . . . . . . . . . . 609
[Link] Identifying Unusual Observations . . . . . . . . . . . . . 610
[Link] High Leverage Observations . . . . . . . . . . . . . . . . 613
12.11.3 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
[Link] Collinearity . . . . . . . . . . . . . . . . . . . . . . . . . 623
[Link] Transformations for Non-Normality and Unequal Error
Variances . . . . . . . . . . . . . . . . . . . . . . . . . . 626
12.12 Interpreting a Logarithmically Transformed Model . . . . . . . . . . . . . 630
12.13 Qualitative Predictors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
12.14 Estimation of the Mean Response for New Values Xh . . . . . . . . . . . 638
12.15 Prediction and Sampling Distribution of New Observations Yh(new) . . . . 639
12.16 Simultaneous Confidence Intervals . . . . . . . . . . . . . . . . . . . . . . 642
12.16.1 Simultaneous Confidence Intervals for Several Mean Responses —
Confidence Band . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
12.16.2 Predictions of g New Observations . . . . . . . . . . . . . . . . . . 643
12.16.3 Distinguishing Pointwise Confidence Envelopes from Confidence
Bands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
12.17 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
xvi
A S Commands 659
References 675
Index 683
List of Figures
1.1 Structures in S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Examples of the plot() function . . . . . . . . . . . . . . . . . . . . . . . 24
1.3 Size, color, and choice of plotting symbol . . . . . . . . . . . . . . . . . . . 24
1.4 Autonomous communities in Spain . . . . . . . . . . . . . . . . . . . . . . 26
2.1 Graphical representation of the data in Grades and Age with the function
barplot() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2 Graphical representation of the data in Grades and Age with the function
dotchart() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3 Graphical representation of the data in Grades and Age with the function
pie() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.4 Nine different graphs labeled according to their shape . . . . . . . . . . . 34
2.5 Strip chart of the number of home runs Babe Ruth hit while playing for the
New York Yankees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.6 Strip chart of the number of home runs Babe Ruth hit per season according
to the team for which he was playing . . . . . . . . . . . . . . . . . . . . . 36
2.7 Histograms created using different bin definitions for the number of home
runs hit by Babe Ruth while playing for the New York Yankees . . . . . . 37
2.8 Histograms created using different bin definitions for the eruption duration
of Old Faithful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.9 Histogram of waiting time between Old Faithful eruptions with superim-
posed density estimate as well as a density plot . . . . . . . . . . . . . . . 39
2.10 Boxplot illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.11 Boxplot of car prices with five-number summaries labeled . . . . . . . . . . 47
2.12 Stacked and side-by-side barplots for levels of palpitation (Teasy) and physi-
cian (Doctor) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.13 Side-by-side barplots showing percentages in obstructive contacts categories
by treatments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.14 Side-by-side barplots showing percentages in obstructive contacts’ categories
by treatments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.15 Side-by-side boxplots of BWI in the traditional sitting and hamstring stretch
positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.16 Density plots of BWI in the traditional sitting and hamstring stretch posi-
tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.17 Quantile-quantile plot of BWI in the traditional sitting and hamstring
stretch positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.18 Scatterplot of log(brain) versus log(body) for Example 2.21 . . . . . . 58
2.19 Graph depicting residuals. The vertical distances shown with a dotted line
between the Yi s, depicted with a solid circle, and the Ŷi s, depicted with a
clear square, are the residuals. . . . . . . . . . . . . . . . . . . . . . . . . 62
2.20 Scatterplot of log(brain) versus log(body) with superimposed regression
lines computed with (solid line) and without (dashed line) dinosaurs . . . 63
xvii
xviii
5.1 Graphical representation of the domain of interest for joint PDF example . 174
5.2 Graphical representation of fX,Y (x, y) = 8xy, 0 ≤ y ≤ x ≤ 1 . . . . . . . . 179
5.3 Scatterplots showing positive, negative, and zero covariance between two
random variables where pX,Y (x, y) = 1/10 for each of the ten pairs of plotted
points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.4 Bivariate normal density representations . . . . . . . . . . . . . . . . . . . 188
6.1 Empirical cumulative distribution function of rolling a die 100 times . . . . 206
xix
6.2 Sampling distributions of X̄ and S 2 under random sampling (RS) and simple
random sampling (SRS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
6.3 Comparison of uniform and normal graphs . . . . . . . . . . . . . . . . . . 215
6.4 Simulations of uniform and exponential distributions . . . . . . . . . . . . 216
6.5 Uniform and exponential simulations . . . . . . . . . . . . . . . . . . . . . 217
6.6 Probability histogram with normal density . . . . . . . . . . . . . . . . . . 221
6.7 Illustrations of the pdfs of χ23 , χ26 , and χ216 random variables . . . . . . . . 227
6.8 Probability histograms for simulated distributions of (n − 1)S 2 /σ 2 when
sampling from normal and exponential distributions . . . . . . . . . . . . . 236
6.9 Illustrations of the pdfs of t1 (dashed line), t3 (dotted line), and t∞ (solid
line) random variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
6.10 Illustrations of the pdfs of F2,4 (solid line), F4,9 (dotted line), and F19,19
(dashed line) random variables . . . . . . . . . . . . . . . . . . . . . . . . 239
8.1 Standard normal distribution with an area of α/2 in each tail . . . . . . . 293
8.2 Simulated confidence intervals for the population mean when sampling from
a normal distribution with known variance . . . . . . . . . . . . . . . . . 295
8.3 Quantile-quantile (normal distribution) plot of weekly monies spent on gro-
ceries for 30 randomly selected Watauga households . . . . . . . . . . . . 296
8.4 Quantile-quantile plot of the asking price for 14 randomly selected three-
bedroom/two-bath houses in Watauga County, North Carolina . . . . . . . 302
8.5 Superimposed normal quantile-quantile plots of the hardness values for fresh
and warehoused apples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
8.6 Superimposed normal quantile-quantile plots of mathematical assessment
scores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
8.7 Normal quantile-quantile plot of the time differences between Sun and Dig-
ital workstations to complete complex simulations . . . . . . . . . . . . . . 315
8.8 Quantile-quantile plot of the time differences between Sun and Digital work-
stations to complete complex simulations shown in the middle with normal
quantile-quantile plots of random normal data depicted on the outside plots 315
8.9 Chi-square distribution with six degrees of freedom depicting the points
χ2α/2;6 and χ21−α/2;6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
8.10 Quantile-quantile plot of 1932 barley yield in bushels/acre . . . . . . . . . 318
8.11 F distribution with ten and ten degrees of freedom depicting the points
fα/2;10,10 and f1−α/2;10,10 . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
11.24 Barplot of the mean wear by tire with superimposed individual 95% confi-
dence intervals from Example 11.7 . . . . . . . . . . . . . . . . . . . . . . . 546
11.25 Graphs resulting from [Link](Microamps, Glass, Phosphor) . . 551
11.26 Graphs resulting from using [Link]() on the model [Link] from
Example 11.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
11.27 Interaction plots of glass and phosphor . . . . . . . . . . . . . . . . . . . . 554
11.28 Tukey HSD 95% family-wise confidence intervals for the model [Link] . 555
11.29 Barplot of the means for the six treatment combinations of factors Glass
and Phosphor with individual superimposed 95% confidence intervals . . . 555
5.1 B.S. graduate grades in Linear Algebra and Calculus III . . . . . . . . . . 172
5.2 Values used to compute covariance for scatterplots with positive, negative,
and zero covariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.3 Joint probability distribution for X and Y . . . . . . . . . . . . . . . . . . 184
xxiii
xxiv
1.2 Using S
When S is launched, the prompt, >, is displayed in the commands window, indicating
that the software is ready to receive input. The convention used in this text is to show
what is typed after the command prompt (>) followed by the output generated from what
is typed. A single expression or assignment is carried out once the user presses the Enter
key. There is no punctuation required for single expressions and assignments. However,
if the user wants to issue multiple expressions and/or assignments on a single line, each
expression or assignment must be separated with a semicolon (;). To terminate an S session,
either type q() at the command-line in the commands window or choose Exit from the
File menu in a GUI environment. On-line help can be accessed by clicking HELP in a
GUI environment or by typing help(name of command) or ?(name of command) at the
command-line. Another way of learning about a function or data set in R is to use the
function example(). This runs the code in the examples section of the help page. For
instance, to execute the code for the function plot(), enter the following code at the R
prompt:
1
2 Probability and Statistics with R
> par(ask=TRUE)
> example(plot)
The par(ask=TRUE) prompts the user before moving to the next example. The default
is par(ask=FALSE); and with that setting, examples available are shown without a pause,
making reading code and output nearly impossible. S is a case sensitive language! Conse-
quently, X and x refer to different objects. If the user omits a comma or a parenthesis, or
any other type of typographical error occurs when typing at the command-line, a + sign
will appear to indicate that the command is incomplete.
> library(MASS)
after which one would be able to use the data set quine. The data stored in quine can be
seen by typing quine at the command prompt after MASS is loaded. To see all the data sets
in a given package, type data(). If a more complete description of a particular data file,
say Cars93, is desired, enter ?Cars93 at the prompt.
A Brief Introduction to S 3
The functions and data sets used in this book are available in the PASWR package, which
can be downloaded from CRAN at [Link] Scripts for each chapter are
available from [Link] ∼ arnholta/PASWR which also contains func-
tions and data sets for using this book with S-PLUS as well as the R PASWR package.
The use of an editor is highly encouraged for viewing and executing the on-line scripts.
Tinn-R is a free, Windows-only editor the authors have used extensively that can be
found at [Link] Using an editor will also help when one is
writing and debugging code. For more on editors for a variety of operating systems, see
[Link] rgui/projects/[Link].
1.4.1 S Structures
Before the examples, it will be useful to have a picture in mind of how S structures are
related to one another. Figure 1.1 graphically displays the fact that
As the examples progress, it will become clear how S treats these different structures.
Broadly speaking, elements are generally numeric, character, or factor. Factors are categor-
ical data whose categories are called levels. For example, “the cities of North Carolina” is
a categorical variable. A factor with four levels could be cities with populations between 1
and 1000, 1001 and 10,000, and 10,001 and 100,000, and greater than 100,000 inhabitants.
.
.....................................................................................................................................................................
....... ............. . ..
....... .......
An Array .. ...
..........
............................................................................................................................................................................
.....
...............................................................................................................................................................
..... ....
....
...
......
...
.... ...
.
. ...
Matrices
. ..
.. ...
... ..
. ..
.
........................................................................................................................................................ . ... .
. .
......
..
....
. .
.......... .
.
.
.. ....
...
.. .
.... ..
.. . . ...
..
..... ............. . ..
..... ... ... ...
.. ....
..... .................................................................................................................................................................................. . . . ... .
.. .
... ...
.... . ...
....
....... .
....
...... ..
...
...... ..
...
...... .
...
....... ........... ... ...
...
..
.
...
...
................................................................................................................................................................................... ... .. ...
. .
... .
... .
.. .
... ...
.... .... .... .... .... .... ... ... .
.. ... ...
... ...
...
...
...
←− Elements −→ ...
...
...
...
...
...
....
....
..
...
...
...
... .............. ....
.
.
....
...
.
.
..
.
.
....
.
..
.
.
....
...
.
...
...
...
...
...
.............................................................................................................................................................. ... ... ... ... ...
... ... ... . . .
... ...
... .. ..
... ... . . ...
↑ ... .. .. ...
... ... ... .
.. .
.. ... ...
... ... ..... . . . ...
... . .. .. ...
... ... ............. ... .
. .
. ... ...
....................................................................................................................................................... .... .... .... ... ...
... ... ... ... ... ... ...
... ... ... ... ... ... ...
...
... Vectors ...
... .................
. . .
..
.
.
.
..
.
.
.
.
..
.
.
...
...
.............................................................................................................................................................. .... .... .... .... ...
... ... ... ... ... ... ............
... ... ... ... ... ..............
...
↓ ... ... ... ... .
..... .
... ... ...... ... ... .............
... .. ........... .... ... .......
..
............................................................................................................................................................ ... ... ..............
... .... ... . .
.....
..
... ... ... ..............
... ... . .....
... ........
... .......
... ... ..............
....................................................................................................................................................
1.4.3 Vectors
One special type of object is the vector. When working with univariate data, one will
often store information in vectors. The S command to create a vector is c(...). To store
the values 1.5, 2, and 3 in a vector named x, type
> x <- c(1.5,2,3)
> x
[1] 1.5 2.0 3.0
A Brief Introduction to S 5
1.4.4 Sequences
The command seq() creates a sequence of numbers. Sequences of numbers are often
used when creating customized graphs. The three arguments that are typically used with
the command seq() are the starting value, the ending value, and the incremental value.
For example, if a sequence of numbers from 0 to 1 in increments of 0.2 is needed, type
seq(0,1,0.2):
> seq(0,1,0.2)
[1] 0.0 0.2 0.4 0.6 0.8 1.0
When the incremental value is 1, it suffices to use only the starting and ending values of
the sequence:
> seq(0,8)
[1] 0 1 2 3 4 5 6 7 8
6 Probability and Statistics with R
> 0:8
[1] 0 1 2 3 4 5 6 7 8
> 8:0
[1] 8 7 6 5 4 3 2 1 0
The command rep(a, n) is used to repeat the number or character a, n times. For
example,
> rep(1,5)
[1] 1 1 1 1 1
repeats the value 1 five times. S is extremely flexible and allows several commands to be
combined:
> rep(c(0,"x"), 3)
[1] "0" "x" "0" "x" "0" "x"
> rep(c(1,3,2), length=10)
[1] 1 3 2 1 3 2 1 3 2 1
> c(rep(1,3), rep(2,3), rep(3,3))
[1] 1 1 1 2 2 2 3 3 3
> rep(1:3, rep(3,3))
[1] 1 1 1 2 2 2 3 3 3
Specific values in a vector are referenced using square braces []. It is important to keep
in mind that S uses parentheses () with functions and square braces [] to reference values
in vectors, arrays, and lists. A list is an S object whose elements can be of different types
(character, numeric, factor, etc.). The following values, stored in typos, represent the
number of mistakes made per page in the first draft of a research article:
To select the number of mistakes made on the fourth page, type typos[4]:
> typos[4]
[1] 3
To get the number of mistakes made on pages three through six, enter typos[3:6]:
> typos[3:6]
[1] 2 3 3 0
To extract the number of mistakes made on non-continuous pages such as the third, sixth,
and tenth pages, key in typos[c(3,6,10)]:
> typos[c(3,6,10)]
[1] 2 0 4
To extract the number of mistakes on every page except the second and third, input
typos[-c(2,3)]:
A Brief Introduction to S 7
> typos[-c(2,3)]
[1] 2 3 3 0 3 4 6 4
Note that forward slashes (/) are used to specify the path names. To see the gender for
subjects 3 through 6, type
> FAT$sex[3:6]
[1] M M F F
Levels: F M
For R only The file argument of the [Link]() command may be a complete url,
allowing one to read data into R from the Internet. To read the file [Link] stored on the
Internet at
[Link]
type
One of the pitfalls to storing information using write() is that the file will no longer contain
column headings:
23 9.5 M
23 27.9 F
27 7.8 M
. . .
. . .
. . .
61 34.5 F
The R function [Link]() avoids many of the inconveniences associated with the S
function write(). It may be used without transposing the data, it does not lose column
headings, and it generally stores the data as a data frame. To save the data frame FAT to
a pen drive, type [Link](FAT, file="D:/[Link]") at the R prompt. To read
the data stored on the pen drive at a later time, use the function [Link]().
Suppose one is interested in locating subjects whose fat percentages are less than 25%. This
can be accomplished using the with() command in conjunction with fat<25:
To find subjects whose body fat percentages are less than 25% or greater than 35%, enter
To see the fat percentages for subjects with less than 25% fat, type
To remove the subject whose body fat is 7.8 from the previous output, the following may
be used:
R returns the word TRUE or FALSE for a logical condition while S-PLUS returns the letters
T or F, where T represents true and F represents false. From the R output it can be seen
that only the first, third, and fourth subjects have fat percentages less than 25%. To select
subjects whose fat percentage is less than 25% or greater than 35% without using the with()
command, attach the data set Bodyfat and input fat<25|fat>35:
> attach(Bodyfat)
> fat<25|fat>35
[1] TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[11] TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
A Brief Introduction to S 11
Once the data set has been attached, the data set being used remains on the search path
until it is detached. If one wants to extract the values from a given vector that satisfy a
certain condition, use square braces, []. For example, to store the fat values for all subjects
whose fat measured less than 25% in [Link], key in [Link] <- fat[fat<25]:
> [Link] <- fat[fat<25]
> [Link]
[1] 9.5 7.8 17.8
It is also possible to extract values satisfying more complicated logical conditions. For
example, to extract all fat percentages that are less than 25% and different from 7.8, enter
fat[fat<25 & fat !=7.8]:
> fat[fat<25 & fat !=7.8]
[1] 9.5 17.8
> detach(Bodyfat)
When working with real data, values are often unavailable (the experiment failed, the
subject did not show up, the value was lost, etc.). S uses NA to denote a missing value or to
denote the result of an operation performed on values that contain NA values. The function
[Link](x) returns a logical vector of the same size as x that takes on the value TRUE if and
only if the corresponding element in x is NA. If x is a vector with NA values, but only the
non-missing values are of interest, the function  can be used as shown next:
> x <- c(1,6,9,2, NA)
> [Link](x)
[1] FALSE FALSE FALSE FALSE TRUE
> y<-x[]
> y
[1] 1 6 9 2
The following example illustrates how to select the quantitative values of a variable that
fulfill a particular character condition (that of receiving treatment A):
> x <- c(19,14,15,17,20,23,19,19,21)
> treatment <- c(rep("A",3), rep("B",3), rep("C",3))
> x[treatment=="A"]
[1] 19 14 15
To select the value for patients who received treatment=A or treatment=B, the appropriate
command is x[treatment=="A"|treatment=="B"]:
> x[treatment=="A" | treatment=="B"]
[1] 19 14 15 17 20 23
The function split() splits the values of a variable A according to the categories of a
variable B:
> split(x, treatment)
$A
[1] 19 14 15
$B
[1] 17 20 23
$C
[1] 19 19 21
12 Probability and Statistics with R
1.4.7 Matrices
Matrices are used to arrange values in rows and columns in a rectangular table. In the
following example, different types of barley are in the columns, and different provinces in
Spain are in the rows. The entries in the matrix represent the weight in thousands of metric
tons for each type of barley produced in a given province. The [Link] matrix will
be used to illustrate various functions and manipulations that can be applied to a matrix.
Given the matrix ⎛ ⎞
190 8 22.0
⎝ 191 4 1.7 ⎠ ,
223 80 2.0
the values are written to a matrix (reading across the rows with the command byrow=TRUE)
with name [Link] as follows:
> dim([Link])
[1] 3 3
The following code creates two objects where the names of the three provinces are assigned
to province, and the three types of barley to type:
Assign the names stored in province to the rows of the matrix as follows:
Next, assign the names stored in type to the columns of the matrix:
To assign row and column names simultaneously, the command that should be used is
dimnames([Link]) <- list(province, type):
A Brief Introduction to S 13
[[2]]
[1] "typeA" "typeB" "typeC"
To delete the row and column name assignments, type
> dimnames([Link]) <- NULL
If one is interested in only the second row of data, one can enter
> [Link][2,]
typeA typeB typeC
191 4 1.7
or
> [Link]["Zaragoza", ]
typeA typeB typeC
191 4 1.7
To see the third column, key in
> [Link][,"typeC"]
Navarra Zaragoza Madrid
22 1.7 2
To add an additional column for a fourth type of barley (typeD), use the cbind() command:
> typeD <- c(2,3.5,2.75)
> [Link] <- cbind([Link], typeD)
> rm("typeD")
> [Link]
typeA typeB typeC typeD
Navarra 190 8 22.0 2.00 (1.1)
Zaragoza 191 4 1.7 3.50
Madrid 223 80 2.0 2.75
The function apply() allows the user to apply a function to one or more of the dimensions
of an array. To calculate the mean of the columns for the matrix [Link], type
apply([Link], 2, mean):
> apply([Link], 2, mean)
typeA typeB typeC typeD
201.333333 30.666667 8.566667 2.750000
14 Probability and Statistics with R
The second argument, a 2 in the previous example, tells the function apply() to work on
the columns. For the function to work on rows, the second argument should be a 1. For
example, to find the average barley weight for each province, type apply([Link],1,
mean):
> apply([Link], 1, mean)
Navarra Zaragoza Madrid
55.5000 50.0500 76.9375
The function names() allows the assignment of names to vectors:
> x <- c(1,2,3)
> names(x) <- c("A","B","C")
> x
A B C
1 2 3
To suppress the names of a vector, type names(x)<-NULL:
> names(x) <- NULL
> x
[1] 1 2 3
3 x + 2 y + 1 z = 10
2 x − 3 y + 1 z = −1
1x+ 1y + 1z = 6
To solve this system of equations, enter A and b into S and type solve(A, b) at the
command prompt:
> x
[,1]
[1,] 1
[2,] 2
[3,] 3
The operator %*% is used for matrix multiplication. If x is an (n × 1) column vector, and A
is an (m × n) matrix, then the product of A and x is computed by typing A%*%x. To verify
S’s solution, multiply A × x, and note that this is equal to b:
> A%*%x
[,1]
[1,] 10
[2,] -1
[3,] 6
Other common functions used with vectors and matrices are included in Table A.2 on
page 660.
1.4.9 Arrays
An array generalizes a matrix by extending the number of dimensions to more than
two. Consequently, a two-dimensional array of numbers is simply a matrix. If one were
to place three (3 × 3) matrices each in back of the other, the resulting three-dimensional
array could be visualized as a cube. Consider a three-dimensional array consisting of 27
elements. Specifically, the elements will be the values 1 through 27. Using the indexing
principles illustrated earlier, one can reference an element in the three-dimensional array
by specifying the row, column, and depth. For example,
> cube <- 1:27
> dim(cube) <- c(3,3,3)
assigns the values 1 through 27 into a three-dimensional array. To reference the value in
the middle of the cube, one would specify cube[2,2,2]:
> cube[2,2,2]
[1] 14
If any of the indices are left blank, the entire range is reported for that dimension. For
example, to extract all the values in the second column with depth 2, type cube[ ,2,2]:
> cube[ ,2,2]
[1] 13 14 15
Another way to create the array is to specify its elements and dimensions directly. The
following code also lists the values in the array so one can see how S processes the entries.
Note how a[, , 1] can be visualized as the facing matrix in Figure 1.1 on page 3, a[, ,
2] as the second matrix (slice) in Figure 1.1, and so on:
> a <- array(1:27, dim=(c(3,3,3)))
> a[, , 1]
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
16 Probability and Statistics with R
> a[, , 2]
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
> a[, , 3]
[,1] [,2] [,3]
[1,] 19 22 25
[2,] 20 23 26
[3,] 21 24 27
1.4.10 Lists
A list is an S object whose elements can be of different types (character, numeric, factor,
etc.). Lists are used to unite related data that have different structures. For example, a
student record might be created by
The object student is composed of four components. This can be verified by typing
length(student) in the commands window. Note that length() counts the number of
components in a list. Three of the components are character, while the fourth is numeric.
The individual components of any list can be extracted by using the [[ operator or by
specifying the name of the list and the name of the component, separated by a dollar sign
($). For example, to see the number of semester hours, one might type
> student[[4]]
[1] 15
or
> student$[Link]
[1] 15
> [Link]<-c("no","no","yes")
> city <- [Link]([Link], [Link])
> rm("[Link]")
> city
typeA typeB typeC typeD [Link]
Navarra 190 8 22.0 2.00 no
Zaragoza 191 4 1.7 3.50 no
Madrid 223 80 2.0 2.75 yes
> city$typeA
[1] 190 191 223
To make the columns of a data frame available by name, use the command attach(). After
attaching the data frame city, one can view barley of typeA by simply typing typeA:
> attach(city)
> typeA
[1] 190 191 223
Note that when finished working with an attached object, one should detach the object
using the detach() command to avoid inadvertently masking a system object:
> detach(city)
> typeC
Error: object "typeC" not found
To sort a data frame according to another variable (typeC in this example), one can
use one of the following: city[[Link](city[,3]),], city[order(city[,3]),], or
city[order(typeC),], all of which produce the same result. Note that city will need to
be attached again to use the command as given:
> attach(city)
> city[[Link](city[,3]),]
typeA typeB typeC typeD [Link]
Zaragoza 191 4 1.7 3.50 no
Madrid 223 80 2.0 2.75 yes
Navarra 190 8 22.0 2.00 no
> detach(city)
The function order() will accept more than one argument to break ties, making it generally
more useful than the function [Link]().
1.4.12 Tables
A common use of table() is its application to cross-classifying factors to create a table
of the counts at each combination of factor levels. In S, factors are simply character vectors.
Consider the data set Cars93, which contains several numeric and factor variables and is
available in the MASS package for both R and S-PLUS. To construct a contingency table of
Origin by AirBags, use the following S commands:
> library(MASS)
> attach(Cars93)
18 Probability and Statistics with R
When using three-way contingency tables, ftable() provides more compact output than
table():
AirBags
Origin Driver & Passenger Driver only None
USA 0 3 2
non-USA 0 2 3
, , DriveTrain = Front
AirBags
Origin Driver & Passenger Driver only None
USA 6 15 13
non-USA 5 13 15
, , DriveTrain = Rear
AirBags
Origin Driver & Passenger Driver only None
USA 3 5 1
non-USA 2 5 0
tapply(x, y, FUN) applies the function F U N to each value in x that corresponds to one
of the categories in y. In this example, F U N is the mean. However, in general, F U N
can be any S or user-defined function. The categories of y are the factors created from
list(Origin, AirBags), and the x is the vector of car prices, Price. The final output is
a matrix.
The function aggregate() is also used to compute the same quantities; however, the
output is a data frame:
The function apply(A, MARGIN, FUN) is used to apply a function FUN to the rows or
columns of an array. For example, given a matrix A, the function FUN is applied to every
row if MARGIN = 1 and to every column if MARGIN = 2. The function apply() is used
to compute various statistics with the data frame Baberuth as follows:
> attach(Baberuth)
> apply(Baberuth[,3:14],2, mean)
G AB R H X2B
113.7727273 381.7727273 98.8181818 130.5909091 23.0000000
X3B HR RBI SB BB
6.1818182 32.4545455 100.5000000 5.5909091 93.7272727
BA SLG
0.3228636 0.6340000
A summary of the functions covered in this and the previous section can be found in
Table A.3 on page 661.
Example 1.1 Assign the values (19, 14, 15, 17, 20, 23, 19, 19, 21, 18) to a vector x such that
the first five values of x are in treatment A and the next five values are in treatment B.
Compute the means for the two treatment groups using tapply().
Solution: First assign the values to a vector x, where the first five elements are in
treatment A and the next five are in treatment B in one of two ways:
> x <- c(19,14,15,17,20,23,19,19,21,18)
> treatment <- c(rep("A",5), rep("B",5))
> treatment
[1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B"
or
> treatment <- rep(LETTERS[1:2], rep(5,2))
> treatment
[1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B"
Next, use tapply() to calculate the means for treatments A and B:
> tapply(x, treatment, mean)
A B
17 20
When illustrating pedagogical concepts, the user will often want to generate the same
set of “random” numbers at a later date. To reproduce the same set of “random” numbers,
one uses the [Link]() function. The [Link]() function puts the random number
generator in a reproducible state. Verify for yourself that the following code produces
identical values stored in the vectors set1 and set2:
> [Link](136)
> set1 <- rbinom(10,10,.3)
> [Link](136)
> set2 <- rbinom(10,10,.3)
This class of functions will also accept a vector as well as a scalar for the function’s
arguments. For example, dpois(x=0:10, lambda=3).
If one wanted to sum the squared values of each column in the matrix [Link] defined
in (1.1), one could use
In the next section of code, approximate values for converting temperature values from
Farenheit (60 to 90 by 5 degree increments) to Celsius are given:
Another way to compute the sum of the first n natural numbers (50 in the code) is to
use the function while() as follows:
When one creates new functions, storing them in a single file can be convenient. By
storing all of the functions in a single file, one will be able to read all of them into the S
session by typing
> source("C:/Sfolder/[Link]")
assuming the functions are all stored in a text file named [Link] in the Sfolder
of the machine’s C drive.
A Brief Introduction to S 23
1.8 Graphs
One technique used to summarize numerical data is the proper use of graphs. The
S language provides a rich set of commands for creating graphs and altering the default
graphical parameters. Tables A.12 on page 667, A.13 on page 668, and A.14 on page 669
outline some of the basic commands used to create graphs and to customize the graphical
parameters. In addition to typing commands for graph creation, a large collection of two-
and three-dimensional graphs as well as Trellis graphs can be created in S-PLUS from the
menu bar by selecting Graph>2D plot. . . or 3D plot. . . . For further detail on any S
function or parameter, the user should seek help from the extensive system help files by
typing help([Link]), ?[Link], help(par), or ?par.
The S function plot() produces an appropriate graph whose form depends on the
type of data. The axes, labels, scales, and plotting symbols are all default values chosen
automatically, any or all of which may be changed by the user. Changing or adding
background color, line types, titles, text, and plotting symbols is all controlled by specifying
additional arguments inside S functions such as plot() or hist(), or by changing certain
values in the par settings. Table A.14 on page 669 provides a list of some of the more
commonly changed graphical parameters. For users who prefer a point and click approach
for modifying graphical output, S-PLUS has several buttons on the main menu bar such as
Annotation, GraphTools, and Auto Legend.
The following code illustrates the use of various parameters in the S function plot()
and can be used to recreate Figure 1.2 on the next page. At first the last graph in Figure
1.2 may seem worthless; however, it will often prove useful to create an empty plotting area
to which one can later add points, lines, text, and so on. Two of the more frequently used
arguments with par() are mfrow and mfcol, which subdivide the plotting region into an
array of figure regions. For example, par(mfrow=c(3,3)) divides the screen into nine figure
regions (3 columns by 3 rows). The command \n tells R to make a new line in the title.
The following R code illustrates the use of different plotting symbols, different colors,
and different character expansion (cex) values and can be used to create a graph similar
24 Probability and Statistics with R
Default values with limits Default plotting character Lines connecting the data
for x and y axes altered changed to x
20
20
20
x x
15
15
15
10
10
10
x x
y
x x
5
x x x
0
-5 0 5 -5 0 5 -5 0 5
x x x
20
20
20
15
15
15
10
10
10
y
y
5
5
0
0
-5 0 5 -5 0 5 -5 0 5
x x x
20
15
15
Y Axis
10
10
y
5
0
-5 0 5 0 -5 0 5
x X Axis
FIGURE 1.2: Examples of the plot() function using different values for the parameters
main, pch, xlim, ylim, type, xlab, ylab, and axes.
to Figure 1.3. Color names can be used with a col= specification in graphics functions.
Numbers or names of colors can be assigned to col= as vectors.
5 67
3
0 1 2 3 4
2
1
8 9 10 11 12 13 14 15
0
16 17 18 19 20 21 22 23
−1
5 10 15
FIGURE 1.3: The numbers in the second row correspond to the plotting symbol directly
above them in the first row. The different plotting symbols in the first row and their
corresponding numbers in the second row also reflect a character expansion of 1 through 8.
The plotting symbols in rows three and four have their corresponding numbers printed to
the right.
A Brief Introduction to S 25
1.9 Problems
1. Calculate the following numerical results to three decimal places with S:
√
(a) (7 − 8) + 53 − 5 ÷ 6 + 62
√
(b) ln 3 + 2 sin(π) − e3
√
(c) 2 × (5 + 3) − 6 + 92
(d) ln(5) − exp(2) + 23
√
(e) (9 ÷ 2) × 4 − 10 + ln(6) − exp(1)
3. Create a vector named Treatment with the entries “Treatment One” appearing 20
times, “Treatment Two” appearing 18 times, and “Treatment Three” appearing 22
times.
(a) Assign the names x and y to the values 5 and 7, respectively. Find xy and assign
the result to z. What is the valued stored in z?
(b) Create the vectors u = (1, 2, 5, 4) and v = (2, 2, 1, 1) using the c() and scan()
functions.
(c) Provide S code to find which component of u is equal to 5.
(d) Provide S code to give the components of v greater than or equal to 2.
(e) Find the product u × v. How does S perform the operation?
(f) Explain what S does when two vectors of unequal length are multiplied together.
Specifically, what is u × c(u, v)?
(g) Provide S code to define a sequence from 1 to 10 called G and subsequently to
select the first three components of G.
(h) Use S to define a sequence from 1 to 30 named J with an increment of 2 and
subsequently to choose the first, third, and eighth values of J.
(i) Calculate the scalar product (dot product) of q = (3, 0, 1, 6) by r = (1, 0, 2, 4).
(j) Define the matrix X whose rows are the u and v vectors from part (b).
(k) Define the matrix Y whose columns are the u and v vectors from part (b).
(l) Find the matrix product of X by Y and name it W.
(m) Provide S code that computes the inverse matrix of W and the transpose of that
inverse.
6. Wheat harvested surface in Spain in 2004: Figure 1.4 on the next page, made
with R, depicts the autonomous communities in Spain. The Wheat Table that follows
gives the wheat harvested surfaces in 2004 by autonomous communities in Spain
measured in hectares. Provide S code to answer all the questions.
26 Probability and Statistics with R
Asturias Cantabria
Pais Vasco
Galicia Navarra
Rioja
Castilla−Leon Cataluna
Aragon
Madrid
Murcia
Andalucia
Canarias
Wheat Table
community [Link] community [Link]
Galicia 18817 Castilla y León 619858
Asturias 65 Madrid 13118
Cantabria 440 Castilla-La Mancha 263424
Paı́s Vasco 25143 C. Valenciana 6111
Navarra 66326 Región de Murcia 9500
La Rioja 34214 Extremadura 143250
Aragón 311479 Andalucı́a 558292
Cataluña 74206 Islas Canarias 100
Islas Baleares 7203
(a) Create the variables community and [Link] from the Wheat Table in this
problem. Store both variables in a [Link] named wheatspain.
(b) Find the maximum, the minimum, and the range for the variable [Link].
(c) Which community has the largest harvested wheat surface?
(d) Sort the autonomous communities by harvested surface in ascending order.
(e) Sort the autonomous communities by harvested surfaces in descending order.
(f) Create a new file called wheat.c where Asturias has been removed.
(g) Add Asturias back to the file wheat.c.
(h) Create in wheat.c a new variable called acre indicating the harvested surface in
acres (1 acre = 0.40468564224 hectares).
(i) What is the total harvested surface in hectares and in acres in Spain in 2004?
(j) Define in wheat.c the [Link]() using the names of the communities. Remove
the community variable from wheat.c.
(k) What percent of the autonomous communities have a harvested wheat surface
greater than the mean wheat surface area?
A Brief Introduction to S 27
7. The data frame wheatUSA2004 from the PASWR package has the USA wheat harvested
crop surfaces in 2004 by states. It has two variables, STATE for the state and ACRES
for thousands of acres.
(a) Attach the data frame wheatUSA2004 and use the function [Link]() to define
the states as the row names.
(b) Define a new variable called ha for the surface area given in hectares where
1 acre = 0.40468564224 hectares.
(c) Sort the file according to the harvested surface area in acres.
(d) Which states fall in the top 10% of states for harvested surface area?
(e) Save the contents of wheatUSA2004 in a new file called [Link] in your
favorite directory. Then, remove wheatUSA2004 from your workspace, and check
that the contents of wheatUSA2004 can be recovered from [Link].
(f) Use the command [Link]() to store the contents of wheatUSA2004 in
a file with the name [Link]. Explain the differences between storing
wheatUSA2004 using dump() and using [Link]().
(g) Find the total harvested surface area in acres for the bottom 10% of the states.
8. Use the data frame vit2005 in the PASWR package, which contains data on the 218 used
flats sold in Vitoria (Spain) in 2005 to answer the following questions. A description
of the variables can be obtained from the help file for this data frame.
(a) Create a table of the number of flats according to the number of garages.
(b) Find the mean of totalprice according to the number of garages.
(c) Create a frequency table of flats using the categories: number of garages and
number of elevators.
(d) Find the mean flat price (total price) for each of the cells of the table created in
part (c).
(e) What command will select only the flats having at least one garage?
(f) Define a new file called data.c with the flats that have category=“3B” and have
an elevator.
(g) Find the mean of totalprice and the mean of area using the information in
data.c.
28 Probability and Statistics with R
(a) How many patients have been treated with the Hamstring Stretch?
(b) What proportion of the patients treated with Hamstring Stretch were classified
as each of Easy, Difficult, and Impossible?
(c) What proportion of the patients classified as Easy to palpate were assigned to the
Traditional Sitting position?
(d) What is the mean weight for each cell in a contingency table created with the
variables Ease and Treatment?
(e) What proportion of the patients have a body mass index (BMI = kg/(cm/100)2)
less than 25 and are classified as Easy to palpate?
10. The millions of tourists visiting Spain in 2003, 2004, and 2005 according their nation-
alities are given in the following table:
(a) Store the values in this table in a matrix with the name tourists.
(b) Calculate the totals of the rows.
(c) Calculate the totals of the columns.
11. Use a for loop to convert a sequence of temperatures (18 to 28 by 2) from degrees
centigrade to degrees Fahrenheit.
12. If 1 km = 0.6214 miles, 1 hectare = 2.471 acres, and 1 L = 0.22 gallons, write a
function that converts kilometers, hectares, and liters into miles, acres, and gallons,
respectively. Use the function to convert 10.2 km, 22.4 hectares, and 13.5 L.
Chapter 2
Exploring Data
2.2 Data
Data, according to The American Heritage Dictionary, are “Information, especially
information organized for analysis or used as the basis for a decision.” A characteristic that
is being studied in a statistical problem is called a variable. A variable will be either
qualitative or quantitative. When a variable is qualitative, it is essentially defining
groups or categories. When the categories have no ordering the variable is called nominal.
For example, the variable gender can take on the values male and female or the variable
“music preference” could have values such as “classical,” “jazz,” “rock,” or “other.” When
the categories have a distinct ordering, the variable is called ordinal. Such a variable might
be educational level with values elementary school, high school, college graduate, graduate
or professional school. Values on a scale can be either interval or ratio. Interval data have
interpretable distances, while ratio data have a true zero. A variable that is quantitative
(numeric) may be either discrete or continuous. A discrete variable is a numerical variable
that can assume a finite number or at most a countably infinite number of values. Such
variables include the number of people arriving at a bank on Thursday, students in a class,
or dogs in the pound. A continuous variable is a numerical variable that can assume an
infinite number of values associated with the numbers on an interval of the real number
line, for example, the height of a tree, the life of a light bulb, the weight of an apple. An
important distinction between discrete and continuous variables is that discrete variables
29
30 Probability and Statistics with R
can take on the same value repeatedly while continuous variables have few or no repeated
values. It is important to be able to distinguish between different types of variables since
methods for viewing and summarizing data are dependent on variable type. More to the
point, it will be imperative to distinguish between qualitative (categorical) variables and
quantitative (numerical) variables.
When a data set consists of a single variable, it is called a univariate data set. When
there are two variables in the data set, the data is set is called a bivariate data set; and
when there are two or more variables, the data set is called a multivariate data set. In
the remainder of this section, the discussion will cover univariate variables. Recall that
a qualitative variable defines categories or groups. The membership in these categories is
summarized with tables and graphically illustrated with bar graphs.
Example 2.1 Suppose the letter grades of an English essay in a small class are A, D, C,
D, C, C, C, C, F, and B. Create both a frequency table showing the numbers and a relative
frequency table showing the proportions of the various grades.
Solution: First, the character data are read into a vector named Grades. Then, the S
function table() is applied to Grades:
> Grades <- c("A","D","C","D","C","C","C","C","F","B")
> Grades
[1] "A" "D" "C" "D" "C" "C" "C" "C" "F" "B"
> table(Grades)
Grades
A B C D F
1 1 5 2 1
> table(Grades)/10 # Relative frequency table
Grades
A B C D F
0.1 0.1 0.5 0.2 0.1
Clearly, there is no need for a computer with such a small data set; however, tables for
much larger data sets can be created with no more work than that required for this small
data set.
Example 2.2 The quine data frame in the MASS package has information on children
from Walgett, New South Wales, Australia, that were classified by Culture, Age, Sex, and
Learner status including the number of Days absent from school in a particular school year.
Use the function table() to create a frequency table for the variable Age.
Solution: To gain access to information stored in MASS, first load the package and attach
the data frame quine:
Exploring Data 31
> library(MASS)
> attach(quine)
> table(Age)
Age
F0 F1 F2 F3
27 46 40 33
2.3.2 Barplots
One of the better graphical methods to summarize categorical data is with a barplot.
Barplots are also known as bar charts or bar graphs. The S function barplot() is used
to create barplots using a summarized version of the data, often the result of the table()
function. This summarized form of the data can be either frequencies or percentages. Re-
gardless of whether one uses frequencies or percentages, the resulting shape looks identical,
but the scales on the y-axes are different.
Example 2.3 Construct barplots for the variables Grades used in Example 2.1 and Age
in the quine data set from the MASS package in Example 2.2 on the facing page using both
frequencies and proportions.
Solution: Before creating any barplots, the device region is split into four smaller regions
with the command par(mfrow=c(2,2)):
> par(mfrow=c(2,2))
> barplot(table(Grades), col=3, xlab="Grades", ylab="Frequency")
> barplot(table(Grades)/length(Grades), col=3, xlab="Grades", ylab=
+ "Proportion")
> barplot(table(Age), col=7, xlab="Age", ylab="Frequency")
> barplot(table(Age)/length(Age), col=7, xlab="Age", ylab="Proportion")
0.5
5
0.4
4
Frequency
Proportion
0.3
3
0.2
2
0.1
1
0.0
0
A B C D F A B C D F
Grades Grades
0.30
40
0.20
30
Frequency
Proportion
20
0.10
10
0.00
0
F0 F1 F2 F3 F0 F1 F2 F3
Age Age
FIGURE 2.1: Graphical representation of the data in Grades and Age with the function
barplot()
32 Probability and Statistics with R
Example 2.4 Construct dot charts for the variables Grades from Example 2.1 and Age
used in the quine data set from the MASS package in Example 2.2 on page 30.
Solution: Before creating any dot charts, the device region is split into two smaller regions
with the command par(mfrow=c(2,1)):
> par(mfrow=c(1,2))
> dotchart(table(Grades))
> dotchart(table(Age))
F
F3
D
F2
C
F1
B
F0
A
1 2 3 4 5 30 35 40 45
FIGURE 2.2: Graphical representation of the data in Grades and Age with the function
dotchart()
Example 2.5 Construct pie charts for the variables Grades in Example 2.1 and Age from
the quine data set in the MASS package used in Example 2.2 on page 30.
Solution: Before creating any pie charts, the device region is split into two regions with
the command par(mfrow=c(2,1)):
Exploring Data 33
> par(mfrow=c(1,2))
> pie(table(Grades))
> title("Grades")
> pie(table(Age))
> title("Age")
The graph depicted in Figure 2.3 was produced in R with the additional arguments
radius=2.5 and col=gray(c(.1,.4,.7,.8,.95)).
Grades Age
B F1
F0
C A
F3
F2
D
FIGURE 2.3: Graphical representation of the data in Grades and Age with the function
pie()
digit. Although it is possible to use a stem-and-leaf plot with a moderately sized data
set (more than 100 values), the plot becomes increasingly hard to read as the number of
values plotted increases. Consequently, it is recommended that stem-and-leaf plots be used
graphically to illustrate smallish data sets (less than 100 values). The S command to create
a stem-and-leaf plot is stem(x), where x is a numeric vector.
Example 2.6 Use the data frame Baberuth to construct a stem-and-leaf plot for the
number of home runs (HR) Babe Ruth hit while he played for the New York Yankees.
Solution: A quick glance at the data frame Baberuth shows that Babe Ruth played for
the New York Yankees for his seventh through twenty-first seasons. The information in HR
is for Babe Ruth’s entire (22 seasons) professional career. To extract the home runs he hit
while he was a New York Yankee, use HR[Team=="NY-A"] or HR[7:21] (seventh through
twenty-first season home runs):
> attach(Baberuth) # Assumes package PASWR is loaded
> NYYHR <- HR[Team=="NY-A"]
> NYYHR
[1] 54 59 35 41 46 25 47 60 54 46 49 46 41 34 22
> stem(NYYHR)
2 | 25
3 | 45
4 | 1166679
5 | 449
6 | 0
> detach(Baberuth)
Exploring Data 35
In this example, see how the stems 2–6 represent the values twenty through sixty and
the leaves represent the second digit of the numbers in HR. Reading the first row of the
stem-and-leaf plot notice the values 22 and 25. The stem-and-leaf plot reveals a fairly
symmetric distribution.
(b) Figure 2.6 on the following page is a strip chart of the number of home runs Babe Ruth
hit per season according to the team for which he was playing. The code to construct this
graph is
> par(mfrow=c(1,2), pty="s")
> stripchart(HR~Team, pch=1, method="stack",
+ main="Dotplot of home runs \n by team",
+ xlab="Home runs per season")
> par(las=1) # Makes labels horizontal
> stripchart(HR~Team, pch=19, col=c("red","green","blue"),
+ method="stack", main="Color dotplot of home runs \n by team",
+ xlab="Home runs per season")
> par(mfrow=c(1,1), las=0, pty="m")
> detach(Baberuth)
36 Probability and Statistics with R
30 40 50 60
FIGURE 2.5: Strip chart of the number of home runs Babe Ruth hit while playing for the
New York Yankees
NY−A
Bos−N
Bos−N
Bos−A
Bos−A
0 10 20 30 40 50 60 0 10 20 30 40 50 60
FIGURE 2.6: Strip chart of the number of home runs Babe Ruth hit per season according
to the team for which he was playing
2.4.3 Histograms
The histogram is a graphical means of illustrating quantitative (numerical) data. Although
the barplot and the histogram look similar, the barplot is used for qualitative data while
Exploring Data 37
the histogram is used for numerical data. Yet, the bins that either the user specifies or
those that S uses by default are in essence categories. Histograms created in S with the
function hist(x), where x is a numeric vector, are by default frequency histograms. To
create density histograms, use the optional argument prob=TRUE. A density histogram has
a total area of one.
Example 2.8 Construct a histogram that resembles the stem-and leaf plot from Example
2.6 using the Baberuth data.
Solution: The first histogram uses the default arguments for hist(). Since the bins S
uses are of the form (], the default histogram does not resemble the stem-and-leaf plot. To
change the bins to the form [) in R, use the argument right=FALSE:
> attach(Baberuth)
> par(mfrow=c(1,2))
> bin <- seq(20,70,10) # Creating bins 20-70 by 10
> hist(HR[7:21], breaks=bin, xlab="Home Runs")
> hist(HR[7:21], breaks=bin, right=FALSE, xlab="Home Runs") # R
> detach(Baberuth)
The graph depicted in Figure 2.7 was produced in R with commands similar to those given.
One way to produce the second graph in S-PLUS is to use a slight fudge factor when creating
the bins, such as bin <- seq(20,70,10)-0.00001.
Histogram using bins of the form (] Histogram using bins of the form [)
7
7
6
6
5
5
Frequency
Frequency
4
4
3
3
2
2
1
1
0
20 30 40 50 60 70 20 30 40 50 60 70
FIGURE 2.7: Histograms created using different bin definitions for the number of home
runs hit by Babe Ruth while playing for the New York Yankees
One of the problems with using histograms to describe the shape of the data is the
arbitrary nature of the bin width. In Example 2.8, it was seen how simply including or
excluding an end point changed the histogram. Consider the differences among the shapes
of the histograms in Figure 2.8 on the next page produced by simply altering the bin width.
The data set used to produce Figure 2.8 on the following page is geyser, available in the
MASS package. A much better choice to get an idea of what the shape of a distribution
looks like is to use a density estimate. The S function density(x), where x is a numeric
38 Probability and Statistics with R
vector, can be used to create a density estimate. Basically, a density estimate uses shapes
with n1 area added up at each point in the data set to create a graph with area 1. The
resulting shape is a density estimate. The result of the density estimate can be viewed with
either the plot() or lines() function. Recall that plot() is a high-level function while
lines() is a low-level function. That is, plot() will create a graph while lines() will add
to an existing graph.
0.8
Density
Density
0.4
0.4
0.0
0.0
0 1 2 3 4 5 6 0 1 2 3 4 5 6
duration duration
0.8
Density
Density
0.4
0.4
0.0
0.0
0 1 2 3 4 5 6 0 1 2 3 4 5 6
duration duration
FIGURE 2.8: Histograms created using different bin definitions for the eruption duration
of Old Faithful
Example 2.9 Construct a density histogram of the waiting time until the next eruption
using the data frame geyser available in the MASS package. Superimpose a density estimate
over the density histogram. In the same graph, show the estimated density without showing
the histogram.
Solution: Note that to superimpose a density over a histogram, the histogram must be a
density histogram. Recall that density histograms are produced with the optional argument
prob=TRUE:
> library(MASS)
> par(mfrow=c(1,2)) # Make device region 1 by 2
> attach(geyser)
> hist(waiting, prob=TRUE)
> lines(density(waiting)) # Add density to Histogram
> plot(density(waiting)) # Create density by itself
> detach(geyser)
Based on the density estimates, it appears there are two modes for waiting time until the
next eruption. It seems one will usually have to wait close to either 50 or 80 minutes until
the next eruption.
Exploring Data 39
0.04
0.030
0.03
0.020
Density
Density
0.02
0.010
0.01
0.000
0.00
FIGURE 2.9: Histogram of waiting time between Old Faithful eruptions with superimposed
density estimate as well as a density plot
The S function mean(x) will compute the mean of a data vector x. Additional arguments
to mean(x) include [Link]=TRUE, for removal of missing values, and trim=, to compute a
trimmed mean. The trimmed mean is generally used to estimate the center when working
with long-tailed distributions. When a p% trimmed mean is computed, p% of the sorted data
is deleted from each end of the distribution, and a mean is computed from the remaining
40 Probability and Statistics with R
values. When p × n is not an integer, the integer portion, ( p × n ), should be deleted from
each end of the sorted values and the mean computed from the remaining values.
Example 2.10 Compute the mean number of home runs per season Babe Ruth hit while
playing for the New York Yankees. Compute a 5%, a 10%, a 15%, and a 50% trimmed
mean for the number of home runs per season Babe Ruth hit while playing for the New
York Yankees using the information stored in the data frame Baberuth.
Solution: In Example 2.6 on page 34, the variable NYYHR was created that contained the
number of home runs Babe Ruth made while playing for the New York Yankees. If NYYHR
is no longer available, recreate it with the command NYYHR <- HR[7:21] once the data
frame Baberuth has been attached. Since there are 15 values in NYYHR, to compute 5%,
10%, 15%, and 50% trimmed means, 0.05 × 15 = 0.75 = 0, 0.10 × 15 = 1.5 = 1,
0.15 × 15 = 2.25 = 2, and 0.50 × 15 = 7.5 = 7 values, respectively, will need to be
deleted from the sorted values of NYYHR before computing means on the remaining values.
A second solution is also presented using the S function mean() using the trim= argument:
> attach(Baberuth)
> NYYHR <- HR[7:21]
> NYYHR
[1] 54 59 35 41 46 25 47 60 54 46 49 46 41 34 22
> SNYYHR <- sort(NYYHR)
> SNYYHR
[1] 22 25 34 35 41 41 46 46 46 47 49 54 54 59 60
> p.05 <- floor(.05*15)
> p.10 <- floor(.10*15)
> p.15 <- floor(.15*15)
> p.50 <- floor(.50*15)
> [Link] <-c(p.05, p.10, p.15, p.50)
> [Link]
[1] 0 1 2 7
> m.05 <- mean(SNYYHR[(1+p.05):(15-p.05)])
> m.10 <- mean(SNYYHR[(1+p.10):(15-p.10)])
> m.15 <- mean(SNYYHR[(1+p.15):(15-p.15)])
> m.50 <- mean(SNYYHR[(1+p.50):(15-p.50)])
> t.m <- c(m.05, m.10, m.15, m.50)
> names(t.m) <- c("5%tmean","10%tmean","15%tmean","50%tmean")
> t.m
5%tmean 10%tmean 15%tmean 50%tmean
43.93333 44.38462 44.81818 46.00000
> tm.05 <- mean(NYYHR, trim=.05)
> tm.10 <- mean(NYYHR, trim=.10)
> tm.15 <- mean(NYYHR, trim=.15)
> tm.50 <- mean(NYYHR, trim=.50)
> tms <- c(tm.05, tm.10, tm.15, tm.50)
> names(tms) <- c("5%tmean","10%tmean","15%tmean","50%tmean")
> tms
5%tmean 10%tmean 15%tmean 50%tmean
43.93333 44.38462 44.81818 46.00000
> detach(Baberuth)
Exploring Data 41
The trimmed means are all fairly similar, confirming a rather symmetric distribution. Note
that the 50% trimmed mean is the value in the middle of the sorted observations. This
value is also known as the median.
To find the sample median with S use the function median(x), where x is a numeric vector.
Example 2.11 Means and Medians The numerical grades achieved by three
students on four exams during the course of a semester are recorded in Table 2.1. Compute
means and medians for the students. Could the three students be characterized?
Solution: First the students exam scores are read into individual vectors denoted Student1,
Student2, and Student3. The S function median() is used first to find the median test
score for each student. It is possible to compute the mean test score for each student in
a similar fashion to that used to find the median test score for each student. However,
another solution is provided by using the S functions rbind(), cbind(), and apply():
As seen in the S output, the mean test score for the three students is 74. One possible char-
acterization of the three students might be: Student 1: consistent; Student 2: overconfident;
Student 3: procrastinator. Would the mean or the median be the better representative in
assigning their final grades? There are good reasons one may want to consider using the
median instead of the mean.
2.5.3 Quantiles
The pth quantile, 0 ≤ p ≤ 1, of a distribution is the value xp such that P(X ≤ xp ) ≥ p
and P(X ≥ xp ) ≥ 1 − p. For discrete data, there are often many values of xp that satisfy the
definition of the pth quantile. In this book, the definition used by S to compute quantiles
will be used. S defines the pth quantile of a distribution to be the p(n − 1) + 1 st order
statistic. When p(n − 1) + 1 is not an integer, linear interpolation is used between order
statistics to arrive at the pth quantile. Given values x1 , x2 , . . . , xn , the pth quantile for the
k th order statistic, p(k), is
(k − 1)
p(k) = , k ≤ n. (2.3)
(n − 1)
By this definition, it is seen that the 50% quantile (50th percentile) is the median since
k−1 n+1
0.50 = ⇒k= ,
n−1 2
which by definition is the location of the order statistic that is the median. Other definitions
for quantiles exist and are used in other texts and other statistical software packages.
However, the definition used here is consistent with S-PLUS and the default algorithm used
in R for computing quantiles. To read about alternative algorithms for computing quantiles
with R, type ?quantile at the R prompt. To compute the quantiles of a data set stored
in a vector x, use the S function quantile(x). By default, the S function quantile(x)
returns the 0%, 25%, 50%, 75%, and 100% quantiles of the data vector x. The pth quantile
is the same thing as the (p × 100)th percentile. That is, percentiles and quantiles measure
the same thing; however, percentiles use a scale from 0 to 100 instead of the 0 to 1 scale
used by quantiles.
Just as the sample median is the value that divides the sample into equal halves, the
sample quartiles can be thought of as the values that divide the sample into quarters. The
first, second, and third sample quartiles are denoted as Q1 , Q2 , and Q3 , respectively, and
are (by default) computed with the S function quantile(x). To compute other quantiles,
Exploring Data 43
use the argument probs= to specify either a single value or to pass a vector of values to the
quantile() function.
Example 2.12 Compute Q1 , Q2 , and Q3 for the values x(1) = 1, x(2) = 4, x(3) = 7,
x(4) = 9, x(5) = 10, x(6) = 14, x(7) = 15, x(8) = 16, x(9) = 20, and x(10) = 21.
Solution: First, the order statistics for the 0.25, 0.50, and 0.75 quantiles are computed
using (2.3):
Linear interpolation is then used on the order statistics to find the requested quan-
tiles/quartiles. Specifically, since Q1 , Q2 , and Q3 occur at the 3.25, 5.50, and 7.75 order
statistics, 0.25 of the distance between the third and fourth order statistics is added to the
third order statistic to arrive at Q1 . Likewise, 0.50 of the distance between the fifth and
sixth order statistics is added to the fifth order statistic to compute Q2 . Finally, 0.75 of
the distance between the seventh and eighth order statistics is added to the seventh order
statistic to compute Q3 :
Code to compute the requested quartiles according to the quantile definition follows. Sub-
sequently, the S function quantile() is used to compute the same quantiles/quartiles.
n+1
2 +1
j= . (2.4)
2
In short, the lower hinge is the median of the lower half of the data and the upper hinge
is the median of the upper half of the data. Lower and upper hinges can be different from
quartiles. For example, consider Example 2.12 on the previous page where the locations
of the first, and third quartiles were found to be at the 3.25th and 7.75th order statistics.
However, since
n+1 10+1
2 +1 2 +1
= = 3,
2 2
the locations for the lower and upper hinges are at the 3rd , x(3) , and 8th , x(n−3+1) =
x(10−3+1) = x(8) , order statistics.
Hinges are typically returned as part of the five-number summary. A five-number
summary for a data set consists of the smallest value, the lower hinge, the median, the
upper hinge, and the largest value, all of which are computed with R’s function fivenum().
Example 2.13 Compute the 0.25, 0.50, and 0.75 quantiles as well as a five-number
summary for the number of runs batted in (RBIs) by Babe Ruth while he played for the
New York Yankees. The variable RBI in the data frame Baberuth contains the RBIs per
season for Babe Ruth over his professional baseball career.
Solution: The quartiles and hinges are first computed by their definitions. Subsequently,
the S function quantile() and the R function fivenum() are used to obtain the same
results:
In this particular example, the first and third quartile are equal to the lower and upper
hinge, respectively.
2.5.5 Boxplots
A popular method of representing the information in a five-number summary is the
boxplot. To show spread, a box is drawn from the lower hinge (HL ) to the upper hinge
(HU ) with a vertical line drawn through the box to indicate the median or second quartile
(Q2 ). A “whisker” is drawn from HU to the largest data value that does not exceed the
upper fence. This value is called the adjacent value. The upper fence is defined as
FenceU = HU + 1.5 × Hspread , where Hspread = HU − HL . A whisker is also drawn from HL
to the smallest value that is larger than the lower fence, where the lower fence is defined
as FenceL = HL − 1.5 × Hspread . Any value smaller than the lower fence or larger than
the upper fence is considered an outlier and is generally depicted with a hollow circle.
Figure 2.10 on the following page illustrates a boxplot for the variable fat from the data
frame Bodyfat.
To create a boxplot with S, use the command boxplot(). By default, boxplots in R
have a vertical orientation. To create a horizontal boxplot with R, use the optional argu-
ment horizontal=TRUE. Currently, S-PLUS does not have an option to produce horizontal
boxplots with the boxplot() function. However, S-PLUS does have the function bwplot(),
which produces horizontal boxplots. Common arguments for boxplot() include col= to
set the box color and notch=TRUE to add a notch to the box to highlight the median.
Example 2.14 Use the data frame Cars93 in the MASS package to create a boxplot of the
variable [Link]. Use the text() function to label the five-number summary values in
the boxplot.
Solution: Two solutions are presented: one for R and one for S-PLUS. The solution
for S-PLUS is slightly more involved because S-PLUS does not have a built-in function to
compute the five-number summary. The final boxplot from R is shown in Figure 2.11 on
page 47. Additionally, the labels in R contain mathematical notation. To learn more about
R’s ability to plot mathematical expressions, type ?mathplot at the R prompt.
46 Probability and Statistics with R
M in FenceL HL Q 2 HU M ax FenceU
10 20 30 40 50
FIGURE 2.10: Graph depicting the five-number summary in relationship to original data
and the boxplot
Solution for R:
> library(MASS)
> attach(Cars93)
> boxplot([Link], ylab="Minimum Price (in $1000) for basic
+ version", col="gray")
> f <- fivenum([Link])
> text(rep(1.25,5), f, labels=c("Min", expression(H[L]),
+ expression(Q[2]) , expression(H[U]), "Max"), pos=4)
> detach(Cars93) # Clean up
Solution for S-PLUS:
> library(MASS)
> attach(Cars93)
> n <- length([Link])
> smp <- sort([Link])
> count <- (floor((n+1)/2)+1)/2 # Using Equation 2.4
> count
[1] 24
> [Link] <- smp[count]
> [Link] <- smp[(n-count+1)]
> [Link] <- c(min(smp), [Link], median(smp), [Link],
+ max(smp))
> boxplot([Link], ylab="Minimum Price (in $1000) for basic
+ version")
> text(rep(85,5), [Link], labels=c("Minimum", "Lower Hinge",
+ "Median", "Upper Hinge", "Maximum"))
> detach(Cars93) # Clean up
Exploring Data 47
40
30
HU
20
Q2
HL
10
Min
Boxplots are useful for detecting skewness, finding outliers, and comparing two or more
variables that are all measured on the same scale. However, a boxplot will not detect
multi-modality.
2.6.1 Range
The easiest measure of spread to compute is the range. At times, the range refers to
the difference between the smallest value in a data set and the largest value in the data set.
Other times, the range refers to the smallest and largest values of a data set as a pair. The
S function range(x) returns the smallest and largest values in x. If the distance between
the largest and smallest value is desired, one can use diff(range(x)):
> range(1:10)
[1] 1 10
> diff(range(1:10))
[1] 9
> quantile(1:10)
0% 25% 50% 75% 100%
1.00 3.25 5.50 7.75 10.00
> IQR(1:10)
[1] 4.5
2.6.3 Variance
The sample variance, s2 , can be thought of as the average squared distance of the
sample values from the sample mean. It is not quite the average because the quantity is
divided by n − 1 instead of n in the formula
n
(xi − x̄)2
s2 = . (2.5)
i=1
n−1
When the positive square root of the sample variance is taken, the sample standard
deviation, s, results. It is often preferable to report the sample standard deviation instead
of the variance since the units of measurement for the sample standard deviation are the
same as those of the individual data points in the sample. To compute the variance with S,
use the function var(x). One could compute the standard deviation by taking the square
root of the variance sqrt(var(x)) or use the built-in function to do so. However, be aware
that the function to compute the standard deviation in R is sd(x), while the function to
compute the standard deviation in S-PLUS is stdev(x). The standard deviation is an
appropriate measure of spread for normal distributions:
An interesting function that will return different results depending on the class of the
object to which it is applied is the S function summary(). When the object is a numeric
vector, as is the case with x, six summary statistics are returned: the minimum, the first
quartile, the median, the mean, the third quartile, and the maximum:
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1 2 3 3 4 5
Example 2.15 Consider the data frame EPIDURAL, which contains information from a
study to determine whether the traditional sitting position or the hamstring stretch position
is superior for administering epidural anesthesia to pregnant women in labor as measured
by the number of obstructive (needle to bone) contacts (OC). The variable Doctor specifies
which of the four physicians in the study administered the procedure. Ease is the physician’s
assessment prior to administering the epidural of how well bony landmarks for epidural
placement can be felt. Produce a two-way contingency table for the variables Doctor and
Ease.
Solution: The goal is to produce a two-way table such as the one in Table 2.2 on the next
page with S. The levels of categorical variables by default are alphabetical. Consequently,
the levels of Ease are Difficult, Easy, and Impossible. Pay particular attention to how the
levels of a variable can be rearranged in the code that follows.
50 Probability and Statistics with R
Example 2.16 Produce stacked and side-by-side barplots of the information contained
in Table 2.2 on the facing page.
Solution: Barplots where the variable of interest is Ease then Doctor are created first.
Subsequently, side-by-side barplots where the variables of interest are Ease then Doctor
are created. The graphs in Figure 2.12 on the next page were created using R. Output
from S-PLUS will look slightly different. The user should consult the on-line documentation
using ?barplots for the differences between R and S-PLUS.
> attach(EPIDURAL)
> Teasy <- factor(Ease, levels=c("Easy","Difficult","Impossible"))
> X <- table(Doctor, Teasy)
> X
Teasy
Doctor Easy Difficult Impossible
Dr. A 19 3 1
Dr. B 7 10 4
Dr. C 18 3 0
Dr. D 13 4 3
> t(X) # Transpose X
Doctor
Teasy Dr. A Dr. B Dr. C Dr. D
Easy 19 7 18 13
Difficult 3 10 3 4
Impossible 1 4 0 3
> par(mfrow=c(2,2))
> barplot(X, main="Barplot where Doctor is Stacked \n within Levels
+ of Palpitation")
> barplot(t(X), main="Barplot where Levels of Palpitation \n is
+ Stacked within Doctor")
> barplot(X, beside=TRUE, main="Barplot where Doctor is Grouped \n
+ within Levels of Palpitation")
> barplot(t(X), beside=TRUE, main="Barplot where Levels of Palpitation
+ \n is Grouped within Doctor")
> par(mfrow=c(1,1))
> detach(EPIDURAL)
From the example, it is seen that the categories for the barplot are the numeric columns in
a two-way contingency table. If the user wants the categories to be reversed, transpose the
table using the command t(table(x, y)), where table(x, y) is the two-way contingency
table.
52 Probability and Statistics with R
50
20
40
15
30
10
20
5
10
0
0
Easy Difficult Impossible Dr. A Dr. B Dr. C Dr. D
15
10
10
5
5
0
0
Easy Difficult Impossible Dr. A Dr. B Dr. C Dr. D
FIGURE 2.12: Stacked and side-by-side barplots for levels of palpitation (Teasy) and
physician (Doctor)
Relationships are often better represented with proportions than with counts. R has the
function [Link](x), which can be used to compute proportions based on the number
of entries in either the entire table, x, which is the default, or by entering [Link](x,
margin=1) for row totals or [Link](x, margin=2) for column totals.
Example 2.17 Using the data frame EPIDURAL, create a side-by-side barplot of Treatment
versus OC.
Solution: Since there have been 25 patients treated with the hamstring stretch position
and 49 patients treated with the traditional sitting position, it would not be rational to
compare the frequencies. Instead, one should compare the percentages within the categories
of OC by Treatment:
> attach(EPIDURAL)
> table(Treatment, OC)
OC
Treatment 0 1 2 3 4 5 6 10
Hamstring Stretch 17 6 6 2 1 1 0 2
Traditional Sitting 23 16 3 1 2 2 2 0
> addmargins(table(Treatment, OC)) # addmargins is an R command
OC
Treatment 0 1 2 3 4 5 6 10 Sum
Hamstring Stretch 17 6 6 2 1 1 0 2 35
Traditional Sitting 23 16 3 1 2 2 2 0 49
Sum 40 22 9 3 3 3 2 2 84
> X <-[Link](table(Treatment, OC),1) # Percents by rows
Exploring Data 53
> X
OC
Treatment 0 1 2 3
Hamstring Stretch 0.48571429 0.17142857 0.17142857 0.05714286
Traditional Sitting 0.46938776 0.32653061 0.06122449 0.02040816
OC
Treatment 4 5 6 10
Hamstring Stretch 0.02857143 0.02857143 0.00000000 0.05714286
Traditional Sitting 0.04081633 0.04081633 0.04081633 0.00000000
> par(mfrow=c(2,1))
> barplot(X, beside=TRUE, legend=TRUE)
> barplot(t(X), beside=TRUE, legend=TRUE)
> par(mfrow=c(1,1))
> detach(EPIDURAL)
Hamstring Stretch
0.4
Traditional Sitting
0.3
0.2
0.1
0.0
0 1 2 3 4 5 6 10
0
0.4
1
2
3
0.3
4
5
6
0.2
10
0.1
0.0
Note that the categories for the barplot in the upper graph of Figure 2.13 are the OC
categories in the two-way contingency table. Within each OC category, comparisons are
shown side-by-side based on the treatment. If the user wants the categories to be reversed,
transpose the table using the command t(table(x, y)), where table(x, y) is the two-
way contingency table.
some added benefit. For example, the data in the EPIDURAL data frame are from an ongoing
experiment to see which of two positions results in fewer obstructive bone contacts (the times
the needle hits a bone). When comparing two samples, typically some type of inference to
the samples’ populations is desired. That is, are the centers the same? Are the spreads
similar? Are the shapes of the two distributions similar? Graphs such as histograms, density
plots, boxplots, and quantile-quantile plots can help answer these questions. Histograms
and density plots were introduced in Section 2.4, and boxplots were introduced in Section
2.5. A quantile-quantile (Q-Q) plot plots the quantiles of one distribution against the
quantiles of another distribution as (x, y) points. When the two distributions have similar
shapes, the points will fall along a straight line. The S function to make a quantile-quantile
plot is qqplot(x, y). Histograms can be used to compare two distributions. However, it
is rather challenging to put both histograms on the same graph. Example 2.18 shows the
user how histograms can be used to compare distributions. However, a better approach is
to use Trellis/lattice graphics, which are explained in Section 2.8.
Example 2.18 Use histograms to compare the body weight index (BWI) for the two
treatments (traditional sitting and hamstring stretch stored in Treatment) using the data
frame EPIDURAL.
Solution: First, BWI is typically defined as kg/m2 . Since the data frame EPIDURAL does
not contain a BWI variable, one is created. Subsequently, the default options for the BWI
histograms of the control and treatment groups are shown in the first column of Figure 2.14
on the next page, while the BWI histograms of the control and treatment groups are shown
in the second column of Figure 2.14 after the axes limits for both the x- and y-axes have
been set to the same values for both histograms:
> attach(EPIDURAL)
> BWI <- kg/(cm/100)^2
> Control <- BWI[Treatment=="Traditional Sitting"]
> Treated <- BWI[Treatment=="Hamstring Stretch"]
> par(mfrow=c(2,2)) # 2*2 plotting region
> hist(Control)
> hist(Control, xlim=c(20,60), ylim=c(0,17))
> hist(Treated)
> hist(Treated, xlim=c(20,60), ylim=c(0,17))
> par(mfrow=c(1,1)) # 1*1 plotting region
> detach(EPIDURAL)
Note that it is misleading to compare histograms where the bin widths and/or units on the
axes of the two histograms are different. Note that both axes are different in the first column
of Figure 2.14 on the facing page. The bins of the two histograms are set with the argument
breaks=, and the x- and y-axes are set with the arguments xlim= and ylim=, respectively.
The general shape of the BWI for the patients administered epidurals in the hamstring
stretch position is unimodal skewed to the right. While the distribution of BWI for patients
administered epidurals in the traditional sitting position is also unimodal skewed to the
right, it is not quite as skewed as the distribution where patients are administered epidurals
from the hamstring stretch position.
Example 2.19 Use side-by-side boxplots and superimposed density plots to compare the
BWI for the two treatments (traditional sitting and hamstring stretch stored in Treatment)
using the data frame EPIDURAL.
Exploring Data 55
15
15
10
Frequency
Frequency
10
5
5
0
0
20 25 30 35 40 45 50 20 30 40 50 60
Control Control
15
10
8
Frequency
Frequency
10
6
4
5
2
0
0
20 30 40 50 60 20 30 40 50 60
Treated Treated
Solution: The argument horizontal=TRUE used in the boxplot() function will only
work in R. One way to create horizontal boxplots with S-PLUS is to use the Trellis function
bwplot(). Specifically, one might enter bwplot(Treatment~BWI) to produce side-by-side
boxplots with S-PLUS. Trellis/lattice graphs will be discussed in more detail in Section 2.8.
Using boxplots, as seen in Figure 2.15 on the next page, one sees that the median for both
treatments is around 30 kg/m2 and both distributions appear to be skewed to the right.
> attach(EPIDURAL)
> par(pty="s") # Make plotting region square
> BWI <- kg/(cm/100)^2 # Define body weight index
> Control <- BWI[Treatment=="Traditional Sitting"]
> Treated <- BWI[Treatment=="Hamstring Stretch"]
> boxplot(Control, Treated, horizontal=TRUE, col=c(13,4),
+ names=c("Traditional Sitting","Hamstring Stretch"), las=1)
> plot(density(Control), xlim=c(20,60), col=13, lwd=2, main="", xlab="")
> lines(density(Treated), lty=2, col=4, lwd=2)
> detach(EPIDURAL)
The density plot in Figure 2.16 on the following page further indicates that the distributions
for the BWI for both the traditional sitting and the hamstring stretch position are skewed
to the right.
Example 2.20 Use a quantile-quantile plot to compare the BWI for the two treat-
ments (traditional sitting and hamstring stretch stored in Treatment) using the data frame
EPIDURAL.
Hamstring Stretch
Traditional Sitting
20 30 40 50 60
FIGURE 2.15: Side-by-side boxplots of BWI in the traditional sitting and hamstring stretch
positions
0.06
Density
0.04
0.02
0.00
20 30 40 50 60
FIGURE 2.16: Density plots of BWI in the traditional sitting (solid line) and hamstring
stretch positions (dashed line)
> attach(EPIDURAL)
> par(pty="s") # Make plotting region square
> qqplot(Control, Treated, xlim=c(20,60), ylim=c(20,60))
> abline(a=0, b=1) # Line y=0+1*x
> par(pty="m") # Maximize plotting region
> detach(EPIDURAL)
The quantile-quantile plot in Figure 2.17 suggests the distributions are fairly similar since
the points roughly follow the y = x line.
60
50
Treated
40
30
20
20 30 40 50 60
Control
FIGURE 2.17: Quantile-quantile plot of BWI in the traditional sitting and hamstring
stretch positions
“Is there a relationship between a person’s height and his weight?” or “Is there a relationship
between a student’s grades and the time spent studying?” are typical. Given two numeric
variables, say x and y, entering the S function plot(x, y) produces a scatterplot.
Example 2.21 Use the data frame Animals from the MASS package to investigate whether
the brain weights of animals are related to their body weights. In other words, is a bigger
brain required to govern a bigger body?
Solution: Because of the large range in body and brain weights, (0.023 kg to 87,000
kg) and (0.4 g to 5712 g), respectively, a scatterplot of the values in body and brain is
too distorted to reveal any clear pattern. Consequently, the data is transformed by taking
natural logarithms of both variables and plotting the resulting values as shown in Figure 2.18
on the following page.
> library(MASS)
> attach(Animals)
> range(body)
[1] 2.3e-02 8.7e+04
> range(brain)
[1] 0.4 5712.0
> range(log(body))
[1] -3.772261 11.373663
> range(log(brain))
[1] -0.9162907 8.6503245
> par(pty="s")
> plot(log(body), log(brain))
> identify(log(body), log(brain), labels=[Link](Animals))
> detach(Animals)
The function identify() was used to label several of the points in Figure 2.18 on the next
page. The function identify() labels the closest point in the scatterplot with each mouse
click (left click with windows) until instructed to stop. How the function is instructed to
stop varies by operating system. Right clicking with windows, middle clicking with Linux,
and using the escape key in Mac OS X will generally stop the identification process. Based
on Figure 2.18, there appears to be linear relationship between the logarithm of the body
weights and the logarithm of the brain weights. The dinosaurs can be classified as bivariate
outliers as they do not fit the overall pattern seen in the rest of the data.
58 Probability and Statistics with R
8
Human
Chimpanzee
6
Rhesus monkey Brachiosaurus
log(brain)
Triceratops
4
Dipliodocus
2
0
0 5 10
log(body)
2.7.5 Correlation
The correlation coefficient, denoted by r, measures the strength and direction of the
linear relationship between two numeric variables X and Y and is defined by
n
1 xi − x̄ yi − ȳ
r= (2.6)
n−1 i=1
sX sY
The value for r will always be between −1 and +1. When r is close to +1, it indicates
a strong positive linear relationship. That is, when x increases so does y, and vice versa.
When the value of r is close to −1, it indicates a strong negative linear relationship. Values
of r close to zero indicate weak linear relationships. To compute the correlation between
two numeric vectors with S, one may use the function cor(x, y).
Example 2.22 Find the correlation coefficient, r, between the logarithms of the body
and brain weights in the data frame Animals from the MASS package using (2.6). Verify the
calculated answer using the S function cor().
Solution: First, the variables logbody, logbrain, Zbody, and Zbrain are created. The
new variables are subsequently column binded to the Animals data frame and stored in a
new data frame named Anim. Note that the output uses the R function sd() to compute
the standard deviation. To compute the standard deviation with S-PLUS, use stdev().
> attach(Animals)
> options(digits=3) # Three digits for output
> logbody <- log(body)
> logbrain <- log(brain)
> Zbody <- (logbody - mean(logbody))/sd(logbody)
> Zbrain <- (logbrain - mean(logbrain))/sd(logbrain)
> Anim <- cbind(Animals, logbody, logbrain, Zbody, Zbrain)
> n <- length(logbody)
Exploring Data 59
The correlation between logbrain and logbody is 0.78, which indicates a positive linear
relationship between the two variables. An alternative to computing the z-scores directly
is to use the function scale():
> t(DF)
1 2 3 4 5 6 7 8 9
x 1 1 1 3 3 3 2 2 2
y 3 2 3 6 2 6 10 4 4
z 7 4 2 9 6 4 5 3 1
> t(DF[order(x, y, z),])
2 3 1 9 8 7 5 6 4
x 1 1 1 2 2 2 3 3 3
y 2 3 3 4 4 10 2 6 6
z 4 2 7 1 3 5 6 4 9
> detach(DF)
Example 2.23 Find the correlation coefficient, r, between the logarithms of the body
and brain weights in the data frame Animals from the MASS package with and without
dinosaurs.
Solution: To save space, only four rows of the data frames SA and NoDINO are shown in
the output. Note that there are a total of 28 animals in the data frame Animals.
> attach(Animals)
> cor(log(body), log(brain))
[1] 0.7794935
> SA <- Animals[order(body),] # Sorted by body weight
> detach(Animals)
> tail(SA, n=4) # Equivalently SA[25:28,], shows four heaviest animals
body brain
African elephant 6654 5712.0
Triceratops 9400 70.0
Dipliodocus 11700 50.0
Brachiosaurus 87000 154.5
> NoDINO <- SA[-(28:26),] # Remove rows 26-28 of SA
> attach(NoDINO) # NoDINO contains 25 rows
> NoDINO[22:25,] # Show four heaviest animals
body brain
Horse 521 655
Giraffe 529 680
Asian elephant 2547 4603
African elephant 6654 5712
> cor(log(body), log(brain)) # Correlation without dinosaurs
[1] 0.9600516
> detach(NoDINO)
The correlation between log(brain) and log(body) when dinosaurs are included is 0.78
and the correlation between log(brain) and log(body) is 0.96 when the dinosaurs are
removed from the computation.
addressing more complicated models, this material attempts to provide a foundation for the
simpler models (simple linear regression) from which more complicated models can later be
built. Chapter 12 is devoted to standard regression techniques for both the simple and
multiple linear regression model. The simple linear regression model is written
Yi = β0 + β1 xi + εi (2.7)
Model (2.7) is said to be simple, linear in the parameters (β0 and β1 ), and linear in the
predictor variable (xi ). It is simple because there is only one predictor; linear in the
parameters because no parameter appears as an exponent nor is multiplied or divided
by another parameter; and linear in the predictor variable since the predictor variable is
raised only to the first power. When the predictor variable is raised to a power, this power
is called the order of the model. For now, only the simple linear model will be discussed.
The goal is to estimate the coefficients β0 and β1 in (2.7). The most well-known method of
estimating the coefficients β0 and β1 is to use ordinary least squares (OLS). OLS provides
estimates of β0 and β1 by minimizing the sum of the squared deviations of the Yi s for all
possible lines. Specifically, the sum of the squared residuals (ε̂i = Yi − Ŷi ) is minimized
when the OLS estimators of β0 and β1 are
A graphical representation of the residuals and a line fit to some data using OLS can be
seen in Figure 2.19 on the following page.
The OLS estimators of β0 and β1 are affected by outliers just as the mean and standard
deviation are subject to outliers. Recall that the median and IQR were suggested as
measures of center and spread, respectively, when working with skewed distributions. This
recommendation was made because the median and IQR provide more robust measures of
center and spread in the presence of outliers. In the presence of bivariate outliers, several
robust alternatives exist for computing estimates of β0 and β1 . Two alternatives to OLS
implemented in the MASS package will be considered. Specifically, least-trimmed squares
using the function lqs() and robust regression using an M estimator with the function
rlm() are discussed. Just as OLS sought to minimize the squared vertical distance between
all of the Yi s over all possible lines, least-trimmed squares minimizes the q smallest residuals
over all possible lines where q = (n + p + 1)/2 . Fitting for the function rlm() is done
by iterated re-weighted least squares. Although lqs() and rlm() are computationally
intensive, the interfaces for lm(), lqs(), and rlm() are essentially identical. All three
functions require a model formula of the form y ∼ x. The ∼ in this notation is read “is
modeled by.”
Example 2.24 In Exercise 2.23 on the preceding page, the correlation between the loga-
rithms of the body and brain weights in the data frame Animals from the MASS package with
and without dinosaurs was computed. Find the estimates for the least squares regression
lines with and without dinosaurs where the logarithm of brain is modeled by the logarithm
of body using Equations (2.8) and (2.9) as well as the S function lm(). Superimpose both
lines on the scatterplot using the function abline() (see Table A.12 on page 667).
62 Probability and Statistics with R
Y 3 Ŷ2
2
ε̂2 = Y2 − Ŷ2
1
0
Y2
0 1 2 3 4 5 6
FIGURE 2.19: Graph depicting residuals. The vertical distances shown with a dotted line
between the Yi s, depicted with a solid circle, and the Ŷi s, depicted with a clear square, are
the residuals.
Solution: Recall that there are a total of 28 animals in the data frame Animals and
25 animals in the NoDINO data frame. The scatterplot with superimposed regression lines
including the dinosaurs and omitting the dinosaurs is shown in Figure 2.20 on the next
page.
> attach(Animals)
> Y <- log(brain)
> X <- log(body)
> plot(X, Y, xlab="log(body)", ylab="log(brain)")
> b1 <- sum((X-mean(X))*(Y-mean(Y)))/sum((X-mean(X))^2)
> b0 <- mean(Y) - b1*mean(X)
> estimates <- c(b0, b1)
> estimates
[1] 2.5548981 0.4959947
> modDINO <- lm(Y~X)
> modDINO
Call:
lm(formula = Y ~ X)
Coefficients:
(Intercept) X
2.555 0.496
> detach(Animals)
> attach(NoDINO) # NoDINO contains 25 rows
> Y <- log(brain)
> X <- log(body)
> b1 <- sum((X-mean(X))*(Y-mean(Y)))/sum((X-mean(X))^2)
> b0 <- mean(Y) - b1*mean(X)
> estimates <- c(b0, b1)
> estimates
[1] 2.1504121 0.7522607
> modNODINO <- lm(Y~X)
> modNODINO
Call:
lm(formula = Y ~ X)
Coefficients:
(Intercept) X
2.1504 0.7523
4
2
0
0 5 10
log(body)
The intercept and slope of the regression line with dinosaurs are 2.555 and 0.496, respec-
tively. Without the dinosaurs, the intercept and slope of the regression line are 2.1504 and
0.7523, respectively.
64 Probability and Statistics with R
Example 2.25 From Figure 2.20 in Exercise 2.24 one notices three bivariate outliers
(dinosaurs). Fit regression lines to the same data used in Exercise 2.20 using ordinary least
squares, least-trimmed squares, and robust regression with an M estimator. Superimpose
the resulting regression lines on a scatterplot and label the lines accordingly.
Solution: The scatterplot with the three superimposed regression lines is shown in Fig-
ure 2.21 on the facing page.
> attach(Animals)
> plot(log(body), log(brain), col="blue")
> f <- log(brain)~log(body)
> modelLM <- lm(f)
> modelLM
Call: lm(formula = f)
Coefficients:
(Intercept) log(body)
2.555 0.496
Coefficients:
(Intercept) log(body)
1.816 0.776
Coefficients:
(Intercept) log(body)
2.0486717 0.7512927
The least-trimmed squares (lqs()) procedure and the robust line with M estimator (rlm())
method produce lines that put relatively little importance on outliers (dinosaurs). This is
Exploring Data 65
8
Robust line: M−estimator
6
log(brain)
4
2
0
0 5 10
log(body)
further highlighted when one considers the estimates β0 and β1 for the OLS estimates
without dinosaurs compared to the estimates of β0 and β1 for the least-trimmed squares
and robust procedures given in Table 2.3.
Table 2.3: Different values for b0 and b1 with various regression methods
Method b0 b1
OLS with dinosuars 2.555 0.496
OLS without dinosaurs 2.150 0.752
least-trimmed squares 1.816 0.776
robust line with M estimator 2.049 0.751
graphs are created with a formula syntax. The formula expresses the dependencies between
the variables as follows:
response ∼ predictor | [Link]
The expression y ∼ x | z is read “y is modeled as x given z.” Depending on the type
of graph, all three components may not need to be specified. Table A.11 on page 666 lists
the arguments for some of the more popular Trellis functions. If there is more than one
conditioning variable, they are all listed separated by the multiplication symbol (*).
Example 2.26 Use Trellis histograms to compare the body weight index (BWI) for the
two treatments (traditional sitting and hamstring stretch stored in Treatment) using the
data frame EPIDURAL.
Solution: Recall that BWI is typically defined as kg/m2 . Since the data frame EPIDURAL
does not contain a BWI variable, one is created:
> attach(EPIDURAL)
> BWI <- kg/(cm/100)^2
> library(lattice) # only for R
> histogram(~BWI|Treatment, layout=c(1,2))
> detach(EPIDURAL)
Traditional Sitting
40
30
20
Percent of Total
10
0
Hamstring Stretch
40
30
20
10
20 30 40 50 60
BWI
The histogram() function used the additional argument of layout=c(1,2). The first
value of layout determines the number of columns (1) in the Trellis graph and the second
value determines the number of rows (2) in the Trellis graph. This is in contrast to how
dimensions are specified in a matrix, which is number of rows by number of columns. The
basic shapes of the two histograms shown in Figure 2.22 are quite similar, just as was
observed in Example 2.18 on page 54 when the histograms were created using traditional S
graphs.
Exploring Data 67
Example 2.27 In Example 2.19 on page 54 side-by-side boxplots were used to compare the
BWI for the two treatments. An additional concern is that not only should the distribution
of BWI be similar for treatments, but it should also be similar for each physician. Use
Trellis graphs to create side-by-side boxplots of BWI by treatments given Doctor using the
data frame EPIDURAL.
Solution: The argument [Link]=TRUE used in the bwplot() function orders the graphs
the way one reads a book. The default arrangement of graphs is to start in the lower left
and move to the upper right. This is done so that the graphs appear with the smallest
values in the lower left, analogous to a scatterplot.
> attach(EPIDURAL)
> BWI <- kg/(cm/100)^2
> library(lattice)
> bwplot(Treatment~BWI|Doctor, [Link]=TRUE) # Order: as one reads
20 30 40 50 60
Dr. A Dr. B
Traditional Sitting
Hamstring Stretch
Dr. C Dr. D
Traditional Sitting
Hamstring Stretch
20 30 40 50 60
BWI
FIGURE 2.23: Trellis side-by-side boxplots of BWI in the traditional sitting and hamstring
stretch positions given Doctor
Since the number of observations for each of the treatments is relatively small (range is
from 6 to 15), it might be a better to look at the data with a stripplot. A stripplot of the
treatments conditioning on physician is illustrated in Figure 2.24 on the following page.
> stripplot(Treatment~BWI|Doctor, jitter=TRUE, [Link]=TRUE)
> detach(EPIDURAL)
The optional argument jitter=TRUE adds a small amount of noise to the values in the
stripplot so that overlapping values are easier to distinguish. Based on the stripplots shown
in Figure 2.24 on the next page, it seems that Dr. C’s patients have a consistently smaller
BWI for both treatment positions. Further investigation is needed to see why Dr. C’s
patients have consistently smaller BWI measurements versus the other physicians.
20 30 40 50 60
Dr. A Dr. B
Traditional Sitting
Hamstring Stretch
Dr. C Dr. D
Traditional Sitting
Hamstring Stretch
20 30 40 50 60
BWI
FIGURE 2.24: Trellis side-by-side stripplots of BWI in the traditional sitting and hamstring
stretch positions given Doctor
arranging several graphs on a single page. The first approach discussed is to arrange the
graphs in equally sized rectangles based on the dimensions of a matrix. In other words, if
one wants to plot six graphs on a single page, it might be accomplished with a 3 by 2 or a
2 by 3 matrix where each position of the matrix represents a graph. To print each graph,
the following structure is used:
Example 2.28 Use Trellis graphs to create boxplots of BWI given Doctor, a scatterplot
of cm versus kg given Doctor, a histogram of BWI, and a density plot of BWI given Treatment
using the data frame EPIDURAL. Show all four graphs on the same page.
Solution: The solution provided is for R. The commands that follow will work in S-PLUS
for graphs 2–4. However, the command bwplot(~BWI|Doctor) (graph 1) will not work in
S-PLUS. The argument [Link]=TRUE used in the bwplot() and the xyplot() functions
are not requested in the problem. However, they are used since most people like to read
from left to right and top to bottom. The four graphs are created and stored in variables
named graph1, graph2, graph3, and graph4, respectively. By splitting the graph into a
2 by 2 matrix or by specifying the position for each of the four graphs one can reproduce
Figure 2.25 on the facing page using the commands that follow.
> attach(EPIDURAL)
> library(lattice)
> graph1 <- bwplot(~BWI|Doctor, [Link]=TRUE)
> graph2 <- xyplot(cm~kg|Doctor, [Link]=TRUE)
Exploring Data 69
20 30 40 50 60
Percent of Total
40
Hamstring Stretch Traditional Sitting
30
0.06
Density
20
0.04
10 0.02
0 0.00
20 30 40 50 60 20 30 40 50 60
BWI BWI
20 30 40 50 60 60 80 120 160
20 30 40 50 60 60 80 120 160
BWI kg
FIGURE 2.25: Arrangement of four different Trellis graphs on the same page
Example 2.29 Create a Trellis x-y plot of cm versus kg given Doctor using the data
frame EPIDURAL. Use panel functions to superimpose the ordinary least squares line and a
least-trimmed squares line over the x-y plot.
70 Probability and Statistics with R
> library(lattice)
> library(MASS) # Needed for lqs
> attach(EPIDURAL)
> xyplot(cm~kg|Doctor, [Link]=TRUE,
+ panel=function(x, y)
+ {
+ [Link](x, y) # x-y plot
+ [Link](lm(y~x)) # Least sq line
+ [Link](lqs(y~x), col=3, lty=2, lwd=2) # Least trim sq line
+ }
+ )
Dr. A Dr. B
180
170
160
cm
Dr. C Dr. D
180
170
160
FIGURE 2.26: x-y plot of height (cm) versus weight (kg) given physician (Doctor) with
superimposed least squares and least-trimmed squares lines
Another approach is to create a panel function that will superimpose the least squares
and least-trimmed squares lines on an x-y plot and then to call that function within the
xyplot() as follows:
Both approaches produce identical output. The dashed lines (lty=2) in Figure 2.26 are the
least-trimmed squares lines.
Exploring Data 71
2.9 Problems
1. Load the MASS package.
(a) Enter the command help(package="MASS") and read about the functions and data
contained in this package.
(b) What does the description in the help file say about the function lqs()?
Enter help(lqs, package="MASS") to obtain information about the command lqs.
(c) What command shows the loaded packages?
(a) Create density histograms for the variables [Link], [Link], Weight, and
Length variables using a different color for each histogram.
(b) Superimpose estimated density curves over the histograms.
(c) Load the lattice package and do a box and whiskers plot of Price for every type of
vehicle according to the drive train. Do you observe any differences between prices?
(a) Find the quantiles, deciles, mean, maximum, minimum, interquartile range, variance,
and standard deviation of the variable hectares. Comment on the results. What
was Spain’s 2004 total harvested wheat area in hectares?
(b) Create a function that calculates the quantiles, the mean, the variance, the standard
deviation, the total, and the range of any variable.
(c) Which communities are below the 10th percentile in hectares? Which communities
are above the 90th percentile? In which percentile is Navarra?
(d) Create and display in the same graphics device a frequency histogram of the variable
acres and a density histogram of the variable acres. Superimpose a density curve
over the second histogram.
(e) Explain why using breaks of 0; 100,000; 250,000; 360,000; and 1,550,000 automati-
cally results in a density histogram.
(f) Create and display in the same graphics device a barplot of acres and a density
histogram of acres using break points of 0; 100,000; 250,000; 360,000; and 1,550,000.
(g) Add vertical lines to the density histogram of acres to indicate the locations of the
mean and the median, respectively.
(h) Create a boxplot of hectares and label the communities that appear as outliers in
the boxplot. (Hint: Use identify().)
(i) Determine the community with the largest harvested wheat surface area using either
acres or hectares. Remove this community from the data frame and compute the
mean, median, and standard deviation of hectares. How do these values compare
to the values for these statistics computed in (a)?
(a) Find the quantiles, deciles, mean, maximum, minimum, interquantile range, vari-
ance, and standard deviation for the variable ACRES. Comment on what the most
appropriate measures of center and spread would be for this variable. What is the
USA’s 2004 total harvested wheat surface are?
(b) Which states are below the 20th percentile? Which states are above the 80th
percentile? In which quantile is WI (Wisconsin)?
(c) Create a frequency and a density histogram in the same graphics device using square
plotting regions of the values in ACRES.
(d) Add vertical lines to the density histogram from (c) to indicate the location of the
mean and the median.
(e) Create a boxplot of the ACRES and locate the outliers’ communities and their values.
(f) Determine the state with the largest harvested wheat surface in acres. Remove this
state from the data frame and compute the mean, median, and standard deviation
of ACRES. How do these values compare to the values for these statistics computed
in (a)?
5. The data frame vit2005 in the PASWR package contains descriptive information and the
appraised total price (in euros) for apartments in Vitoria, Spain.
(a) Create a frequency table, a piechart, and a barplot showing the number of apartments
grouped by the variable out. For you, which method conveys the information best?
(b) Characterize the distribution of the variable totalprice.
(c) Characterize the relationship between totalprice and area.
(d) Create a Trellis plot of totalprice versus area conditioning on toilets. Are there
any outliers? Ignoring any outliers, between what two values of area do apartments
have both one and two bathrooms?
(e) Use the area values reported in (d) to create a subset of apartments that have both
one and two bathrooms. By how much does an additional bathroom increase the
appraised value of an apartment? Would you be willing to pay for an additional
bathroom if you lived in Vitoria, Spain?
(a) Create a “clean” data set that removes subjects if any observations on the subject
are “unknown.” Note that bwt, gestation, parity, height, weight, and smoke use
values of 999, 999, 9, 99, 999, and 9, respectively, to denote “unknown.” Store the
modified data set in an object named CLEAN.
(b) Use the information in CLEAN to create a density histogram of the birth weights of
babies whose mothers have never smoked (smoke=0) and another histogram placed
directly below the first in the same graphics device for the birth weights of babies
whose mothers currently smoke (smoke=1). Make the range of the x-axis 30 to 180
(ounces) for both histograms. Superimpose a density curve over each histogram.
Exploring Data 73
(c) Based on the histograms in (b), characterize the distribution of baby birth weight
for both non-smoking and smoking mothers.
(d) What is the mean weight difference between babies of smokers and non-smokers?
Can you think of any reasons not to use the mean as a measure of center to compare
birth weights in this problem?
(e) Create side-by-side boxplots to compare the birth weights of babies whose mother’s
never smoked and those who currently smoke. Use traditional graphics (boxplot())
as well as Trellis/lattice graphs to create the boxplots (bwplot()).
(f) What is the median weight difference between babies who are firstborn and those
who are not?
(g) Create a single graph of the densities for pre-pregnancy weight for mothers who
have never smoked and for mothers who currently smoke. Make sure both densities
appear on the same graphics device and place a color coded legend in the top right
corner of the graph.
(h) Characterize the pre-pregnancy distribution of weight for mothers who have never
smoked and for mothers who currently smoke.
(i) What is the mean pre-pregnancy weight difference between mothers who do not
smoke and those who do? Can you think of any reasons not to use the mean as a
measure of center to compare pre-pregnancy weights in this problem?
(j) Compute the body weight index (BWI) for each mother in CLEAN. Recall that BWI
is defined as kg/m2 (0.0254 m= 1 in., and 0.45359 kg= 1 lb.). Add the variables
weight in kg, height in m, and BWI to CLEAN and store the result in CLEANP.
(k) Characterize the distribution of BWI.
(l) Group pregnant mothers according to their BWI quartile. Find the mean and
standard deviation for baby birth weights in each quartile for mothers who have
never smoked and those who currently smoke. Find the median and IQR for baby
birth weights in each quartile for mothers who have never smoked and those who
currently smoke. Based on your answers, would you characterize birth weight in
each group as relatively symmetric or skewed? Create histograms and densities of
bwt conditioned on BWI quartiles and whether the mother smokes to verify your
previous assertions about the shape.
(m) Create side-by-side boxplots of bwt based on whether the mother smokes conditioned
on BWI quartiles. Does this graph verify your findings in (l)?
(n) Does it appear that BWI is related to the birth weight of a baby? Create a scatterplot
of birth weight (bwt) versus BWI while conditioning on BWI quartiles and whether
the mother smokes to help answer the question.
(o) Replace baby birth weight (bwt) with gestation length (gestation) and answer
questions (l), (m), and (n).
(p) Create a scatterplot of bwt versus gestation conditioned on BWI quartiles and
whether the mother smokes. Fit straight lines to the data using lm(), lqs(), and
rlm(); and display the lines in the scatterplots. What do you find interesting about
the resulting graphs?
(q) Create a table of smoke by parity. Display the numerical results in a graph. What
percent of mothers did not smoke during the pregnancy of their first child?
7. Some claim the final hours aboard the RMS Titanic were marked by class warfare; others
claim it was characterized by male chivalry. The data frame titanic3 from the PASWR
74 Probability and Statistics with R
8. Use the Cars2004EU data frame from the PASWR package which contains the numbers of
cars per 1000 inhabitants (cars), the total number of known mortal accidents (deaths),
and the country population/1000 (population) for the 25 member countries of the
European Union for the year 2004.
(a) Compute the total number of cars per 1000 inhabitants in each country, and store
the result in an object named [Link]. Determine the total number of known
automobile fatalities in 2004 divided by the total number of cars for each country
and store the result in an object named [Link].
(b) Create a barplot showing the automobile death rate for each of the European Union
member countries. Make the bars increase in magnitude so that the countries with
the smallest automobile death rates appear first.
(c) Which country has the lowest automobile death rate? Which country has the highest
automobile death rate?
(d) Create a scatterplot of population versus [Link]. How would you characterize
the relationship?
(e) Find the least squares estimates for regressing population on [Link]. Super-
impose the least squares line on the scatterplot from (d). What population does the
least squares model predict for a country with a [Link] value of 19224.630?
Find the difference between the population predicted from the least squares model
and the actual population for the country with a [Link] value of 19224.630.
(f) Create a scatterplot of [Link] versus [Link]. How would you characterize
the relationship between the two variables?
(g) Compute Spearman’s rank correlation coefficient of [Link] and [Link].
(Hint: Use cor(x, y, method="spearman").) What is this coefficient measuring?
(h) Plot the logarithm of [Link] versus the logarithm of [Link]. How would
you characterize the relationship?
(i) What are the least squares estimates for the regression of log([Link]) on
log([Link]). Superimpose the least squares line on the scatterplot from
Exploring Data 75
(h). What death rate does the least squares model predict for a country with a
log([Link]) value of 9.863948? Make sure you express your answer in the
same units as those used for [Link].
9. The data frame SurfaceSpain in the PASWR package contains the surface area (km2 ) for
seventeen autonomous Spanish communities.
(a) Use the function merge() to combine the data frames WheatSpain (from problem 3)
and SurfaceSpain into a new data frame named DataSpain.
(b) Create a variable named surface.h containing the surface area of each autonomous
community in hectares. (Note: 100 hectares = 1 km2 .) Create a variable named
wheat.p containing the percent surface area in each autonomous community ded-
icated to growing wheat. Add the newly created variables to the data frame
DataSpain and store the result as a data frame with the name DataSpain.m.
(c) Assign the names of the autonomous communities as row names for DataSpain.m
and remove the variable community from the data frame.
(d) Create a barplot showing the percent surface area dedicated to growing wheat for
each of the seventeen Spanish autonomous communities. Arrange the communities
by decreasing percentages.
(e) Display the percent surface area dedicated to growing wheat for each of the seventeen
Spanish autonomous communities using the function dotchart(). To read about
dotchart(), type ?dotchart at the command prompt. Do you prefer the barchart
or the dotchart? Explain your answer.
(f) Describe the relationship between the surface area in an autonomous community
dedicated to growing wheat (hectares) and the total surface area of the autonomous
community (surface.h).
(g) Describe the relationship between the surface area in an autonomous community
dedicated to growing wheat (hectares) and the percent of surface area dedicated to
growing wheat out of the communities’ total surface area (wheat.p).
(h) Develop a model to predict the surface area in an autonomous community dedicated
to growing wheat (hectares) based on the total surface area of the autonomous
community (surface.h).
Chapter 3
General Probability and Random Variables
3.1 Introduction
One of the main objectives of statistics is to help make “good” decisions under conditions
of uncertainty. Probability is one way to quantify outcomes that cannot be predicted with
certainty. For example, when throwing two dice, the outcome of the experiment cannot
be known before the dice are thrown. Random variables, as well as counting techniques,
will facilitate the analysis of problems such as the example of throwing two dice. This
chapter provides a brief introduction to counting techniques, axiomatic probability, random
variables, and moment generating functions.
Example 3.1 A computer store sells three brands of laptops. Each laptop is sold with a
carrying case and four different options for upgrading RAM. Suppose the store only carries
two styles of carrying cases. How many different combinations of laptop, carrying case, and
RAM are possible?
77
78 Probability and Statistics with R
Example 3.2 How many different license plates can be made from four digits?
Solution: First, note that there is no restriction forbidding repeated digits. That is,
0001, 0002, 0003, . . ., 9999 are all permissible. In essence, this translates to sampling with
replacement. Since there are 10 choices for each of the four license plate digits, there are a
total of 10 × 10 × 10 × 10 = 104 = 10, 000 possible license plates.
n!
Pm,n = n(n − 1)(n − 2) · · · (n − m + 1) = .
(n − m)!
Any ordered sequence of m objects taken from n distinct objects is called a permutation
and is denoted Pm,n .
Example 3.3 How many different ways can the first three places be decided in a race
with four runners?
Solution: The number of ways the first three places can be decided using the basic
principle of counting is by reasoning as follows:
Any one of the four runners might arrive in first place (four outcomes for the first
experiment). After the first runner crosses the finish line, there are three possible choices
for second place (three outcomes for the second experiment). Then, after second place is
decided, there are only two runners left (two outcomes for the third experiment). Conse-
quently, there are 4 · 3 · 2 = 24 possible ways to award the first three places. The problem
may also be solved by applying the permutation formula:
4! 4!
P3,4 = = = 4 · 3 · 2 = 24.
(4 − 3)! 1!
Example 3.4 How many ways can seven students form a line?
Solution: First, note that once a student is selected for a place in line, the number of
students for subsequent orderings is diminished by one. That is, this is a problem where
sampling is done without replacement. A useful strategy for this type of problem is actually
to think through assigning the students to positions before using a formula (permutation
in this case). If seven slots are drawn, then the reasoning is as follows:
There are seven ways a student can be assigned to the first slot. Once the first slot has
been assigned, there are six possible ways to pick a student for the next slot. Continue
with this logic until all of the students have been assigned a slot. Appealing to the basic
principle of counting, it is seen that there are 7 × 6 × 5 × 4 × 3 × 2 × 1 = 7! = 5040 possible
ways to form a line with seven students. This is the same number calculated by considering
7!
a permutation of seven things taken seven at a time P7,7 = (7−7)! = 7!
0! = 5040. Note that
0! = 1.
Example 3.5 How many different letter arrangements can be formed using the letters
DATA?
Solution: Note that there are 4! permutations of the letters D1 A1 T1 A2 when the two A’s
are distinguished from each other. However, since the A’s are indistinguishable, there are
4!
only 2!·1!·1! = 12 possible permutations of the letters DATA.
3.2.3 Combinations
In many problems, selecting m objects from n total objects without regard to order is the
scenario of interest. For example, when selecting a committee, the order of the committee
is rarely important. That is, a committee consisting of John, Mary, and Paul is considered
the same committee if the members are listed Mary, Paul, and John. An arrangement of m
objects taken from n objects without regard to order is called a combination. The n number
of combinations of n distinct objects taken m at a time is denoted as Cm,n or m and is
calculated as
n n!
Cm,n = = .
m m!(n − m)!
Example 3.6 A committee of three people is to be formed from a group of eight people.
How many different committees are possible?
Solution: There are C3,8 = 83 = 3!·(8−3)!
8!
= 56 possible committees.
Example 3.7 How many different three-letter sequences can be formed from the letters
A, B, C, and D if
(c) letter repetition is not permissible and sequences containing the same letters are con-
sidered equal regardless of letter order?
Example 3.8 If nine people are to be assigned into three committees of sizes two, three,
and four, respectively, how many possible assignments are possible?
Solution: There are 92 ways to pick the first committee. Once that committee is selected,
there
7 are seven members left from which a committee of size three is selected. So, there are
3 ways to pick the second committee. Using the same logic, there are finally four members
left from which one committee of size four must be selected. There is only one way to select
the remaining committee, which is to select all of the remaining members to be
on the
committee. Using the basic rule of multiplication, there are a total of 92 × 73 × 44 =
1260 ways to form the three committees. To compute the final answer, the S commands
choose(), prod(), or a combination of the two can be used.
80 Probability and Statistics with R
> choose(9,2)*choose(7,3)*choose(4,4)
[1] 1260
> prod(9:1)/(prod(2:1)*prod(3:1)*prod(4:1))
[1] 1260
> choose(9,2)*(prod(7:1)/(prod(3:1)*prod(4:1)))
[1] 1260
3.3 Probability
3.3.1 Sample Space and Events
An experiment is any action or process that generates observations. The sample
space of an experiment, denoted by Ω, is the set of all of the possible outcomes of an
experiment. Although the outcome of an experiment cannot be known before it has taken
place, it is possible to define the sample space for a given experiment. The sample space
may be either finite or infinite. For example, the number of unoccupied seats in a train
corresponds to a finite sample space. The number of passengers arriving at an airport also
produces a finite sample space, assuming a one to one correspondence between arriving
passengers and the natural numbers. The sample space for the lifetime of light bulbs,
however, is infinite, since lifetime may be any positive value.
An event is any subset of the sample space, which is often denoted with the letter E.
Events are said to be simple when they contain only one outcome; otherwise, events are
considered to be compound. Consider an experiment where a single die is thrown. Since
the die might show any one of six numbers, the sample space is written Ω = {1, 2, 3, 4, 5, 6};
and any subset of Ω, such as E1 = {even numbers}, E2 = {2}, E3 = {1, 2, 4}, E4 = Ω, or
E5 = ∅, is considered an event. Specifically, E2 is considered a simple event while all of the
remaining events are considered to be compound events. Event E5 is known as the empty
set or the null set, the event that does not contain any outcomes. In many problems, the
events of interest will be formed through a combination of two or more events by taking
unions, intersections, and complements.
1. Commutative laws
2. Associative laws
3. Distributive laws
• (E ∩ F ) ∪ G = (E ∪ G) ∩ (F ∪ G)
• (E ∪ F ) ∩ G = (E ∩ G) ∪ (F ∩ G)
4. DeMorgan’s laws
∞ c ∞
• Ei = Eic
i=1 i=1
∞
c ∞
• Ei = Eic
i=1 i=1
1. 0 ≤ P(E) ≤ 1
2. P(Ω) = 1
The following results are all easily derived using some combination of the three axioms
of probability:
1. P(E c ) = 1 − P(E)
Proof: Note that E ∪ F can be represented as the union of two mutually exclusive
events, E and (E c ∩F ). That is, E∪F = E∪(E c ∩F ). Event F can also be represented
as the union of two mutually exclusive events, (E ∩ F ) and (E c ∩ F ). By probability
axiom 3, P(E ∪ F ) = P(E) + P(E c ∩ F ) as well as P(F ) = P(E ∩ F ) + P(E c ∩ F ).
By solving for P(E c ∩ F ) in the second equation and substituting the answer into the
first equation, the desired result of P(E ∪ F ) = P(E) + P(F ) − P(E ∩ F ) is obtained.
3. P(∅) = 0
Solution: Let the event E denote two or more students with the same birthday. In this
problem, it is easier to find E c , as there are a number of ways that E can take place. There
are a total of 365m possible outcomes in the sample space. E c can occur in 365 × 364 ×
· · · × (365 − m + 1) ways. Consequently,
The following S code can be used to create or modify a table such as Table 3.1 on the next
page, which gives P(E) for m = 10, 15, . . . , 50:
Table 3.1: Probability of two or more students having the same birthday
m P(E)
10 0.1169482
15 0.2529013
20 0.4114384
25 0.5686997
30 0.7063162
35 0.8143832
40 0.8912318
45 0.9409759
50 0.9703736
Example 3.10 Given two events E and F , suppose that P(E) = 0.3, P(F ) = 0.5, and
P(E ∪ F ) = 0.6. Find P(E ∩ F ).
Solution: Since P(E ∪ F ) = P(E) + P(F ) − P(E ∩ F ), 0.6 = 0.3 + 0.5 − P(E ∩ F ). Thus,
P(E ∩ F ) = 0.2.
P(E ∩ F )
P(F |E) = . (3.1)
P(E)
It is left as an exercise for the reader to verify that P(F |E) satisfies the three axioms of
probability.
Example 3.11 Suppose two fair dice are tossed where each of the 36 possible outcomes
is equally likely to occur. Knowing that the first die shows a 4, what is the probability that
the sum of the two dice equals 8?
Solution: The sample space for this experiment is given as Ω = {(i, j), i = 1, 2, . . . , 6,
j = 1, 2, . . . , 6}, where each pair (i, j) has a probability 1/36 of occurring. Define “the
sum of the dice equals 8” to be event F and “a 4 on the first toss” to be event E. Since
E ∩ F corresponds to the outcome (4, 4) with probability P(E ∩ F ) = 1/36 and there are
84 Probability and Statistics with R
six outcomes with a 4 on the first toss, (4, 1), (4, 2), . . . , (4, 6), the probability of event E,
P(E) = 6/36 = 1/6 and the answer is calculated as
P(E ∩ F ) 1/36 1
P(F |E) = = = .
P(E) 1/6 6
Example 3.12 Suppose a box contains 50 defective light bulbs, 100 partially defective
light bulbs (last only 3 hours), and 250 good light bulbs. If one of the bulbs from the box is
used and it does not immediately go out, what is the probability the light bulb is actually
a good light bulb?
Solution: The conditional probability the light bulb is good given that the light bulb is
not defective is desired. Using (3.1), write
P(Good) 250/400 5
P(Good|Not Defective) = = = .
P(Not Defective) 350/400 7
Solution: Let C correspond to the event “the battery does not work properly,” A to the
event “the battery was supplied by A,” and B to the event “the battery was supplied by
B.” The Venn diagram in Figure 3.1 on the next page provides a graphical illustration of
the sample space for this example. Since a working battery might come from either supplier
A or B, A and B are disjoint events. Consequently, P(C) = P(C ∩ A) + P(C ∩ B). Given
that
P(A) = 0.55, P(C|A) = 0.05, P(C ∩ A) = P(C|A)P(A),
P(B) = 0.45, P(C|B) = 0.04, and P(C ∩ B) = P(C|B)P(B),
write P(C) = (0.05)(0.55) + (0.04)(0.45) = 0.0455. Then, the probability that the battery
works properly is 1 − P(C) = 0.9545.
General Probability and Random Variables 85
.....................................................................................................................................................................................................................................................................................................................................
... ... ...
... ... ...
... .... ....
... ... ...
... ... ...
...
...
A ...
...
...
... B
... ... ...
... ..... .....
... ... ...
... ... ...
...
...
....
... P(C ∩ B) = 0.018 ....
...
...
...
... ..
.........
.....................................................
.
.
.
.
........
..
...
...
...
.... ... .
.... ...
...
...
.
.....
.
C .
.....
....
.. .....
... ...
..... .
.. .... ...
... ......... .
. ..
.. ...
... .................. .... ......................... ....
..............
...
...
...
.....
...
...
...
...
...
... P(C ∩ A) = 0.0275 ...
...
...
...
... ..... .....
... ... ...
... ... ...
... .... ....
... ... ...
P(A) = 0.55 ...
...
...
...
...
P(B) = 0.45 ...
...
...
... ... ..
.
.
..................................................................................................................................................................................................................................................................................................................................
Example 3.14 Suppose a student answers all of the questions on a multiple-choice test.
Let p be the probability the student actually knows the answer and 1 − p be the probability
the student is guessing for a given question. Assume students that guess have a 1/a proba-
bility of getting the correct answer, where a represents the number of possible responses to
the question. What is the conditional probability a student knew the answer to a question
given that he answered correctly?
Solution: Let the events E, F1 , and F2 represent the events “question answered correctly,”
“student knew the correct answer,” and “student guessed,” respectively. Using (3.3), write
P(F1 ∩ E) P(F1 ) p
P(F1 |E) = = =
P(E) P(E|F1 )P(F1 ) + P(E|F2 )P(F2 ) p + (1 − p)/a
As a special case, if a = 4 and p = 1/2, then the probability a student actually knew the
answer given their response was correct is 4/5.
Example 3.15 Bayes’ Rule: Choose a Door The television show Let’s Make a
Deal, hosted by Monty Hall, gave contestants the chance to choose, among three doors, the
one that concealed the grand prize. Behind the other two doors were much less valuable
prizes. After the contestant chose one of the doors, say Door 1, Monty opened one of the
other two doors, say Door 3, containing a much less valuable prize. The contestant was
then asked whether he or she wished to stay with the original choice (Door 1) or switch
to the other closed door (Door 2). What should the contestant do? Is it better to stay
with the original choice or to switch to the other closed door? Or does it really matter?
The answer, of course, depends on whether contestants improve their chances of winning by
switching doors. In particular, what is the probability of winning by switching doors when
given the opportunity; and what is the probability of winning by staying with the initial
door selection? First, simulate the problem with S to provide approximate probabilities for
the various strategies. Following the simulation, show how Bayes’ Rule can be used to solve
the problem exactly.
Solution: To simulate the problem, generate a random vector named actual of size 10,000
containing the numbers 1, 2, and 3. In the vector actual, the numbers 1, 2, and 3 represent
the door behind which the grand prize is contained. Then, generate another vector named
guess of size 10,000 containing the numbers 1, 2, and 3 to represent the contestant’s initial
guess. If the ith values of the vectors actual and guess agree, the contestant wins the grand
86 Probability and Statistics with R
prize by staying with his initial guess. On the other hand, if the ith values of the vectors
actual and guess disagree, the contestant wins the grand prize by switching. Consider the
following S code and the results that suggest the contestant is twice as likely to win the
grand prize by switching doors:
Next use (3.3) after defining events Di and Oj to find P(D1 |O3 ) and P(D2 |O3 ). Start by
assuming the contestant initially guesses Door 1 and that Monty opens Door 3. Let the
event Di = Door i conceals the prize and Oj = Monty opens door j after the contestant
selects Door 1. When a contestant initially selects a door, P(D1 ) = P(D2 ) = P(D3 ) = 1/3.
Once Monty shows the grand prize is not behind Door 3, the probability of winning the
grand prize is now one of P(D1 |O3 ) or P(D2 |O3 ). Note that P(D1 |O3 ) corresponds to the
strategy of sticking with the initial guess and P(D2 |O3 ) corresponds to the strategy of
switching doors. Based on how the show is designed, the following are known:
• P(O3 |D1 ) = 1/2 since Monty can open one of either Door 3 or Door 2.
• P(O3 |D2 ) = 1 since the only door Monty can open without revealing the grand prize
is Door 3.
• P(O3 |D3 ) = 0 since Monty will not open Door 3 if it contains the grand prize.
event F does not alter the probability of obtaining E. When this happens, event E is
independent of event F . More formally, two events E and F are independent if and only
if P(E|F ) = P(E) or P(F |E) = P(F ). An equivalent way to define independence between
two events is to use (3.1) and to show that P(E ∩ F ) = P(E)P(F ). Independence between
two events is really a special case of independence among n events. Define events E1 , . . . , En
to be independent if, for every k where k = 2, . . . , n and every subset of indices i1 , i2 , . . . , ik ,
P(Ei1 ∩Ei2 ∩· · ·∩Eik ) = P(Ei1 )P(Ei2 ) · · · P(Eik ). It is important to point out that events in
any subset of the original independent events of size r, where r ≤ k, are also independent.
Further, if events E1 , . . . , En are independent, then so are E1c , . . . , Enc .
1j 2j
3j
Solution: Let Ai (i = 1, 2, 3) be the event the ith component works, and E the event the
entire system works. Consequently, event E = (A1 ∩A2 )∪A3 , and P(E) = P[(A1 ∩A2 )∪A3 ].
P(E) = P[(A1 ∩ A2 ) ∪ A3 ]
= P(A1 ∩ A2 ) + P(A3 ) − P(A1 ∩ A2 ∩ A3 )
= P(A1 )P(A2 ) + P(A3 ) − P(A1 )P(A2 )P(A3 )
= (0.9)(0.9) + 0.9 − (0.9)(0.9)(0.9)
= 0.981
space Ω into the real numbers. Random variables will always be denoted with uppercase
letters, for example, X or Y , and the realized values of the random variable will be denoted
with lowercase letters, for example, x or y. Here are some examples of random variables:
3. Individual 40 kilometer cycling time trial. X = the time to complete the course.
1. 0 ≤ F (x) ≤ 1.
2. If a < b, then F (a) ≤ F (b) for any real numbers a and b. In other words, F (x) is a
non-decreasing function of x.
3. lim F (x) = 1.
x→∞
4. lim F (x) = 0.
x→−∞
5. F (x) is a step function, and the height of the step at x is equal to f (x) = P(X = x).
Example 3.17 Toss a fair coin three times and let the random variable X represent the
number of heads in the three tosses. Produce graphical representations of both the pdf and
cdf for the random variable X.
General Probability and Random Variables 89
The random variable X can take on the values 0, 1, 2, and 3 with probabilities 18 , 38 , 38 , and
8 , respectively. Define the cdf for X, F (x) = P(X ≤ x) as follows:
1
⎧
⎪
⎪ 0 if x < 0,
⎪
⎪
⎪
⎪ 0≤x<1
⎨1/8 if
F (x) = 4/8 if 1≤x<2
⎪
⎪
⎪
⎪ 7/8 if 2≤x<3
⎪
⎪
⎩1 if x≥3
The code for producing a graph similar to Figure 3.3 on the next page with placement
of specific values along the axes for both the pdf and cdf using the function axis() is as
follows:
P(X ≤ x)
P(X = x)
4/8
1/8
0 1 2 3 -1 0 1 2 3 4 5
x x
FIGURE 3.3: The pdf and cdf for the random variable X, the number of heads in three
tosses of a fair coin
Also denote E[X] as μX , recognizing that E[X] is the mean of the random variable X. In
this definition, it is assumed the sum exists; otherwise, the expectation is undefined. It can
be helpful to think of E[X] as the fulcrum on a balance beam as illustrated in Figure 3.4.
Example 3.18 A particular game is played where the contestant spins a wheel that can
land on the numbers 1, 5, or 30 with probabilities of 0.50, 0.45, and 0.05, respectively. The
contestant pays $5 to play the game and is awarded the amount of money indicated by the
number where the spinner lands. Is this a fair game?
Solution: By fair, it is meant that the contestant should have an expected return equal
to the price she pays to play the game. To answer the question, the expected (average)
General Probability and Random Variables 91
winnings from playing the game need to be computed. Let the random variable X represent
the player’s winnings:
Therefore, this game is not fair, as the house makes an average of 75 cents each time the
game is played.
Another interpretation of the expected value of the random variable X is to view it as
a weighted mean. Code to compute the expected value using (3.4) and using the function
[Link]() is
> x <- c(1,5,30)
> px <- c(0.5,0.45,0.05)
> EX <- sum(x*px)
> WM <- [Link](x, px)
> c(EX, WM)
[1] 4.25 4.25
Often, a random variable itself is not of interest, but rather some function of it is
important, say g(X), of the random variable X. The expected value of a function g(X) of
the random variable X with pdf p(x) is
E g(X) = g(x) · p(x). (3.5)
x
Example 3.19 Consider Example 3.18, for which the random variable Y is defined to be
the player’s net return. That is, Y = X − 5 since the player spends $5 to play the game.
What is the expected value of Y ?
Solution: The expected value of Y is
Rules of Expected Value The function g(X) is often a linear function a + bX, where a
and b are constants. When this occurs, E[g(X)] is easily computed from E[X]. In Example
3.19, a and b were -5 and 1, respectively, for the linear function g(X). The following rules
for expected value, when working with a random variable X and constants a and b, are
true:
1. E[bX] = bE[X].
2. E[a + bX] = a + bE[X].
Unfortunately, if g(X) is not a linear function of X, such as g(X) = X 2 , the E X 2 =
2
(E[X]) . In general, E g(X) = g E[X] .
92 Probability and Statistics with R
3.4.4 Moments
Another way to define the expected value of a random variable is with moments.
However, knowing the mean (expected value) of a distribution does not tell the whole
story. Several distributions may have the same mean. In this case, additional information,
such as the spread of the distribution and the symmetry of the distribution, is helpful in
distinguishing among various distributions.
The r th moment about the
origin of a random variable X, denoted αr , is defined as
E [X r ] . Note that α1 = E X 1 is called the mean of the distribution of X, also denoted
μX or simply μ. The special moments defined next are important in the field of statistics as
they help describe a random variable’s distributional shape. The r th moment about the
mean of a random variable X, denoted μr , is the expected value of (X − μ)r . However, all
th
moments do not exist.∞ Forrthe r moment about the origin of a discrete random variable
to be well-defined, i=1 |xi |P(X = xi ) must be less than ∞.
[Link] Variance
The second moment about the mean is called the variance of the distribution of X, or
simply the variance of X:
2
Var[X] = σX = E (X − μ)2 = E X 2 − μ2 (3.7)
The positive square root of the variance is called the standard deviation and is denoted
σX . The units of measurement for standard deviation are always the same as those for the
random variable X. One way to avoid this unit dependency is to use the coefficient of
variation, a unitless measure of variability.
Definition 3.2: Coefficient of variation — When E[X] = 0,
σX
CVX = , (3.8)
E[X]
∞
2. f (x) dx = 1, and (3.9)
−∞
b
3. P(a ≤ X ≤ b) = f (x) dx.
a
a b b a
b b a
a
f (x) dx −∞ f (x) dx −∞ f (x) dx
Condition 3 from (3.9) for the definition of a pdf for a continuous random variable is
illustrated in Figure 3.5.
Definition 3.3: Cumulative Density Function — The cdf, F (x), of a continuous
random variable X with pdf f (x) is
x
F (x) = P(X ≤ x) = f (t) dt, −∞ < x < ∞ (3.10)
−∞
According to Definition 3.3, the cdf is derived from an existing pdf. Further, according
94 Probability and Statistics with R
to the fundamental theorem of calculus, the other direction is also true since F (x) = f (x)
for all values of x for which the derivative F (x) exists.
1. 0 ≤ F (x) ≤ 1.
4. lim F (x) = 0.
x→−∞
(a) Find the constant k so that f (x) is a pdf of the random variable X.
∞ 1
1= f (x) dx = k(1 − x2 ) dx
−∞ −1
! "1 # $
x3 1 −1
=k x− =k 1− − −1 −
3 −1 3 3
! "
2 −2 4 3
=k − =k ⇒k= .
3 3 3 4
(c) Using property 3 from (3.9) for the pdf of a continuous random variable, write
1.0
0.8
0.8
0.6
0.6
F (x)
f (x)
0.4
0.4
0.2
0.2
0.0
0.0
-2 -1 0 1 2 -2 -1 0 1 2
x x
1 1
3 3x x3
P(−0.5 ≤ X ≤ 1) = 1 − x dx =
2
− = 0.84375.
4 4 4 −0.5
−0.5
The following code computes P(−0.5 ≤ X ≤ 1) using the function integrate() for R and
S-PLUS, respectively:
xj
j
f (x) dx = .
100
−∞
m
−e−2x 0 = 0.5
−e−2m + 1 = 0.5
−e−2m = 0.5 − 1
ln(e−2m ) = ln(0.5)
ln(0.5)
m= = 0.3466.
−2
x25
(b) The 25th percentile is the value x25 such that 2e−2x dx = 0.25, which implies
0
x25
−e−2x 0 = 0.25
−e−2x25 + 1 = 0.25
−e−2x25 = 0.25 − 1
ln(e−2x25 ) = ln(0.75)
ln(0.75)
x25 = = 0.1438.
−2
x60
(c) The 60th percentile is the value x60 such that 2e−2x dx = 0.60, which implies
0
x60
−e−2x 0 = 0.60
−e−2x60 + 1 = 0.60
−e−2x60 = 0.60 − 1
ln(e−2x60 ) = ln(0.40)
ln(0.40)
x60 = = 0.4581.
−2
(c) Draw the pdf and add vertical lines to indicate the values found in part (b).
(a) The function 2 cos 2x does not have a maximum in the open interval (0, π/4) since the
derivative f (x) = −4 sin 2x does not equal 0 in the open interval (0, π/4).
98 Probability and Statistics with R
1.0
0.5
0.0
π
FIGURE 3.7: Graph of 2 cos(2x) from 0 to 4 with R
When the integral in (3.12) does not exist, neither does the expectation of the random
variable X. The expected value of a function of X, say g(X), is
∞
E g(X) = g(x) · f (x) dx. (3.13)
−∞
General Probability and Random Variables 99
Using the definitions for moments about 0 and μ given in (3.6), which relied strictly on
expectation in conjunction with (3.13), the variance of a continuous random variable X is
written as
∞
Var[X] = σX = E (X − μ) =
2 2
(x − μ)2 f (x) dx. (3.14)
−∞
(a) Find the value of k to make f (x) a pdf. Use this k for parts (b) and (c).
1
k dx = 1
−1
1
kx−1 = 1
1
2k = 1 ⇒ k = .
2
1
1
E[X] = μX = x dx
2
−1
1
x2
= =0
4 −1
∞
Var[X] = 2
σX = E (X − μ) 2
= (x − μ)2 f (x) dx
−∞
1
1
= (x − 0)2 dx
2
−1
1
x3 1
= =
6 −1 3
100 Probability and Statistics with R
E[g(X)]
E[I(g(X))] ≤ .
K
Step 4. Clearly
E I g(X) = I g(x) · p(x)
x
% & '( % & '(
= 1 · P I g(X) = 1 + 0 · P I g(X) = 0
= 1 · P g(X) ≥ K + 0 · P g(X) < K
= P g(X) ≥ K .
Step 5. Rewriting,
E[g(X)]
P g(X) ≥ K ≤ ,
K
which is the inequality from (3.15) to be proven.
Example 3.24 Consider Example 3.17 on page 88, where X was defined to be the number
of heads in three tosses of a fair coin. Chebyshev’s Inequality guarantees at least what
fraction of the distribution of X is within k = 2 standard deviations from its mean? What
is the actual fraction of the distribution of X that is within k = 2 standard deviations from
its mean?
Solution: Using version (d) of Chebyshev’s Inequality, P(|X −μ| < kσ) ≥ 1− k12 , compute
the first answer to be 1 − 212 = 34 . To answer the second question, first find the mean and
variance of X:
1 3 3 1 3
E[X] = x p(x) = 0 × + 1 × + 2 × + 3 × = = 1.5
x
8 8 8 8 2
1 3 3 1 3
E X 2
= x2 p(x) = 02 × + 12 × + 22 × + 32 × = = 3
x
8 8 8 8 2
2
Var[X] = E X 2 − E[X] = 3 − 1.52 = 0.75
For this example,
√
P(|X − μ| < kσ) = P(|X − 1.5| < 2 0.75)
= P(|X − 1.5| < 1.732)
= P(−0.232 < X < 3.232) = 1.
X is within k = 2 standard deviations from the mean illustrates the conservative nature of
Chebyshev’s Inequality.
To compute the needed quantities with S, use the following code:
Proof: Consider the random variables X1 , . . . , Xn such that the mean of each one is μ and
the variance of each one is σ 2 . Since
! n " ! n "
i=1 Xi i=1 Xi σ2
E = μ and Var = ,
n n n
3.4.8 Skewness
Earlier it was discussed that the second moment about the mean of a random variable
X is the same thing as the variance of X. Now, the third moment about the mean of a
random variable X is used in the definition of the skewness of X. To facilitate the notation
used with skewness, first define a standardized random variable X ∗ to be:
X−μ
X∗ = ,
σ
General Probability and Random Variables 103
where μ is the mean of X and σ is the standard deviation of X. Using the standardized
form of X, it is easily shown that E[X ∗ ] = 0 and Var[X ∗ ] = 1. Define the skewness of a
random variable X, denoted γ1 , to be the third moment about the origin of X ∗ :
∗ 3 E (X − μ)3
γ1 = E (X ) = (3.19)
σ3
Positive values for γ1 indicate a distribution that is skewed to the right while negative values
for γ1 indicate a distribution that is skewed to the left. If the distribution of X is symmetric
with respect to its mean, then its skewness is zero. That is, γ1 = 0 for distributions that
are symmetric about their mean. Examples of distributions with various γ1 coefficients are
shown in Figure 3.8.
FIGURE 3.8: Distributions with γ1 (skewness) coefficients that are negative, zero, and
positive, respectively.
E[(X − μ)3 ]
γ1 = E[(X ∗ )3 ] = = −0.5879747
σ3
which means the distribution has a negative skew. To compute the answer with S, the
following facts are used:
1. μ = E[X].
2. σ = E[X 2 ] − E[X]2 .
3. X ∗ = X−μ
σ .
4. γ1 = E (X ∗ )3 .
∞
MX (t) = E e tX
= etx f (x) dx, −h < t < h. (3.20)
−∞
of the random variable X, find the mgf of the distribution using (3.20).
1
Solution: The reader may verify that a value of k = 2 produces a valid pdf. The mgf of
the distribution will then be
∞
MX (t) = E e tX
= etx f (x) dx, −h < t < h
−∞
1 1
tx 1 etx
= e dx =
2 2t −1
−1
e − e−t
t
= , t = 0.
2t
Note that if t = 0, then MX (t) = 1 since MX (t) = E etX = E e0 = 1. Therefore, the
mgf is written ⎧ t
⎨ e − e−t
if t = 0
MX (t) = 2t
⎩1 if t = 0.
Theorem 3.2 If X has mgf MX (t), then the derivatives of MX (t) of all orders exist at
t = 0, and
dr
E[X r ] = r MX (t)|t=0 .
dt
General Probability and Random Variables 105
A proof of the last theorem is beyond the scope of this text. However, assuming the
distribution is discrete and summation and differentiation may be interchanged, note that
d1 d1 d1 tx
E X 1 = 1 MX (t)|t=0 = 1 etx p(x)|t=0 = e p(x)|t=0
dt dt x x
dt1
x x
n!
P (X = x|n, π) = π x (1 − π)(n−x) x = 0, 1, . . . , n
(n − x)!x!
n
n tx x
tx
M (t) = E[e ] = e π (1 − π)n−x
x=0
x
n
n
= (πet )x (1 − π)n−x
x=0
x
= [(1 − π) + πet ]n
The first and second derivatives of M (t) at t = 0 give E[X] and E[X 2 ], respectively,
which are used to calculate the mean and variance of X:
M (t) = n(n − 1)[(1 − π) + πet ]n−2 (πet )2 + n[(1 − π) + πet ]n−1 (πet ).
This yields
3.5 Problems
1. Three dice are thrown. What fraction of the time does a a sum of 9 appear on the faces?
What percent of the time does a sum of 10 appear?
2. How many different six-place license plates are possible if the first two places are letters
and the remaining places are numbers?
3. How many different six-place license plates are possible (first two places letters, remaining
places numbers) if repetition among letters and numbers is not permissible?
4. Susie has 25 books she would like to arrange on her desk. Of the 25 books, 7 are
statistics books, 6 are biology books, 5 are English books, 4 are history books, and 3
are psychology books. If Susie arranges her books by subject, how many ways can she
arrange her books?
5. A hat contains 20 consecutive numbers (1 to 20). If four numbers are drawn at random,
how many ways are there for the largest number to be a 16 and the smallest number to
be a 5?
6. A university committee of size 10, consisting of 2 faculty from the college of fine and
applied arts, 2 faculty from the college of business, 3 faculty from the college of arts and
sciences, and 3 administrators, is to be selected from 6 fine and applied arts faculty, 7
college of business faculty, 10 college of arts and sciences faculty, and 5 administrators.
How many committees are possible?
7. How many different letter arrangements can be made from the letters BIOLOGY, PROB-
ABILITY, and STATISTICS, respectively.
8. A doll house must be painted and assembled before it can be given as a gift. If there
are 12 equal-sized rooms in the doll house and there is enough white paint for 4 rooms,
enough pink paint for 3 rooms, and enough blue paint for 5 rooms, in how many ways
can the 12 rooms be painted?
10. A multiple-choice test consists of 10 questions. Each question has 5 answers (only one
is correct). How many different ways can a student fill out the test?
11. How many ways can five politicians stand in line? In how many ways can they stand in
line if two of the politicians refuse to stand next to each other?
12. There are five different colored jerseys worn throughout the Tour de France. The yellow
jersey is worn by the rider with the least accumulated time; the green jersey is worn by
the best sprinter; the red and white polka dot jersey is worn by the best climber. The
white jersey is worn by the best youngest rider, and the red jersey is worn by the rider
with the most accumulated time still in the race. If 150 riders finish the Tour, how many
different ways can the yellow, green, and red and white polka dot jerseys be awarded if
(a) a rider can receive any number of jerseys and (b) each rider can receive at most one
jersey.
108 Probability and Statistics with R
13. A president, treasurer, and secretary, all different, are to be chosen from among the 10
active members of a university club. How many different choices are possible if
14. On a multiple-choice exam with three possible answers for each of the five questions,
what is the probability that a student would get four or more correct answers just by
guessing?
15. Suppose four balls are chosen at random without replacement from an urn containing
six black balls and four red balls. What is the probability of selecting two balls of each
color?
16. What is the probability that a hand of five cards chosen randomly and without replace-
ment from a standard deck of 52 cards contains the ace of hearts, exactly one other ace,
and exactly two kings?
17. Verify that P(F |E) satisfies the three axioms of probability on page 81.
19. In the New York State lottery game, six of the numbers 1 through 54 are chosen by a
customer. Then, in a televised drawing, six of these numbers are selected. If all six of
a customer’s numbers are selected, then that customer wins a share of the first prize. If
five or four of the numbers are selected, the customer wins a share of the second or the
third prize. What is the probability that any customer will win a share of the first prize,
the second prize, and the third prize, respectively?
20. Assume that P(A) = 0.5, P(A ∩ C) = 0.2, P(C) = 0.4, P(B) = 0.4, P(A ∩ B ∩ C) = 0.1,
P(B ∩ C) = 0.2, and P(A ∩ B) = 0.2. Calculate the following probabilities:
(a) P(A ∪ B ∪ C)
(b) P(Ac ∩ (B ∪ C))
(c) P ((B ∩ C)c ∪ (A ∩ B)c )
(d) P(A) − P(A ∩ C)
21. Let the random variable X be the sum of the numbers on two fair dice. Find an upper
bound on P(|X − 7| ≥ 4) using Chebyshev’s Inequality as well as the exact probability
for P(|X − 7| ≥ 4).
22. A new drug test being considered by the International Olympic Committee can detect
the presence of a banned substance when it has been taken by the subject in the last
90 days 98% of the time. However, the test also registers a “false positive” in 2% of the
population that has never taken the banned substance. If 2% of the athletes in question
are taking the banned substance, what is the probability a person that has a positive
drug test is actually taking the banned substance?
General Probability and Random Variables 109
23. The products of an agricultural firm are delivered by four different transportation com-
panies, A, B, C, and D. Company A transports 40% of the products; company B, 30%;
company C, 20%; and, finally, company D, 10%. During transportation, 5%, 4%, 2%,
and 1% of the products spoil with companies A, B, C, and D, respectively. If one product
is randomly selected,
24. Two lots of large glass beads are available (A and B). Lot A has four beads, two of which
are chipped; and lot B has five beads, two of which are chipped. Two beads are chosen
at random from lot A and passed to lot B. Then, one bead is randomly selected from lot
B. Find:
25. A box contains 5 defective bulbs, 10 partially defective (they start to fail after 10 hours
of use), and 25 perfect bulbs. If a bulb is tested and it does not fail immediately, find
the probability that the bulb is perfect.
26. A salesman in a department store receives household appliances from three suppliers:
I, II, and III. From previous experience, the salesman knows that 2%, 1%, and 3% of
the appliances from supplier I, II, and III, respectively, are defective. The salesman sells
35% of the appliances from supplier I, 25% from supplier II, and 40% from supplier III.
If an appliance randomly selected is defective, find the probability that it comes from
supplier III.
27. A garage has two machines, A and B, to balance the wheels of a car. Suppose that 95%
of the wheels are correctly balanced by machine A, while 85% of the wheels are correctly
balanced by machine B. A machine is randomly selected to balance 20 wheels, and 3
of them are not properly balanced. What is the probability that machine A was used?
What is the probability machine B was used?
28. An urn contains 14 balls; 6 of them are white, and the others are black. Another urn
contains 9 balls; 3 are white, and 6 are black. A ball is drawn at random from the first
urn and is placed in the second urn. Then, a ball is drawn at random from the second
urn. If this ball is white, find the probability that the ball drawn from the first urn was
black.
29. An office supply store is selling packages of 100 CDs at a very affordable price. However,
roughly 10% of all packages are defective. If a package of 100 CDs containing exactly 10
defective CDs is purchased, find the probability that exactly 2 of the first 5 CDs used
are defective.
30. A box contains six marbles, two of which are black. Three are drawn with replacement.
What is the probability two of the three are black?
31. The ASU triathlon club consists of 11 women and 7 men. What is the probability of
selecting a committee of size four with exactly three women?
110 Probability and Statistics with R
32. Four golf balls are to be placed in six different containers. One ball is red; one, green;
one, blue; and one, yellow.
(a) In how many ways can the four golf balls be placed into six different containers?
Assume that any container can contain any number of golf balls (as long as there
are a total of four golf balls).
(b) In how many ways can the golf balls be placed if container one remains empty?
(c) In how many ways can the golf balls be placed if no two golf balls can go into the
same container?
(d) What is the probability that no two golf balls are in the same container, assuming
that the balls are randomly tossed into the containers?
33. Previous to the launching of a new flavor of yogurt, a company has conducted taste
tests with four new flavors: lemon, strawberry, peach, and cherry. It obtained the
following probabilities of a successful launch: P(lemon) = 2/10 , P(strawberry) = 3/10 ,
P(peach) = 4/10 , and P(cherry) = 5/10 . Let X be the random variable “number of
successful flavors launched.” Obtain its probability mass function.
34. A family has three cars, all with electric windows. Car A’s windows always work. Car
B’s windows work 30% of the time, and Car C’s windows work 75% of the time. The
family uses Car A 2/3 of the time; Car B, 2/9 of the time; and Car C, the remaining
fraction.
(a) On a particularly hot day, when the family wants to roll the windows down, compute
the probability the windows will work.
(b) If the electric windows work, find the probability the family is driving Car C.
35. John and Peter play a game with a coin such that P(head) = p. The game consists of
tossing a coin twice. John wins if the same result is obtained in the two tosses, and Peter
wins if the two results are different.
36. A bank is going to place a security camera in the ceiling of a circular hall of radius r.
What is the probability that the camera is placed nearer the center than the outside
circumference if the camera is placed at random?
37. Anthony and Mark make a bet at the beginning of the school year. If Anthony passes
one exam, Mark will pay him e 10, but if Anthony fails the exam, he will give e 10 to
Mark. If Anthony takes 10 exams and the probability of passing an exam is 0.5, find the
probability that
38. Louis and Joseph have decided to play a beach volleyball match. Each of them put e
50 into a pot, so the winner will get e 100. The first one to reach 21 points wins. When
the score was 19 points for Louis and 18 for Joseph, the match was rained out, and
they decided to share the prize so that each one received winnings proportional to the
probability of winning the match given their current points. How much money did each
receive?
General Probability and Random Variables 111
39. Consider tossing three well-made coins. The eight possible outcomes are
Define X as the random variable “number of heads showing when three coins are tossed.”
Obtain the mean and the variance of X. Simulate tossing three fair coins 10,000 times.
Compute the simulated mean and variance of X. Are the simulated values within 2% of
the theoretical answers?
40. Every month, a family must decide what to do on Sundays. If they stay at home, they
do one of two things with equal probability: have lunch in a restaurant, which costs e
100, or go to the park, which is free. Assuming four weeks in a month, compute the
probability distribution of expenditures.
41. In a lottery game, one can win e 10,000 with probability 0.01 and e 1000 with probability
0.05. How much should one pay for a lottery ticket to make the game fair?
42. To play a game, one must bet e 100 every time, and the probability of winning e 100 is
1/ . Every day, a person plays uninterruptedly until he loses once. Then, he leaves the
2
game.
(a) Find the probability that he plays more than four times in one day.
(b) Find the probability that one day he leaves the game having won e 600.
(c) Calculate the expected winnings per day.
43. Consider the random variable X, which takes the values 1, 2, 3, and 4 with probabilities
0.2, 0.3, 0.1, and 0.4, respectively. Calculate E[X], 1/E[X], E[1/X], E[X 2 ], and E[X]2 ,
and check empirically that E[X]2 = E[X 2 ] and E[1/X] = 1/E[X].
44. Show that the following distribution is a probability mass function. Construct a plot of
the probability mass function and obtain the cumulative probability function.
45. Two stockbrokers on the floor of the New York Stock Exchange, Alvin and Bob, are
interested in purchasing shares from a single company. In a given day, Alvin or Bob
buys shares with probability p. Assume that Alvin starts the buying process; when he
finishes, Bob is allowed to buy, and so on.
(a) Find the probability that Alvin buys shares on a given day.
(b) If two lots of shares are purchased, find the probability that they have been purchased
by the same stockbroker.
∞ i
i=0 r = 1−r is |r| < 1.)
1
(Hint:
47. Consider an experiment where two dice are rolled. Let the random variable X equal the
sum of the two dice and the random variable Y be the difference of the two dice.
48. The number of hits on a faculty member’s homework solutions page has an average of
100 hits per day.
(a) Give an upper bound for the probability the faculty member’s homework solutions
page has more than 112 hits per day.
(b) Suppose the variance of the number of hits is known to be 36. Now, give an upper
bound for the probability the faculty member’s homework solutions page has more
than 112 hits per day.
(c) The probability that the number of hits is between 88 and 112 inclusive must be at
least what?
(d) How many days must visits to the site be recorded so that the average number of
hits is within 6 of 100 with a probability of at least 0.9?
49. Find the values of k such that the following functions are probability density functions:
Construct plots of the these functions and their corresponding cumulative density func-
tions.
derive the probability density function f (x). Calculate the median of the distribution.
52. The number of bottles of milk that a dairy farm fills per day is a random variable with
mean 5000 and standard deviation 100. Assume the farm always has a sufficient number
of glass bottles to be used to store the milk. However, for a bottle of milk to be sent to a
grocery store, it must be hermetically sealed with a metal cap that is produced on site.
Calculate the minimum number of metal caps that must be produced on a daily basis so
that all filled milk bottles can be shipped to grocery stores with a probability of at least
0.9.
53. Define X as the space occupied by certain device in a 1 m3 container. The probability
density function of X is given by
630 4
f (x) = x 1 − x4 , 0 < x < 1.
56
(a) Graph the probability density function.
(b) Calculate the mean of X by hand.
(c) Calculate the variance X by hand.
(d) Calculate P(0.20 < X < 0.80) by hand.
(e) Calculate the mean of X using integrate().
(f) Calculate the variance of X using integrate().
(g) Calculate P(0.20 < X < 0.80) using integrate().
Derive the moment generating function, and calculate the mean and the variance.
Chapter 4
Univariate Probability Distributions
4.1 Introduction
This chapter examines univariate (single variable) probability distributions that are used
frequently to model random phenomena. Discrete probability distributions are introduced
first, followed by continuous probability distributions. Discrete distributions can be used
to model the number of failures until a successful rocket launch, the number of passing
students in a class, or the number of taxis that pass a street corner, as well as many other
phenomena with countable outcomes. Continuous distributions are used to model measure-
ment variables such as weight, height, and time. Joint distributions will be introduced in
Chapter 5.
n+1 n2 −1
When xi = i for i = 1, . . . , n, it can be shown that E[X] = 2 and that Var[X] = 12 ,
respectively.
115
116 Probability and Statistics with R
Example 4.1 One light bulb is randomly selected from a box that contains a 40 watt
light bulb, a 60 watt light bulb, a 75 watt light bulb, a 100 watt light bulb, and a 120
watt light bulb. Write the probability function for the random variable that represents the
wattage of the randomly selected light bulb, and determine the mean and variance of that
random variable.
Solution: The random variable X can assume the set of values Ω = {40, 60, 75, 100, 120}.
The probability density function for the random variable X is
The expected value of X, E[X] = 79, and the variance of X, Var[X] = 804. S can be used
to alleviate the arithmetic:
> Watts <- c(40,60,75,100,120)
> meanWatts <- (1/5)*sum(Watts)
> varWatts<- (1/5)*sum((Watts-meanWatts)^2)
> ans <- c(meanWatts, varWatts)
> ans
[1] 79 804
Bernoulli Distribution
X ∼ Bernoulli (π)
4. The random variable of interest, X, is the number of observed successes during the n
trials.
The probability that X is equal to x can be found in the following fashion. Any particular
sequence of x successes occurs with probability
n π (1
x
− π)(n−x) since there are x successes
and (n − x) failures. However, there are x = x!(n−x)! possible sequences of x successes.
n!
Binomial Distribution
X ∼ Bin(n, π)
n x
P(X = x|n, π) = π (1 − π)n−x , x = 0, 1, 2, . . . , n.
x (4.3)
E[X] = nπ
Var[X] = nπ(1 − π)
MX (t) = (πet + )n
It is left as an exercise for the student to verify that E[X] = nπ, Var[X] = nπ(1 − π), and
that the moment generating function of a binomial random variable is MX (t) = (πet + )n .
(See Problem 40 on page 167.)
Code to create graphs that represent the probability density function and the cumulative
distribution function for a Bin(8, 0.3) random variable follows. The graphs that are created
are similar to those in Figure 4.1 on the next page.
0.30
1.0
0.25
0.8
0.20
P(X ≤ x)
P(X = x)
0.6
0.15
0.4
0.10
0.2
0.05
0.00
0.0
0 2 4 6 8 0 2 4 6 8
x x
FIGURE 4.1: Left graph is the probability density function (pdf ) of a binomial random
variable with n = 8 and π = 0.3. Right graph is the cumulative distribution function (cdf )
of a binomial random variable with n = 8 and π = 0.3.
Then, the results from using the function to generate 1000 samples where n = 5 and π = 0.5
answer Example 4.2 in particular:
$[Link]
0 1 2 3 4 5
0.031 0.156 0.312 0.312 0.156 0.031
Univariate Probability Distributions 119
0.30
0.20
0.10
0.00
0 1 2 3 4 5
Successes
FIGURE 4.2: Histogram of 1000 simulated samples where n = 5 and π = 0.5 superimposed
on the theoretical distribution for a random variable following a Bin(5, 0.5) distribution.
Using the function rbinom(), one can generate 1000 samples of a Bin(n = 5, π = 0.5)
distribution by entering
If one wants to generate the same numbers at a later date, the command [Link]() can
be used. The graph in Figure 4.2 was created with [Link](31).
10!
P(X = 6) = × 0.336 × (1 − .0.33)(10−6) = 0.0546515
6!(10 − 6)!
10!
P(X = 7) = × 0.337 × (1 − .0.33)(10−7) = 0.0153817
7!(10 − 7)!
10!
P(X = 8) = × 0.338 × (1 − .0.33)(10−8) = 0.0028410
8!(10 − 8)!
10!
P(X = 9) = × 0.339 × (1 − .0.33)(10−9) = 0.0003110
9!(10 − 9)!
10!
P(X = 10) = × 0.3310 × (1 − .0.33)(10−10) = 0.0000153
10!(10 − 10)!
120 Probability and Statistics with R
Thus,
There are several approaches one might take to solve the problem with S. One should realize
that the following are all equivalent statements:
> sum(dbinom(6:10,10,0.33))
[1] 0.07320046
> 1 - pbinom(5,10,0.33)
[1] 0.07320046
> 1 - sum(dbinom(5:0,10,0.33))
[1] 0.07320046
(1) The number of outcomes in non-overlapping intervals are independent. In other words,
the number of outcomes in the interval of time (0, t] are independent from the number
of outcomes in the interval of time (t, t + h] for any h > 0.
(2) The probability of two or more outcomes in a sufficiently short interval is virtually zero.
In other words, provided h is sufficiently small, the probability of obtaining two or more
outcomes in the interval (t, t + h] is negligible compared to the probability of obtaining
one or zero outcomes in the same interval of time.
(3) The probability of exactly one outcome in a sufficiently short interval or small region
is proportional to the length of the interval or region. In other words, the probability
of one outcome in an interval of length h is λh.
Univariate Probability Distributions 121
When an experiment satisfies the conditions for the Poisson process, the resulting
random variable, X, the number of outcomes, is called a Poisson random variable. The
probability distribution of the Poisson random variable X, representing the number of
outcomes in a given time interval or space region denoted by t, is
e−λ (λt)x
P(X = x|λt) = x = 0, 1, . . . , λ > 0. (4.4)
x!
Although the Poisson distribution is typically used for problems involving time or space,
it can be viewed as the limiting form of the binomial distribution. Suppose there is an
experiment that satisfies the three criteria for an approximate Poisson process. Let X
represent the number of outcomes in an interval of length 1 (t = 1). To find P(X = x),
divide the interval of length 1 into n subintervals of equal length. Provided n is much larger
than x, the probability of one outcome in any given interval of length 1/n is approximately
λ/n by criterion (3) of the Poisson process on the preceding page. Substituting π = λ/n
into the binomial probability distribution gives
x n−x n −x
n λ λ n(n − 1) · · · (n − x + 1) λx λ λ
1− = 1− 1−
x n n x! nx n n
! " n −x
λ n (n − 1)
x
(n − x + 1) λ λ
= ··· 1− 1− .
x! n n n n n
Poisson Distribution
X ∼ Pois(λ)
λx e−λ
P(X = x|λ) = , x = 0, 1, 2, . . .
x! (4.5)
E[X] = λ
Var[X] = λ
t
−1)
MX (t) = eλ(e
Note that the parameter λ, referred to as the intensity parameter, represents the mean
number of outcomes in either a fixed time interval or a fixed spatial region. The Poisson
distribution is particularly appropriate for modeling “rare” phenomena or outcomes where
the probability of success is small. However, whether or not data can be viewed as
Poisson data depends on whether the proportions of 0’s, 1’s, 2’s, and so on, are similar
to those predicted by the Poisson pdf given in (4.5). Given n independent Poisson n random
variables
n X 1 , X 2 , .
. . , X n with parameters λ1 , λ2 , . . . , λn , respectively, Y = i=1 Xi ∼
Pois i=1 λ i = λ .
122 Probability and Statistics with R
Example 4.4 Poisson: World Cup Soccer The World Cup is played once
every four years. National teams from all over the world compete. In 2002 and in 1998,
36 teams were invited; whereas, in 1994 and in 1990, only 24 teams participated. The
data frame Soccer contains three columns: CGT, Game, and Goals. All of the information
contained in Soccer is indirectly available from the FIFA World Cup website, located at
[Link] The numbers of goals scored in the regulation 90 minute
periods of World Cup soccer matches from 1990 to 2002 are listed in column Goals. There
were a total of 575 goals scored during regulation time. The game in which the goals were
scored is in column Game. There were 232 World Cup soccer games played from 1990 to
2002. There were 64 games played in each of 2002 and 1998 and 54 games played in each
of 1994 and 1990. The cumulative goal time is provided in column CGT. For example, the
first goal was scored at the 67th minute of the first game and the second goal was scored at
the 42nd minute of the second game. Consequently, the times listed in CGT for the first two
goals are 67, and 132 = 90 + 42. For consistency, all goals scored during injury time are
recorded in either the 45th or 90th minute, depending on the half when the injury occurred.
Analyze the number of goals scored during regulation play (90 minutes) of World Cup soccer
matches to verify that the scores follow an approximate Poisson distribution (Chu, 2003).
Solution: To investigate whether criterion (1) of the Poisson process on page 120 is
reasonable, examine the one, two, three, four, and five game lagged correlation coefficients:
> attach(Soccer)
> L1 <- Goals[1:228]
> L2 <- Goals[2:229]
> L3 <- Goals[3:230]
> L4 <- Goals[4:231]
> L5 <- Goals[5:232]
> LAG <- cbind(L1, L2, L3, L4, L5)
> # or more succinctly
> LAG <- sapply( 1:5, function(x){Goals[x:(x+227)]} )
> round(cor(LAG),3)
[,1] [,2] [,3] [,4] [,5]
[1,] 1.000 -0.049 0.055 -0.138 -0.008
[2,] -0.049 1.000 -0.046 0.044 -0.138
[3,] 0.055 -0.046 1.000 -0.054 0.045
[4,] -0.138 0.044 -0.054 1.000 -0.057
[5,] -0.008 -0.138 0.045 -0.057 1.000
Independence seems reasonable due to the small correlation coefficients (near zero) but
should also be computed with time periods smaller than 90 minutes. Criterion (2) of the
Poisson process on page 120, appears satisfied since two goals are never registered during the
same one minute period. One way to investigate this is to create a table of the interarrival
goal times and note that 0 is not in the table. Whether criterion (3) of the Poisson process
on page 120 is satisfied is addressed in Problem 45 on page 169 at the end of the chapter.
Next, examine the data to see how well they conform to the Poisson distribution. To
calculate the observed number of goals scored during regulation time for the 232 World
Cup soccer matches, use table():
> table(Goals)
0 1 2 3 4 5 6 7 8
19 49 60 47 32 18 3 3 1
Univariate Probability Distributions 123
Since there are NA values in the Goals column, use the [Link]=TRUE and [Link]="omit"
options for the S functions mean() and var(), respectively. To verify that the mean and
the variance of Goals are approximately equal, type
Because the mean and variance of Goals are approximately equal, it is reasonable to proceed
in analyzing the frequencies of Goals in comparison to those of a Poisson distribution
with λ = 2.478448. Create a table to facilitate comparing the observed values (OBS) to
the expected values (EXP) as well as the empirical proportions (Empir) to the theoretical
proportions (TheoP) for a Poisson distribution with λ = 2.478448, the mean number of
goals per game. The empirical proportions are merely the number of goals in each category
divided by the total number of goals.
Since the observed values are close to the expected values, the empirical proportions will
be close to the theoretical probabilities. This, in conjunction with the fact that the sample
mean (2.478448) is roughly equal to the sample variance (2.458408), implies that modeling
the number of goals scored during World Cup soccer games with a Poisson distribution is
reasonable.
1.0
0.3
0.8
P(X ≤ x)
P(X = x)
0.6
0.2
0.4
0.1
0.2
0.0
0.0
0 2 4 6 8 0 2 4 6 8
x x
FIGURE 4.3: Left graph is the probability density function (pdf) of a Poisson random
variable with λ = 1. Right graph is the cumulative distribution function (cdf) of a Poisson
random variable with λ = 1.
Example 4.5 Given a random variable X that follows a Poisson distribution with pa-
rameter λ, find the mean and variance of X. Use the fact that
∞
λr λ λ2
eλ = =1+ + + ··· .
r=0
r! 1! 2!
Solution:
∞ ∞
λr λr−1
E[X] = r e−λ = λe−λ =λ
r=0
r! r=1
(r − 1)!
∞
λr −λ
Var [X] = (r − λ)2 e
r=0
r!
Rearranging terms,
∞ ∞ ∞
r r
−λ 2λ 2λ λr
Var [X] = e r + λ − 2λ r
r=0
r! r=0
r! r=0
r!
∞ ∞
−λ λr λr−1
= e r + λ e − 2λ · λ
2 λ
r=1
(r − 1)! r=1
(r − 1)!
∞
−λ λr
= e (r − 1 + 1) + λ e − 2λ e
2 λ 2 λ
r=1
(r − 1)!
∞ ∞
−λ λr λr
= e (r − 1) + + λ e − 2λ e
2 λ 2 λ
r=1
(r − 1)! r=1 (r − 1)!
) *
= e−λ λ2 + λ + λ2 − 2λ2 eλ = λ.
Univariate Probability Distributions 125
Example 4.6 More accidents are registered in auto body repair shops during the months
of May and June than in the rest of the year. Suppose a particular auto body repair shop
has an average of four accidents per month. What is the probability there will be more than
seven accidents in this auto body shop during the month of May? What is the probability
no more than three accidents will occur during the months of May and June?
Solution: Assuming accidents in the auto body shop follow an approximate Poisson
process, the probability of x accidents in one month is
4x e−4
P(X = x) = for x = 0, 1, 2, . . .
x!
The probability more than seven accidents occur during the month of May is
7
4i e−4
P(X > 7) = 1 − P(X ≤ 7) = 1 − = 0.051.
i=0
i!
Since the expected number of accidents during May and June is λ = 2·4 = 8, the probability
no more than three accidents occur for the two months in question is calculated as
3
8i e−8
P(X ≤ 3) = = 0.042.
i=0
i!
Example 4.7 Telephone calls to a local 911 number are known to follow a Poisson
distribution with an average of two calls per minute. Compute the probability that
(a) There will be zero calls during a one minute period.
(b) There will be less than five calls in a one minute period.
(c) There will be less than six calls in one hour.
Solution: The answers are as follows:
0 −λ 0
(a) P(X = 0; λ = 2) = λ 0!
e
= 20! e−2 = 0.135.
& '
4 r −λ 2 3 4
(b) P(X ≤ 4; λ = 2) = r=0 λ r! e
= e−2 1 + 2 + 22! + 23! + 24! = 0.947.
(c) Note that the time period changes from one minute to one hour (60 minutes). Conse-
quently, the average number of calls in one hour is λ = 2 × (60) = 120.
5
λ r e−λ
P(X ≤ 5; λ = 120) =
r=0
r!
−120 1202 1203 1204 1205
=e 1 + 120 + + + + = 0.
2! 3! 4! 5!
The S commands to find P(X = 0; λ = 2), P(X ≤ 4; λ = 2), and P(X ≤ 5; λ = 120) are
dpois(0,2), ppois(4,2), and ppois(5,120), respectively:
126 Probability and Statistics with R
> dpois(0,2)
[1] 0.1353353
> ppois(4,2)
[1] 0.947347
> ppois(5,120)
[1] 1.658476e-44
Example 4.8 Numerically show the results from approximating a Bin(n = 100, π = 0.04)
distribution with a Pois(λ = 4).
Solution: The probability distribution function for a Bin(100, 0.04) random variable is
100
PBin (X = x) = (0.04)x (0.96)100−x , x = 0, 1, 2, . . .
x
Since π < 0.1 and λ = nπ = 100(0.04) = 4 < 5, the Poisson distribution can be used to
obtain reasonable approximations to the binomial distribution. The probability distribution
for a Pois(4) is
e−4 4x
PPois (X = x) = , x = 0, 1, 2, . . .
x!
The first eight values of x for PBin (X = x) and PPois (X = x) are given in Table 4.1.
Note that the results between PBin (X = x) and PPois (X = x) are virtually identical out to
two decimal places. The values in Table 4.1 were generated using S commands as follows:
Geometric Distribution
X ∼ Geo(π)
P(X = x; π) = πx , x = 0, 1, . . .
E[X] = (4.6)
π
Var[X] = 2
π
π
MX (t) =
1 − et
(a) Compute the probability that the first applicant who speaks a foreign language and has
passed the CPA exam is the fourth applicant interviewed.
(b) Suppose the first applicant that speaks a foreign language who has passed the CPA
exam is offered the position and that the applicant accepts the offer. If the accounting
firm spends 200 dollars for each interview, what are the expected value and standard
deviation of the firm’s cost for filling the position.
When X ∼ Geo(π = 0.2), the P(X = 3) can be found with S using the command
dgeom(3,0.2):
> dgeom(3,0.2)
[1] 0.1024
(b) Be careful with this problem! The expected value and standard deviation of the cost
for filling the position are not the same as the expected value and standard deviation of the
random variable X as defined in the solution for part (a). Since the question asks for the
expected value and standard deviation of the cost for filling the position (r failures and one
128 Probability and Statistics with R
success),
E[200(X + 1)] = 200E[(X + 1)]
= 200(E[X] + 1)
0.8
= 200 + 1 = 1000 dollars.
0.2
Var[200(X + 1)] = 40, 000Var[(X + 1)]
= 40, 000Var[X]
0.8
= 40, 000 = 800, 000 dollars2
0.22
⇒ σ200(X+1) = Var[200(X + 1)] = 894.43 dollars
Useful Relationships
1. If n independent random variables X1 , . . . , Xn have a geometric distribution with
parameter π, then the sum of the n independent random variables follows a negative
binomial distribution with parameters (n, π).
2. If n independent random variables X1 , . . . , Xn have a negative binomial
distribution
n
with parameters ri and π, then the sum of the n random variables is NB ( i=1 ri , π) .
3. When X ∼ NB (r, π) and r = 1, a negative binomial random variable is the same as
a geometric random variable with parameter π.
Example 4.10 In a particular lot of white wall tires, 10% are missing their white wall.
What is the probability one will have to examine six tires before finding four tires with
white walls?
Univariate Probability Distributions 129
Solution: Let the random variable X represent the number of tires without white walls
examined before obtaining four tires with white walls. In other words, X ∼ NB (4, 0.90)
and it follows that
2+4−1
P(X = 2|4, 0.9) = (0.9)4 (0.1)2
4−1
5!
= (0.9)4 (0.1)2 = 0.066.
3!(2!)
Hypergeometric Distribution
X ∼ Hyper (m, n, k)
m n
x k−x
P(X = x|m, n, k) = ,
N
k (4.8)
for x = max{0, k − n}, . . . , min{m, k}, where N = m + n
m×k
E[X] =
N
m × n × k × (N − k)
Var[X] =
N 2 × (N − 1)
k
One should note that when N is small (≤ 0.10), the distribution of a hypergeometric
random variable does not differ greatly from the distribution of a binomial random variable
with parameters n = k and π = m N.
Example 4.11 A computer manufacturer decides to purchase monitors from a new start-
up company claiming strict quality control standards. The manufacturer orders 150 moni-
tors and decides to accept the lot provided a random sample of size 25 reveals no defective
monitors. If the lot of 150 monitors contains three defective monitors, what is the probability
the lot will be accepted?
130 Probability and Statistics with R
Solution: Let the random variable X represent the number of non-defective monitors
in the sample. Since X ∼ Hyper (147, 3, 25), the P(X = 25|m = 147, n = 3, k = 25) is
computed as
147 3
25 0
P(X = 25|m = 147, n = 3, k = 25) = = 0.5764.
150
25
> dhyper(25,147,3,25)
[1] 0.576365
1
f (x|a, b) = , a ≤ x ≤ b.
b−a
Some common uses of the uniform distribution include random number generation and
modeling waiting times. The pdf, mean, variance, and mgf for a uniform random variable
are found in (4.9).
Uniform Distribution
X ∼ Unif (a, b)
1
f (x|a, b) = , a≤x≤b
b−a
b+a
E[X] =
2 (4.9)
(b − a)2
Var[X] =
12
⎧ tb
⎨ e − eta
if t = 0
MX (t) = t(b − a)
⎩
1 if t = 0
Figure 4.4 on the facing page displays both the pdf and cdf for a Unif (a, b) random variable.
Note that the area beneath the pdf is clearly one since the pdf forms a rectangle whose area
is height × length, (b−a)
1
× (b − a) = 1.
Example 4.12 Given a continuous random variable X defined over [a, b] with pdf
1
f (x|a, b) = b−a , a ≤ x ≤ b, find the expected value and the variance of X.
Univariate Probability Distributions 131
1.2
1.0
1
0.8
F (x)
f (x)
0.6
0.4
1
0.2
b−a
0.0
0
a b a b
x x
FIGURE 4.4: The pdf and cdf for the random variable X ∼ Unif (a, b)
Solution: Using the definition for a continuous random variable from (3.12), write
b b b
x 1 x2
E[X] = x · f (x) dx = dx = ·
b−a b−a 2 a
a a
b 2 − a2 (b + a)(b − a) b+a
= = = .
2(b − a) 2(b − a) 2
Next find E X 2 to use in computing the variance since Var[X] = E (X − μ)2 = E X 2 −
2
E[X] :
b b
2 1 1 x3 b 3 − a3
E X = x2 · dx = · =
b−a b−a 3 a 3(b − a)
a
2 b 3 − a3 (b + a)2
Var[X] = E X 2 − E[X] = −
3(b − a) 4
(b − a)(b + ab + a ) (b + a)
2 2 2
4(b2 + ab + a2 ) 3(b + a)2
= − = −
3(b − a) 4 12 12
4b + 4ab + 4a − (3b + 6ab + 3a )
2 2 2 2
b − 2ab + a
2 2
= =
12 12
(b − a)2
= .
12
Example 4.13 If aerosol particles produced over forested areas have uniformly distributed
diameters between 3 and 5 nanometers, compute the average volume of aerosol particles
found over forested areas.
132 Probability and Statistics with R
4 3
Solution: Recall that the volume of a sphere is 3 πr , or expressed in terms of the
diameter, 16 πd3 . Consequently,
! "
1 3 1
E πd = πE d3 (4.10)
6 6
needs to be found. Let d represent the diameter of aerosol particles produced over forested
areas. Since d ∼ Unif (3, 5),
5 5
1 1 x4
E d3 = · x3 dx = ·
5−3 2 4 3
3
(5)4 (3)4
= − = 68.
8 8
Using the right side of (4.10), compute the average volume of aerosol particles to be
π
· 68 = 35.60472 nanometers3 .
6
Estimate E d3 , denoted by E+ [d3 ], by cubing a large number of values drawn at random
from a Unif (3, 5) distribution and subsequently computing the mean of the cubed values.
Then, the +3
3
estimated mean volume of aerosol particles is computed by substituting E [d ]
for E d in the right-hand side of (4.10). The following S code estimates the mean volume
of aerosol particles by simulating a sample of size 1000 from a Unif (3, 5) distribution:
> (pi/6)*mean(runif(1000,3,5)^3)
[1] 35.61885
Example 4.14 Generate a sample of 1000 random values from a continuous distribution
with pdf f (x) = 43 x(2 − x2 ), 0 ≤ x ≤ 1. Verify that the mean and variance of the 1000
random values are approximately equal to the mean and variance of the given pdf.
Solution: First, the cdf is found. Then, FX (x) is set equal to u and solved.
x
4 4 x4 1 2
FX (x) = t 2 − t dt =
2
x −
2
= x 4 − x2 , 0≤x≤1
0 3 3 4 3
Univariate Probability Distributions 133
1
4 84
μX = E(X) = x · x(2 − x2 )dx = = 0.6222222
0 3 135
1
4 4
E(X 2 ) = x2 · x(2 − x2 )dx = = 0.4444444
0 3 9
2
4 84 116
2
σX = E(X ) − E(X) = −
2 2
= = 0.05728395
9 135 2025
The mean and variance of the 1000 simulated random values using [Link](33) are
0.6152578 and 0.05809062, respectively, which are both within 2% of their theoretical values.
> [Link](33)
> U <- runif(1000)
> X <- sqrt((2-sqrt(4-3*U)))
> mean(X)
[1] 0.6152578
> var(X)
[1] 0.05809062
of outcomes (number of goals), is modeled with the Poisson distribution. However, not only
is X, the number of outcomes in a fixed interval, a random variable, but also is the waiting
time between successive outcomes. If W is the waiting time until the first outcome of a
Poisson process with mean λ > 0, then the pdf for W is
λe−λw if w ≥ 0
f (w) =
0 if w < 0
The pdf, mean, variance, and mgf for an exponential random variable are in (4.12), while
the pdf and cdf for an exponential random variable are illustrated in Figure 4.5 on the facing
page. The cdf, F (x), for the exponential distribution is written
x x
F (x) = P(X ≤ x) = λe−λt dt = −e−λt = 1 − e−λx , x ≥ 0.
0 0
Exponential Distribution
X ∼ Exp(λ)
λe−λx if x ≥ 0
f (x) =
0 if x < 0
(4.12)
1
E[X] =
λ
1
Var[X] = 2
λ
MX (t) = (1 − λ−1 t)−1 for t < λ
Example 4.15 Show that the function f (x) in (4.12) satisfies condition 2 on page 93 from
the properties of all pdfs.
Univariate Probability Distributions 135
1.0
1.0
0.8
0.8
0.6
0.6
f (x)
F (x)
0.4
0.4
0.2
0.2
0.0
0.0
-2 0 2 4 6 8 10 -2 0 2 4 6 8 10
x x
FIGURE 4.5: The pdf and cdf for the random variable X ∼ Exp(λ = 0.75)
Solution: To satisfy condition 2 on page 93, it must be shown that the integral from −∞
to +∞ of the function f (x) given in (4.12) is 1:
∞ 0 ∞
−λx
λe dx = 0 dx + λe−λx dx
−∞ −∞ 0
∞ ∞
= λe−λx dx = −e−λx = 0 − (−1) = 1.
0
0
∞
E[X] = xλe−λx dx.
0
∞ ∞
−λx
E[X] = −xe − −e−λx dx
0
0
1 ∞ 1
= 0 − λx = .
λe 0 λ
Before finding the variance of X, find E X 2 using (3.13) as follows:
∞
E X 2
= x2 λe−λx dx (4.13)
0
136 Probability and Statistics with R
∞ ∞
Note that E[X] = xλe−λx dx ⇒ E[X]
λ = xe−λx dx and integrate (4.13) by parts where
0 0
−λx
u = x2 and dv = λe dx:
∞ ∞
2 −λx
E X 2
= −x e − −2xe−λx dx
0
0
E[X] 2
=0+2 = 2.
λ λ
1 2
Using the fact that Var[X] = E X 2 − (E[X])2 , obtain Var[X] = 2
λ2 − λ = 1
λ2 .
Based on the results from Example 4.16, note that the mean and standard deviation of
the exponential random variable are identical. Quite often, the pdf for the exponential is
expressed as
1
f (x) = e−x/θ , x ≥ 0, θ > 0,
θ
where θ = λ . Of course, the mgf is then written as MX (t) = (1 − θt)−1 and the reparam-
1
eterized mean and variance are θ and θ2 , respectively. Note the relationship between the
Poisson mean and the exponential mean. Given a Poisson process with mean λ, the waiting
time until the first outcome has an exponential distribution with mean λ1 . That is, if λ
represents the number of outcomes in a unit interval, λ1 is the mean waiting time for the
first change. If X denotes the lifetime of an electronic component following an exponential
distribution with mean λ1 , (4.11) implies that the probability the component will work for
t2 + t1 hours given that it has worked for t1 hours is the same as the probability that
the component will function for at least t2 hours. In other words, the component has no
memory of having functioned for t1 hours. Note that (4.11) is equivalent to
P(X > t2 + t1 , X > t1 )
= P(X > t2 ),
P(X > t1 )
which is equivalent to
P(X > t2 + t1 ) = P(X > t2 )P(X > t1 ). (4.14)
Since P(X > t2 + t1 ) = e−λ(t2 +t1 ) = e−λt2 e−λt1 = P(X > t2 )P(X > t1 ) for any exponential
random variable, exponential random variables are memoryless according to (4.14).
The function integrate() can also be used to solve this problem using numerical integra-
tion:
> qexp(0.95,1/8)
[1] 23.96586
(c) The probability that a light bulb that has lasted for 10 months will last more than 25
months mathematically is written P(X > 25|X > 10). Because an exponential distribution
is present, (4.11) can be used to say that this is equal to P(X > 15) = e−15/8 = 0.153355.
> 1-pexp(15,1/8)
[1] 0.1533550
(a) Compute the mean and standard deviation for the time between goals.
(b) Is it reasonable to model the time between goals with the exponential distribution?
(b) To assess the fit of the data to an exponential distribution with a mean of 36.31 minutes,
first split the data into discrete categories. If the underlying distribution is exponential,
1/3
then a good bin width is approximately 12 · μX (Scott, 1992). In our case, the bin
12 1/3 n
width is 574 · 36.31 ≈ 10.
> rate <- 1/(90/(575/232))
> ntot <- length([Link])
> OBS <- table(cut([Link], breaks=c(seq(0,130,10), 330)))
> EmpiP <- round(OBS/ntot,3)
> TheoP <- round(c((pexp(seq(10,130,10),rate) - pexp(seq(0,120,10),rate)),
+ (1 - pexp(130, rate))), 3)
> EXP <-round(TheoP*ntot, 0)
> ANS <-cbind(OBS, EXP, EmpiP, TheoP)
> ANS
OBS EXP EmpiP TheoP
(0,10] 144 138 0.251 0.241
(10,20] 106 105 0.185 0.183
(20,30] 86 80 0.150 0.139
(30,40] 53 60 0.092 0.105
(40,50] 45 46 0.078 0.080
(50,60] 27 35 0.047 0.061
(60,70] 35 26 0.061 0.046
(70,80] 16 20 0.028 0.035
(80,90] 22 15 0.038 0.027
(90,100] 12 11 0.021 0.020
(100,110] 3 9 0.005 0.015
(110,120] 3 7 0.005 0.012
(120,130] 6 5 0.010 0.009
(130,330] 16 16 0.028 0.028
The observed and expected values as well as the empirical and theoretical probabilities are
similar.
(c) The lack of memory property is also evident from the data. Empirically, P(T > 10) =
1 − P(T ≤ 10) = 1 − 144
574 = 574 = 0.749, and P(T > 20 | T > 10) =
430 574−144−106
574−144 = 0.754,
which are both roughly the same and similar to the theoretical P(T > 10), which is 0.759
under the assumption that the mean is 36.31 minutes. Since the observed data appear to
lack memory, the same probability statements could be used to justify independence among
the times between goals using (4.14). Finally, produce a histogram of the observed data
Univariate Probability Distributions 139
similar to Figure 4.6, and superimpose over this the density for an exponential with a mean
of 36.31 minutes. Based on the analysis and Figure 4.6, it seems reasonable to model the
time between goals scored in World Cup competition for the years 1990 to 2002 with an
exponential distribution:
Histogram of [Link]
0.025
0.020
0.015
Density
0.010
0.005
0.000
0 20 40 60 80 100 120
FIGURE 4.6: Histogram of time between goals with superimposed exponential density
curve with mean of 36.31 minutes
∞
Γ(α) = xα−1 e−x dx, α>0 (4.15)
0
140 Probability and Statistics with R
In Section 4.3.2 on page 133, it was proved that the waiting time until the first outcome in
a Poisson process follows an exponential distribution. Now, let W denote the waiting time
until the αth outcome and derive the distribution of W in a similar fashion. Since waiting
time is non-negative, F (w) = 0 for w < 0. When w ≥ 0,
Consequently, when w > 0, the pdf of W is F (w) = f (w) whenever this derivative exists.
It follows then that
α−1
(λw)k e−λw (−λ) + e−λw k(λw)k−1 λ
f (w) = F (w) = −
k!
k=0
α−1
kλ(λw)k−1 − λ(λw)k
= −e−λw
k!
k=0
α−1
kλ(λw)k−1 − λ(λw)k
= λe−λw − e−λw
k!
k=1
α−1 ! "
−λw −λw λ(λw)k−1 λ(λw)k
= λe −e −
(k − 1)! k!
k=1
! 0 1
λ(λw) λ(λw) λ(λw)1 λ(λw)2
= λe−λw − e−λw − + − +
0! 1! 1! 2!
"
λ(λw)α−1
···−
(α − 1)!
! α−1
"
−λw −λw λ(λw)
= λe −e λ−
(α − 1)!
λ(λw)α−1 e−λw λα wα−1 e−λw
= =
(α − 1)! Γ(α)
From the previous derivation, note that the gamma is a generalization of the exponential
distribution. The pdf, mean, variance, and mgf for a gamma random variable are listed in
(4.16). The pdfs for λ = 2 and λ = 1 with α = 1, 2, and 3, respectively, are illustrated in
Figure 4.7 on the facing page. Notice that different shapes are produced in Figure 4.7 for
different values of α. For this reason, α is often called the shape parameter associated with
the gamma distribution. The parameter λ is referred to as the scale parameter. Varying λ
changes the units of measurement (say, from seconds to minutes) and does not affect the
shape of the density.
Univariate Probability Distributions 141
Gamma Distribution
X ∼ Γ(α, λ)
α
λ
xα−1 e−λx if x ≥ 0
f (x) = Γ(α)
0 if x < 0
(4.16)
α
E[X] =
λ
α
Var[X] = 2
λ
MX (t) = (1 − λ−1 t)−α for t < λ
X ∼ Γ(α, λ = 2) X ∼ Γ(α, λ = 1)
2.0
2.0
1.5
1.5
α=1
1.0
1.0
f (x)
f (x)
α=2
α=1
0.5
0.5
α=4 α=2
α=4
0.0
0.0
0 2 4 6 8 10 0 2 4 6 8 10
x x
FIGURE 4.7: Graphical illustration of the pdfs of a Γ(α, 2) and a Γ(α, 1) random variable
for α = 1, 2, and 4, respectively.
Useful Relationships
1. Given X ∼ Γ(α, λ). When α = 1, the resulting random variable is X ∼ Exp(λ). That
is, the exponential distribution is a special case of the gamma distribution.
2. Given X ∼ Γ(α, λ). When α = n/2 and λ = 1/2 , the resulting random variable has a
chi-square distribution with n degrees of freedom. (The chi-square is discussed in Section
6.6.1.)
3. Given X ∼ Γ(α, λ). Provided α is a positive integer, the resulting distribution is known
as the Erlang. In this case, the Erlang distribution gives the waiting time until the αth
occurrence when the number of outcomes in an interval of length t follows a Poisson
distribution with parameter λt.
142 Probability and Statistics with R
Example 4.19 Given X ∼ Γ(α, λ), find the mean and variance of X.
Solution: Using the mgf from (4.16), it is known that the first and second derivatives of
the mgf evaluated at zero, respectively, yield the E[X] and the E[X 2 ]. Consequently,
E[X] = MX (t) t=0
−α−1 −1
= (−α) 1 − λ−1 t −λ =α
t=0 λ
2
E X = MX (t)
t=0
−α−2 −1
= αλ−1 (−α − 1) 1 − λ−1 t −λ = α(α + 1)
t=0 λ2
Var[X] = E X 2 − (E[X])2
α(α + 1) & α '2 α
= 2
− = 2
λ λ λ
α α
So the mean of X is λ and the variance of X is λ2 .
Example 4.20 Suppose that the average arrival rate at a local fast food drive-through
window is three cars per minute (λ = 3). Find
(a) The probability that at least five cars arrive in 120 seconds.
(b) The probability that more than one minute elapses before the second car arrives.
(c) If one car has already gone through the drive-through, what is the average waiting time
before the third car arrives?
4
e−6 6x
P(X ≥ 5) = 1 − P(X ≤ 4) = 1 − = 1 − 0.2850565 = 0.7149435.
x=0
x!
> 1 - ppois(4,6)
[1] 0.7149435
(b) Let W represent the waiting time until the αth outcome. It follows that W ∼ Γ(α =
2, λ = 3). Consequently,
1
32 2−1 −3x
P(W > 1) = 1 − P(W ≤ 1) = 1 − P(Γ(2, 3) ≤ 1) = 1 − x e dx
Γ(2)
0
1
=1− 3x e−3x 3 dx
0
Univariate Probability Distributions 143
In other words, P(W > 1) = 1 − 0.8008517 = 0.1991483. To solve the problem with S, use
the command pgamma() or integrate():
> 1 - pgamma(1,2,3)
[1] 0.1991483
> gam23<-function(x){9*x*exp(-3*x)}
> integrate(gam23,1, Inf) # R
0.1991483 with absolute error < 2.5e-05
> gam23<-function(x){9*x*exp(-3*x)}
> integrate(gam23,1, Inf)$integral # S-PLUS
[1] 0.1991483
(c) This problem is really asking for the mean of a Γ(α = 2, λ = 3) random variable. Note:
α = 2 since one car has already arrived and the problem requests the average waiting time
until the third car arrives. Therefore, E[X] = αλ = 23 . In other words, there is an average
wait of 23 of a minute before the arrival of the third vehicle given one vehicle has already
arrived.
and represents the probability that the lifetime of the component exceeds t. The hazard
function, h(t), is defined as
f (t) f (t)
h(t) = = , t > 0, F (t) < 1. (4.18)
1 − F (t) R(t)
Note that the hazard function is often called the conditional failure rate.
The functions h(t), f (t), and F (t) provide mathematically equivalent specifications of
the distribution of T . In fact, it can be shown that
t
f (t) = h(t)e− 0
h(x) dx
. (4.19)
To gain an intuitive understanding of what h(t) is measuring, let dt represent a small unit of
measurement. Then, the quantity h(t)dt can be thought of as the approximate probability
144 Probability and Statistics with R
that T takes on a value in (t, t + dt). Keeping in mind that 1 − F (t) = P(T > t), write
f (t)dt
h(t)dt = ≈ P[T ∈ (t, t + dt)|T > t].
1 − F (t)
In other words, h(t)dt represents the approximate probability of having a breakdown during
the interval (t, t + dt) given that a component has lasted up to time t. In mathematical
terms,
P(t ≤ T ≤ t + dt | T > t)
lim (4.20)
dt→0 dt
may be written, which represents the instantaneous rate of death or failure at time t, given
the individual or component has survived to time t. It may then be noted that the hazard
function is a rate rather than a probability. The failure rate for an exponential random
variable is a constant λ:
f (t) λeλx
h(t) = = = λ.
1 − F (t) 1 − 1 − e−λx
Not many components have a constant failure rate. As a matter of fact, it stands to
reason that the failure rate should increase as the life of a component ages. For most
manufactured items as well as human populations, this is the case after some initial time
period. However, there are some instances such as breakdowns when equipment is on a
preventative maintenance schedule where it is still reasonable to assume a constant failure
α−1
rate. A very flexible hazard function is h(t) = αtβ α , for all α and β greater than 0, since
the function is monotone increasing for α > 1, monotone decreasing for α < 1, and constant
for α = 1, as illustrated in Figure 4.8. This hazard function corresponds to the Weibull
distribution that is discussed in Section 4.3.5
1.0
8
0.8
α = 1.2 α = 1.2
6
0.6
h(t)
f (t)
4
0.4
2
0.2
α = 1.0 α = 1.0
α = 0.5 α = 0.5
0.0
0
0 1 2 3 4 5 0 1 2 3 4 5
t t
αtα−1
FIGURE 4.8: Illustration of the hazard function h(t) = βα for α = 0.5, α = 1.0, and
α = 1.2 with β = 1 and the corresponding pdfs
Univariate Probability Distributions 145
Example 4.21 Hazard Rate In an effort to attract more business, a local com-
puter outlet has agreed to replace its laser printers with a brand new laser printer in the
event any of its laser printers malfunction within one year of the date of their purchase.
According to the manufacturer of the printer, the useful life (in years) of the printer is a
random variable T with pdf f (t) = K(2000 − 0.1e−2t ) for 0 < t < 5.
(b) Compute the probability a randomly selected laser printer will have to be replaced due
to a malfunction.
(c) What are the mean and standard deviation for laser printer life?
(d) If a small business purchases five laser printers from the computer outlet, what is the
probability there are no malfunctions during the first year?
(e) What should the length of guarantee time be for a laser printer if the outlet store wants
to replace no more than 5% of the laser printers?
(a) To find K such that f (t) is a pdf, the integral over all possible values of t must be one:
5
K(2000 − 0.1e−2t ) dt = 1
set
0
% 5 (
K 2000t + 0.05e−2t 0 = 1
K 10000 + 0.05e−10 − 0.05 = 1
1
K=
9999.95 + 0.05e−10
Let the denominator of K be equal to k1 = 9999.95 + 0.05e−10 for the remainder of the
problem. The solution given is for R. To obtain similar answers with S-PLUS, replace $value
with $integral throughout. To calculate the denominator of K numerically with R, enter
5 1
(c) E(T ) = 0 k1
t(2000 − 0.1e−2t ) dt = 2.50001
(d) Assuming the useful lives of laser printers are independent, the probability none of the
five printers have to be replaced is
t
1
(2000 − 0.1e−2x) dx = 0.05
0 k1
t
2000x + 0.05e−2x 0 = 0.05k1
2000t + 0.05e−2t − 0.05 = 0.05k1
Find roots of 2000t + 0.05e−2t − 0.05 − 0.05k1 = 0
Since t is given in years, multiplying 0.25 × 365 = 91.25 days. In other words, the computer
outlet will have to replace less than 5% of their laser printers if they use a guarantee period
of 91 days.
40
20
0
0 1 2 3 4 5
year
distributions. In particular, the hazard function for the Weibull distribution has a failure
α−1
rate that varies with time. The hazard rate for the Weibull distribution is h(t) = αtβ α ,
for all α and β greater than 0. Using (4.19), derive the pdf for the Weibull distribution as
follows:
The pdf, mean, variance, and hazard function for a Weibull random variable (α > 0 and
β > 0) are in (4.21), while the pdfs for Weib(α, 1) and a Weib(α, 2) random variable
for α = 1, 2, and 5, respectively, are illustrated in Figure 4.10. As with the gamma
distribution, the first parameter in the Weibull distribution, α, is the shape parameter;
and the second parameter, β, is the scale parameter. If X ∼ Weib(α, β) and α = 1, then
X ∼ Exp(λ = 1/β ).
Weibull Distribution
X ∼ Weib(α, β)
α
αβ −α xα−1 e−(x/β) if x ≥ 0
f (x) =
0 if x < 0 (4.21)
E[X] = βΓ 1 + α−1
, 2 -
Var[X] = β 2 Γ 1 + 2α−1 − Γ 1 + α−1
h(x) = αβ −α xα−1 for x ≥ 0
X ∼ Weib(α, β = 1) X ∼ Weib(α, β = 2)
2.0
2.0
1.5
1.5
α=5
1.0
1.0
f (x)
f (x)
α=5
α=1
0.5
0.5
α=2
α=2
α=1
0.0
0.0
0 1 2 3 4 5 0 1 2 3 4 5
x x
FIGURE 4.10: Illustration of the pdfs of a Weib(α, 1) and a Weib(α, 2) random variable
for α = 1, 2, and 5, respectively
Univariate Probability Distributions 149
Example 4.22 The useful life (in thousands of hours) of a certain type of transistor
follows a Weibull distribution with α = 2 and β = 8. Find the probability that a randomly
selected transistor lasts more than 8000 hours. What is the average life for this type of
transistor?
x
α α x α
F (x) = αβ −α tα−1 e−(t/β) dt = −e−(t/β) = 1 − e−(x/β)
0
0
Using the cdf for the Weibull, the probability a randomly selected transistor lasts more than
8000 hours is
% (
P(X > 8) = 1 − F (8) = 1 − 1 − e−(8/8) = e−1 = 0.3678794.
2
−1
1 1 1 √
E[X] = βΓ 1 + α = 8Γ 1 + =8 Γ = 4 π = 7.089815.
2 2 2
3
To solve the first question and to compute Γ 2 with S, use the functions pweibull() and
gamma(), respectively:
> 1 - pweibull(8,2,8)
[1] 0.3678794
> 8*gamma(3/2)
[1] 7.089815
Beta Distribution
X ∼ β(α, β, A, B)
⎧ & 'α−1 & 'β−1
⎨ Γ(α+β)
1
B−A Γ(α)Γ(β)
x−A
B−A
B−x
B−A if A ≤ x ≤ B
f (x) =
⎩0 otherwise (4.22)
α
E[X] = A + (B − A)
α+β
(B − A)2 αβ
Var[X] =
(α + β)2 (α + β + 1)
X ∼ β(α, β) X ∼ β(α, β)
2.0
2.0
β(0.25, 2) β(2, 0.25)
β(2, 2)
1.5
1.5
β(3, 3)
β(1, 1)
1.0
1.0
f (x)
f (x)
β(0.25, 0.25)
0.5
0.5
0.0
0.0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
x x
Solution: Let the random variable X represent the proportion of computers sold in a 15
day period. Since X ∼ β(4, 2), the expected value from (4.22) yields
α 2
E[X] = = .
α+β 3
Univariate Probability Distributions 151
The probability that at least 80% of the computers in the warehouse are sold is
1 1
Γ(4 + 2) 3
P(X ≥ 0.8) = x (1 − x) dx = 20 (x3 − x4 ) dx = 0.26272.
0.8 Γ(4)Γ(2) 0.8
To compute the last answer with S, use the command pbeta() or integrate():
> 1 - pbeta(0.8,4,2)
[1] 0.26272
10 2
1 Γ(5) x−8 16 − x
P(X ≤ 10) = · dx
8 Γ(2)Γ(3) 8 8
8
10
Γ(5)
= 4 (x − 8)(16 − x)2 dx
8 Γ(2)Γ(3)
8
10
24
= (512x − 40x2 + x3 − 2048) dx
4096 · 1 · 2
8
10
3 40 3 x4
= · 256x − x +
2
− 2048x
1024 3 4 8
3 268
= · = 0.2617
1024 3
152 Probability and Statistics with R
Normal Distribution
X ∼ N (μ, σ)
1 (x−μ)2
f (x) = √ e− 2σ2 , −∞ < x < ∞,
2πσ 2
μ μ μ
FIGURE 4.12: Three normal distributions, each with an increasing σ value as read from
left to right
The cdf for a normal random variable, X, with mean, μ, and standard deviation, σ, is
x
1 (t−μ)2
F (x) = P(X ≤ x) = √ e− 2σ2 dt. (4.24)
2πσ 2
−∞
X −μ
Z= ∼ N (0, 1). (4.25)
σ
(x−μ)
x−μ 1 σ z2
F (x) = P(X ≤ x) = P Z ≤ = √ e− 2 dz (4.26)
σ 2π −∞
Neither the integral for (4.26) nor the integral for (4.24) can be computed with standard
techniques of integration. However, (4.26) has been numerically evaluated and tabled.
Further, any normal random variable can be converted to a standard normal random
variable using (4.25). The process of computing P(a ≤ X ≤ b), where X ∼ N (μ, σ), is
graphically illustrated in Figure 4.13 on the following page.
Throughout the text, the convention zα is used to represent the value of the standard
normal random variable Z that has α of its area to the left of said value. In other words,
P(Z < zα ) = α. Another notation that is also used in the text is Φ(zα ) = α. Basically,
the Φ(value) is the same as P(Z < value). That is, Φ is the cdf of the standard normal
distribution. Likewise, Φ−1 (α) = zα . The Φ notation for the cdf and inverse cdf is used
more in Chapter 10.
To find the numerical value of Xα , where X ∼ N (μ, σ) and α is the area (or probability)
to the left of the value Xα , use the S command qnorm(p, mean=MValue, sd=SValue),
where p is the area or probability (this is equivalent to α) to the left of Xα , MValue is the
value of the mean, and SValue is the value of the standard deviation. Note that if one is
dealing with the standard normal distribution, the mean=MValue or sd=SValue arguments
are not needed.
154 Probability and Statistics with R
X ∼ N (μ, σ)
a b b a
b b a
f (x)dx f (x)dx f (x)dx
a −∞ −∞
P( a−μ
σ ≤ Z ≤
b−μ
σ ) P(Z ≤ b−μ
σ )
P(Z ≤ a−μ
σ )
Example 4.25 Scores on a particular standardized test follow a normal distribution with
a mean of 100 and standard deviation of 10.
(a) What is the probability that a randomly selected individual will score between 90 and
115?
(a) To find P(90 ≤ X ≤ 115), first draw a picture representing the desired area such as the
one in Figure 4.14 on page 156. Note that finding the area between 90 and 115 is equivalent
to finding the area to the left of 115 and from that area, subtracting the area to the left of
90. In other words,
To find P(X ≤ 115) and P(X ≤ 90), one can standardize using (4.25). That is,
115 − 100
P(X ≤ 115) = P Z ≤ = P(Z ≤ 1.5),
10
and
90 − 100
P(X ≤ 90) = P Z ≤ = P(Z ≤ −1.0).
10
Using the S commands pnorm(1.5) and pnorm(-1), find the areas to the left of 1.5 and
−1.0 to be 0.9332 and 0.1586, respectively. Consequently,
(b) Finding the value c such that 90% of the area is to its left is equivalent to finding the
value c such that 10% of its area is to the right. That is, finding the value c that satisfies
P(X ≤ c) = 0.90 is equivalent to finding the value c such that P(X ≥ c) = 0.10. Since the
qnorm() function refers to areas to the left of a given value by default, solve
X − 100 c − 100
P(X ≤ c) = P Z = ≤ = 0.90 for c.
10 10
Using qnorm(.9), find the Z value (1.2816) such that 90% of the area in the distribution
is to the left of that value. Consequently, to be in the top 10%, one needs to be more than
1.2816 standard deviations above the mean:
c − 100 set
= 1.2816
10
and solve for c ⇒ c = 112.816.
Using pnorm(.5),
105 − 100
P Z≤ = P(Z ≤ 0.5) = 0.6915.
10
It follows then that P(X ≤ c) = 0.7915. Using qnorm(.7915) gives 0.8116:
X − 100 c − 100
P(X ≤ c) = P Z = ≤ = 0.7915
10 10
c − 100
is found by solving = 0.8116 ⇒ c = 108.116
10
Note that a Z value of 0.8116 has 79.15% of its area to the left of that value.
156 Probability and Statistics with R
The following S commands can be used to solve (a),(b), and (c), respectively:
> qnorm(.90,100,10)
[1] 112.8155
X ∼ N (100, 10)
90 115 115 90
115 115 90
f (x)dx f (x)dx f (x)dx
90 −∞ −∞
P( 90−100
10 ≤Z≤ 115−100
10 ) P(Z ≤ 115−100
10 ) P(Z ≤ 90−100
10 )
−1 1.5 1.5 −1
1.5 1.5 −1
f (z)dz f (z)dz f (z)dz
−1 −∞ −∞
FIGURE 4.14: Graphical representation for finding P(90 ≤ X ≤ 115) given X ∼ N (100, 10)
allows the on-resistance flatness for both the microphone and speaker to be lower than ever
before. The cell phone company requires that the on-resistance flatness be less than 0.7
ohms (Ω). If it is known that 50% of the components from the new company have an ohm
rating of 0.5 Ω or less, 10% have an ohm rating of 0.628 Ω or greater, and the distribution
of the ohm ratings is normal, then:
(a) Find the mean and standard deviation for the distribution of the ohm rating of the
components.
(b) If a component is selected at random, what is the probability that its on-resistance
flatness will be less than 0.7 Ω?
(c) If 20 components are selected at random, what is the probability that at least 19
components will have on-resistance flatness values less than 0.7 Ω?
(b) Calculate that the probability a component has an on-resistance flatness less than 0.7
Ω:
X − 0.5 0.7 − 0.5
P(X ≤ 0.7) = P Z = ≤
.1 0.1
= P(Z ≤ 2)
= 0.97725
To compute the pairs of values plotted in an S quantile-quantile plot, issue the following
commands:
> attach(Score)
> par(pty="s")
> X <- (1:20-1/2)/20
> Xs <- qnorm(X)
> Ys <- sort(scores)
> plot(Xs, Ys)
Univariate Probability Distributions 159
120
115
110
Sample Quantiles
105
100
95
90
−2 −1 0 1 2
Theoretical Quantiles
FIGURE 4.15: Quantile-quantile plot of the standardized test scores of 20 randomly selected
college freshmen
Generally, the command qqnorm() is used to generate the pairs of values that are plotted
for a normal quantile-quantile plot, while the command qqline() adds a line to a normal
quantile-quantile plot that passes through the first and third quartiles. The commands
qqnorm(scores) and qqline(scores) were used to create Figure 4.15.
It is possible to tell from a quantile-quantile plot whether the distribution has shorter
or longer tails than a normal distribution. In addition, the quantile-quantile plot will show
whether a distribution is skewed and in which direction the distribution is skewed. The
right quantile-quantile plots in Figure 4.16 on the following page illustrate how distributions
that have a positive skew will appear as upward opening U shapes in the quantile-quantile
plot, while distributions with a negative skew have downward facing U shapes. The left
quantile-quantile plots in Figure 4.16 on the next page illustrate how distributions that have
short tails relative to the normal distribution will have an S shape while distributions with
tails longer than the normal distribution will have an inverted S shape.
The graphs in Figure 4.16 can be slightly misleading in the sense that they were
constructed from large data sets (n = 500). When n is smaller, reading a quantile-quantile
plot is slightly more challenging. However, the plotted values still need to fall close to a
straight line. One way to train the eye with the quantile-quantile plot is to use simulation to
generate data from a normal distribution for various values of n and observe the resulting
quantile-quantile plots. When this is done, what one realizes is that for small values of
n, even when sampling from a normal distribution, the resulting quantile-quantile plot is
not always linear. The function ntester(), available in the PASWR package, demonstrates
how samples (n < 5000) from a normal distribution that have the same sample size as the
actual data can appear in quantile-quantile plots. One is strongly encouraged to run this
160 Probability and Statistics with R
Normal Normal
10 Short SkewRight
Long 5 SkewLeft
-5 -5
-10
-10
-4 -2 0 2 -4 -2 0 2
FIGURE 4.16: Superimposed quantile-quantile plots for simulated data from a skew left,
skew right, and normal distribution (on the right) and from a short-tailed, long-tailed, and
normal distribution (on the left)
function before finalizing the assessment about the normality of a smaller sized sample. The
results from using ntester() on the standardized test scores from Table 4.2 on page 158
are shown in Figure 4.17 on the next page. Note that the actual data are the center normal
quantile-quantile plot and all of the surrounding quantile-quantile plots are for simulated
normal data having the same sample size as the center plot. One should pay close attention
to how variable the eight surrounding graphs can be even when the data are coming from a
normal distribution. If the data are no more variable than the surrounding plots, it should
be safe to assume they are normal.
It is often helpful to look at several graphs at once when assessing the general shape of a
distribution. The function EDA() in the PASWR package displays a histogram, a density
plot, a boxplot, and a normal quantile-quantile plot of a numeric variable as well as
computing various numerical summaries that are returned in the console. In order to allow
the user to focus strictly on the resulting shapes, no measurement scales are given in the
graphical output. Figure 4.18 on the facing page shows the graphical results from using
EDA(scores). All four graphs in Figure 4.18 confirm normality as a reasonable assumption
for the distribution of the variable scores.
Univariate Probability Distributions 161
FIGURE 4.17: Resulting quantile-quantile plots using the function ntester() on the
standardized test scores from Table 4.2 on page 158
4.4 Problems
1. Derive the mean and variance for the discrete uniform distribution.
n n(n + 1) n n(n + 1)(2n + 1)
(Hints: i=1 xi = ; i=1 x2i = , when xi = 1, 2, . . . , n.)
2 6
2. Construct a plot for the probability mass function and the cumulative probability dis-
tribution of a binomial random variable Bin(n = 8, π = 0.3). Find the smallest value
of k such that P(X ≤ k) ≥ 0.44 when X ∼ Bin(n = 8, π = 0.7). Calculate P(Y ≥ 3) if
Y ∼ Bin(20, 0.2).
3. Let X be a Poisson random variable with mean equal to 2. Find P(X = 0), P(X ≥ 3),
and P(X ≤ k) ≥ 0.70.
4. Let X be an exponential random variable Exp(λ = 3). Find P(2 < X < 6).
5. Fix the seed value at 500, and generate a random sample of size n = 10000 from a
Unif (0,1) distribution. Calculate the sample mean and the sample variance. Are your
answers within 2% of the theoretical values for the mean and variance of a Unif (0,1)
distribution?
6. Fix the seed value at 50, and generate a random sample of size n = 10000 from an
exponential distribution with λ = 2. Create a density histogram and superimpose the
histogram with a theoretical Exp(λ = 2) distribution. Calculate the sample mean and
the sample variance of the randomly generated values. Are your answers within 2% of
the theoretical values for the mean and variance of an Exp(λ = 2) distribution?
7. The Laplace distribution, also known as a double exponential, has a pdf given by
λ −λ |x−μ|
f (x) = ·e , where − ∞ < x < ∞, −∞ < μ < ∞, λ > 0.
2
(a) Find the theoretical mean and variance of a Laplace distribution. (Hint: Integrals
of absolute values should be done as a positive and negative part, in this case, with
limits from −∞ to μ and from μ to ∞.)
(b) Let X1 and X2 be independent exponential random variables, each with parameter
λ. The distribution of Y = X√ 1 − X2 is a Laplace distribution with a mean of zero
and a standard deviation of 2/λ.
Set the seed equal to 3, and generate 25,000
X1 values from an Exp λ = 12 and 25,000 X2 values from another Exp λ = 12
distribution. Use these values to create the simulated distribution of Y = X1 − X2 .
(i) Superimpose a Laplace distribution over a density histogram of the Y values.
(Hint: The R function curve() can be used to superimpose the Laplace distri-
bution over the density histogram.)
(ii) Is the mean of Y within 0.02 of the theoretical mean?
(iii) Is the variance of Y within 2% of the theoretical variance?
8. Let X be a normal random variable N (μ = 7, σ = 3). Calculate P(X > 7.1). Find the
value of k such that P(X < k) = 0.8.
√
9. Let X be a normal random variable N μ = 3, σ = 0.5 . Calculate P(X > 3.5).
10. Let X be a gamma random variable Γ(α = 2, λ = 6). Find the value a such that
P(X < a) = 0.95.
Univariate Probability Distributions 163
11. If X is the number of 3’s that appear when 60 dice are tossed, what is the E X 2 ?
12. An importing company knows that 80% of its imported Chinese socks are suitable for
sale. If a sample of 60 pairs is drawn at random, find the probability that a percentage
between 70% and 90% (inclusive) of the sample is suitable for sale.
13. It is known that 3% of the seeds of a certain variety of tomato do not germinate. The
seeds are sold in individual boxes that contain 20 seeds per box with the guarantee that
at least 18 seeds will germinate. Find the probability that a randomly selected box does
not fulfill the aforementioned requirement.
14. Traffic volume is an important factor for determining the most cost effective method to
surface a road. Suppose that the average number of vehicles passing a certain point on
a road is 2 every 30 seconds.
(a) Find the probability that more than 3 cars will pass the point in 30 seconds.
(b) What is the probability that more than 10 cars pass the point in 3 minutes?
15. The retaining wall of a dam will break if it is subjected to the pressure of two floods. If
the average number of floods in a century is two, find the probability that the retaining
wall lasts more than 20 years.
16. A particular competition shooter hits his targets 70% of the time with any pistol. To
prepare for shooting competitions, this individual practices with a pistol that holds 5
bullets on Tuesday, Thursday, and Saturday, and a pistol that holds 7 bullets the other
days. If he fires at targets until the pistol is empty, find the probability that he hits only
one target out of the bullets shot in the first round of bullets in the pistol he is carrying
that day. In this case, what is the probability that he used the pistol with 7 bullets?
17. A binomial,
Bin(n, π), distribution can be approximated by a normal distribution,
N nπ, nπ(1 − π) , when nπ > 10 and n(1 − π) > 10. The Poisson distribution
√
can also be approximated by a normal distribution N λ, λ if λ > 10. Consider a
sequence from 7 to 25 of a variable X (binomial or Poisson) and show that for n = 80,
π = 0.2, and λ = 16 the aforementioned approximations are appropriate. The normal
approximation to a discrete distribution can be improved by adding 0.5 to the normal
random variable when finding the area to the left of said random variable. Specifically,
create a table showing P(X ≤ x) for the range of X for the three distributions and a
graph showing the density of the normal distribution with vertical lines at X − .1 and
X + .1 showing P(X = x) for the binomial and Poisson distributions, respectively.
18. Verify that if k/N is small (≤ 0.1) and N = m+n is large, a hypergeometric distribution,
Hyper (m, n, k), can be adequately approximated by a Bin(n = k, π = m/N ) distribution.
Compute the probabilities for each distribution using the values n = 20, m = 300, k = 10.
Show the numerical results to three decimal places as well as a graph depicting the
probabilities of the hypergeometric distribution with a vertical line and the probabilities
of the binomial distribution in the same plot with an open circle.
19. In 1935, Fisher described the following experiment in his book, Design of Experiments:
A friend of Fisher’s said that when she drank tea with milk, she was able to determine
if the tea was poured first or if the milk was poured first. Find the probability that
Fisher’s colleague guesses 3 cups in which milk has been added before tea, given that in
4 out of 8 cups, milk has been added before tea.
164 Probability and Statistics with R
20. Consider the function
g(x) = (x − a)2 , where a is a constant and E (X − a)2 is finite.
Find a so that E (X − a)2 is minimized.
21. Suppose the percentage of drinks sold from a vending machine are 80% and 20% for soft
drinks and bottled water, respectively.
(a) What is the probability that on a randomly selected day, the first soft drink is the
fourth drink sold?
(b) Find the probability that exactly 1 out of 10 drinks sold is a soft drink.
(c) Find the probability that the fifth soft drink is the seventh drink sold.
(d) Verify empirically that P Bin(n, π) ≤ r − 1 = 1 − P NB (r, π) ≤ (n − r) , with
n = 10, π = 0.8, and r = 4.
22. The hardness of a particular type of sheet metal sold by a local manufacturer has a
normal distribution with a mean of 60 micra and a standard deviation of 2 micra.
(a) This type of sheet metal is said to conform to specification provided its hardness
measure is between 57 and 65 micra. What percent of the manufacturer’s sheet
metal can be expected to fall within the specification?
(b) A building contractor agrees to purchase metal from the local metal manufacturer
at a premium price provided four out of four randomly selected pieces of metal
test between 57 and 65 micra. What is the probability the building contractor will
purchase metal from the local manufacturer and pay a premium price?
(c) If an acceptable sheet of metal is one whose hardness is not more than c units away
from the mean, find c such that 97% of the sheets are acceptable.
(d) Find the probability that at least 10 out of 20 sheets have a hardness greater than
60.
23. The weekly production of a banana plantation can be modeled with a normal random
variable that has a mean of 5 tons and a standard deviation of 2 tons.
(a) Calculate the mean number of weeks in which the production is greater than the
third quartile.
(b) Find the probability that, in at most 1 out of the 8 randomly chosen weeks, the
production has been less than 3 tons.
(c) Find the probability that at least 3 weeks are needed to obtain a production greater
than 10 tons.
24. The lifetime of a certain engine follows a normal distribution with mean and standard
deviation of 10 and 3.5 years, respectively. The manufacturer replaces all catastrophic
engine failures within the guarantee period free of charge. If the manufacturer is willing
to replace no more than 4% of the defective engines, what is the largest guarantee period
the manufacturer should advertise?
25. A bank has 50 deposit accounts with e 25,000 each. The probability of having to close
a deposit account and then refund the money in a given day is 0.01. If account closings
are independent events, how much money must the bank have available to guarantee it
can refund all closed accounts in a given day with probability greater than 0.95?
Univariate Probability Distributions 165
26. The vendor in charge of servicing coffee dispensers is adjusting the one located in the
department of statistics. To maximize profit, adjustments are made so that the average
quantity of liquid dispensed per serving is 200 milliliters per cup. Suppose the amount
of liquid per cup follows a normal distribution and 5.5% of the cups contain more than
224 milliliters.
(a) Find the probability that a given cup contains between 176 and 224 milliliters.
(b) If the machine can hold 20 liters of liquid, find the probability that the machine must
be replenished before dispensing 99 cups.
(c) If 6 random samples of 5 cups are drawn, what is the probability that the sample
mean is greater than 210 milliliters in at least 2 of them?
27. The mean number of calls a tow truck company receives during a day is 5 per hour. Find
the probability that a tow truck is requested more than 4 times per hour in a given hour.
What is the probability the company waits for less than 1 hour before the tow truck is
requested 3 times?
28. The pill weight for a particular type of vitamin follows a normal distribution with a mean
of 0.6 grams and a standard deviation of 0.015 grams. It is known that a particular
therapy consisting of a box of vitamins with 125 pills is not effective if more than 20%
of the pills are under 0.58 grams.
(a) Find the probability that the therapy with a box of vitamins is not effective.
(b) A supplement manufacturer sells vitamin bottles containing 125 vitamins per bottle
with 50 bottles per box with a guarantee that at least 47 bottles per box weigh more
than 74.7 grams. Find the probability that a randomly chosen box does does not
meet the guaranteed weight.
29. A canning industry uses tins with weight equal to 20 grams. The tin is placed on a scale
and filled with red peppers until the scale shows the weight μ. Then, the tin contains Y
grams of peppers. If the scale is subject to a random error X ∼ N (0, σ = 10),
(a) Find the probability that more than 8 buckets remain unpainted.
(b) Find the probability that the first bucket, after the electricity is restored, arrives
before 10 seconds have passed.
31. Give a general expression to calculate the quantiles of a Weibull random variable.
32. A used-car salesman offers a guarantee period of one year for his cars. He knows that
the distribution of the elapsed time until the first breakdown occurs follows a Weibull
distribution, Weib(3, 25). If the salesman expects to sell 50 cars per year, and the repair
cost per car is on average 800 dollars, what is the mean cost of the guarantee?
166 Probability and Statistics with R
4
f (x) = x(2 − x2 ), 0 ≤ x ≤ 1.
3
(a) Verify that the area under f (x) is 1.
(b) Fix the seed at 13 ([Link](13)), and generate 10000 realizations of X. What are
the mean and variance of the random sample?
(c) Calculate the theoretical mean and variance of X.
(d) How close are the estimates in (b) to the theoretical values in (c)?
(e) Find the cumulative density function.
(f) What is P(X > .75)?
36. Let X be a random variable with probability density function
38. A copper wire manufacturer produces conductor cables. These cables are of practical
use if their resistance lies between 0.10 and 0.13 ohms per meter. The resistance of the
cables follows a normal distribution, where 50% of the cables have resistance under 0.11
ohms and 10% have resistance over 0.13 ohms.
(a) Determine the mean and the standard deviation for cable resistance.
(b) Find the probability that a randomly chosen cable can be used.
(c) Find the probability that at least 3 out of 5 randomly chosen cables can be used.
39. Consider the random variable X ∼ Weib(α, β).
(a) Find the cdf for X.
(b) Use (4.18) and verify that for X ∼ Weib(α, β), the hazard function is given by
αtα−1
h(t) = .
βα
40. If X ∼ Bin(n, π), derive the moment generating function of X and use it to derive the
mean and variance of X. The binomial pdf can be found on page 117.
41. If X ∼ Bin(n, π), use the binomial expansion to find the mean and variance of X. To
find the variance, use the second factorial moment E[X(X −1)] and note that x!
x 1
= (x−1)!
when x > 1.
42. The speed of a randomly chosen gas molecule in a certain volume of gas is a random
variable, V , with probability density function
. 32
2 M M v2
f (v) = v 2 e− 2RT for v ≥ 0
π RT
where R is the gas constant (= 8.3145 J/mol · K ), M is the molecular weight of the gas,
and T is the absolute temperature measured in degrees Kelvin.
(Hints:
∞
1 k+1 1 √
xk e−x dx =
2
Γ Γ (α + 1) = αΓ (α) Γ = π)
0 2 2 2
(a) Derive a general expression for the average speed of a gas molecule.
168 Probability and Statistics with R
(b) If 1 J = 1kg · m /s2 , what are the units for the answer in part (a)?
2
Mv 2
(c) Kinetic energy for a molecule is Ek = 2 . Derive a general expression for the
average kinetic energy of a molecule.
(d) The weight of hydrogen is 1.008 g/mol . Note that there are 6.0221415×1023 molecules
in 1 mole. Find the average speed of a hydrogen molecule at 300◦K using the result
from part (a).
(e) Use numerical integration to verify the result from part (d).
(f) Show the probability density functions for the speeds of hydrogen, helium, and
oxygen on a single graph. The molecular weights for these elements are 1.008 g/mol ,
4.003 g/mol , and 16.00 g/mol , respectively.
43. Consider the equilateral triangle ABC with side l. Given a randomly chosen point R
in the triangle, calculate the cumulative and the probability density functions for the
distance from R to the side BC. Construct a graph of the cumulative√ density function
for different values of l. (Hint: The equation of the line CA is y = 3x.)
l l
M R N
C B
44. In Pamplona, Spain, a tombola organizes different raffles during the festivals. In each
raffle, only 2 tickets out of n win a prize. The tickets are sold consecutively, and the
Univariate Probability Distributions 169
prize is immediately announced when one person wins. Two friends have decided to take
part in one of the raffles in the following way: One of them buys the first ticket on sale,
and the other one buys the first ticket after the first prize has been announced. Derive
the probability that each of them wins a prize. If there are m raffles during the night in
which the two friends participate, what is the probability that each of them wins more
than one prize?
45. Example 4.4 on page 122 introduced the World Cup Soccer data stored in the data
frame Soccer. The observed and expected number of goals for a 90 minute game were
computed. To verify that the Poisson rate λ is constant, compute the observed and
expected number of goals with the time intervals 45, 15, 10, 5, and 1 minute(s). Compute
the means and variances for both the observed and expected counts in each time interval.
Based on the results, is criterion (3) of the Poisson process on page 120 satisfied? (Note:
See the code at the end of the Chapter 4 script for ideas on how to do this.)
Chapter 5
Multivariate Probability Distributions
for each pair of values (x, y) within the domain of X and Y is called the joint pdf of X and
Y . Any function pX,Y (x, y) can be used as a joint pdf provided the following properties are
satisfied:
(i) pX,Y (x, y) ≥ 0 for all x and y.
(ii) pX,Y (x, y) = 1.
x y
(iii) P [(X, Y ) ∈ A] = pX,Y (x, y).
(x,y)∈A
Property (iii) states that when A is composed of pairs of (x, y) values, the probability
P [(X, Y ) ∈ A] is obtained by summing the joint pdf over pairs in A.
171
172 Probability and Statistics with R
Table 5.1: B.S. graduate grades in Linear Algebra and Calculus III
Linear Algebra
A B C
A 2 13 6
Calculus III B 5 85 40
C 7 33 9
2 + 5 + 7 + 13 + 85 + 33 145
P [(X, Y ) ∈ A] = pX,Y (x, y) = = .
200 200
(x,y)∈A
(b) Let the random variables X and Y represent the grades in Calculus III and Linear
Algebra, respectively. If A represents the pairs of Calculus III and Linear Algebra values
such that the grade in Calculus III is a B or better, then the probability of getting a B or
better in Calculus III is written
2 + 13 + 6 + 5 + 85 + 40 151
P [(X, Y ) ∈ A] = pX,Y (x, y) = = .
200 200
(x,y)∈A
(c) Let the random variables X and Y represent the grades in Calculus III and Linear
Algebra, respectively. If A represents the pairs of Calculus III and Linear Algebra values
such that the grade in both Calculus III and Linear Algebra is a B or better, then the
probability of getting a B or better in both Calculus III and Linear Algebra is written
2 + 5 + 13 + 85 105
P [(X, Y ) ∈ A] = pX,Y (x, y) = = .
200 200
(x,y)∈A
For any random variables X and Y , the joint cdf is defined in (5.2), while the marginal
pdfs of X and Y , denoted pX (x) and pY (y), respectively, are defined in Equations (5.3) and
(5.4):
FX,Y (x, y) = P(X ≤ x, Y ≤ y), −∞ < x < ∞, −∞ < y < ∞ (5.2)
In (a) of Example 5.1 on the preceding page, the problem requests the probability of
getting a B or better in Linear Algebra. Another way to compute the answer is by adding
14
the two marginals pY (A) + pY (B) = 200 + 131 145
200 = 200 . Likewise, (b) of Example 5.1 on the
previous page can also be solved with the marginal distribution for X: pX (A) + pX (B) =
21 130 151
200 + 200 = 200 .
Multivariate Probability Distributions 173
Property (3) implies that P [(X, Y ) ∈ A] is the volume of a solid over the region A bounded
by the surface fX,Y (x, y).
For any random variables X and Y , the joint cdf is defined in (5.5), while the marginal
pdfs of X and Y , denoted fX (x) and fY (y), respectively, are defined in Equations (5.6) and
(5.7):
x y
FX,Y (x, y) = fX,Y (r, s) ds dr, −∞ < x < ∞, −∞ < y < ∞ (5.5)
−∞ −∞
∞
fX (x) = fX,Y (x, y) dy, −∞ < x < ∞ (5.6)
−∞
∞
fY (y) = fX,Y (x, y) dx, −∞ < y < ∞ (5.7)
−∞
(b)
(c)
1
fX (x) = fX,Y (x, y) dy = 1, 0≤x≤1
0
Example 5.3 Joint PDF Find the value c to make fX,Y (x, y) = cx a valid joint
pdf for x > 0, y > 0, and 2 < x + y < 3.
Solution: The domain of interest is lightly shaded in Figure 5.1. To solve the problem,
first compute the volume bounded by x = 0, y = 0, and y = 3 − x beneath the surface
fX,Y (x, y) = cx, which is denoted V 1. Next, find the volume bounded by x = 0, y = 0, and
y = 2 − x beneath the surface fX,Y (x, y) = cx, which is denoted V 2. For fX,Y (x, y) to be
a valid pdf, c must be found such that the difference between V 1 and V 2 is one.
3 3−x
3 # 3 $
3x2 x3 27c
V1= cx dy dx = c (3x − x ) dx = c
2
− =
2 3 0 6
0 0 0
2 2−x
2 # 2 $
x3 8c
V2= cx dy dx = c (2x − x ) dx = c x − =
2 2
3 0 6
0 0 0
27c 8c set 6
V1−V2= − = 1⇒c=
6 6 19
y =3−x
3
2
y =2−x
x
2 3
FIGURE 5.1: Graphical representation of the domain of interest for Example 5.3
Example 5.4 Use Table 5.1 on page 172 to decide if the random variables X, grade in
Calculus III, and Y , grade in Linear Algebra, are dependent.
Solution: The random variables X and Y are dependent if pX,Y (x, y) = pX (x) · pY (y)
for any (x, y). Consider the pair (x, y) =(A, A), that is, an A in both Calculus III and in
Linear Algebra.
?
pX,Y (A, A) = pX (A) · pY (A)
2 ? 21 14
= ·
200 200 200
2 21 × 14
=
200 40, 000
0.01 = 0.00735
Since 0.01 = 0.00735, the random variables X and Y , the grades in Calculus III and
Linear Algebra, respectively, are dependent. It is important to note that the definition
of independence requires all the joint probabilities to be equal to the product of the
corresponding row and column marginal probabilities. Consequently, if the joint probability
of a single entry is not equal to the product of the corresponding row and column marginal
probabilities, the random variables in question are said to be dependent.
Example 5.5 Are the random variables X and Y in Example 5.2 on page 173 indepen-
dent? Recall that the pdf for Example 5.2 was defined as
1 if 0 ≤ x ≤ 1, 0 ≤ y ≤ 1
fX,Y (x, y) =
0 otherwise
Solution: Since the marginal pdf for X, fX (x) = 1, and the marginal pdf for Y , fY (y) = 1,
it follows that X and Y are independent since fX,Y (x, y) = fX (x)·fY (y) for all x and y.
(c)
P [(X1 , X2 , . . . , Xn ) ∈ A] = ··· fX1 ,X2 ,...,Xn (x1 , x2 , . . . , xn )dx1 dx2 · · · dxn
(x1 ,x2 ,...,xn )∈A
Independence for several random variables is simply a generalization of the notion for the
independence between two random variables. X1 , X2 , . . . , Xn are independent if, for every
subset of the random variables, the joint pdf of the subset is equal to the product of the
marginal pdfs. Further, if X1 , X2 , . . . , Xn are independent random variables with respective
moment generating
n functions MX1 (t), MX2 (t), . . . , MXn (t), then the moment generating
function of Y = i=1 ci Xi is
In the case where X1 , X2 , . . . , Xn are independent normal random variables, a theorem for
the distribution of Y = a1 X1 + · · · + an Xn , where a1 , a2 , . . . , an are constants, is stated.
2 t2
σi
Proof: Since Xi ∼ N (μi , σi ), the mgf for Xi is MXi (t) = eμi t+ 2 using the mgf from
(4.23). Further, since the X1 , X2 , . . . , Xn are independent,
Example 5.6 Use moment generating functions to show that the sum of two independent
Poisson random variables is a Poisson random variable.
t
Solution: First recall that the mgf of a Poisson random variable is MX (t) = eλ(e −1) . If
X is a Poisson random variable with mean λ and Y is a Poisson random variable with mean
μ, then Z = X + Y is also a Poisson random variable with mean λ + μ since
t t t
−1) −1) −1)
MZ (t) = MX (t) × MY (t) = eλ(e × eμ(e = e(λ+μ)(e .
Multivariate Probability Distributions 177
pX,Y (x, y)
pX|Y (x|y) = P(X = x|Y = y) = . (5.9)
pY (y)
If the random variables are continuous, the conditional pdf of X given that Y = y provided
that fY (y) > 0 is defined as
fX,Y (x, y)
fX|Y (x|y) = . (5.10)
fY (y)
Example 5.7 Let the random variables X and Y have a joint pdf:
12
x(2 − x − y) for 0 < x < 1, 0 < y < 1
fX,Y (x, y) = 5
0 otherwise
Solution: Using the definition for the conditional pdf of X given Y = y from (5.10), write
The random variable X represents the percentage of radiators that pass test A, and Y
represents the percentage of radiators that pass test B.
178 Probability and Statistics with R
1 1
(d) Compute the probability that less than 8 of the radiators will pass test B given that 2
have passed test A.
(e) Compute the quantities: E[X], E X 2 , Var (X), E[Y ], E Y 2 , and Var (Y ).
(a) The function (5.11) is a pdf since fX,Y (x, y) is non-negative and
⎡ ⎤
1 x 1 x 1
⎣x x3
8xy dy dx = 8 y dy ⎦ dx = 8 dx = 1
2
0 0 0 0 0
x
fX (x) = f (x, y)dy = 8xy dy = 4x3 , 0≤x≤1
0
1
fY (y) = f (x, y)dx = 8xy dx = 4y(1 − y 2 ), 0≤y≤1
y
fX,Y (x, y) 8xy 2x
fX|Y (x|y) = = = , y≤x≤1
fY (y) 4y(1 − y 2 ) 1 − y2
fX,Y (x, y) 8xy 2y
fY |X (y|x) = = 3 = 2, 0 ≤ y ≤ x
fX (x) 4x x
(c) The random variables X and Y are dependent since fX,Y (x, y) = 8xy = fX (x) · fY (y) =
16x3 y − 16x3 y 3 .
1 1
8 8
2y 1 1
P (Y < 1/8 | X = 1/2 ) = fX,Y (y|1/2 ) dy = 1 dy = 4y 2 |08 =
4
16
0 0
Multivariate Probability Distributions 179
(e) The quantities E[X], E X 2 , Var (X), E[Y ], E Y 2 , and Var (Y ) are
1 1
4
E[X] = x · 4x dx = 4 3
x4 dx =
5
0 0
1 1
2
E X 2
= x · 4x dx = 4
2 3
x5 dx =
3
0 0
2 16 2
Var(X) = E X 2 − [E[X]]2 = − =
3 25 75
1 1
8
E[Y ] = y · 4y(1 − y 2 ) dy = 4 (y 2 − y 4 ) dy =
15
0 0
1 1
1
E Y 2
= y · 4y(1 − y ) dy = 4
2 2
(y 3 − y 5 ) dy =
3
0 0
1 64 11
Var(Y ) = E Y 2 − [E[Y ]]2 = − =
3 225 225
(f) The following code can be used to create a graph similar to Figure 5.2:
> [Link] <- function(f, low=-1, hi=1, n=30){
+ r <- seq(low, hi, length=n)
+ z <- outer (r, r, f)
+ persp(r, r, z, xlab="X", ylab="Y", zlab="Z")}
> f3 <- function(x, y) {ifelse(x >= y, 8*x*y, 0)}
> [Link](f3,0,1,25)
Z4
0
1
8
0.
1
0.8
6
0.
0.6
4
0.
Y
0.4
X
0.2
2
0.
0
0
Be careful not to assume the variance of the sum of two random variables is the sum
of the variances of each random variable. Only if X and Y are independent is it true that
Var[X + Y ] = Var[X] + Var[Y ]. A simple example to show why this is not true in general
is computing Var[X + X] = Var[X] + Var[X] since Var[X + X] = Var[2X] = 4Var[X].
However, if X1 , X2 , . . . Xn are n independent random variables with means μ1 , μ 2 , . . . , μn ,
n
and variances σ12 , σ22 , . . . , σn2 , respectively, then the
mean and variance ofY = i=1 ci Xi
n 2 n 2 2
where the ci s are real-valued constants are μY = i=1 ci μi and σY = i=1 ci σi . The
proofs of the last two statements are left as exercises for the reader. (See problem 36 on
page 196.)
Example 5.10 Let the random variables X and Y have a joint pdf:
e−y/x e−x
fX,Y (x, y) = x > 0, y>0
x
Compute E [Y |X = x].
Solution: First, compute the conditional pdf fY | X (y|x):
e−y/x e−x e−y/x
f (x, y) x x
fY | X (y|x) = = ∞ = ∞
fX (x) e−y/x e−x e−y/x
x dy x dy
0 0
e−y/x
= , x > 0, y>0
x
Multivariate Probability Distributions 181
e−y/x
Integrating by parts with u = y and dv = x , obtain
∞ ∞ ∞
−y/x
E[Y |X = x] = −ye + e−y/x dy = 0 + −xe−y/x = x, x>0
0 0
0
When two random variables, say X and Y , are independent, recall that f (x, y) = fX (x)·
fY (y) for the continuous case and pX,Y (x, y) = pX (x) · pY (y) for the discrete case. Further,
E[XY ] = E[X] · E[Y ]. The last statement is true for both continuous and discrete X and
Y . A proof for the discrete case is provided. Note that the proof in the continuous case
would simply consist of exchanging the summation signs for integral signs.
Proof:
Example 5.11 Use the joint pdf provided in Example 5.8 on page 177 and compute
E[XY ].
Solution:
⎡ ⎤
1 x 1 x 1
⎣x2 x5 4
E[XY ] = xy · 8xy dy dx = 8 y dy ⎦ dx = 8
2
dx =
3 9
0 0 0 0 0
Since the random variables X and Y were found to be dependent in part (c) of Example 5.8
on page 177, note that
4 4 8 32
E[XY ] = = E[X] · E[Y ] = · =
9 5 15 75
5.5.2 Covariance
When two variables, X and Y , are not independent or when it is noted that E[XY ] =
E[X]·E[Y ], one is naturally interested in some measure of their dependency. The covariance
of X and Y , written Cov [X, Y ], provides one measure of the degree to which X and Y tend
to move linearly in either the same or opposite directions. The covariance of two random
variables X and Y is defined as
Cov [X, Y ] = E (X − μX )(Y − μY )
⎧
⎨ x y (x − μX )(y − μY )pX,Y (x, y)
⎪ X, Y discrete
(5.14)
= ∞ ∞
⎪
⎩ (x − μX )(y − μY )f (x, y) dx dy X, Y continuous
−∞ −∞
182 Probability and Statistics with R
A Cov [X, Y ] > 0 indicates that, generally, as X increases, so does Y (that is, X and Y
move in the same direction); whereas, a Cov [X, Y ] < 0 indicates that, generally, as X
increases Y decreases (that is, X and Y move in opposite directions). To gain an intuitive
understanding of covariance, see Figure 5.3, which has both horizontal and vertical dotted
lines to indicate μXi and μYi in each of the three plots. The first plot in Figure 5.3 exhibits
a strong positive relationship. By this it is meant that large values of X tend to occur with
large values of Y and small values of X tend to occur with small values of Y . Consequently,
(x − μX ) will tend to have the same sign as (y − μY ), so their product will be positive. In
the center plot of Figure 5.3, the relationship between the two variables is negative, and
note that (x − μX2 ) and (y − μY2 ) tend to have opposite signs, which makes most of their
products negative.
34
1100
110
32
1000
100
30
28
900
90
26
800
80
FIGURE 5.3: Scatterplots showing positive, negative, and zero covariance between two
1
random variables where pX,Y (x, y) = 10 for each of the ten pairs of plotted points.
Example 5.12 Compute the covariance between X1 and Y1 for the values provided in
Table 5.2 given that pX,Y (x, y) = 1/10 for each (x, y) pair.
Solution:
pX1 (x) = pX1 ,Y1 (x, y)
y
58 + 72 + · · · + 128
μX1 = x · pX1 (x) = = 93
x
10
80 + 80 + · · · + 120
μY1 = y · pY1 (y) = = 100
y
10
At times, it will be easier to work with the shortcut formula Cov [X, Y ] = E[XY ]−μX ·μY
instead of using the definition in (5.14).
Example 5.13 Compute the covariance between X and Y for Example 5.8 on page 177.
In part (e) of Example 5.8, E[X] and E[Y ] were computed to be 45 and 15
8
, respectively,
4
and in Example 5.11 on page 181, it was found that E[XY ] = 9 .
Solution: Using the shortcut formula,
4 4 8 4
Cov [X, Y ] = E[XY ] − μX μY = − · = .
9 5 15 225
When one examines the first two plots in Figure 5.3 on the preceding page, the de-
pendency in the left plot seems to be about as strong as the dependency in the center
plot, just in the opposite direction. However, the Cov [X, Y ] = 280 in the left plot and
Cov [X, Y ] = −2800 in the center plot. It turns out that the dependencies are the same
(just in opposite directions), but the units of measurement for the Y variable in the center
plot are a factor of 10 times larger than those in the left plot. So, it turns out that covariance
is unit dependent. To eliminate this unit dependency, scale the covariance.
5.5.3 Correlation
The correlation coefficient between X and Y , denoted ρX,Y , or simply ρ, is a scale in-
dependent measure of linear dependency between two random variables. The independence
184 Probability and Statistics with R
Example 5.15 Given the random variables X and Y with their joint probability distri-
bution provided in Table 5.3, verify that although Cov [X, Y ] = 0, X and Y are dependent.
Solution: Start by computing the quantities E[XY ], E[X], and E[Y ] to use in the
shortcut formula for the covariance:
3 2 3
E[X] = (−1) · + (0) · + (1) · = 0
8 8 8
3 2 3
E[Y ] = (−1) · + (0) · + (1) · = 0
8 8 8
1 1
E[XY ] = (−1 · −1) · + · · · + (1 · 1) · = 0
8 8
Cov [X, Y ] = E[XY ] − E[X] · E[Y ] = 0
The covariance for this problem is 0. However, the random variables are dependent since
1 3 3 9
P(X = −1, Y = −1) = = P(X = −1) · P(Y = −1) = · = .
8 8 8 64
This example reinforces the idea that a covariance or correlation coefficient of 0 does not
imply independence.
Multivariate Probability Distributions 185
Example 5.16 Compute ρX1 ,Y1 for Example 5.12 on page 182. Recall that μX1 = 93,
μY1 = 100, and Cov [X1 , Y1 ] = 280.
Solution: Start by computing the quantities E X12 , E Y12 , σX1 , and σX2 :
E X12 = x2 pX1 (x)
x
1 1 1
= 582 · + 722 · + · · · + 1282 · = 9090
10 10 10
E Y12 = y 2 pY1 (y)
y
1 1 1
= 802 · + 802 · + · · · + 1202 · = 10200
10 10 10
2
Var[X1 ] = E X12 − E[X1 ] = 9090 − 932 = 441
√
σX1 = Var[X1 ] = 441 = 21
2
Var[Y1 ] = E Y12 − E[Y1 ] = 10200 − 1002 = 200
√
σY1 = Var[Y1 ] = 200 = 14.14214
Cov [X1 , Y1 ] 280
ρX1 ,Y1 = = = 0.9428087
σX1 σY1 21 × 14.14214
It is also possible to get the answer directly from S by entering
> cor(X1, Y1)
[1] 0.942809
It is worthwhile to note that ρX1 ,Y1 = 0.9428087 and ρX2 ,Y2 = −0.9428087 for the left
and center plots, respectively, in Figure 5.3 on page 182. In other words, the correlations
have the same absolute magnitude for both plots, even though the absolute values of the
covariances differ by a factor of ten.
Multinomial Distribution
X ∼ MN (n, π1 , . . . , πk )
n!
P(X = (x1 , . . . , xk )|n, π1 , . . . , πk ) = π x1 π x2 · · · πkxk
x1 !x2 ! · · · xk ! 1 2
(5.16)
E [Xi ] = nπi
Var [Xi ] = nπi (1 − πi )
given that each Xi ∼ Bin(n, πi )
MX (t) = (π1 et1 + π2 et2 + · · · + πk−1 etk−1 + πk etk )n
Example 5.17 The probability a particular type of light bulb lasts less than 500 hours
is 0.5 and the probability the same type of light bulb lasts more than 800 hours is 0.2. In
a random sample of ten light bulbs, what is the probability of obtaining exactly four light
bulbs that last less than 500 hours and two light bulbs that last more than 800 hours?
Solution: Let the random variables X1 , X2 , and X3 denote the number of light bulbs that
last less than 500 hours, the number of light bulbs that last between 500 and 800 hours, and
the number of light bulbs that last more than 800 hours, respectively. Since π1 = 0.5, π2 =
0.3, and π3 = 0.2, use the first equation in (5.16) and compute P(X1 = 4, X2 = 4, X3 = 2)
as
10!
P(X1 = 4, X2 = 4, X3 = 2|10, 0.5, 0.3, 0.2) = (0.5)4 (0.3)4 (0.2)2 = 0.0638.
4!4!2!
# 2
1 1 x − μX
fX,Y (x, y) = exp −
2πσX σY 1 − ρ2 2(1 − ρ2 ) σX
2 $
x − μX y − μY y − μY
− 2ρ + , (5.17)
σX σY σY
⎛ ⎞
Var[X] Cov [X, Y ]
Σ=⎝ ⎠.
Cov [Y, X] Var[Y ]
Multivariate Probability Distributions 187
3 4
1 −1/2 1 T −1
f (x) = √ |Σ| exp − (X − μ) Σ (X − μ) . (5.18)
( 2π)2 2
The shorthand notation used to denote a multivariate (bivariate being a subset) normal
distribution is X ∼ N (μ, Σ). In general, Σ represents what is called the variance covariance
matrix. When X = (X1 , X2 , . . . , Xn )T and μ = (μ1 , μ2 , . . . , μn )T it is defined as
⎡⎛ ⎞ ⎤
X1 − μ1
⎢⎜ .. ⎟ ⎥
Σ = E[(X − μ)(X − μ)T ] = E ⎣⎝ . ⎠ (X1 − μ1 , . . . , Xn − μn )⎦
Xn − μn
⎛ 2
⎞
σX 1
. . . Cov (X1 , Xn )
⎜ .. .. .. ⎟
=⎝ . . . ⎠.
2
Cov (Xn , X1 ) . . . σX n
The following facts about the bivariate normal distribution are listed without proof:
(c) If X and Y have a bivariate normal distribution, the conditional density of Y given
σY
X = x is a normal distribution with mean μY |x = E(Y |x) = μY + ρ (x − μX ) and
σX
variance σY |x = σY (1 − ρ ).
2 2 2
3
2
2
1
1
y
y
0
0
-1
-1
-1
-1
-2
-2
-2
-2
-3
-3
-3
-3
-3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3
x x x x
3
3
2
2
1
1
0
0
-1
-1
-1
-1
-2
-2
-2
-2
-3
-3
-3
-3
-3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3 -3 -2 -1 0 1 2 3
FIGURE 5.4: The first row uses the function perspective to represent bivariate normal
densities with parameters μX = μY = 0, σX = σY = 1, and ρ values of 0, 0.30, 0.60,
and 0.95, respectively. The second row represents the same bivariate densities with contour
plots, while the third row represents the densities with image plots.
Example 5.18 Bivariate Normal Grades Let us assume that the distribution
of grades for a particular group of students where X and Y represent the grade point
averages in high school and the first year of college, respectively, follow a bivariate normal
distribution with parameters μX = 3.2, μY = 2.4, σX = 0.4, σY = 0.6, and ρ = 0.6. Find
the following:
Solution: The answers are computed first manually, and then with S.
> pnorm(1.8,2.4,.6)
[1] 0.1586553
σY 0.6
μY |x=2.5 = E(Y |x = 2.5) = μY + ρ (x − μX ) = 2.4 + 0.6 · · (2.5 − 3.2) = 1.77
σX 0.4
σY2 |x=2.5 = σY2 (1 − ρ2 ) = 0.62 · 1 − 0.62 = 0.2304 ⇒ σY |x=2.5 = 0.48
Y −1.77
P(Y < 1.8|X = 2.5) = P 0.48 < 1.8−1.77
0.48 = P(Z < 0.0625) = 0.5249.
> pnorm(1.8,1.77,.48)
[1] 0.5249177
> 1-pnorm(3,2.4,.6)
[1] 0.1586553
> 1-pnorm(3,1.77,.48)
[1] 0.005196079
190 Probability and Statistics with R
5.8 Problems
1. Let X and Y have the following joint distribution:
2. Given the random variables X and Y and their joint probability pX,Y (X, Y ):
Y
1 2 3
1 0.05 0.05 0.1
X 2 0.05 0.1 0.35
3 0 0.2 0.1
(a) Show that pX,Y (X, Y ) satisfies properties (i) and (ii) given on page 171 for the joint
pdf of two discrete random variables.
(b) Find the mean of X and the mean of Y .
(c) Are X and Y independent?
(d) Find the variances of X and of Y .
(e) Find the covariance of X and Y .
3. A particular unfair coin is constructed so that the probability of obtaining a head is 1/3 .
The unfair coin is flipped twice. Define two random variables: Z = the number of heads
in the first flip and W = the number of heads in two flips.
(a) Construct a table showing the joint probability distribution of both random variables
Z and W including the marginal probabilities.
(b) Find the covariance between Z and W . Are they independent?
(c) Suppose the covariance between Z and W were 0. Would this imply that Z and W
are independent?
4. An international travel agency translates its promotional fliers each season. Translators
are hired to translate the fliers into several languages. The translators are paid either e
60 or e 90 per page, depending on word density. The fliers are all either 5, 7, or 10 pages
in length. The joint density function for X and Y , where X = number of pages and Y =
price per page, is
Multivariate Probability Distributions 191
Y
60 90
5 0.05 0.4
X 7 0.05 0.1
10 0.35 0.05
5. A student uses a free dialup service to access the Internet. Depending on the server to
which the Internet service provider connects the student, there are three transmission
rates: 1800, 2700, and 3600 bytes per second. Let X be the number of transmitted bytes
and Y the transmission rate in bytes per second. The joint probability for X and Y is
given by the following table:
Y
1800 2700 3600
64800 0.3 0.05 0.025
X 324000 0.025 0.15 0.15
972000 0 0.2 0.1
(a) Let Z be the random variable indicating the time necessary for transmission. Write
down the probability function of Z.
(b) Find the expected time spent in transmission.
(c) Find the mean and variance of X and Y and Cov (X, Y ).
6. At the local movie theater, drinks and popcorn come in three sizes: small, medium, and
large. The prices for both drinks and popcorn are $1.50, $2.50, and $3.50 for the small,
medium, and large sizes, respectively. For a given customer, define the random variables
X = amount spent for popcorn and Y = amount spent for drinks. Suppose the joint
distribution for X and Y is
X
1.5 2.5 3.5
1.5 0.03 0.07 0.05
Y 2.5 0.08 0.08 0.30
3.5 0.00 0.30 0.09
(a) Find the probability a given customer spends no more than $2.50 on popcorn. What
is the probability a given customer spends at least $2.50 on popcorn?
(b) What is the average amount of money spent at the movies for a customer buying
both popcorn and a drink, if the cost of the movie ticket is $5.20?
7. The interior diameter of a particular type of test tube is a random variable with a mean
of 5 cm and a standard deviation of 0.03 cm. If the test tube thickness is a random
variable with a mean of 0.5 cm and a standard deviation of 0.001 cm and both variables
are independent, find the mean and standard deviation of the exterior diameter.
192 Probability and Statistics with R
8. The flow of water arriving at an irrigation canal is measured in cubic meters and follows
a N (100, 20) distribution. The canal has a flow capacity that follows a N (120, 30)
distribution. The sluice gate is opened when the water flow exceeds the canal’s capacity.
What is the probability that the flood gate will be opened?
find the E[Y | X ] that is the regression line resulting from regressing Y on X.
10. The time, in minutes, that a car is parked in a mall has the following density function:
⎧
⎨ 1 −x/50
f (x) = 50 e x>0
⎩0 x ≤ 0.
Using S,
(a) Find the probability that a car stays more than 1 hour.
(b) Let Y = 0.5 + 0.03X be the cost in dollars that the mall has to pay a security service
per parked car. Find the mean parking cost for 1000 cars.
(c) Find the variance and skewness coefficient of Y .
11. A poker hand (5 cards) is dealt from a single deck of well shuffled cards. If the random
variables X and Y represent the number of aces and the number of kings in a hand,
respectively,
12. If fX,Y (x, y) = 5x − y 2 in the region bounded by y = 0, x = 0, and y = 2 − 2x, find the
density function for the marginal distribution of X, for 0 < x < 1.
13. If f (x, y) = e−(x+y) , x > 0, and y > 0, find P X + 3 > Y X > 13 .
14. If f (x, y) = 1, 0 < x < 1, 0 < y < 1, what is P Y − X > 12 X + Y > 12 ?
15. If f (x, y) = k(y − 2x) is a joint density function over 0 < x < 1, 0 < y < 1, and y > x2 ,
then what is the value of the constant k?
18. Let X and Y denote the weight (in kilograms) and height (in centimeters), respectively, of
20-year-old American males. Assume that X and Y have a bivariate normal distribution
with parameters μX = 82, σX = 9, μY = 190, σY = 10, and ρ = 0.8. Find
(a) E [ Y | X = 75 ],
(b) E [ Y | X = 90 ],
(c) Var [ Y | X = 75 ],
(d) Var [ Y | X = 90 ],
(e) P (Y ≥ 190 | X = 75 ), and
(f) P (185 ≤ Y ≤ 195 | X = 90 ).
19. Let X and Y denote the heart rate (in beats per minute) and average power output (in
watts) for a 10 minute cycling time trial performed by a professional cyclist. Assume
that X and Y have a bivariate normal distribution with parameters μX = 180, σX = 10,
μY = 400, σY = 50, and ρ = 0.9. Find
(a) E [ Y | X = 170 ],
(b) E [ Y | X = 200 ],
(c) Var [ Y | X = 170 ],
(d) Var [ Y | X = 200 ],
(e) P (Y ≤ 380 | X = 170 ), and
(f) P (Y ≥ 450 | X = 200 ).
20. A certain group of college students takes both the Scholastic Aptitude Test (SAT) and
an intelligence quotient (IQ) test. Let X and Y denote the students’ scores on the SAT
and IQ tests, respectively. Assume that X and Y have a bivariate normal distribution
with parameters μX = 980, σX = 126, μY = 117, σY = 7.2, and ρ = 0.58. Find
(a) E [ Y | X = 1350 ],
(b) E [ Y | X = 700 ],
(c) Var [ Y | X = 700 ],
(d) P (Y ≤ 120 | X = 1350 ), and
(e) P (Y ≥ 100 | X = 700 ).
21. A pepper canning company uses tins weighing 20 grams. The full tin of peppers is placed
on a balance. Customer good will is maximized when the balance shows a quantity μ
and the peppers weight is Y grams. If the balance has a random error X ∼ N (0, σ = 10),
(a) Find the relationship between Y , X, and μ.
(b) What is the distribution of Y ?
194 Probability and Statistics with R
(c) Find μ so that 98% of the tins have at least 400 grams of peppers.
(d) Repeat the exercise assuming that the tin weight is a random variable W ∼
N (20, σ = 5).
(a) Show that properties (1) and (2) on page 173 for the joint pdf of two continuous
random variables are satisfied.
(b) Find the cumulative distribution function.
(c) Find the marginal means of X and Y .
(d) Find the marginal variances of X and Y .
23. The lifetime of two electronic components are two random variables, X and Y . Their
joint density function is given by
1 + x + y + cxy
fXY (x, y) = exp(−(x + y)) x ≥ 0 and y ≥ 0
(c + 3)
∞ ∞
(a) Verify that −∞ −∞ fXY (x, y) dx dy = 1.
(b) Find fX (x).
(c) What value of c makes X and Y independent?
24. A high technology company manufactures circular mirrors used in certain satellites. The
radius of any mirror in inches is a random variable R with density function
⎧
⎨ 24 (2r − r2 ) 1 ≤ r ≤ 3
f (r) = 11 2
⎩0 otherwise.
To place the mirrors in the satellites without any problems, the mirror area, given by
πR2 , cannot be greater than 6.5 inches. Using S,
∞
(a) Verify that −∞
f (r) dr = 1.
(b) Find the mean area of the mirrors.
(c) Find the probability that a mirror’s diameter does not surpass 6.5 inches.
25. Use the package adapt from R to solve Example 5.2 on page 173.
29. An engineer has designed a new diesel motor that is used in a prototype vehicle. The
prototype’s diesel consumption in gallons per mile C follows the equation C = 3 + 2X +
3
2 Y , where X is a speed coefficient and Y is the quality diesel coefficient. Suppose the
joint density for X and Y is fXY (x, y) = ky, 0 ≤ x ≤ 2, 0 ≤ y ≤ x.
30. To make porcelain, kaolin X and feldspar Y are needed to create a soft mixture that
later becomes hard. The proportion of these components for every tone of porcelain has
the density function fXY (x, y) = Kx2 y, 0 ≤ x ≤ y ≤ 1, x + y ≤ 1.
31. A device can fail in four different ways with probabilities π1 = 0.2, π2 = 0.1, π3 = 0.4,
and π4 = 0.3. Suppose there are 12 devices that fail independently of one another. What
is the probability of 3 failures of the first kind, 4 of the second, 3 of the third, and 2 of
the fourth?
32. The wait time in minutes a shopper spends in a local supermarket’s checkout line has
distribution f (x) = exp(−x/2)/2 , x > 0. On weekends, however, the wait is longer, and
the distribution then is given by g(x) = exp(−x/3)/3 , x > 0. Find
(a) The probability that the waiting time for a customer will be less than 1 minute.
(b) The probability that, given a waiting time of 2 minutes, it will be a weekend.
196 Probability and Statistics with R
(c) The probability that the customer waits less than 2 minutes.
33. An engineering team has designed a lamp with two light bulbs. Let X be the lifetime
for bulb 1 and Y the lifetime for bulb 2, both in thousands of hours. Suppose that X
and Y are independent and they follow an exp(λ = 1) distribution.
(a) Find the joint density function of X and Y . What is the probability neither bulb
lasts longer than 1000 hours?
(b) If the lamp works when at least one bulb is lit, what is the probability that the lamp
works no more than 2000 hours?
(c) What is the probability that the lamp works between 1000 and 2000 hours?
34. The national weather service has issued a severe weather advisory for a particular county
that indicates that severe thunderstorms will occur between 9 p.m. and 10 p.m. When
the rain starts, the county places a call to the maintenance supervisor who opens the
sluice gate to avoid flooding. Assuming the rain’s start time is uniformly distributed
between 9 p.m. and 10 p.m.
(a) At what time, on the average, will the county maintenance supervisor open the sluice
gate?
(b) What is the probability that the sluice gate will be opened before 9:30 p.m.?
(a) Set the seed equal to 194 ([Link](194)), and use the function mvrnorm() from
the MASS package to simulate the population, assuming the population of interest
consists of 200 students. (Hint: Use empirical=TRUE.)
(b) Compute the means of X and Y . Are they equal to 3.2 and 2.4, respectively?
(c) Compute the variance of X and Y as well as the covariance between X and Y . Are
the values 0.16, 0.36, and 0.144, respectively?
(d) Create a scatterplot of Y versus X. If a different seed value is used, how do the
simulated numbers differ?
stated on page 180 of the text. (Hint: Use moment generating functions.)
Chapter 6
Sampling and Sampling Distributions
6.1 Sampling
The objective of statistical analysis is to gain knowledge about certain properties in a
population that are of interest to the researcher. When the population is small, the best
way to study the population of interest is to study all of the elements in the population
one by one. This process of collecting information on the entire population of interest is
called a census. However, it is usually quite challenging to collect information on an entire
population of interest. Not only do monetary and time constraints prevent a census from
being taken easily, but also the challenges of finding all the members of a population can
make gathering an accurate census all but impossible. Under certain conditions, a random
selection of certain elements actually returns more reliable information than can be obtained
by using a census. Standard methods used to learn about the characteristics of a population
of interest include simulation, designed experiments, and sampling.
Simulation studies typically generate numbers according to a researcher-specified
model. For a simulation study to be successful, the chosen simulation model must closely
follow the real life process the researcher is attempting to simulate. For example, the effects
of natural disasters, such as earthquakes, on buildings and highways are often modeled with
simulation.
When the researcher has the ability to control the research environment, or at least
certain variables of interest in the study, designed experiments are typically employed.
The objective of designed experiments is to gain an understanding about the influence that
various levels of a factor have on the response of a given experiment. For example, an
agricultural researcher may be interested in determining the optimal level of nitrogen when
his company’s fertilizer is used to grow wheat in a particular type of soil. The designed
experiment might consist of applying the company’s fertilizer to similar plots using three
different concentrations of nitrogen in the fertilizer.
Sampling is the most frequently used form of collecting information about a population
of interest. Many forms of sampling exist, such as random sampling, simple random
sampling, systematic sampling, and cluster sampling. It will be assumed that the population
from which one is sampling has size N and that the sample is of size n < N .
Random sampling is the process of selecting n elements from a population where each
of the n elements has the same probability of being selected, namely, N1 . More precisely,
the random variables X1 , X2 , . . . , Xn form a random sample of size n from a population
with a pdf f (x) if X1 , X2 , . . . , Xn are mutually independent random variables such that
the marginal pdf of each Xi is f (x). The statement “X1 , X2 , . . . , Xn are independent and
identically distributed, i.i.d., random variables with pdf f (x)” is often used to denote a
random sample. The objective of random sampling is to obtain a representative sample of
the population that can be used to make generalizations about the population.
This process of making generalizations about the population from sampled information
197
198 Probability and Statistics with R
is called inferential statistics. For the generalizations to be valid, the sample must meet
certain requirements. The key requirement for a random sample is that it be representative
of the parent population from which it was taken.
The typical method of obtaining a random sample starts with using either a calculator or
a computer random number generator to decide which elements of a population to sample.
The numbers returned from random number generating functions are not, in the strictest
sense, random. That is, because an algorithm is used to generate the numbers, they are
not completely random. Depending on the quality or lack thereof for a given random
number generator, the same numbers may begin to cycle after a number of iterations. This
problem is encountered much less with the random number generating functions written
for computers than it is with those for calculators. In general, random number generators
return pseudo-random numbers from a Unif (0,1) distribution. Since people tend to favor
certain numbers, it is best not to allow humans to pick random numbers unless the process
is one of selecting numbers from an urn or another similar process. To avoid possible biases,
it is best to let a function written to generate random numbers pick a sample.
When the population is finite, it is possible to list all of the possible combinations
of samples of size n using the S command [Link](). For example, suppose all
of the combinations of size n = 3 from a population consisting of N = 4 items are to
be listed. Clearly, there are 4 × 4 × 4 = 64 possible combinations. To enumerate the
possible combinations with S, type [Link](1:4,1:4,1:4). In a similar fashion, if
all of the possible combinations from rolling two fair dice or all possible combinations of
size n = 2 from the population X1 = 2, X2 = 5, and X3 = 8 are to be enumerated, type
[Link](1:6,1:6) or [Link](c(2,5,8), c(2,5,8)), respectively.
Example 6.1 Given a population of size N = 5, use S to list all of the possible samples
of size n = 3. That is, list the 53 = 10 possible combinations.
> Combinations(5,3)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1 1 1 2 1 1 2 1 2 3
N 2 2 3 3 2 3 3 4 4 4
N 3 4 4 4 5 5 5 5 5 5
Sampling and Sampling Distributions 199
The 10 possible combinations are (1, 2, 3), (1, 2, 4), . . . , (3, 4, 5), listed vertically in the out-
put.
Example 6.1 on the facing page assumed all of the values in the population of interest are
sequential starting with the number one. It is not unusual to have non-sequential values for
the population where the user desires to enumerate all possible combinations when sampling
without replacement. To that end, code is provided (SRS()) that works in conjunction with
Combinations() to list all of the possible combinations when using simple random sampling
from a finite population:
> SRS <- function(POPvalues, n)
{ # SRS generates all possible SRS’s of size n
# from the population in vector POPvalues
# by calling the function Combinations.
N <- length(POPvalues)
store <- t(Combinations(N, n))
matrix(POPvalues[t(store)], nrow = nrow(store), byrow = TRUE) }
Example 6.2 Given a population of size N = 5, where X1 = 2, X2 = 5, X3 = 8, X 4= 12,
and X5 = 13, use S to list all of the possible samples of size n = 3. That is, list the 53 = 10
possible combinations.
Solution: First, make sure both the functions Combinations() and SRS() are stored on
your computer by loading the PASWR package. Then, use the command SRS() as follows:
> t(SRS(c(2,5,8,12,13), 3))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2 2 2 5 2 2 5 2 5 8
[2,] 5 5 8 8 5 8 8 12 12 12
[3,] 8 12 12 12 13 13 13 13 13 13
The 10 possible combinations are (2, 5, 8), (2, 5, 12), . . . , (8, 12, 13), listed vertically in the
output. The S command t() was used to transpose the data to conserve space. It is not
obligatory to transpose the output; it is just as valid to type SRS(c(2,5,8,12,13),3) so
that the samples are listed across the rows instead of down the columns.
Example 6.3 A teacher wants an algorithm that will randomly select 5 students from a
large lecture section of 180 students to present their work at the board.
Solution: Assume the students in the class are numbered from 1 to 180 according to
the class roll and that the students know their numbers. Then, an unbiased procedure for
selecting 5 students starts with using the following S code to determine which students
should be in the sample:
> sample(1:180, 5, replace=FALSE)
[1] 138 52 135 58 160
Example 6.4 Randomly select 5 people from a group of 20 where the individuals are
labeled from 1 to 20 and the individuals labeled 19 and 20 are four times more likely to be
selected than the individuals labeled 1 through 18.
Solution: An unbiased procedure to select 5 people starts with using the following S code
to determine which people will be in the sample:
> sample(x=(1:20), size=5, prob=c(rep(1/26,18), rep(4/26,2)), replace=FALSE)
[1] 20 19 1 17 16
200 Probability and Statistics with R
1. In a study of the eating habits of a certain species, geographical areas often form
natural strata.
3. The Internal Revenue Service (IRS) might audit tax returns based on the reported
taxable income by creating three groups: returns with reported taxable income less
than $ 50,000; returns with reported income less than $75,000 but more than $ 50,000;
and returns with reported taxable income of more than $ 75,000.
In addition to taking random samples within the strata, stratified samples are typically
proportional to the size of their strata or proportional to the variability of the strata.
Example 6.5 A botanist wants to study the characteristics of a common weed and its
adaptation to various geographical regions on a remote island. The island has well-defined
strata that can be classified as dessert, forest, mountains, and swamp. If 5000 acres of the
island are desert, 1000 acres are forest, 500 acres are mountains, and 3500 acres are swamp,
and the botanist wants to sample 5% of the population using a stratified sampling scheme
that is proportional to the strata, how many acres of each of the four regions will he have
to sample?
Solution: Since the size of the island is 10,000 acres, the botanist will need to sample a
5000
total of 10000×0.05 = 500 acres. The breakdown of the 500 acres is as follows: 500× 10000 =
250 desert acres; 500 × 10000 = 50 forest acres; 500 × 10000 = 25 mountain acres; and
1000 500
500 × 10000
3500
= 175 swamp acres.
example, suppose a systematic sample is desired where 1 in k = 100 members is chosen from
a list containing 1000 members. That is, every 100th member of the list is to be sampled.
To pick the initial starting point, select a number at random between 1 and 100. If the
random number generated is 53, then the researcher simply samples the values numbered
53, 153, 253, . . . , 953 from the master list. The following S code generates the locations to
be sampled using a 1 in 100 systematic sampling strategy:
> seq(sample(1:100,1), 1000, 100)
[1] 53 153 253 353 453 553 653 753 853 953
Example 6.6 Produce a list of locations to sample for a systematic sample if N = 1000
and n = 20.
Solution: To take a systematic sample, every k = 1000 20 = 50
th
item will be observed.
To start the process, select a random number between 1 and 50 using a random number
generator. The following S code can be used to select a 1 in 50 systematic sample when
N = 1000 and k = 50:
> seq(sample(1:50,1), 1000, 50)
[1] 27 77 127 ... 977
6.2 Parameters
Once a sample is taken, the primary objective becomes to extract the maximum and
most precise information as possible about the population from the sample. Specifically, the
researcher is interested in learning as much as possible about the population’s parameters.
A parameter, θ, is a function of the probability distribution, F . That is, θ = t(F ), where
t(·) denotes the function applied to F . Each θ is obtained by applying some numerical
procedure t(·) to the probability distribution function F . Although F has been used to
denote the cdf exclusively until now, a more general definition of F is any description of
X’s probabilities. Note that the cdf, P(X ≤ x), is included in this more general definition.
202 Probability and Statistics with R
Parameters are what characterize probability distributions. More to the point, parameters
are inherent in all probability models, and it is impossible to compute a probability without
prior knowledge of the distribution’s parameters. Parameters are treated as constants in
classical statistics and as random variables in Bayesian statistics. In everything that follows,
parameters are treated as constants.
N
(Xi − μf )2
σf2 ;N = i=1
N
Variance(N ) N
1 2
= Xi2 − (μf )
N i=1
N 2
(Xi − μf )
Variance (N − 1) σf2 ;N −1 = i=1
N −1
πf represents the proportion of
Variance elements in the population with a
σf2 = πf (1 − πf )
(dichotomous) common characteristic
9
N 2
Standard i=1 (Xi − μf )
Deviation σf =
N
6.3 Estimators
Population parameters are generally unknown. Consequently, one of the first tasks is to
estimate the unknown parameters using sample data. Estimates of the unknown parameters
are computed with estimators or statistics. An estimator is a function of the sample,
while an estimate (a number) is the realized value of an estimator that is obtained when a
sample is actually taken. Given a random sample, {X1 , X2 , . . . , Xn } = X, from a probability
distribution F , a statistic, any function of the sample is denoted as T = t(X). Note that
the estimator T of θ will at times also be denoted θ̂. Since a statistic is a function of the
random variables X, it follows that statistics are also random variables. The specific value
of a statistic can only be known after a sample has been taken. The resulting number,
computed from a statistic, is called an estimate. For example, the arithmetic mean of a
sample n
Xi
T = t(X) = X = i=1 , (6.1)
n
is a statistic (estimator) constructed from a random sample {X1 , . . . , Xn }.
204 Probability and Statistics with R
Until a sample is taken, the value of the statistic (the estimate) is unknown. Suppose a
random sample has been taken that contains the following values: x = {3, 5, 6, 1, 2, 7}. It
follows that the value of the statistic T = t(X), where t(X) is defined in (6.1) as t = t(x) =
3+5+6+1+2+7
6 = 4. The quantity t(X) = X1 ×X 6
2
is also a statistic; however, it does not have
the same properties as the arithmetic mean defined in (6.1).
The essential distinction between parameters and estimators is that a parameter is
a constant in classical statistics while an estimator is a random variable, since its value
changes from sample to sample. Parameters are typically designated with lowercase Greek
letters, while estimators are typically denoted with lowercase Latin letters. However, when
working with finite populations, it is standard notation to use different capital Latin letters
to denote both parameters and estimators. At times, it is also common to denote an
estimator by placing a hat over a parameter such as β̂1 . Some common parameters and
their corresponding estimators are provided in Table 6.2.
Some of the statistics used to estimate parameters when sampling from a finite popu-
lation are given in Table 6.3 while the more common statistics used when working with a
random sample of size n are given in Table 6.4 on the facing page.
Table 6.3: Finite population parameter estimators and their standard errors
Parameter Estimator σ̂estimator
n
Xi .
S N −n
Population Mean X f = i=1 √
n n N
.
S N −n
Population Total Tf = N X f √ ·N ·
n N
9
Y P (1 − P ) N − n
Population Proportion P =
n n−1 N
Here, I {xi ≤ t} is the indicator function that returns a value of 1 when xi ≤ t and 0 when
xi > t.
Sampling and Sampling Distributions 205
9 2
n
Standard i=1 Xi − X
Deviation S=
n−1
Example 6.8 Simulate rolling a die 100 times and compute the epdf. Graph the ecdf.
Solution: The R code to solve the problem is
where the output following table(rolls)/100 is the empirical distribution function. The
graph of the realized ecdf is found in Figure 6.1 on the next page.
206 Probability and Statistics with R
ecdf(rolls)
1.0
0.8
0.6
Fn(x)
0.4
0.2
0.0
0 1 2 3 4 5 6 7
FIGURE 6.1: Empirical cumulative distribution function of rolling a die 100 times
Example 6.9 What are the plug-in estimators of (a) the expected value and (b) the
variance of a discrete distribution F ?
Solution: The answers are as follows:
(a) When the expected value is θ = EF (X), the plug-in estimator of the expected value is
n
θ̂ = EF: (X) = i=1 Xi · n1 = X.
(b) When the variance is θ = Var F (X) = EF (X− μ)2 , the plug-in estimator of the variance
n
of X is θ̂ = EF: (X − X )2 = i=1 (Xi − X )2 · n1 .
Clearly, as the sample size increases, the variance of the sampling distribution of X de-
creases.
There are 36 possible samples of size 2 from this distribution listed in Table 6.5 on the
next page. Using the fact that each of samples listed in Table 6.5 is equally likely (1/36),
construct both the sampling distribution of X given in Table 6.6 on the following page and
the sampling distribution of S 2 given in Table 6.7 on the next page.
The mean of the sampling distribution, μX = E X , and the variance of the sampling
2
distribution, σX = E[X − μX ]2 , are
1 2 1
μX = E X = 1 × + 1.5 × + ···+ 6 × = 3.5
36 36 36
and
1 2
2
σX = E (X − μX )2 = (1 − 3.5)2 × + (1.5 − 3.5)2 × +
36 36
1
· · · + (6 − 3.5)2 × = 1.4583.
36
208 Probability and Statistics with R
Table 6.5: Possible samples of size 2 with x̄ and s2 for each sample – random sampling
(x1 ,x2 ) x̄ s2 (x1 ,x2 ) x̄ s2
(1,1) 1.0 0.0 (4,1) 2.5 4.5
(1,2) 1.5 0.5 (4,2) 3.0 2.0
(1,3) 2.0 2.0 (4,3) 3.5 0.5
(1,4) 2.5 4.5 (4,4) 4.0 0.0
(1,5) 3.0 8.0 (4,5) 4.5 0.5
(1,6) 3.5 12.5 (4,6) 5.0 2.0
(2,1) 1.5 0.5 (5,1) 3.0 8.0
(2,2) 2.0 0.0 (5,2) 3.5 4.5
(2,3) 2.5 0.5 (5,3) 4.0 2.0
(2,4) 3.0 2.0 (5,4) 4.5 0.5
(2,5) 3.5 4.5 (5,5) 5.0 0.0
(2,6) 4.0 8.0 (5,6) 5.5 0.5
(3,1) 2.0 2.0 (6,1) 3.5 12.5
(3,2) 2.5 0.5 (6,2) 4.0 8.0
(3,3) 3.0 0.0 (6,3) 4.5 4.5
(3,4) 3.5 0.5 (6,4) 5.0 2.0
(3,5) 4.0 2.0 (6,5) 5.5 0.5
(3,6) 4.5 4.5 (6,6) 6.0 0.0
2
Note that the computed values of E X and σX are in agreement with the formulas
σ2
E X = μ and σX = n given in (6.3) and (6.4). Also note that E S 2 = σ 2 . Specifically,
2
6 10 2
E S2 = 0 × + 0.5 × + · · · + 12.5 × = 2.9166.
36 36 36
Case 2 When the sampling is performed without replacement, the outcomes can be viewed
as a simple random sample of size 2 drawn from a discrete uniform distribution. Note
Sampling and Sampling Distributions 209
that fewer samples exist when sampling without replacement 62 = 15 , but that each
sample is equally likely to be drawn. The 15 possible samples of size 2 from this distribution
are listed in Table 6.8. Using the fact that each of the samples listed in Table 6.8 is equally
likely (1/15), construct the sampling distribution of X given in Table 6.9, and the sampling
distribution of S 2 given in Table 6.10 both on on the current page.
Table 6.8: Possible samples of size 2 with x̄ and s2 – simple random sampling
(x1 ,x2 ) x̄ s2
(1,2) 1.5 0.5
(1,3) 2 2.0
(1,4) 2.5 4.5
(1,5) 3 8.0
(1,6) 3.5 12.5
(2,3) 2.5 0.5
(2,4) 3 2.0
(2,5) 3.5 4.5
(2,6) 4 8.0
(3,4) 3.5 0.5
(3,5) 4 2.0
(3,6) 4.5 4.5
(4,5) 4.5 0.5
(4,6) 5 2.0
(5,6) 5.5 0.5
1 1 1
μX = E X = 1.5 × +2× + · · · + 5.5 × = 3.5,
15 15 15
1 1
σX2
= E (X − μX )2 = (1.5 − 3.5)2 × + (2 − 3.5)2 × +
15 15
1
· · · + (5.5 − 3.5)2 × = 1.16666,
15
5 4 1
and E S 2 = 0.5 × +2× + · · · + 12.5 × = 3.5.
15 15 15
Remarkably, the sample mean is identical when sampling with and without replacement.
In fact, the expected value of the sample mean is μ whether sampling with or without
replacement. The variance of the sample mean and the expected value of the sample
variance have changed, however. These changes are due to the fact that sampling is from a
finite population without replacement. A summary of the formulas used to compute these
results is found in Table 6.11.
Table 6.11: Summary results for sampling without replacement (finite population)
μX = μf
σ2 N − n
2
σX = ·
n N −1
N
E S2 = · σ2
N −1
N n−1 2
E Su2 = · ·σ
N −1 n
2
−n
Note that the computed values of E X = μf = 3.5, σX 2
= σn · N
N −1 = · 6−1 = 1.1666,
2.9166 6−2
2 N 2 6
2
and E S = N −1 σ = 5 (2.9166) = 3.5 for this example are in agreement with the formulas
for sampling without replacement given in Table 6.11. A comparison of the results from
Case 1 and Case 2 can be found in Table 6.12.
Table 6.12: Computed values for random sampling (Case 1) and simple random sampling
(Case 2)
μ E X σ2 E S2 2
σX
Graphical comparisons for the sampling distributions of X and S 2 when sampling with
replacement (random sampling) and when sampling without replacement (simple random
sampling) are depicted in Figure 6.2 on the next page. The following S code can be used
to verify all the results in this solution:
> N <- 6
> n <- 2
> pop <- 1:N
> rs <- [Link](Draw1=pop, Draw2=pop) # Possible random samples
> xbarN <- apply(rs, 1, mean) # Means of all rs values
> s2N <- apply(rs, 1, var) # Variance of all rs values
> TOT1<- cbind(rs, xbarN=xbarN, s2N=s2N)
6/36
5/36 10/36
2 8/36
f (x̄) 4/36 f (s )
3/36 6/36
2/36 4/36
1/36 2/36
0 0
1 2 3 4 5 6 0 2 4 6 8 10 12
x̄ s2
2/15 4/15
1 2 3 4 5 6 0 2 4 6 8 10 12
x̄ s2
FIGURE 6.2: Sampling distributions of X and S 2 under random sampling (RS) and simple
random sampling (SRS) for Example 6.10 on page 207 are given. Note that the dispersion
for the sampling distribution of X is smaller under Case 2 than it is with Case 1.
the underlying population. So, the mean and variance of the sampling distribution of X
are always known. However, it is not always true that the resulting sampling distribution
of X is known. If X ∼ N (μ, σ), then X ∼ N (μ, √σn ).
Example 6.11 If X ∼ N (μ, 12), find the required sample size to guarantee |X − μ| < 3
with a probability of 0.95.
Solution: Changing the prose into a mathematical statement,
P X − μ < 3 = 0.95
needs to be solved.
Since X ∼ N (μ, σ = 12), it follows that
σ 12
X ∼ N μ, σX = √ = √ .
n n
Consequently,
X − μ
P √ < 1.96 = 0.95.
σ/ n
Multiplying both sides by √σ and substituting 12 for σ gives
n
12
P X − μ < (1.96) √ = 0.95.
n
√
Multiplying both sides by n, dividing both sides by 3, and finally squaring both sides, gives
n = 61.47. Consequently, a sample size of at least 62 is needed to guarantee X − μ < 3
with a probability of 0.95.
Example 6.12 A small town in the Pyrenean mountains wants to reduce the bear pop-
ulation because several sheep have recently been killed by bears. Three autonomous com-
munities (Cataluña, Aragón, and Navarra) have made bids to remove 10 bears. The three
autonomous communities indicated in their bids that they are willing to spend 5, 7.5, and
10 thousand dollars per bear to capture the bears. Decide which autonomous communities
can capture 10 bears with a probability of at least 0.999 knowing that the cost to capture
a bear follows a normal distribution with a mean of 5 thousand dollars and a standard
deviation of 0.6 thousand dollars.
Solution: Assuming that the costs to capture the bears act as independent random
variables, such that if Xi is the cost to capture one bear, the total cost to capture 10 bears
is also a random variable, given by Y = X1 + · · · + X10 . Since Xi ∼ N (5, 0.6), it follows
using Theorem 5.1 onpage 176 that the mean of Y will be 5 · 10 = 50 and the standard
deviation of Y will be 10 · (0.6)2 = 1.897367. Mathematically, write Y ∼ N (50, 1.897367).
Cataluña will be able to capture 10 bears provided Y ≤ 50, Aragón will be able to capture
10 bears provided Y ≤ 75, and Navarra will be able to capture 10 bears provided Y ≤ 100.
The probabilities of these events are
50 − 50
P(Y ≤ 50) = P Z ≤ = P(Z ≤ 0) = 0.5,
1.897367
75 − 50
P(Y ≤ 75) = P Z ≤ = P(Z ≤ 13.17616) = 1,
1.897367
100 − 50
and P(Y ≤ 100) = P Z ≤ = P(Z ≤ 26.35231) = 1.
1.897367
214 Probability and Statistics with R
> pnorm(50,50,1.897367)
[1] 0.5
> pnorm(75,50,1.897367)
[1] 1
> pnorm(100,50,1.897367)
[1] 1
There is only a 50% chance that the Catalan bid would provide sufficient funds to catch
10 bears. On the other hand, the bids from Navarra and Aragón would both have a 100%
chance of catching all 10 bears.
(a) The maximum error for measuring a zipper a single time with 0.95 probability.
(b) The maximum error of the mean measurement of the zipper with 0.95 probability if it
is measured 10 times.
(c) The number of times one needs to measure a zipper to ensure the maximum measure-
ment error of the mean is less than 1 millimeter with 0.95 probability.
X
P(−1.96 < < 1.96) = 0.95.
2
Basic algebra then gives
|X| < 2(1.96) = 3.92.
(b) In this question, the distribution
√ of X is no longer the focus, but rather the distribution
of X is. Since X ∼ N 0, 2/ 10 , it follows that the maximum error committed when
measuring a zipper 10 times is
X = √2 (1.96) = 1.24.
10
√
(c) Since X ∼ N (0, 2/ n ), it follows that
X = √2 (1.96) ≤ 1
n
must be solved for n. The solution is n ≥ (3.92)2 = 15.36. In other words, at least 16
zippers must be measured to ensure the maximum measurement error of the mean is no
more than 1 millimeter with 0.95 probability.
Sampling and Sampling Distributions 215
0.30
0.30
0.20
0.20
0.20
0.10
0.10
0.10
0.0
0.0
0.0
-2 0 2 4 6 8 10 12 -2 0 2 4 6 8 10 12 -2 0 2 4 6 8 10 12
FIGURE 6.3: The far left graph depicts a Unif (0, 10) distribution. The middle graph
depicts the theoretical sampling distribution of X for samples of size n = 2 when the
samples are drawn from a Unif (0, 10) distribution. The far left graph depicts a N (5, 2.0412)
distribution overlayed with the theoretical distribution of X for samples of size n = 2 when
the samples are drawn from a Unif (0, 10) distribution.
In what follows, the various graphs depicted in Figure 6.4 on the next page and Figure 6.5
on page 217 are examined to gain insight into how large the sample size, n, needs to be when
working with both symmetric distributions and skewed distributions such as the uniform
216 Probability and Statistics with R
distribution and the exponential distribution, respectively. S is used to simulate m = 50, 000
samples of sizes n = 2, 16, 36, and 100 from a Unif (−3.66025, 13.66025) distribution and
an Exp(5) distribution. Note that both the means and standard deviations are 5 and 5 for
these distributions.
Figure 6.4 depicts the simulated sampling distribution of X for samples of sizes n = 2 and
16 when one samples from a Unif (−3.66025, 13.66025) distribution and an Exp(5) distribu-
tion, respectively. Figure 6.5 on the facing page depicts the simulated sampling distribution
of X for samples of sizes n = 36 and 100 when sampling from a Unif (−3.66025, 13.66025)
distribution and an Exp(5) distribution, respectively. What should become evident from
looking at Figures 6.4 and 6.5 is that the sampling distribution of X when sampling from a
uniform distribution becomes approximately normal much sooner than does the sampling
distribution of X when sampling from an exponential distribution.
In addition to assessing the simulated sampling distributions of X graphically by su-
perimposing a normal density with mean and standard deviation equal to the mean and
standard deviation of the sampling distribution of X as shown in Figures 6.4 and 6.5,
Table 6.13 on the next page is provided which contains the percent of the simulated sampling
distribution of X that falls within (−∞, μX − 2σX ], (μX − 2σX , μX − σX ], (μX − σX , μX ],
(μX , μX + σX ], (μX + σX , μX + 2σX ], and (μX + 2σX , ∞] for sample sizes n = 2, 16,
36, and 100 when sampling from a Unif (−3.66025, 13.66025) distribution and an Exp(5)
distribution. By studying the percentages from the simulations in Table 6.13 on the facing
page, one can see that the simulated sampling distribution of X when sampling from an
exponential distribution is still slightly skewed even for sample sizes as large as n = 100.
To verify the numbers presented in Table 6.13 on the next page and to create graphs
similar to those in Figures 6.4 and 6.5, the user can use the code n2UNIFsim provided at
[Link] arnholta/PASWR in the Chapter 6 script.
Simulation 1 Simulation 2
Density
Density
0.10
0.10
0.00
0.00
−10 −5 0 5 10 15 20 −10 −5 0 5 10 15 20
x̄ x̄
Simulation 3 Simulation 4
0.4
0.4
Density
Density
0.2
0.2
0.0
0.0
0 2 4 6 8 10 0 2 4 6 8 10
x̄ x̄
FIGURE 6.4: Simulation 1 depicts the simulated sampling distribution of X for samples
of size n = 2 that are selected from a Unif (−3.66025, 13.66025) distribution. Simulation 2
depicts the simulated sampling distribution of X for samples of size n = 2 that are selected
from an Exp(5) distribution. Simulation 3 depicts the simulated sampling distribution of X
for samples of size n = 16 that are selected from a Unif (−3.66025, 13.66025) distribution.
Simulation 4 depicts the simulated sampling distribution of X for samples of size n = 16
that are selected from an Exp(5).
Sampling and Sampling Distributions 217
Simulation 5 Simulation 6
0.6
0.6
Density
Density
0.3
0.3
0.0
0.0
2 4 6 8 2 4 6 8
x̄ x̄
Simulation 7 Simulation 8
1.2
1.2
Density
Density
0.6
0.6
0.0
0.0
3 4 5 6 7 3 4 5 6 7
x̄ x̄
FIGURE 6.5: Simulation 5 depicts the simulated sampling distribution of X for samples of
size n = 36 that are selected from a Unif (−3.66025, 13.66025) distribution. Simulation 6
depicts the simulated sampling distribution of X for samples of size n = 36 that are selected
from an Exp(5) distribution. Simulation 7 depicts the simulated sampling distribution of X
for samples of size n = 100 that are selected from a Unif (−3.66025, 13.66025) distribution.
Simulation 8 depicts the simulated sampling distribution of X for samples of size n = 100
that are selected from an Exp(5).
Table 6.13: Comparison of simulated uniform and exponential distributions to the normal
distribution, Int1 = (−∞, μX − 2σX ], Int2 = (μX − 2σX , μX − σX ], Int3 = (μX − σX , μX ],
Int4 = (μX , μX + σX ], Int5 = (μX + σX , μX + 2σX ], Int6 = (μX + 2σX , ∞]
Example 6.14 Suppose that the shelf life, the number of days a product is on a store’s
shelf, for 1-gallon cartons of milk is a random variable with a Unif [1, 7] distribution. If a
store puts out 100 cartons of 1-gallon of milk for sale, find the probability that the average
number of days the cartons remain on the shelf exceeds 4.5 days.
Solution: Let the random variable X represent the number of days a 1 gallon carton of
milk is on a store’s shelf. Since X ∼ Unif [1, 7], using the equations from (4.9), the pdf of
X can be written as
1
f (x) = if x ∈ [1, 7],
6
and the mean and variance of X as
a+b 1+7 (b − a)2 (7 − 1)2
E[X] = = = 4, and Var[X] = = = 3.
2 2 12 12
Let Xi , i = 1, . . . , 100, represent the actual times cartons of milk remain on the store’s shelf.
Since
Example 6.15 A building contractor provides a detailed estimate of his charges by listing
the price of all of his material and labor charges to the nearest dollar. Suppose the rounding
charge errors can be treated as independent random variables following Unif [−10, 10]
distributions. If a recent estimate from the building contractor listed 100 charges, find
the maximum error for the contractor’s estimate with probability of 0.95.
Solution: Using the equations from (4.9), if ei , i = 1, . . . , 100 are the estimate errors, then
400
(b−a)2
E[ei ] = b+a
2 = 0 and Var[ei ] = 12 = 400 2
12 . It follows then that μē = 0 and σē = 100 = 3 .
12 1
Because of the relatively large (n = 100) sample size, the Central Limit Theorem tells 2us
1
that the distribution of ē is approximately normal with mean 0 and standard deviation 3.
Sampling and Sampling Distributions 219
Since the absolute error of the sum of the 100 charges is the sum of each one of the rounded
errors, e = e1 + · · · + e100 , e = ē · n. Written mathematically,
⎛ ⎞
ē − 0
P ⎝−1.96 < 2 < 1.96⎠ = 0.95.
1
3
2
Multiplying by n = 100 and 13 gives a probability expression for e:
. .
1 1
P · 100 · (−1.96) < e < · 100 · (1.96) = 0.95
3 3
2
From the last expression, note that the maximum error for the estimate eMax , is 13 · 100 ·
(1.96) = 113.1607. In other words, the final job will not deviate more than 113 dollars from
the original estimate with 95% confidence.
Proof: Using the results from Theorem 5.1 on page 176 and letting X1 = X, X2 = Y ,
a1 = 1, and a2 = −1, obtain
⎛ 9 ⎞
σ 2 σ 2
X − Y ∼ N ⎝μX − μY , X
+ Y ⎠. (6.5)
nX nY
Solution: In the S code that follows, m represents the number of samples, and nx, mux,
sigx, ny, muy, sigy, muxy, meansX, meansY, and XY represent nX , μX , σX , nY , μY , σY ,
μX − μY , X, Y , and X − Y , respectively. The [Link]() command is used so the same
values can be generated at a later
date.
Before running the simulation, note √ that
the
theoretical distribution X − Y ∼ N 100 − 50 = 50, 10 2 /100 + 92 /81 =
2 . The
probability histogram for the empirical distribution of X − Y is shown in Figure
6.6 on
the next page. Note that the empirical mean and standard deviation for X − Y are 50.01
√
to the theoretical values of 50 and 2 ≈ 1.41.
and 1.44, respectively, whichare very close
The
empirical
probability P X − Y < 52 is computed by determining the proportion of
X − Y values that are less than 52. Note that the empirical answer for P X − Y < 52
is 0.918, which is in agreement with the theoretical answer to two decimal places.
> [Link](4)
> m <- 1000
> nx <- 100
> ny <- 81
> mux <- 100
> sigx <- 10
> muy <- 50
> sigy <- 9
> muxy <- mux - muy
> sigxy <- sqrt((sigx^2/nx) + (sigy^2/ny))
> meansX <- array(0, m) # Array of m zeros
> meansY <- array(0, m) # Array of m zeros
> for(i in 1:m) {meansX[i] <- mean(rnorm(nx, mux, sigx))}
> for(i in 1:m) {meansY[i] <- mean(rnorm(ny, muy, sigy))}
> XY <- meansX - meansY
> ll <- muxy - 3.4 * sigxy
> ul <- muxy + 3.4 * sigxy
> hist(XY, prob = TRUE, xlab = "xbar-ybar", nclass = "scott", col = 13,
+ xlim = c(ll, ul), ylim = c(0, 0.3), main="", ylab="")
> lines(seq(ll, ul, 0.05), dnorm(seq(ll, ul, 0.05), muxy, sigxy), lwd = 3)
> print(round(c(mean(XY), sqrt(var(XY))), 2))
[1] 50.01 1.44
> sum(XY < 52)/1000
[1] 0.918
> round(pnorm(52, 50, sqrt(2)), 2)
[1] 0.92
E[P ] = μP = π (6.7)
Sampling and Sampling Distributions 221
X−Y
FIGURE 6.6: Probability histogram for simulated distribution of X−Y with
√
superimposed normal density with μ = 50 and σ = 2.
and
π(1 − π)
. Var[P ] = σP2 = (6.8)
n
Equations (6.7) and (6.8) are easily derivable using the mean and variance of Y . Since
Example 6.17 In plain variety M&M candies, the percentage of green candies is 10%.
Suppose a large bag of M&M candies contains 500 candies. What is the probability there
will be
Solution: First, note that the population proportion of green M&Ms is π = 0.10. Since
neither n × π = 400 × 0.10 = 40 nor n × (1 − π) = 400 × 0.90 = 360 is less than 5, it seems
reasonable to appeal to the Central Limit Theorem for the approximate distribution of P .
Consequently,
.
q π(1 − π)
P ∼ N π, ,
n
If the random variable Y is equal to the number of green M&Ms, then the distribution of
Y can be approximated by
& '
q
Y ∼ N nπ, nπ(1 − π) ,
It is also possible to give the exact distribution of Y , which is Y ∼ Bin(n = 500, π = 0.10).
(a) The probabilities that at least 11% of the candies will be green M&Ms using the
approximate distribution of P , the approximate distribution of Y , and finally using the
exact distribution of Y are as follows:
P −π 0.11 − π 0.11 − 0.10
P(P ≥ 0.11) = P ≥ ≈P Z≥
σP σP 0.01341641
= P(Z ≥ 0.745356) = 0.2280283
Y − nπ 55 − nπ 55 − 50
P(Y ≥ 55) = P > ≈P Z≥
nπ(1 − π) nπ(1 − π) 6.708204
= P(Z ≥ 0.745356) = 0.2280283
500
500
P(Y ≥ 55) = (0.10)i (0.90)500−i = 0.2476933
i=55
i
(b) The probability that no more than 12% of the candies will be green M&Ms is
Sampling and Sampling Distributions 223
P −π 0.12 − π 0.12 − 0.10
P(P ≤ 0.12) = P ≤ ≈P Z≤
σP σP 0.01341641
= P(Z ≤ 1.490712) = 0.9319814
Y − nπ 60 − nπ 60 − 50
P(Y ≤ 60) = P > ≈P Z≤
nπ(1 − π) nπ(1 − π) 6.708204
= P(Z ≤ 1.490712) = 0.9319814
60
500
P(Y ≤ 60) = (0.10)i (0.90)500−i = 0.9381745.
i=0
i
The following S commands compute the answers for (a) and (b):
The astute observer will notice that the approximations are not equal to the exact answers.
This is due to the fact that a continuous distribution has been used to approximate a
discrete distribution. The accuracy of the answers can be improved by applying what is
called a continuity correction. Using the continuity correction, (6.9) and (6.10) become
P± 0.5
−π q
Z= 2 n
∼ N (0, 1) (6.11)
π(1−π)
n
and
Y ± 0.5 − nπ q
Z= ∼ N (0, 1). (6.12)
nπ(1 − π)
When solving less than or equal type inequalities, add the continuity correction; and
when solving greater than or equal type inequalities, subtract the continuity correction.
Notice how much closer the approximations are to the exact answers when the appropriate
continuity corrections are applied:
224 Probability and Statistics with R
P− 0.5
0.11 − 500
−π 0.5
−π
P(P ≥ 0.11) = P ≥
500
σP σP
0.11 − 500
0.5
− 0.10
≈P Z ≥
0.01341641
= P(Z ≥ 0.6708204) = 0.2511675
Y − 0.5 − nπ 55 − 0.5 − nπ
P(Y ≥ 55) = P >
nπ(1 − π) nπ(1 − π)
55 − 0.5 − 50
≈P Z ≥
6.708204
= P(Z ≥ 0.6708204) = 0.2511675
500
500
P(Y ≥ 55) = (0.10)i (0.90)500−i = 0.2476933
i=55
i
P+ 0.5
−π 0.5
0.12 + 500 −π
P(P ≤ 0.12) = P 500
≤
σP σP
0.12 + 500 − 0.10
0.5
≈P Z≤
0.01341641
= P(Z ≤ 1.565248) = 0.9412376
Y + 0.5 − nπ 60 + 0.5 − nπ
P(Y ≤ 60) = P >
nπ(1 − π) nπ(1 − π)
60 + 0.5 − 50
≈P Z≤
6.708204
= P(Z ≤ 1.565248) = 0.9412376
60
500
P(Y ≤ 60) = (0.10)i (0.90)500−i = 0.9381745
i=0
i
Example 6.18 The 1999 North Carolina Department of Public Instruction, NC Youth
Tobacco Use Survey, reported that 38.3% of all North Carolina high school students used
tobacco products. If a random sample of 250 North Carolina high school students is taken,
find the probability that the sample proportion that use tobacco products will be between
0.36 and 0.40 inclusive.
Solution: Since neither n×π = 250×0.383 = 95.75 nor n×(1−π) = 250×0.617 = 154.25 is
less than 5, it seems reasonable to appeal to the Central Limit Theorem for the approximate
distribution of P . Consequently,
.
q π(1 − π)
P ∼ N π, ,
n
Due to the discrete nature of the problem, appropriate continuity corrections should be
used:
0.5 0.5
P .36 − ≤ P ≤ .40 + = P(.358 ≤ P ≤ .402) = 0.5236417
250 250
To calculate P(0.358 ≤ P ≤ 0.402) with S, use pnorm():
> sig <- sqrt((0.383*0.617)/250)
> pnorm(0.402,0.383, sig) - pnorm(0.358,0.383, sig)
[1] 0.5236417
The exact answer to the problem can be solved using the binomial distribution as follows:
> pbinom(100,250,.383) - pbinom(89,250,.383)
[1] 0.5241166
1 n % 2 (
E Su2 = E Xi − X . (6.13)
n i=1
Expanding the right-hand side of (6.13) gives
n
2 n
2
Xi − X = (Xi − μ) + μ − X
i=1 i=1
n % 2 (
= (Xi − μ)2 + 2 μ − X (Xi − μ) + μ − X
i=1
n
n
2
= (Xi − μ) + 2 μ − X
2
(Xi − μ) + n μ − X
i=1 i=1
(6.14)
n
2
= (Xi − μ) + 2 μ − X nX − nμ + n μ − X
2
i=1
n
2 2
= (Xi − μ)2 − 2n μ − X + n μ − X
i=1
n
2
= (Xi − μ)2 − n μ − X .
i=1
n 2 n 2
i=1 (Xi −μ) −n μ − X for i=1 Xi − X in (6.13) gives
2
Substituting the expression
# n $
1 2
E Su = E 2
(Xi − μ)2 − n μ − X
n i=1
2 1 σ2
E Su = nσ − n
2
n n (6.15)
σ 2
E Su2 = σ 2 −
n
n−1
= σ2 .
n
226 Probability and Statistics with R
As (6.15) shows, the expected value of Su2 , σ 2 n−1
n , is less than σ 2 . However, as n increases,
this difference diminishes. The variance for the uncorrected variance Su2 , is given by
μ4 − μ22 2(μ4 − 2μ22 ) μ4 − 3μ22
Var Su2 = − + , (6.16)
n n2 n3
where μk = E (X − μ)k is the k th central moment. Using the definition for the sample
variance from (6.4), the expected value of S 2 is readily verified to be σ 2 .
The probability distributions for Su2 and S 2 are typically skewed to the right. The
skewness diminishes as n increases. Of course, the Central Limit Theorem indicates that
the distributions of both are asymptotically normal. However, the convergence to a normal
distribution is very slow and requires a very large n. The distributions of Su2 and S 2 are
extremely important in statistical inference. Two special cases, examined next, are the
sampling distributions of Su2 and S 2 when sampling from normal populations.
Chi-Square Distribution
X ∼ χ2n
⎧
⎪ 1
⎨ n n · x n2 −1 e− x2 if x ≥ 0
f (x) = Γ 2 2
2
⎪
⎩
0 if x < 0 (6.17)
E[X] = n
Var[X] = 2n
n 1
MX (t) = (1 − 2t)− 2 for t <
2
The chi-square distribution is strictly dependent on the parameter n, called the degrees
of freedom. In general, the chi-square distribution is unimodal and skewed to the right.
Three different chi-square distributions are represented in Figure 6.7 on the next page. The
notation used with the chi-square distribution to indicate α of the distribution is in the left
tail when the distribution has n degrees of freedom is χ2α;n . For example, χ20.95;10 denotes
the value such that 95% of the area is to the left of said value in a χ210 distribution.
To find the value corresponding to χ20.95;10 , use the S command qchisq(p, df), where
p is the area to the left (probability) and df is the degrees of freedom. The command gives
Sampling and Sampling Distributions 227
n=3
n=6
n = 16
0 5 10 15 20 25 30 35
FIGURE 6.7: Illustrations of the pdfs of χ23 , χ26 , and χ216 random variables
Asymptotic properties. For large values of n (n √ > 100), the distribution of 2χ2n
has an approximate normal distribution
q √ a mean of 2n −
with 1 and a√standard qdeviation
of 1. In other words, because 2χ2n ∼ N ( 2n − 1, 1), Y = 2χ2n − 2n − 1 ∼ N (0, 1).
For very large values of n, the approximation
χ2n − n q
Y = √ ∼ N (0, 1)
2n
may also be used.
(b)
2
P(40 ≤ χ265 ≤ 50) = P( 2(40) ≤ 2χ265 ≤ 2(50))
√ √ 2 √ √ √
= P( 80 − 129 ≤ 2χ265 − 129 ≤ 100 − 129)
≈ P(−2.41 ≤ Z ≤ −1.36) = 0.079.
(c)
2 √
P(χ2220 ≥ 260) = P( 2χ2220 ≥ 2 · 260)
2 √
= P( 2χ2220 − 2(220) − 1 ≥ 2 · 260 − 2(220) − 1)
≈ P(Z ≥ 1.85) = 0.032.
> 1 - pchisq(260,220)
[1] 0.03335803
(d)
P(χ2100 ≤ a) = 0.6
2
√
P 2χ100 − 2(100) − 1 ≤ 2a − 2(100) − 1 = 0.6
2
& √ '
P Z ≤ 2a − 2(100) − 1 = 0.6
√ √
0.2533 = 2a − 199
⇒ a = 103.106.
> qchisq(.6,100)
[1] 102.9459
Note that the approximations are close to the answers from S, but they are not exactly
equal.
[Link] The Relationship between the χ2 Distribution and the Normal Distri-
bution
In addition to describing the χ2 distribution as a special case of the gamma distribution,
the χ2 distribution can be defined as the sum of independent, squared, standard normal
random variables. If n is the number of summed independent, squared, standard normal
random variables, then the resulting distribution is a χ2 distribution with n degrees of
freedom, written χ2n . That is,
n
χ2n = Zi2 , Zi ∼ N (0, 1). (6.18)
i=1
Sampling and Sampling Distributions 229
To complete the proof of Theorem 6.1, recall that the derivative inside the integral when
certain characteristics are satisfied is
b(θ)
b(θ)
d d d ∂f (x, θ)
f (x, θ)dx = f (b(θ), θ) b(θ) − f (a(θ), θ) a(θ) + dx.
dθ dθ dθ ∂θ
a(θ) a(θ)
dFY (y) 2 1 1
f (y) = =√ √ e−y/2 = √ y (1/2)−1 e−y/2 , 0 ≤ y < ∞,
dy 2π 2 y 2Γ(1/2)
Proof:
;
r ;
r
MY (t) = E[etY ] = E[etXi ] = MXi (t)
i=1 i=1
r
;
r
ni − 12 ni
−
= (1 − 2t) 2 = (1 − 2t) i=1
i=1
Example 6.20 Given 10 independent and identically distributed (i.i.d.) random variables
Yi , where Yi ∼ N (0, σ = 5) for i = 1, . . . , 10, compute
10
(a) P Yi2 ≤ 600
i=1
10
1
(b) P Yi2 ≥ 12.175
10 i=1
⎛/
0 ⎞
01 10
(c) The number a such that P ⎝1 Yi2 ≥ a⎠ = 0.5
10 i=1
Yi −0 Yi
Solution: The answers are computed using S. Be sure to note that Z = 5 = 5 .
(a)
10
10 2
Yi 600
P Yi2 ≤ 600 =P ≤
i=1 i=1
5 25
= P(χ210 ≤ 24) > 0.99.
Using the S command pchisq(24,10) gives P χ210 ≤ 24 = 0.9923996:
> pchisq(24,10)
[1] 0.9923996
(b)
10
10 2
1 Yi 12.175(10)
P Yi2 ≥ 12.175 =P ≥
10 i=1 i=1
5 25
= P(χ210 ≥ 4.87) = 0.90.
> 1 - pchisq(4.87,10)
[1] 0.8996911
Sampling and Sampling Distributions 231
(c)
10
10 2
1 Yi 10a2
P Yi2 ≥a 2
=P ≥
10 i=1 i=1
5 25
2
10a
= P χ210 ≥ = 0.5
25
Using the S command qchisq(), the value χ210,0.50 = 9.34 is calculated:
> qchisq(0.50,10)
[1] 9.341818
10a2
Consequently, 25 = 9.34, which yields a = 4.83.
[Link] Sampling Distribution for Su2 and S 2 when Sampling from Normal
Populations
In this section, the resulting sampling distributions for Su2 and S 2 given in Table
6.4 on
page 205 when sampling from a normal distribution are considered. Note that ni=1 (Xi −
X)2 = nSu2 = (n − 1)S 2 and that dividing this by σ 2 yields
n 2
Xi − X nS 2 (n − 1)S 2
2
= 2u = (6.19)
i=1
σ σ σ2
The first term in (6.19) appears to be some type of standardized normal random variable.
However, it is not, since the sample mean of a random variable is itself a random variable
and not a constant. So, what is the distribution then of nSu2 /σ 2 ? Theorem 6.3 tells us that
the distribution of nSu2 /σ 2 is χ2n−1 .
Proof: A detailed proof of part (1) in Theorem 6.3 is beyond the scope of the text, and
the statement will simply be assumed to be true. The independence between X and S 2 is
a result of normal distributions. Almost without exception, the estimators X and S 2 are
dependent in all other distributions.
n
To prove part (2) of Theorem 6.3, use Corollary 6.3 to say that i=1 (Xiσ−μ)
2
2 ∼ χ2n .
n (Xi −X ) 2
Then, rearrange the terms to find an expression for i=1 σ2 for which the distribution
is recognizable. Start by rearranging the numerator of the χ2n distribution:
n n
2
(Xi − μ)2 = Xi − X + X − μ
i=1 i=1
n
2 n
2 n
= Xi − X + X−μ +2 Xi − X X−μ
i=1 i=1 i=1
232 Probability and Statistics with R
Since n
n
Xi − X X−μ = X−μ Xi − X = 0,
i=1 i=1
it follows that
n n
2
(Xi − μ)2 = Xi − X + n(X − μ)2 . (6.20)
i=1 i=1
(n−1)
(1 − 2t)− 2 = E etY1 = MY1 (t) ⇒ Y1 ∼ χ2n−1 .
n
(X −X̄)2
Note that Y1 = i=1
σ2
i
∼ χ2n−1 is based on the n quantities X1 − X, X2 −
X, . . . , Xn − X, which sum to zero. Consequently, specifying the values of any n − 1 of
the quantities determines then
remaining value. That is, only n − 1 of the quantities are free
(X −μ)2
to vary. In contrast, Y = i=1 σ2 i ∼ χ2n has n degrees of freedom since there are no
restrictions on the quantities X1 − μ, X2 − μ, . . . , Xn − μ. In general, when statistics are
used to estimate parameters, one degree of freedom is lost for each estimated parameter.
(n−1)σ2
Example 6.21 Show that E(Su2 ), E(S 2 ), Var(Su2 ), and Var(S 2 ) are equal to n , σ2 ,
4 4
2(n−1)σ 2σ
n2 , and n−1 , respectively, when sampling from a normal distribution.
2
nSu (n−1)S 2
Solution: It is known that σ2 = σ2 ∼ χ2n−1 according to Theorem 6.3 on the
previous page. Therefore,
(a)
! "
nSu2
E 2
= E χ2n−1 = n − 1 , so
σ
n 2 2 (n − 1)σ 2
E S u = n − 1 ⇒ E Su =
σ2 n
(b)
! "
(n − 1)S 2
E 2
= E χ2n−1 = n − 1
σ
(n − 1) 2
2
E S = n − 1 ⇒ E S 2 = σ2
σ
Sampling and Sampling Distributions 233
(c)
! "
nSu2
Var 2
= Var χ2n−1 = 2(n − 1)
σ
n2 2 2 2(n − 1)σ 4
Var S u = 2(n − 1) ⇒ Var Su =
σ4 n2
(d)
! "
(n − 1)S 2
Var 2
= Var χ2n−1 = 2(n − 1)
σ
(n − 1)2 2 2 2σ 4
Var S = 2(n − 1) ⇒ Var S =
σ4 (n − 1)
Example 6.22 A random sample of size 11 is taken from a N (μ, σ) distribution where
both the mean and the standard deviation are unknown and the sample variance S 2 is
2
computed. Compute the P(0.487 < Sσ2 < 1.599).
(n−1)S 2 10S 2
Solution: According to Theorem 6.3 on page 231, σ2 ∼ χ2n−1 , which implies σ2 ∼
χ210 :
S2 10S 2
P 0.487 < 2 < 1.599 = P 0.487(10) < < 1.599(10)
σ σ2
= P(4.87 < χ210 < 15.99)
= P(χ210 < 15.99) − P(χ210 < 4.87)
= 0.90 − 0.10 = 0.80
To find P(χ210 < 15.99) and P(χ210 < 4.87), one can use the S command pchisq():
Example 6.23 A custom door manufacturer knows that the measurement error in the
height of his final products (the door height minus the order height) follows a normal
distribution with a variance of σ 2 = 225 mm2 . A local contractor building custom bungalows
orders 31 doors. What is the P(S > 18.12 mm) for the 31 doors, and what is the expected
value of S 2 ?
Solution:
n−1 2 30
P(S > 18.12) = P S > 18.122 = P(χ230 > 43.78) ≈ 0.05
σ2 225
> 1 - pchisq(43.78,30)
[1] 0.04992715
Since the expected value of S 2 is the population variance, E S 2 = 225.
234 Probability and Statistics with R
6.6.2 t-Distribution
Given√ a random sample X1 , . . . , Xn that is drawn from a N (μ, σ) distribution, X ∼
N (μ, σ/ n), which implies
X−μ
√ ∼ N (0, 1). (6.22)
σ/ n
The quantity (6.22) is used primarily for inference regarding μ. However, this inference
assumes σ is known. The assumption of a known σ is generally not reasonable. That is,
if μ is unknown, it almost certainly follows that σ will be unknown as well. Fortunately,
inference regarding μ can still be performed if σ is replaced by S in (6.22). Specifically, the
quantity
X−μ
√ (6.23)
S/ n
follows a well-known distribution, described next.
Definition 6.1: Given two independent random variables Z and U , where Z ∼ N (0, 1)
and U ∼ χ2ν , we define the t-distribution with ν degrees of freedom as the ratio of Z divided
by the square root of U divided by its degrees of freedom. That is,
Z
T = 2 ∼ tν . (6.24)
Uν
ν
236 Probability and Statistics with R
0.06
0.06
0.00
0.00
0 10 20 30 40 50 60 0 10 20 30 40 50 60
NC14 EC14
0.020
0.020
0.000
0.000
0 50 100 150 200 0 50 100 150 200
NC99 EC99
2
FIGURE 6.8: Probability histograms for simulated distributions of (n−1)S
σ2 when sampling
from normal and exponential distributions. NC14 designates the simulated sampling
2
distributions of (n−1)S
σ2 when taking samples of sizes n = 15 from a normal distribution. In
a similar fashion, NC99 denotes the simulated sampling distribution when taking samples
of size n = 100 from a normal distribution. EC14 and EC99 are analogous to NC14 and
NC99 with the exception that the sampling is done from an exponential distribution. The
superimposed density on all curves is a χ2n−1 .
Using definition 6.1, one can readily see why (6.23) follows a t-distribution with n − 1
degrees of freedom since
X−μ
√
X−μ X−μ σ/ n Z
√ = 2 =2 =2 ∼ tn−1 .
S/ n √σ
(n−1)S 2 χ2n−1 Un−1
n (n−1)σ2 n−1 n−1
The t-distribution, also called Student’s t-distribution, was first described in a paper pub-
lished by William Sealy Gosset under the pseudonym “Student.” Gosset was employed by
Guiness Breweries when his research relating to the t-distribution was published. Since Gui-
ness Breweries had a policy preventing research publications by its staff, Gosset published
his findings under the pseudonym “Student.” Consequently, the t-distribution is often called
Student’s t-distribution in his honor. The pdf, expectation, and variance of a t-distribution
with ν degrees of freedom are given in (6.25).
t-Distribution
X ∼ tν
− ν+1
Γ ν+1
2 x2 2
f (x) = √ 1 + for − ∞ < x < ∞ (6.25)
πνΓ ν2 ν
E[X] = 0
ν
Var[X] = for ν > 2
ν −2
Sampling and Sampling Distributions 237
The shape of the t-distribution is similar to that of the normal distribution; but for small
sample sizes, it has heavier tails than the N (0, 1). Figure 6.9 illustrates the densities for
t-distributions with 1, 3, and ∞ degrees of freedom, respectively. Note that tα;∞ = zα . To
find the quantity tα;ν , the S command pt(α, ν) can be used. In particular, suppose t0.80;1 ,
depicted in Figure 6.9, is desired. Using the S command pt(0.80,1) gives 1.376382 for the
answer.
t∞
t3
t1
−6 −4 −2 0 t0.80;1 4 6
FIGURE 6.9: Illustrations of the pdfs of t1 (dashed line), t3 (dotted line), and t∞ (solid
line) random variables.
Example 6.25 The tensile strength for a type of wire is normally distributed with an
unknown mean μ and an unknown variance σ 2 . Five pieces of wire are randomly selected
from a large roll, and the strength of each segment of wire is measured. Find the probability
2S
that Y will be within √ n
of the true population mean, μ.
Theorem 6.4 Given two random samples X1 , . . . , XnX and Y1 , . . . , YnY that are taken
from independent normal populations where X ∼ N (μX , σX ), Y ∼ N (μY , σY ), and σX =
σY , the random variable
X − Y − (μX − μY )
. & ' ∼ tnX +nY −2 . (6.26)
(nX −1)SX
2 +(n −1)S 2
Y Y 1 1
nX +nY −2 nX + nY
238 Probability and Statistics with R
2 2
σ 2
σY
Proof: Since X − Y ∼ N μX − μY , nX X
+ nY , according to Theorem 5.1 on page 176,
X − Y − (μX − μY )
Z= 2 2 2
∼ N (0, 1).
σX σY
nX + nY
(nX −1)SX
2
(nY −1)SY
2
By Theorem 6.3 on page 231, σX2 ∼ χ2nX −1 and 2
σY
∼ χ2nY −1 . Since X and Y
are independent, it follows that
(nX − 1)SX
2
(nY − 1)SY2
W = 2 + ∼ χ2nX +nY −2
σX σY2
from Theorem 6.2 on page 229. Using the definition of the t-distribution, given in defi-
nition 6.1 on page 235, √ZW ∼ tν . In this particular case, ν = nX + nY − 2 and, since
ν
σX = σY = σ is assumed,
X − Y − (μX − μY )
9
2
σX σ2
+ Y
Z nX nY
. = /
0
W 0 (nX − 1)SX 2
(nY − 1)SY2
0 +
ν 1 2
σX σY2
nX + nY − 2
X − Y − (μX − μY ) 1
= . · 9
1 1 1 (nX − 1)SX + (nY − 1)SY2
2
σ +
nX nY σ nX − nY − 2
X − Y − (μX − μY )
= 9 ∼ tnX +nY −2 .
(nX − 1)SX2
+ (nY − 1)SY2 1 1
+
nX + nY − 2 nX nY
Definition 6.2: If U and V are independent random variables, each with a χ2 distribution
with ν1 and ν2 degrees of freedom, respectively, then
U
ν1
V
∼ Fν1 ,ν2 .
ν2
The pdf, expected value, and variance of an F distribution are given in (6.27).
Sampling and Sampling Distributions 239
F Distribution
X ∼F
νν11 ,ν2 − 12 (ν1 +ν2 )
Γ ν1 +ν 2
ν1 2 ν1 −1 ν1
f (x) = ν1 ν2
2
x2 1+ x
Γ 2 Γ 2 ν2 ν2
(6.27)
ν2
E[X] =
ν2 − 2
2ν22 (ν1 + ν2 − 2)
Var[X] = provided ν2 > 4
ν1 (ν2 − 2)2 (ν2 − 4)
F19,19
F4,9
F2,4
0 f0.025;19,19 f0.975;19,19 6
FIGURE 6.10: Illustrations of the pdfs of F2,4 (solid line), F4,9 (dotted line), and F19,19
(dashed line) random variables
Theorem 6.5 If there are two random samples X1 , . . . , XnX and Y1 , . . . , YnY that are
taken from independent normal populations where X ∼ N (μX , σX ) and Y ∼ N (μY , σY ),
then the random variable
2
SX
2
σX
2
SY
∼ FnX −1,nY −1 . (6.28)
2
σY
240 Probability and Statistics with R
2
SX χ2n −1 2
SY χ2n −1
Proof: Since 2
σX
∼ X
nX −1 and 2
σY
∼ Y
nY −1 , by Theorem 6.3 on page 231, it follows that
2
SX
2
σX
2
SY
∼ FnX −1,nY −1 .
2
σY
To find the value fα; ν1 , ν2 , where P(Fν1 , ν2 < fα; ν1 , ν2 ) = α, with S, use the command qf(p,
df1, df2), where p is the area to the left (probability) in an F distribution with ν1 =df1
and ν2 =2.
Example 6.26 Find the constants c and d such that P(F5,10 < c) = 0.95 and P(F5,10 <
d) = 0.05.
Solution: Using the S commands qf(0.95,5,10) and qf(0.05,5,10) returns the values
3.325835 and 0.2111904, respectively.
Example 6.27 Use S to find the values associated with the points f0.025;19,19 and
f0.975;19,19 depicted in Figure 6.10 on the previous page.
Note that a relationship exists between the t- and F distributions. Namely, t2ν = F1,ν ,
and the relationship between the values in both distributions is
6.7 Problems
1. How many ways can a host randomly choose 8 people out of 90 in the audience to
participate in a TV game show?
2. Let X be a t5 .
3. If (1 − 2t)−5 , t < 12 , is the mgf of a random variable X, find P(X < 15.99).
4. If X ∼ χ210 , find the constants a and b so that P(a < X < b) = 0.90 and P(X < a) = 0.05.
5. Let X be a χ210 . Calculate P(X < 8) and P(X > 6). Calculate a so that P(X < a) = .05.
What are the population mean and population variance of X?
6. Let X be distributed as an F2,5 . Calculate P(X < 1) and the median of X. Calculate a
so that P(X < a) = 0.10. What are the population mean and population variance of X?
X1 = 0, X2 = 1, X3 = 2, X4 = 3, X5 = 4.
(a) Enumerate all the samples of size 2 that can be drawn with and without replacement.
(b) Calculate the mean of the population.
(c) Calculate the variance of the population.
(d) Calculate the standard deviation of the population.
(e) Calculate the mean of the sample mean, E[X ].
(f) Calculate the variance of the sampled mean, Var X .
(g) Calculate the standard deviation of the sample mean.
(h) Calculate the mean of the sample variance, E S 2 .
(i) Is the variance of X larger when sampling with or without replacement? Explain
your answer.
10. Use the data frame wheatUSA2004 from the PASWR package; draw all samples of sizes 2, 3,
and 4; and calculate the mean of the means. What size provides the best approximation
to the population mean? What is the variance of these means?
12. Constant velocity joints (CV joints) allow a rotating shaft to transmit power through a
variable angle, at constant rotational speed, without an appreciable increase in friction
or play. An after-market company produces CV joints. To optimize energy transfer, the
drive shaft must be very precise. The company has two different branches that produce
CV joints where the variability of the drive shaft is known to be 2 mm. A sample of
n1 = 10 is drawn from the first branch, and a sample of n2 = 15 is drawn from the
second branch. Suppose that the diameter follows a normal distribution. What is the
probability that the drive shafts coming from the first branch will have greater variability
than those of the second branch?
13. Given a population N (μ, σ) with unknown mean and variance, a sample of size 11 is
drawn and the sample variance S 2 is calculated. Calculate the probability P(0.5 <
S 2 / 2 < 1.2).
σ
14. Simulate 20,000 random samples of sizes 30, 100, 300, and 500 from an exponential
distribution with a mean of 1/5 . Estimate the density of the sampling distribution with
the function density(). Superimpose a theoretical normal density with appropriate
mean and standard deviation. What sample size is needed to get an estimated density
close to a normal density?
15. The plastic tubes produced by company X for the irrigation system used in golf courses
have a length of 1.5 meters and a standard deviation of 0.1 meter. The plastic tubes
produced by company Y have a length of 1 meter and a standard deviation of 0.09 meter.
Suppose that both tube lengths follow normal distributions.
(a) Calculate the probability that a random sample of 15 tubes from company X has a
mean length at least 0.45 meter greater than the mean length of a random sample
of size 20 from company Y .
(b) Suppose that the population variances are unknown but equal, Sx = 0.1, and
Sy = 0.09. Calculate the probability that a random sample of 15 plastic tubes from
company X has a mean length at least 0.45 meter greater than the mean length of
a random sample of 20 plastic tubes from company Y .
16. Plot the density function of an F4,6 random variable. Find the area to the left of x = 3
and shade this region in the original plot.
17. Let X1 , X2 , X3 , X4 be a random sample from a N (0, σ). Calculate the distribution of
(X1 −X2 )2
(X +X )2
.
3 4
Sampling and Sampling Distributions 243
19. Consider a random sample of size n from an exponential distribution with parameter
λ. Use moment generating functions to show that the sample mean follows a Γ(n, λn).
Graph the theoretical sampling distribution of X when sampling from an Exp(λ = 1) for
n = 30, 100, 300, and 500. Superimpose an appropriate normal density for each Γ(n, λn).
At what sample size do the sampling distribution and superimposed density virtually
coincide?
20. Set the seed
√ equal to 10, and simulate 20,000 random samples of size n√x = 65 from a
N (4, σx = 2 ), 20,000 random samples of size ny = 90 from a N (5, σy = 3 ) and verify
2 2
Sx /σx
that the simulated statistic Sy2 /σy2
follows an F64,89 distribution.
21. Set the seed equal to 95, and simulate m = 20, 000 random samples of size n = 1000
from a Bernoulli (π = 0.4). Verify that the sample proportion follows an approximate
normal distribution with a mean approximately equal to 0.4 and a standard deviation
approximately equal to 0.01549.
22. A communication system consists of n components, where the probability that each
component works is π. The system will work if at least half of its components work. For
what values of π will a system consisting of 5 components have a greater probability of
working than a system consisting of 3 components? Plot the probability each system
(n = 5 and n = 3) works for values of π from 0 to 1 in increments of 0.01.
23. Given X ∼ N (0, σ = 1), Y ∼ N (2, σ = 2), and Z ∼ N (4, σ = 3), what is the distribution
of W = X + Y + Z? Set the seed equal to 368 and simulate 1000 samples, each of
size 1 for X, Y , and Z. Add the values in the three vectors to obtain W ’s empirical
distribution. Create a density histogram of the simulated values of W and superimpose
the theoretical density of W .
24. Set the seed equal to 48, and simulate a χ23 distribution by summing the squares of
three simulated standard normal random variables, each having length 20,000. Create a
density histogram of the simulated χ23 random variable. Superimpose the theoretical χ23
density over the histogram.
25. Verify empirically that
N (0, 1)
1 2 12 ∼ t5
5 χ5
by setting the seed equal to 36 and generating a sample of size 1000 from a N (0, 1)
distribution. Generate another sample of size 1000 from a χ25 distribution. Perform the
appropriate arithmetic to arrive at the simulated sampling distribution. Create a density
histogram of the results and superimpose a theoretical t5 density.
26. A farmer is interested in knowing the mean weight of his chickens when they leave the
farm. Suppose that the standard deviation of the chickens’ weight is 500 grams.
(a) What is the minimum number of chickens needed to ensure the a standard deviation
of the mean is no more than 100 grams with a confidence level of 0.95?
(b) If the farm has three coops and the mean chicken weight in each coop is 1.8, 1.9,
and 2 kg, respectively, calculate the probability that a random sample of 50 chickens
with an average weight larger than 1.975 kg comes from the first coop. Assume the
weight of the chickens follows a normal distribution.
244 Probability and Statistics with R
27. Find the required sample size (n) to estimate the proportion of students spending more
than e 10 a week on entertainment with a 95% confidence interval so that the margin of
error is no more than 0.02.
28. 15.3% of the Spanish Internet domain names are “.org.” If a sample of 2000 Spanish
domain names is taken,
(a) Calculate the exact probability that at least 200 domain names will be “.org.”.
(b) Compute an approximate answer that at least 200 domain names will be “.org.” with
a normal approximation.
29. Set the seed equal to 86, and simulate m1 = 20, 000 samples of size n1 = 1000 from a
Bin(n1 , π = 0.3) and m2 = 20, 000 samples of size n2 = 1100 from a Bin(n2 , π = 0.7).
Verify that the difference of sampling proportions follows a normal distribution.
30. Given a random sample of size n from an exponential distribution with parameter λ,
prove that the sample mean follows a Γ(n, λn). Set the seed equal to 679, and simulate
m = 1000 random samples of size n = 100 from an Exp(λ = 1), and check that the
normal approximation of the mean is appropriate. Repeat this exercise with random
samples of size n = 3, and verify that, in this case, Γ(3, 3) is more appropriate to use
than the normal distribution.
Chapter 7
Point Estimation
7.1 Introduction
Throughout this chapter, random samples drawn from a known distribution where
the parameters that characterize the distribution are unknown will be of interest. To
specify completely a probability distribution, whether it be discrete or continuous, the
distribution’s parameters must be specified. For example, a random variable may follow a
normal distribution; however, if both the mean and the standard deviation of the normal
distribution are not known, the distribution at hand cannot be completely specified. In a
similar fashion, a Poisson random variable requires knowledge of the parameter λ to specify
completely that distribution. In general, the pdf of a random variable X is f (x | θ ), where θ
is the vector of parameters that characterize the pdf. The vector of parameters θ is defined
in a parameter space denoted Θ. For each value of θ ∈ Θ, there is a different pdf. To
obtain possible values for the vector of parameters, a random sample from the population
of interest is taken and statistics called estimators are constructed. The values of the
estimators are called point estimates. For example, X may be used as a point estimator
for μ, in which case x̄ is a point estimate of μ.
Since estimators are statistics or functions of random variables, they themselves are
random variables. Studying the sampling distributions of estimators as well as their sta-
tistical properties such as mean square error, bias or unbiasedness, efficiency, consistency,
and robustness, all of which will be defined in this chapter, will give guidelines about which
estimators to employ.
245
246 Probability and Statistics with R
estimator T and the squared bias of the estimator T , where bias is defined as E[T ] − θ since
MSE [T ] = E (T − E[T ] + E[T ] − θ)2
2
= E T − E[T ] + E (E[T ] − θ)2 + 2E (T − E[T ])(E[T ] − θ)
= Var[T ] + (E[T ] − θ)2 + 2(E[T ] − E[T ])(E[T ] − θ)
= Var[T ] + (E[T ] − θ)2
= Var[T ] + (Bias [T ])2 . (7.1)
The concepts of variance and bias are illustrated in Figure 7.1, which depicts the shot
patterns for four marksmen on their respective targets. When the marksman’s weapon is
properly sighted, the center of the target represents θ.
It seems logical to think that the most desirable estimators are those that minimize the
MSE . However, estimators that minimize the MSE for all possible values of θ do not always
exist. In other words, an estimator may have the minimum MSE for some values of θ and
not others.
Point Estimation 247
Example 7.1 Show that the sample mean and the sample variance are unbiased estima-
tors of the population mean and the population variance, respectively.
Solution: To solve the problems, keep in mind that if X ∼ Pois(λ), E[X] = λ and
Var[X] = λ.
n Xi n E[Xi ]
(a) Since E X = E i=1 n = i=1 n = nλ n = λ, it follows that X is an unbiased
estimator of λ.
(b) Since E 2X = 2E X = 2λ, it follows that 2X is an unbiased estimator of 2λ.
2 2
(c) Since E X = Var X + μ2X = nλ + λ2 , it follows that X is a biased estimator of λ2 .
2
However, X is an asymptotically unbiased estimator of λ2 . That is, as n tends to infinity,
the estimator becomes unbiased.
%√ ( !√ "
n−1
E X =E ·S .
σ
√ ∞ √
Since X ∼ χ2n−1 , the expected value of X is xf (x)dx, where f (x) is the pdf of a
−∞
chi-square random variable:
%√ ( √ ∞
1 n−1 x
E X = x n−1 n−1 x 2 −1 e− 2 dx
0 Γ 2 2 2
∞
1 n−1 x
x 2 −1+ 2 e− 2 dx
1
= n−1 n−1
Γ 2 2 2 0
∞
1 n x
= n−1 n−1 x 2 −1 e− 2 dx (7.2)
Γ 2 2 2 0
Next, use the change of variable x/2 = t where dx = 2dt in an attempt to force the
right-hand side of (7.2) to look like a gamma function. Specifically, recall that Γ(α) =
∞
xα−1 e−x dx for α > 0 :
0
%√ ( ∞
1 n
E X = n−1 n−1 (2t) 2 −1 e−t 2 dt
Γ 2 2 2 0
n ∞
√ n
2 2 n 2Γ
2 −1 −t
dt = n−12
= n−1 n−1 t e
Γ 2 2 2 0 Γ 2
Since
%√ ( !√ " √
n−1 2 Γ( n2 )
E X =E S = ,
σ Γ( n−1
2 )
it follows that
√ n
2Γ 2
E[S] = σ √ =
σ (7.3)
n − 1Γ n−1
2
1.00
> m <- 50
> coeff <- array(0, m)
0.95
> for (n in 2:m)
+ { coeff[n] <- (sqrt(2/(n-1))
2 )
n−1Γ( n−1
+ *gamma(n/2))/gamma((n-1)/2)}
2)
0.90
2Γ( n
0.85
√
[1] 0.9869343
0 10 20 30 40 50
n
A more compact solution using R is
> curve(sqrt(2/(x-1))*gamma(x/2)/gamma((x-1)/2),2,50)
> abline(h=1, lty=2)
7.2.3 Efficiency
A desirable property of a good estimator is not only to be unbiased, but also to have
a small variance, which translates into a small MSE for estimators, regardless of whether
they are biased or unbiased. One way to compare the MSE s of two estimators is by using
relative efficiency. Given two estimators T1 and T2 , the efficiency of T1 relative to T2 ,
written eff (T1 , T2 ), is
MSE [T2 ]
eff (T1 , T2 ) = . (7.4)
MSE [T1 ]
When the estimators in (7.4) are unbiased, the efficiency of T1 relative to T2 is simply the
ratio of estimators variances, written
Var [T2 ]
eff (T1 , T2 ) = .
Var [T1 ]
The estimator T1 is more efficient than the estimator T2 if, for any sample size, MSE [T1 ] ≤
MSE [T2 ], which then implies that eff (T1 , T2 ) ≥ 1. When the estimators are unbiased,
the estimator T1 is more efficient than the estimator T2 if, for any sample size, Var [T1 ] ≤
Var [T2 ], which also implies that eff (T1 , T2 ) ≥ 1. If a choice is to be made among a
small number of unbiased estimators, simply compute the variance of all of the estimators
and select the estimator with minimum variance. However, if the estimator that has the
smallest variance among all possible unbiased estimators must be chosen, an infinite number
of variances would need to be calculated. Clearly, this is not a viable solution.
Thankfully, it can be shown that if T = θ̂ is an unbiased estimator of θ and a random
sample of size n, X1 , X2 , . . . , Xn , has pdf f (x|θ), then the variance of the unbiased estimator,
θ̂, must satisfy the inequality
1
Var θ̂ ≥ !& '2 " , (7.5)
∂ ln f (X|θ)
n·E ∂θ
where f (X|θ) is the density function of the distribution of interest evaluated at the random
variable X. In the discrete case, p(X|θ) is used instead of f (X|θ). In general, the probability
250 Probability and Statistics with R
distributions of both discrete and continuous distributions are referred to using the notation
f (x). The inequality in (7.5) is known as the Cramér-Rao inequality, and the quantity
on the right-hand side of the equation is known as the Cramér-Rao lower bound (CRLB).
Definition 7.1: If θ̂ is an unbiased estimator of θ and
1
Var θ̂ = !& '2 "
(7.6)
n · E ∂ ln f∂θ(X|θ)
Not all parameters have unbiased estimators whose variance equals the CRLB. However,
when the variance of an unbiased estimator equals the CRLB, the estimator is efficient or
minimum variance. The quantity in the denominator of (7.6) is known as the Fisher
information about θ that is supplied by the sample. That is, the smaller the variance of
the estimator, the greater the information.
Example 7.5 Show that X is a minimum variance unbiased estimator of the mean λ of
a Poisson population.
Solution: If X ∼ Pois(λ), then, according to (4.5), E[X] = λ, Var[X] = λ, and the pdf
of X is
λx e−λ
P(X = x|λ) = . (7.7)
x!
n E[Xi ]
Since E X = = nλ
n = λ, it follows that X is an unbiased estimator of λ,
i=1 n n Xi
λ
with variance n because the Var X = Var i=1 n = n12 ni=1 Var [Xi ] = nλ λ
n2 = n .
λ
Consequently, if the CRLB equals n , X is a minimum variance unbiased estimator of λ
according to Definition 7.1. By taking the natural logarithm of (7.7),
Example 7.6 Show that X is a minimum variance unbiased estimator of the mean θ of
an exponential population.
Point Estimation 251
Solution: If X ∼ Exp θ1 , then, according to (4.12), when using the substitution θ = λ1 ,
E[X] = θ, Var[X] = θ2 , and the pdf of X is
⎧
⎪
⎨ 1 e−x/θ if x ≥ 0
f (x) = θ . (7.9)
⎪
⎩0 if x < 0
n E[Xi ]
Since E X = = nθ = θ, it follows that X is an unbiased estimator of
θ 2
i=1 n n n X n 2
θ2
θ, with variance n since Var X = Var i=1 n
i
= n12 i=1 Var [Xi ] = nθn2 = n .
2
Consequently, if the CRLB equals θn , X is a minimum variance unbiased estimator of θ
according to Definition 7.1 on the facing page. By taking the natural logarithm of (7.9),
x
ln f (x|θ) = − ln(θ) − . (7.10)
θ
Taking the derivative of (7.10) with respect to θ gives
∂ ln f (x|θ) 1 x x−θ
=− + 2 = .
∂θ θ θ θ2
Hence # 2 $ # 2 $
∂ ln f (X|θ) X −θ E[(X − θ)2 ] Var[X]
E =E = = .
∂θ θ2 θ4 θ4
Therefore, # 2 $
∂ ln f (X|λ) Var[X] θ2 1
E = = = 2,
∂θ θ4 θ4 θ
θ2
Consequently, since X is unbiased and Var X = n, it follows that X is a minimum
variance unbiased estimator of θ.
Example 7.7 Comparing Estimators: Blue Jean Length Suppose the true
manufactured length of new 32L blue jeans follows a normal distribution with unknown μ
and σ = 0.5 inch. It is known that 32L blue jeans sold in stores have a length of at least
31 inches. If a random sample of size n = 3 of 32L blue jeans is taken to estimate μ, which
of the estimators μ̂1 or μ̂2 is better in terms of bias, variance, and relative efficiency where
μ̂1 = 0.33 · (X1 + X2 + X3 ) and μ̂2 = 0.50 · (X1 + X2 )?
Solution: Since
it follows that μ̂1 is a biased estimator of μ with bias 0.99μ − μ = −0.01μ. On the other
hand,
E[μ̂2 ] = 0.50 · E [X1 + X2 ] = 0.50 · E X1 + E X2 = 0.50 · (μ + μ) = μ,
252 Probability and Statistics with R
which makes μ̂2 an unbiased estimator of μ. The variances of μ̂1 and μ̂2 are
Var [μ̂1 ] = Var 0.33 · X1 + X2 + X3
= 0.332 · (Var[X1 ] + Var[X2 ] + Var[X3 ])
= 0.332 · (0.25 + 0.25 + 0.25) = 0.081675, and
Var [μ̂2 ] = Var 0.50 · X1 + X2 = 0.502 · (Var [X1 ] + Var [X2 ])
= 0.25 · (0.25 + 0.25) = 0.125, respectively.
Before looking at the relative efficiency of μ̂1 to μ̂2 , compute the MSE for each estimator
using the fact that MSE = Variance + Bias2 :
Since
MSE (μ̂2 ) 0.125
eff (μ̂1 , μ̂2 ) = = < 1 for all |μ| > 20.82,
MSE (μ̂1 ) 0.081675 + 0.0001μ2
conclude that μ̂2 is both more efficient and has a smaller MSE than does μ̂1 , since it is known
that μ ≥ 31 inches according to the problem. See Figure 7.2 for a graphical representation
of the distributions of μ̂1 and μ̂2 .
Distribution of μ̂1
Distribution of μ̂2
Bias
0.99μ μ
FIGURE 7.2: Graphical representations for the sampling distributions of μ̂1 and μ̂2
Both definitions (7.11) and (7.12) state that a consistent sequence of estimators converges
in probability to the parameter θ, where θ is the parameter the consistent sequence of
estimators is estimating. In practical terms, this implies that the variance of a consistent
estimator decreases as n increases and that the expected value of Tn tends to θ as n increases.
Further, given a consistent sequence of estimators, say Tn , Chebyshev’s inequality (3.17)
guarantees that
E[(Tn − θ)2 ]
P(|Tn − θ| ≥ ) = P(|Tn − θ|2 ≥ 2
)≤ 2
,
for every θ ∈ Θ. Since Eθ (Tn − θ)2 can be expressed as
2
Eθ (Tn − θ)2 = Var[Tn ] + (Bias[Tn ]) ,
if
lim Var[Tn ] = 0 and lim (Bias[Tn ])2 = 0, (7.13)
n→∞ n→∞
1 σ2
= ,
k2 n 2
from which it follows that
σ2
P(|X n − μ| ≥ ) ≤ . (7.14)
n 2
254 Probability and Statistics with R
Given that σ 2 < ∞ (finite), taking the limit as n → ∞ on both sides of the ≤ sign of (7.14)
gives
lim P(|X n − μ| ≥ ) = 0 for all .
n→∞
When working with normal distributions, a robust estimator of σ is MAD1, where MAD1 =
1 th
0.6745 MAD. The value 0.6745 corresponds to the 75 percentile of a N (0, 1) distribution
(z0.75 = 0.6745). When working with S, the default value returned when working with the
function mad() corresponds to the definition of MAD1. To compute the MAD as defined
in (7.15), use the S option constant=1 inside the mad() function.
Example 7.9 A botanist interested in studying the effects of a new herbicide on trifolium
repens (white clover) measures and records the stem lengths in centimeters of ten specimens
as 5.3, 2.8, 3.4, 7.2, 8.3, 1.7, 6.2, 9.3, 3.2, and 5.9. Compute the mean, median, standard
deviation, and MAD. Suppose the botanist makes a field error and records an 83 instead
of an 8.3. What effect will the recording error have on the computed quantities?
Solution: The stem measurements are entered without the recording error in the vector
stem1 (in increasing order) and the stem measurements with the recording error in the
vector stem2. That is, stem2 has an 83 rather than an 8.3.
> stem1 <- c(1.7, 2.8, 3.2, 3.4, 5.3, 5.9, 6.2, 7.2, 8.3, 9.3)
> stem2 <- c(1.7, 2.8, 3.2, 3.4, 5.3, 5.9, 6.2, 7.2, 83, 9.3)
> c(mean(stem1), sqrt(var(stem1)))
[1] 5.330000 2.516634
> c(mean(stem2), sqrt(var(stem2)))
[1] 12.80000 24.77185
> c(median(stem1), mad(stem1, constant = 1))
[1] 5.6 2.3
Point Estimation 255
Note that the mean and standard deviation of stem1 (5.33, 2.52) are dramatically different
from the mean and standard deviation of stem2 (12.8, 24.77). However, the median and
MAD (5.6, 2.3) are the same for the values in both stem1 and stem2. What has been
demonstrated is the robustness of the median and the MAD to outliers.
n
1
mr = Xir , (7.17)
n i=1
and subsequently to solve for estimators of the unknown parameters. Recall that the rth
population moment about the origin of a random variable X, denoted αr , was defined in
(3.6) as E [X r ]. It follows that αr = E [X r ] = ∞ x
i=1 i
r
P(X = xi ) for discrete X, and that
∞
αr = E [X r ] = −∞ xr f (x) dx for continuous X. Specifically, given a random sample
X1 , X2 , . . . , Xn from a population with pdf f (x|θ1 , θ2 , . . . , θk ), the method of moments
estimators, denoted θ˜i for i = 1, . . . , k, are found by equating the first k population moments
about the origin to their corresponding sample moments and solving the resulting system
256 Probability and Statistics with R
of simultaneous equations:
⎧
⎪
⎪ α1 (θ1 , . . . , θk ) = m1
⎪
⎪
⎪
⎪
⎪
⎪ α2 (θ1 , . . . , θk ) = m2
⎪
⎪
⎨
.. .. (7.18)
⎪
⎪
⎪
⎪ . .
⎪
⎪
⎪
⎪
⎪
⎪
⎩
αk (θ1 , . . . , θk ) = mk
The method of moments is an appealing technique for deriving estimators due to its
simplicity and to the fact that method of moments estimators are consistent. In fact, the
theoretical justification for equating the sample moments to the population moments is that,
under certain conditions, it can be shown that the sample moments converge in probability
to the population moments and that the sample moments about the origin are unbiased
estimators of their corresponding population moments.
Example 7.10 Given a random sample of size n from a Bin(1, π) population, find the
method of moments estimator of π.
Example 7.11 Given a random sample of size m from a Bin(n, π) population, find the
method of moments estimator of π.
X
which implies that the method of moments estimator for π is π̃ = n.
Example 7.12 Given a random sample of size n from a Pois(λ) population, find the
method of moments estimator of λ.
Example 7.13 Given a random sample of size n from a N (μ, σ) population, find the
method of moments estimators of μ and σ 2 .
Point Estimation 257
n
Solution: The first and second sample moments m1 and m2 are X and n1 i=1 Xi2 ,
respectively. The first
and
second population moments about zero for a normal random
variable are α1 = E X 1 = μ and α2 = E X 2 = σ 2 + μ2 . By equating the first two
population moments to the first two sample moments,
⎧
⎪
⎪
set
α1 (μ, σ 2 ) = μ = X = m1
⎪
⎪
⎨
(7.19)
⎪
⎪ 1
n
⎪ α2 (μ, σ 2 ) = σ 2 + μ2 set
⎪ = X 2
= m .
⎩ n i=1 i
2
<2 = n 2
Solving the system of equations in (7.19) yields μ̃ = X and σ 1
n i=1 Xi2 − X = Su2 as
the method of moments estimators for μ and σ 2 , respectively.
Example 7.14 Given a random sample of size n from a Gamma(α, λ) population, find
the method of moments estimators of α and λ.
Solution: According to (4.16), E [X] = αλ , and Var [X] = λα2 for a random variable X
that follows
na gamma distribution. The first and second sample moments m1 and m2 are
X and n1 i=1 Xi2 , respectively. The first and second population moments for a gamma
random variable are α
α1 = E X 1 = ,
λ
and
2 α α2 α(1 + α)
α2 = E X 2 = σ 2 + E [X] = 2 + 2 = ,
λ λ λ2
respectively. By equating the first two population moments to the first two sample moments,
⎧
⎪ α set
⎪
⎪ α1 (α, λ) = = X = m1
⎪
⎪ λ
⎨
(7.20)
⎪
⎪ n
⎪
⎪
α(1 + α) set 1
X 2 = m2 .
⎪
⎩ α2 (α, λ) = =
λ2 n i=1 i
n
(Xi −X )2
When it is recalled that Su2 = i
n , the system of equations in (7.20) can be solved to
X2 X
obtain α̃ = 2
Su and λ̃ = 2
Su as the method of moments estimators for α and λ, respectively.
The key difference between (7.16) and (7.21) is that the joint pdf given in (7.16) is a function
of x for a given θ and the likelihood function given in (7.21) is a function of θ for given x.
The value of θ that maximizes L(θ|x) is called the maximum likelihood estimate
(mle) of θ. Another way to think of the mle is the mode of the likelihood function. The
maximum likelihood estimate is denoted as θ̂(x), and the maximum likelihood estimator
(MLE), a statistic, as θ̂(X). In general, the likelihood function may be difficult to manipu-
late, and it is usually more convenient to work with the natural logarithm of L(θ|x), called
the log-likelihood function, since it converts
products
into sums. Finding the value θ
that maximizes the log-likelihood function ln L(θ|x) is equivalent to finding the value of θ
that maximizes L(θ|x) since the natural logarithm is a monotonically increasing function.
If L(θ|x) is differentiable with respect to θ, a possible mle is the solution to
∂ ln L(θ|x)
= 0. (7.22)
∂θ
Note that a possible mle is the solution to (7.22). A possible solution is used since a solution
to (7.22) is a necessary but not sufficient condition for the solution to be a maximum, since
the solution to (7.22) could be a local or global minimum, a local or global maximum, or a
point of inflection. Recall that stationary points where,
∂ 2 ln L(θ|x)
< 0, (7.23)
∂θ2 θ=θ̂(x)
indicate some type of maximum, either local or global. Further, the solution to (7.22) does
not include points on the boundaries of the parameter space. Consequently, when evaluating
the maximum of L(θ|x), the boundaries of the parameter space Θ as well as solutions to
(7.22) must be evaluated.
Example 7.15 Given a random sample of size n taken from a Bernoulli (π) distribu-
tion, compute the maximum likelihood estimate and maximum likelihood estimator of the
parameter π.
Solution: According to (4.2), the pdf for X ∼ Bernoulli (π) is
P (X = x|π) = π x (1 − π)1−x ,
where x takes on the value 1 with probability π and 0 with probability 1 − π. The likelihood
function for the n observed values is
;
n
L(π|x) = π xi (1 − π)1−xi .
i=1
To find the value that maximizes (7.24), take the first-order partial derivative of ln L(π|x)
with respect to π and set the answer equal to zero:
n
∂ ln L(π|x) xi n − ni=1 xi set
= i=1 − = 0. (7.25)
∂π π 1−π
Point Estimation 259
n
x i
The solution to (7.25) is π = i=1 n = x̄. For π = x̄ to be a maximum, the second-order
partial derivative of the log-likelihood function must be negative at π = x̄. The second-order
partial derivative is n n
∂ 2 ln L(π|x) − i=1 xi n − i=1 xi
= − .
∂π 2 π2 (1 − π)2
Evaluating the second-order partial derivative at π = x̄ yields
∂ 2 ln L(π|x) −nx̄ (n − nx̄) n n
= 2 − =− − ,
∂π 2 x̄ (1 − x̄)2 x̄ 1 − x̄
which is less than zero since 0 ≤ x̄ ≤ 1 and n > 0. Finally, since the values of the likelihood
function at the boundaries of the parameter space, π = 0 and π = 1, are 0, it follows
that π = x̄ is the value that maximizes the likelihood function. The maximum likelihood
estimate π̂(x) = x̄ and the maximum likelihood estimator π̂(X) = X.
$estimate
[1] 0.2098760
260 Probability and Statistics with R
$gradient
[1] 1.421085e-08
$code
[1] 1
$iterations
[1] 4
Warning messages:
1: In log(1 - p) : NaNs produced
2: In nlm(negloglike, 0.2) : NA/Inf replaced by maximum positive value
The following generic S code can be used to represent graphically the log-likelihood function
in a fashion similar to Figure 7.3:
> par(pty = "s")
> p <- seq(0.1, 0.9, 0.001)
> plot(p, - negloglike(p), type = "n", ylab = "L")
> abline(v = mean(eggs), col = 13, lwd = 3)
> lines(p, - negloglike(p), col = 6, lwd = 3)
-40
-60
ln L(π|x)
-80
-100
-120
-140
The function optimize(), available in both R and S-PLUS, approximates a local opti-
mum of a continuous univariate function (f) within a given interval. The function searches
the user-provided interval for either a minimum (default) or maximum of the function f.
To solve Example 7.16 with optimize(), enter
> loglike <- function(p){(sum(eggs)*log(p) + sum(1-eggs) * log(1-p))}
> optimize(f=loglike, interval=c(0,1), maximum=TRUE)
$maximum
[1] 0.2098906
$objective
[1] -41.61724
Point Estimation 261
Next, look for the value that maximizes the log-likelihood function by taking the first-order
partial derivative of (7.27) and setting the answer to zero:
m
∂ ln L(π|x) i=1 xi mn − m i=1 xi set
= − = 0. (7.28)
∂π π 1−π
m
x i
The solution to (7.28) is π = i=1 mn = nx̄ . For π = nx̄ to be a maximum, the second-order
partial derivative of the log-likelihood function must be negative at π = nx̄ . The second-order
partial derivative is
m m
∂ 2 ln L(π|x) − i=1 xi mn − i=1 xi
= − .
∂π 2 π2 (1 − π)2
x̄
Evaluating
m the second-order partial derivative at π = n and using the substitution
i=1 xi = mx̄ yields
Finally, since the values of the likelihood function at the boundaries of the parameter
space, π = 0 and π = 1, are 0, it follows that π = nx̄ is the value that maximizes
the likelihood function. The maximum likelihood estimate π̂(x) = nx̄ and the maximum
likelihood estimator π̂(X) = X
n.
m
ix
To simulate π = i=1mn = nx̄ , generate 1000 random values from a Bin(n = 3, π = 0.5)
population. Pay particular
m attention to the fact that n = 3 and m = 1000.
xi
Calculation of π = i=1 mn
> [Link](23)
> sum(rbinom(1000, 3, 0.5))/(1000 * 3)
[1] 0.5063333
262 Probability and Statistics with R
x̄
Calculation of π = n
> [Link](23)
> mean(rbinom(1000, 3, 0.5))/3
[1] 0.5063333
Next, look for the value that maximizes the log-likelihood function by taking the first-order
partial derivative of (7.30) and setting the answer to zero:
n
∂ ln L(λ|x) xi set
= −n + i=1 = 0. (7.31)
∂λ λ
n
x i
The solution to (7.31) is λ = i=1 n = x̄. For λ = x̄ to be a maximum, the second-order
partial derivative of the log-likelihood function must be negative at λ = x̄. The second-order
partial derivative is n
∂ 2 ln L(λ|x) i=1 xi
2
= − .
∂λ λ2
Evaluating the second-order partial derivative at λ = x̄ yields
∂ 2 ln L(λ|x) nx̄ n
2
= − 2 = − < 0.
∂λ x̄ x̄
Finally, since the values of the likelihood function at the boundaries of the parameter
space, λ = 0 and λ = ∞, are 0, it follows that λ = x̄ is the value that maximizes
the likelihood function. The maximum likelihood estimate λ̂(x) = x̄ and the maximum
likelihood estimator λ̂(X) = X.
To simulate λ̂(x) = x̄, generate 1000 random values from a Pois(λ = 5) population:
> [Link](99)
> mean(rpois(1000, 5))
[1] 4.986
Example 7.19 A box contains five pieces of candy. Some of the candies are alcoholic,
and some are not. In an attempt to estimate the proportion of alcoholic candies, a sample
of size n = 3 is taken with replacement that results in (a, a, n) (two alcoholic candies and
one non-alcoholic candy). Write out the maximum likelihood function and use it to select
the maximum likelihood estimate of π, the true proportion of alcoholic candies.
Point Estimation 263
Solution: The possible values for π are 05 , 15 , 25 , 35 , 45 , and 55 . Since there is at least one
alcoholic candy and there is at least one non-alcoholic candy, the values π = 0 and π = 1
must be ruled out. In this case, the observed sample values are x=(a, a, n). The likelihood
function is
L(π|x) = f (x|π)
= f (a|π) × f (a|π) × f (n|π).
Box π L(π|a, a, n)
aaaan 4
5
4
5 · 4
5 · 1
5 = 6
125
aaann 3
5
3
5 · 3
5 · 2
5 = 18
125
aannn 2
5
2
5 · 2
5 · 3
5 = 12
125
annnn 1
5
1
5 · 1
5 · 4
5 = 4
125
Example 7.20 General MLE The random variable X can take on the values 0,
1, 2, and 3 with probabilities P(X = 0) = p3 , P(X = 1) = (1 − p)p2 , P(X = 2) = (1 − p)2 ,
and P(X = 3) = 2p(1 − p), where 0 < p < 1.
(a) Do the given probabilities for the random variable X satisfy the conditions for a
probability distribution of X?
(b) Find the maximum likelihood estimate for p if a random sample of size n = 150 resulted
in a 0 twenty-four times, a 1 fifty-four times, a 2 thirty-two times, and a 3 forty times.
(c) Graph the log-likelihood function and determine its maximum using either the function
nlm() or the function nlmin().
Solution: The answers are as follows:
(a) For the distribution of X to be a valid pdf, it must satisfy the following two conditions:
Condition (1) is satisfied since 0 < p < 1. Condition (2) is also satisfied since
Next, look for the value that maximizes the log-likelihood function by taking the first-order
partial derivative of (7.32) with respect to p and setting the answer equal to zero:
The solution to (7.33) is p = 0.58. In order for p = 0.58 to be a maximum, the second-order
partial derivative of (7.32) with respect to p must be negative. Since
this value is a global maximum. Therefore, the maximum likelihood estimate of p, p̂(x) =
0.58.
(c) Generic S code to graph the log-likelihood function depicted in Figure 7.4 is
To compute the maximum of the log-likelihood function, use the command nlm(loglike,
0.001) with R and the command nlmin(loglike, 0.001) with S-PLUS:
Warning messages:
1: In log(1 - p) : NaNs produced
2: In nlm(loglike, 0.001) : NA/Inf replaced by maximum positive value
3: In log(1 - p) : NaNs produced
4: In nlm(loglike, 0.001) : NA/Inf replaced by maximum positive value
5: In log(1 - p) : NaNs produced
6: In nlm(loglike, 0.001) : NA/Inf replaced by maximum positive value
7: In log(1 - p) : NaNs produced
8: In nlm(loglike, 0.001) : NA/Inf replaced by maximum positive value
Example 7.21 A farmer cans and sells mild and hot peppers at the local market. The
farmer recently hired an assistant to label his products. The assistant is new to working
with peppers and has mislabeled some of the hot peppers as mild peppers. The farmer
performs a random check of 100 of the mild pepper cans labeled by the assistant to assess
his work. Out of the 100 cans labeled mild peppers, it turns out that 8 are actually hot
peppers.
(a) Which of the following proportions, 0.05, 0.08, or 0.10, maximizes the likelihood func-
tion?
(b) What is the maximum likelihood estimate for the proportion of cans the assistant has
mislabeled?
Solution: The answers are as follows:
(a) First define the random variable X as the number of mislabeled cans. In this definition
of the random variable X, it follows that n = 100 and m = 1 since X ∼ Bin(100, θ).
The likelihood function for a random sample of size m from a Bin(n, π) population was
computed in (7.26) as
;m
n xi
L(π|x) = π (1 − π)n−xi .
i=1
xi
1
f (x|θ) = , 0 ≤ x ≤ θ.
θ
The likelihood function is
⎧
⎨1 for 0 ≤ x1 ≤ θ, 0 ≤ x2 ≤ θ, . . . , 0 ≤ xn ≤ θ
n
L(θ|x) = θ
⎩
0 otherwise.
In this problem, the standard calculus approach fails since the maximum of the likelihood
function occurs at a point of discontinuity. Consider the graph in Figure 7.5. Clearly θ1n
is maximized for small values of θ. However, the likelihood function is only defined for
θ ≥ max(xi ). Specifically, if θ < max(xi ), L(θ|x) = 0. It follows then that the maximum
likelihood estimator is θ̂(X) = max(Xi ). The following code finds the maximum likelihood
estimate of 1000 randomly generated Unif (0, 3) random variables:
> [Link](2)
> max(runif(1000, 0, 3))
[1] 2.99781
Thus, even though a standard calculus approach could not be used, the mle 2.998667 is
quite good for θ = 3.
L(θ|x)
max(xi ) θ
1 (x−μ)2
f (x) = √ e− 2σ2 , −∞ < x < ∞.
2πσ 2
;
n ;
n
1 (xi −μ)2
L(μ|x) = f (xi ) = √ e− 2σ2 , (7.34)
i=1 i=1 2πσ 2
∂ 2 ln L(μ|x) n
= − 2 < 0. (7.37)
∂μ2 σ
Since (7.34) goes to zero at ±∞, the boundary values, it follows that μ = x̄ is a global
maximum. Consequently, the maximum likelihood estimator of μ is μ̂(X) = X, and the
maximum likelihood estimate of μ is μ̂(x) = x̄.
1 (x−μ)2
f (x) = √ e− 2σ2 , −∞ < x < ∞.
2πσ 2
;
n ;
n
1 (xi −μ)2
L(σ 2 |x) = f (xi ) = √ e− 2σ2 , (7.38)
i=1 i=1 2πσ 2
To find the value of σ 2 that maximizes ln L(σ 2 |x), take the first-order partial derivative of
(7.39) with respect to σ 2 , set the answer equal to zero, and solve. The first-order partial
derivative of ln L(σ 2 |x) with respect to σ 2 is
n
∂ ln L(μ, σ 2 |x) i=1 (xi − μ) set
2
n
= − + = 0. (7.40)
∂σ 2 2σ 2 2σ 4
n n
(x −μ)2 (x −μ)2
The solution to (7.40) is σ 2 = i=1 n i . For σ 2 = i=1 n i to be a maximum, the
second-order partial derivative of the log-likelihood function with respect to σ 2 must be
negative at σ 2 = s2u . For notational ease, let r = σ 2 in (7.39) so that
n
n n (xi − μ)2
ln L(r|x) = ln L(σ 2 |x) = − ln(2π) − ln(r) − i=1 . (7.41)
2 2 2r
The second-order partial derivative of (7.41) is
n
∂ 2 ln L(r|x) n ?
2
= r−2 − (xi − μ)2 r−3 < 0. (7.42)
∂r 2 i=1
Example 7.25 Use [Link](33) to generate 1000 N (4, 1) random variables. Write
log-likelihood functions for the simulated random variables and verify that the simulated
maximum likelihood estimates for μ and σ 2 are reasonably close to the true parameters.
Produce side-by-side graphs of ln L(μ|x) and ln L(σ 2 |x) indicating where the simulated
maximum occurs in each graph.
Solution: The code provided is for R. For the given code to function in S-PLUS, replace
the function nlm() with nlmin().
Note that the maximum likelihood estimates for μ and σ 2 from the simulation are 4.019708
and 1.000426, respectively, which are reasonably close to the parameters μ = 4 and σ 2 = 1.
−1450
ln L(σ 2 |x)
ln L(μ|x)
−1500
−1550
μ σ2
This expression was briefly mentioned as the denominator of (7.6), the CRLB. However,
the denominator of (7.6) used the form
# 2 $
∂ ln f (X|θ)
n·E , (7.45)
∂θ
which is equivalent to (7.44) for random samples. Assume that X is a continuous random
variable with pdf f (x|θ) (discrete random variables are handled in a similar fashion by
exchanging integration for summation), where the following regularity conditions for f (x|θ)
are satisfied:
1. The limits of support of f (x|θ) do not depend on θ.
2. The first two derivatives of f (x|θ) exist.
3. The order of integration and differentiation can be exchanged.
The inverse of the information number provides a bound for the variance of the best unbiased
estimator of θ. Consequently, it makes sense to say the information number for a random
sample of size n denoted In (θ) is the variance of the first-order partial derivative of the
log-likelihood function. That is,
! "
∂ ln f (X|θ)
In (θ) = Var . (7.46)
∂θ
When a random sample X1 , X2 ,
> . . . , Xn is taken from a pdf f (x|θ), recall that f (x|θ) =
n n
i=1 f (xi |θ) so that ln f (x|θ) = i=1 ln f (xi |θ). When the random sample is of size n = 1,
the Fisher information is denoted as simply I(θ), which is defined as
! "
∂ ln f (X|θ)
I(θ) = Var . (7.47)
∂θ
Since the random variables are independent, it should be clear that In (θ) = nI(θ). The two
common forms of expressing the information number for a random sample of size n are
# 2 $ # 2 $
∂ ln f (X|θ) ∂ ln f (X|θ)
In (θ) = E = nI(θ) = nE , (7.48)
∂θ ∂θ
Example 7.26 Given the pdf of a normal distribution with unknown mean μ and known
variance σ 2 , find the Fisher information for μ using both (7.48) and (7.49) given a random
sample of size n from said normal distribution.
Solution: According to (4.23), the pdf of a random variable X ∼ N (μ, σ) is
1 (x−μ)2
f (x) = √ e− 2σ2 , −∞ < x < ∞.
2πσ 2
The likelihood function is
;
n ;
n
1 (xi −μ)2
L(μ|x) = f (xi ) = √ e− 2σ2 ,
i=1 i=1 2πσ 2
and the log-likelihood function is
n
n n i=1 (xi − μ)2
ln L(μ|x) = − ln(2π) − ln(σ 2 ) − .
2 2 2σ 2
Note that
∂ ln f (x|μ) (x − μ)
= ,
∂μ σ2
and
∂ 2 ln f (x|μ) 1
2
= − 2.
∂μ σ
Using (7.48), write
# 2 $
∂ ln f (X|μ)
In (μ) = nE
∂μ
# 2 $
X −μ E (X − μ)2 nσ 2 n
= nE 2
=n 4
= 4 = 2.
σ σ σ σ
and ! "
∂ 2 ln f (X|θ)
In (θ) = −E = nIi,j (θ), (7.52)
∂θi ∂θj
are the generalizations of (7.48) and (7.49), respectively.
Example 7.27 Given a random sample of size n from a N (μ, σ) population, where θ =
(μ, σ 2 ), find In (θ).
Solution: According to (4.23), the pdf of a random variable X ∼ N (μ, σ) is
1 (x−μ)2
f (x) = √ e− 2σ2 , −∞ < x < ∞.
2πσ 2
It follows then that
;
n ;
n
1 (xi −μ)2
f (x|θ) = f (xi |θ) = √ e− 2σ2 ,
i=1 i=1 2πσ 2
and that n
n n (xi − μ)2
ln f (x|θ) = − ln(2π) − ln(σ ) − i=1 2
2
.
2 2 2σ
Taking partial derivatives of ln f (x|θ) with respect to θ1 = μ, and θ2 = σ 2 gives
n
∂ ln f (x|θ) (x − μ)
= i=1 2 ,
∂θ1 σ
∂ 2 ln f (x|θ) n
= − 2,
∂θ1 ∂θ1 σ
n
∂ ln f (x|θ) n (xi − μ)2
= − 2 + i=1 2 2
∂θ2 2σ 2(σ )
n
2
∂ ln f (x|θ) n (xi − μ)2
= 2 2
− i=1 2 3 , and
∂θ2 ∂θ2 2(σ ) (σ )
n
∂ 2 ln f (x|θ) ∂ 2 ln f (x|θ) (xi − μ)
= = − i=1 2 2 .
∂θ1 ∂θ2 ∂θ2 ∂θ1 (σ )
Using (7.52) gives
⎛ % ( % (⎞
∂ 2 ln f (X|θ) ∂ 2 ln f (X|θ)
! " ⎜ −E −E
∂ 2 ln f (X|θ)
∂θ1 ∂θ1 ∂θ1 ∂θ2 ⎟
⎜ ⎟
In (θ) = −E = ⎜ ⎟,
∂θi ∂θj ⎝ % ( % (⎠
∂ 2 ln f (X|θ) ∂ 2 ln f (X|θ)
−E ∂θ2 ∂θ1 −E ∂θ2 ∂θ2
or
⎛ % n ( ⎞
(Xi −μ)
−E − σn2 −E − i=1
⎜ 2
(σ ) 2
⎟
⎜ ⎟
In (θ) = ⎜ ⎟,
⎝ % n ( % n ( ⎠
(Xi −μ) i=1 (Xi −μ)
2
−E − i=1 2
(σ ) 2 −E 2(σ2 )2 −
n
(σ2 )3
The statement in (7.53) is the basis for large sample hypothesis tests (covered in
Chapter 9) and confidence intervals (covered in Chapter 8).
Note that the asymptotic variance of MLEs equals the Cramér-Rao lower bound
since they are asymptotically efficient. That is, MLEs ⇒ asymptotic efficiency.
Consequently, a reasonable approximation to the distribution of θ̂(X) for large sample
sizes can be obtained. However, a normal distribution for θ̂(X) cannot be guaranteed
when the sample size is small.
Example 7.28 In Example 7.17 on page 261, it was found that the sample proportion
m of
xi
successes for a random sample of size m from a Bin(n, π) distributionhad π̂ = i=1
mn for
m
i X
its mle. That is, the MLE for the binomial proportion π is π̂(X) = i=1 mn . What is the
MLE for the
mvariance of the sample proportion of successes where the random variable π̂ is
i=1 Xi
defined as mn ?
Solution: Given that X ∼ Bin(n, π), the variance of X is nπ(1 − π). Therefore,
! m " m
i=1 Xi Var [Xi ] mnπ(1 − π) π(1 − π)
Var [π̂] = Var = i=1 2 2 = 2 2
= .
mn m n m n mn
Since Var [π̂] is a function of the MLE π̂(X), it follows using the invariance property of
MLEs that the MLE of the variance of π̂ is
+ π̂(X) = π̂(1 − π̂) .
Var
mn
Note: Many texts will list the MLE of the variance of the sample proportion of successes
in a binomial distribution as π̂(1−π̂)
n because they use m = 1 in their definition of π̂.
274 Probability and Statistics with R
Example 7.29 MOM and MLE for a Gamma Given a random sample of size
n from a population with pdf
x −x
f (x|θ) = e θ, x ≥ 0, θ > 0,
θ2
(a) Find an estimator of θ using the method of moments.
(b) Find an estimator of θ using the method of maximum likelihood.
(c) Are the method of moments and maximum likelihood estimators of θ unbiased?
(d) Compute the variance of the MLE of θ.
(e) Is the MLE of θ efficient?
Solution: Since X ∼ Gamma α = 2, λ = θ1 , according to (4.16), E [X] = α
λ = 2θ and
Var [X] = λα2 = 2θ2 .
(a) Equating the first population moment about the origin to the first sample moment about
the origin gives
set
α1 (θ) = 2θ = X = m1 ,
X
which implies that the method of moments estimator for θ is θ̃ = 2.
To find the value of θ that maximizes ln L(θ|x), take the first-order partial derivative of
(7.55) with respect to θ, set the answer equal to zero, and solve. The first-order partial
derivative of ln L(θ|x) with respect to θ is
n
∂ ln L(θ|x) 2n xi set
=− + i=1 2
= 0. (7.56)
∂θ θ θ
The solution to (7.56) is θ = X 2 , which agrees with the method of moments estimator.
X
However, to ensure that θ = 2 is a maximum, the second-order partial derivative with
respect to θ must be negative. The second-order partial derivative of (7.55) is
n
∂ 2 ln L(θ|x) 2n 2 i=1 xi ?
= − < 0. (7.57)
∂θ2 θ2 θ3
X
By using θ = 2 in (7.57), arrive at the expression
12n ?
− < 0. (7.58)
X2
0
The ? above the < in (7.58) can be removed since 0 f (x) dx = 0 ⇒ X > 0. Finally,
since (7.54) goes to zero as θ → ∞, it can be concluded that θ = X
2 is a global maximum.
X
Consequently, the maximum likelihood estimator of θ is θ̂ (X) = 2 .
Point Estimation 275
(c) Since both the method of moments and the method of maximum likelihood returned
the same estimator for θ, that is, θ̂ (X) = θ̃ = X
2 , the question is
?
E θ̂(X) = E θ̃ = θ.
θ2 ? 1
Var θ̂ (X) =
2n
= !& '2 "
?
∂ ln f (X|θ)
n·E ∂θ
x
Since f (x|θ) = θx2 e− θ for x ≥ 0, and θ > 0, it follows that ln f (x|θ) = ln x − 2 ln θ − xθ , and
that ∂ ln ∂θ
f (x|θ)
= x−2θ
θ 2 . Consequently,
1 1 1 1 θ2
!& '2 "
=
% (
= = = ,
∂ ln f (X|θ)
X−2θ 2 n·Var [X]
2n
n·E ∂θ n·E θ2 θ4
n·2θ 2
θ4
X
and conclude that 2 is an efficient estimator of θ.
Example 7.30 MLEs for Exponentials Given a random sample of size n from
an exponential distribution with pdf
1 −x
f (x) = e θ x ≥ 0, θ > 0, (7.59)
θ
(a) Find the MLE of θ2 .
(b) Show that the MLE of θ2 is a biased estimator of θ2 .
(c) Provide an unbiased estimator of θ2 .
(d) Find the variance of your MLE of θ2 .
(e) Find the variance of your unbiased estimator of θ2 .
(f) Show that the variance for the MLE of θ2 converges to In (θ)−1 as n → ∞ according to
property 4 of the Properties of MLEs on page 273.
Solution: To find the MLE of θ2 , there are two possibilities. First, the MLE of θ could
be found and the invariance property could be used to say that this estimate squared is the
MLE of θ2 . (See problem 37 of this chapter.) Second, and this is the current approach, the
MLE of θ2 can be found directly.
276 Probability and Statistics with R
√
(a) For notational ease, use the change of variable θ2 = p and θ = p in (7.59). The
resulting pdf using the change of variable is
1 − √x
f (x) = √ e p x ≥ 0, p > 0.
p
;
n ;
n
1 − √xip 1
n
− i=1
√
xi
L(p|x) = f (xi ) = √ e = √ n e p
, (7.60)
p ( p)
i=1 i=1
To find the value of p that maximizes ln L(p|x), take the first-order partial derivative of
(7.61) with respect to p, set the answer equal to zero, and solve. The first-order partial
derivative of ln L(p|x) with respect to p is
n
∂ ln L(p|x) n xi set
=− + i=1
3 = 0. (7.62)
∂p 2p 2p 2
The solution to (7.62) is p = x̄2 . For p = x̄2 to be a maximum, the second-order partial
derivative of the log-likelihood function with respect to p must be negative at p = x̄2 . The
second-order partial derivative of (7.61) is
n
∂ 2 ln L(p|x) n 3 i=1 xi ?
= − 5 < 0. (7.63)
∂p2 2p2 4p 2
By substituting p = x̄2 in the right-hand side of (7.63), the ? above the < can be removed
2 because x̄ > 0 for any sample due to the fact that P(X = 0) = 0 for any
since x̄ < 3x̄
continuous distribution. Finally, since as p → ∞, L(p|x) → 0, it can be concluded that the
MLE of p = θ2 is p̂(X) = θ:2 (X) = X .
2
2
(b) Next, show that X is a biased estimator of θ2 . The easiest way to determine the
2
mean and variance of X is with moment generating functions. It is known that the
moment generating function
n of an exponential random variable, X, is MX (t) = (1 − θt)−1 .
Furthermore, if Y = i=1 ci Xi and each Xi has a moment >n generating function MXi (t),
then the moment
n
generating function of Y is M Y (t) = i=1 MXi (ci t). In the case where
i=1 Xi 1
Y = X = n , each ci = n . For the special case of the exponential, the moment
generating function for X is
;n −1 −n
t θt
MX (t) = MY (t) = 1−θ· = 1− .
i=1
n n
2
Thus, to calculate the mean and variance of X , take
ithe
first through fourth derivatives
of MX (t) and evaluate them when t = 0 to find E X for i = 1, 2, 3, and 4. The first,
Point Estimation 277
Evaluating these derivatives at t = 0 gives the expected values of X to the first, second,
third, and fourth powers:
MX (0) = θ = E X
θ2 (n + 1) 2
MX (0) = =E X
n
θ3 (n + 1)(n + 2) 3
MX (0) = 2
=E X
n
θ4 (n + 1)(n + 2)(n + 3) 4
(4)
MX (0) = 3
=E X
n
2 θ2 (n + 1) 2
Since E X = = θ2 , X is a biased estimator of θ2 .
n
nX2
(c) An unbiased estimator of θ2 would be to use the quantity n+1 .
2
4 & 2 '2
(d) The variance of X can be computed as E X − E X :
2 θ4 (n + 1)(n + 2)(n + 3) θ2 (n + 1) 2
Var X = −
n3 n
4 2
2θ (2n + 5n + 3)
= 3
4
n
2θ (2n + 3)(n + 1)
= (7.64)
n3
4θ 4
Since p = θ2 , it follows that In (p) = In (θ2 ) = n
4θ 4 , and that In (θ2 )−1 = n . Note that
2 2 −1 4θ 4
the variance of the MLE estimator X given in (7.64) converges to In (θ ) = n as
n → ∞.
The value of θ that maximizes L (θ|x) is the mle of θ. In the multiple parameter case, denote
the mle of θ as θ̂(x) and the MLE of θ as θ̂(X).
As with the univariate case, typically work
with the log-likelihood function ln L(θ|x) instead of the likelihood function. If L (θ|x) is
differentiable with respect to θ, a possible mle for θ are the θi s, i = 1, . . . , k, that solve
⎧ n
⎪
⎪ ∂ ln L (θ|x) = ∂ ln f (xi |θ) set
⎪
⎪ =0
⎪
⎪ ∂θ1 ∂θ1
⎪
⎪ i=1
⎪
⎪
⎪
⎪
∂ ln L (θ|x) set ⎨
= 0⇔ .. .. (7.66)
∂θ ⎪ . .
⎪
⎪
⎪
⎪
⎪
⎪
⎪
⎪ n
⎪
⎪ ∂ ln L (θ|x) ∂ ln f (xi |θ) set
⎪
⎩ = = 0.
∂θk i=1
∂θk
Just as with the univariate case, possible mles for θ are the solutions to (7.66). Solutions to
the k equations in (7.66) are a necessary but not sufficient condition for the solutions to be
maximums. However, a sufficient condition to guarantee the solutions to (7.66) are maxima
is for the Hessian matrix (matrix whose elements are the second-order partial derivatives
with respect to the parameters being estimated) to be negative definite when evaluated at
the maximum likelihood estimators. Any symmetric p × p matrix is negative definite pro-
vided the leading principal minors (the determinants of the upper left square submatrices)
have alternating signs where the top left element in the matrix is negative. These principal
minors are denoted by Di for i = 1, . . . , p and satisfy the following conditions: D1 < 0,
D2 > 0, . . . , ending with Dp > 0 if p is even and Dp < 0 if p is odd (Casella and Berger,
1990). Furthermore, the solutions to (7.66) will yield minima when the determinants of the
leading principal minors are all positive.
Example 7.31 Given a random sample of size n from a normal distribution with unknown
mean μ and variance σ 2 , find the MLEs for μ and σ 2 .
Point Estimation 279
To find the θ that maximizes (7.67), take the first-order partial derivatives with respect to
θ = (μ, σ 2 ), set those first-order partial derivatives equal to zero, and solve the simultaneous
equations:
⎧ n
⎪ ∂ ln L μ, σ 2 |x (xi − μ) set
⎪
⎪ = i=1 2 =0
⎪
⎨ ∂μ σ
∂ ln L (θ|x) set
= 0⇔
∂θ ⎪
⎪ n
⎪
⎩ ∂ ln L μ, σ |x = − n + i=1 (xi − μ) set
⎪ 2 2
= 0.
∂σ 2 2σ 2 2σ 4
The solution to the system of equations is
n
2 (xi − x̄)2
μ = x̄ and σ = i=1 .
n
A sufficient condition for the values in θ to be maximums is for the Hessian matrix to be
negative definite. In this case, the Hessian matrix is
⎛ ∂ 2 ln L(μ,σ2 |x) ∂ 2 ln L(μ,σ2 |x)
⎞
⎜ ∂μ2 ∂μ∂σ2
⎟
H= ⎜ ⎟.
⎝ ⎠
∂ 2 ln L(μ,σ2 |x) ∂ 2 ln L(μ,σ2 |x)
∂σ2 ∂μ ∂(σ2 )2
Specifically, the second-order partial derivatives are
∂ 2 ln L μ, σ 2 |x n
= − 2,
∂μ2 σ
n
∂ 2 ln L μ, σ 2 |x n 1
= − 6 (xi − μ)2 , and
∂(σ 2 )2 2σ 4 σ i=1
n
∂ 2 ln L μ, σ 2 |x 1
= − (xi − μ).
∂μ∂σ 2 σ 4 i=1
n
i=1 (xi −x̄)
2
By substituting the values μ = x̄ and σ 2 = n = s2u in the second-order partial
derivatives, the Hessian matrix is expressed as
⎛ ⎞
− sn2 0
⎜ u ⎟
H= ⎜ ⎟.
⎝ ⎠
0 − 2sn4
u
280 Probability and Statistics with R
n2
Note that H is negative definite since D1 = − sn2 < 0 and D2 = 2s6u > 0, implying that
n u
i=1 (xi −x̄)
2
2
the solutions, μ = x̄ and σ = n , are maximums. Finally, the solutions μ = x̄
n
i=1 (xi −x̄)
2
2
and σ = n can be considered global maximums since the likelihood function
goes to zero for both μ = ±∞ and σ 2 = ∞. Consequently,
the MLE of θ is written as
θ̂(X) = X, Su , and the mle of θ as θ̂(x) = x̄, s2u .
2
Example 7.32 Use [Link](11) to generate 500 values from a N (2, 1) population, and
treat the generated values as a random sample of size n = 500 from a normal distribution
with unknown parameters. Find the maximum likelihood estimates for μ and σ 2 based on
the generated sample.
Solution: According to the results of Example 7.31 on page 278, the MLE of θ when
sampling from a normal distribution with unknown mean and variance is θ̂(X) = X, Su2 .
The following S code performs the simulation:
> [Link](11)
> n <- 500
> x <- rnorm(n, 2, 1)
> mean(x)
[1] 1.997360
> S2u <- sum((x - mean(x))^2/n)
> S2u
[1] 0.9764792
Example 7.33 Given a random sample of size n from a N (μ, σ) population, find the MLE
of the variance of X and the variance of Su2 .
Solution: In Example 7.31 on page 278, the MLE of θ was θ̂(X) = X, Su2 , and in
Example 7.27 on page 272, the Fisher information matrix was
⎛ ⎞
n
0
⎜ σ2 ⎟
In (θ) = ⎜
⎝
⎟.
⎠
n
0 2σ4
Consequently,
⎛ ⎞−1
n
0
−1 ⎜ σ2 ⎟
In+
(θ) = In (θ)−1 |θ=θ̂(X) = ⎜
⎝
⎟
⎠
0 n
2σ4 θ=θ̂(X)
⎛ ⎞−1 ⎛ 2
⎞
n Su
2 0 0
⎜ Su ⎟ ⎜ n ⎟
=⎜
⎝
⎟
⎠ =⎜
⎝
⎟,
⎠
4
n 2Su
0 4
2Su 0 n
7.4 Problems
1. Use the data from the data frame WheatSpain to answer the questions.
(a) Find the mean, median, standard deviation, and MAD of the [Link].
(b) Remove the Castilla-Leon community and find again the mean, median, standard
deviation, and MAD of the same variable. Which statistics are preferred as measures
for these data? Comment on the results.
2. Given the estimators of the mean T1 = (X1 + 2X2 + X3 )/4 and T2 = (X1 + X2 + X3 )/3,
where X1 , X2 , X3 is a random sample from a N (μ, σ) distribution, prove that T2 is more
efficient than T1 .
3. Given a random sample of size n + 1 from a N (μ, σ) distribution, show that the median,
m, is roughly 64% less efficient than the sample mean for estimating the population
mean. (Hint: In large samples Var(m) = πσ 2 /4n.)
(a) Find the mean squared error of the π parameter estimators T1 = X/n and T2 =
(X + 1)/(n + 2).
(b) When n = 100 and π = 0.4, which estimator, T1 or T2 , has the smaller MSE ?
(c) Plot the efficiency of T1 relative to T2 versus π values in (0, 1) for n values from 1 to
10.
5. Given a random sample of size n from a Γ(2, λ) distribution, consider the following
estimators for 1/λ: n
X Xi
T1 = and T2 = i=1
2 2(n + 1)
(a) Graph the relative efficiency of T2 with regard to T1 for values of λ from 0.01 to 100
with a sample size of 50.
(b) Interpret the graph in (a).
(c) Plot the relative efficiency of both estimators versus sample sizes from 1 to 100.
(d) Interpret the graph in (c).
(e) Generalize your findings.
6. Consider a random variable X ∼ Exp(λ) and two estimators of 1/λ , the expected value
of X: n
Xi + 1
T1 = X and T2 = i=1 .
n+2
(a) Derive an expression for the relative efficiency of T2 with respect to T1 .
(b) Plot eff(T2 , T1 ) versus n values of 1, 2, 3, 4, 20, 25, 30.
(c) Generalize your findings.
7. A baseball pitching machine launches fast balls whose speed follows a N (μ, σ = 5 km/h)
distribution. Given the independent random samples X and Y, where nX = nY = 6,
Point Estimation 283
6
i=2 Yi
(a) Show that the estimators T1 = X and T2 = 5 are unbiased estimators of μ.
(b) Given the estimator T3 = θT1 + (1 − θ)T2 , find the value of θ so that the MSE is a
minimum.
% ( !& '2 " %& '(
∂ ln f (X|θ) ∂ ln f (X|θ) ∂ ln f (X|θ)
8. Verify that Var ∂θ = E ∂θ . (Hint: show that E ∂θ =
0.)
!& '2 " %& '(
∂ ln f (X|θ) ∂ 2 ln f (X|θ)
9. Verify that E ∂θ = −E ∂θ 2 . (Hint: differentiate with respect to
∞
∂ ln f (x|θ)
θ the equation ∂θ f (x|θ)dx = 0.)
−∞
10. The probability of obtaining a tail when flipping a coin can be π = 1/2 , π = 1/3 , or
π = 2/3 . To estimate π, the coin is flipped three times and one head is obtained on the
first flip and tails on the second and third flips. Find the maximum likelihood estimator
of π.
11. A manufacturer produces needles for a sewing machine in 5 units per parcel. The parcels
are in boxes of 120 units. The manufacturer guarantees that only one out of 100 parcels
is defective; however, the owner of a store thinks that at least 4 parcels out of 100 are
defective. To solve the controversy, the manufacturer randomly chooses 18 boxes and
checks the number of defective parcels. The results follow:
13. The following random samples X=(x1 , . . . , x7 ) and Y=(y1 , . . . , y10 ) are drawn from
Pois(λ) and Pois(2λ), respectively:
X ∼ Pois(λ) 4 2 5 7 3 4 3
Y ∼ Pois(2λ) 6 10 1 6 3 5 5 4 7 5
(a) Derive the maximum likelihood estimator of λ and calculate its variance.
(b) Compute the maximum likelihood estimate of λ and its variance using the two
random samples given.
14. Find the maximum likelihood estimator for μ if samples of size n are taken from a
√
N (μ, σ = μ) distribution.
284 Probability and Statistics with R
(a) Use the maximum likelihood estimator to calculate the maximum likelihood estimate
that results from the sample
4.37, 9.30, 1.67, 1.25, 4.30, 6.97, 2.68, 5.49, 4.36, 4.46.
(b) Plot the log-likelihood function versus μ for values between 4 and 5.
15. Given a random sample of size n from a distribution with a density function given by
θ+1
1
f (x) = θ , x ≥ 1, θ > 1,
x
(a) Find the method of moments and the maximum likelihood estimators of θ.
(b) Find the method of moments and maximum likelihood estimates of θ for the sample
{3, 4, 2, 1.5, 4, 2, 3, 2, 4, 2}.
(c) Set the seed equal to 42, and generate 1000 values from f (x) using θ = 3. Compute
the method of moments and maximum likelihood estimates of θ using the generated
values.
16. Given the density function
(a) Find the maximum likelihood estimator of θ for a random sample of size n.
(b) Set the seed equal to 88, and generate 1000 values from f (x) when θ = 2. Calculate
the maximum likelihood estimate of θ from the generated values.
(c) How close is the maximum likelihood estimate in (b) to θ = 2?
17. Given the density function
3 2 −x3 /λ
f (x) = x e , x > 0, λ > 0,
λ
(a) Find the maximum likelihood estimator of λ for a random sample of size n.
(b) Verify that the maximum likelihood estimator is unbiased, consistent, and efficient.
(c) Find the method of moments estimator of λ for a random sample of size n.
18. Given an exponential distribution with mean θ and the following estimators of θ:
X1 + X2
θ̂1 = X1 , θ̂2 = , θ̂3 = X, θ̂4 = min{X1 , X2 , X3 },
2
(a) Find the mean and variance of each estimator.
(b) Are any of the estimators efficient?
(c) Which estimator is the MLE?
(d) Let X be an exponential random variable with mean θ + 2. Which estimator is an
unbiased estimator of θ?
19. Given a random sample of size n from a population of size N , where the items in the
population are sequentially numbered from 1 to N ,
(a) Derive the method of moments estimator of N .
Point Estimation 285
20. The lifetime of a particular resistor follows an exponential distribution with parameter
λ. The manufacturer claims the mean life of the resistor is 6 years. A distributor of the
resistor is suing the manufacturer for excess warranty claims, saying that the mean life
of the resistor is a mere 4 years. To resolve the issue, an accelerated test of the predicted
lifetimes of 20 resistors is undertaken, yielding the following values:
3.70 1.76 3.63 15.73 5.85 0.20 9.87 14.55 0.43 2.46
0.45 5.09 10.53 12.41 3.19 3.41 3.80 1.66 0.40 1.10
(a) The judge calls you as an expert witness to determine the validity of the suit. What
do you tell the judge?
(b) What value of λ maximizes the probability for values reported from the experiment.
(c) Graph the log-likelihood function versus λ values ranging from 0 to 0.5.
21. Data frame birthwt from the MASS package has 10 variables recorded for each of 189
babies born at a U.S. hospital. The variable low takes the value 1 when the baby weighs
less than 2.5 kg and 0 otherwise.
22. In 1876, Charles Darwin had his book The Effect of Cross- and Self-Fertilization in
the Vegetable Kingdom published. Darwin planted two seeds, one obtained by cross-
fertilization and the other by auto-fertilization, in two opposite but separate locations of
a pot. Self-fertilization, also called autogamy or selfing, is the fertilization of a plant with
its own pollen. Cross-fertilization, or allogamy, is the fertilization with pollen of another
plant, usually of the same species. Darwin recorded the plants’ heights in inches. The
data frame Fertilize from the PASWR package contains the data from this experiment.
(a) Create a variable DD defined as the difference between the variables Cross-fert and
Self-fert.
(b) Perform an exploratory analysis of DD to see if DD might follow a normal distribution.
(c) Use the function fitdistr() found in the MASS package to obtain the maximum
likelihood estimates of μ and σ if DD did follow a normal distribution.
(d) Verify that the results from (c) are the sample mean and the uncorrected sample
standard deviation of DD.
286 Probability and Statistics with R
(a) Find the method of moments estimates of α and λ if the data come from a Γ(α, λ)
distribution.
(b) Create a density histogram of times until failure. Superimpose a gamma distribution
using the estimates from part (a) over the density histogram.
(c) Find the maximum likelihood estimates of α and λ if the data come from a Γ(α, λ)
distribution by using the function fitdistr() from the MASS package.
(d) Create a density histogram of times until failure. Superimpose a gamma distribution
using the estimates from part (c) over the density histogram.
(e) Plot the cumulative distribution for time until failure using the ecdf() function.
Superimpose the theoretical cumulative gamma distribution using both the method
of moments and the maximum likelihood estimates of α and λ. Which estimates
appear to model the data better?
26. The time a client waits to be served by the mortgage specialist at a bank has density
function
1
f (x) = 3 x2 e−x/θ x > 0, θ > 0.
2θ
(a) Derive the maximum likelihood estimator of θ for a random sample of size n.
(b) Show that the estimator derived in (a) is unbiased and efficient.
(c) Derive the method of moments estimator of θ.
(d) If the waiting times of 15 clients are 6, 12, 15, 14, 12, 10, 8, 9, 10, 9, 8, 7, 10, 7, and
3 minutes, compute the maximum likelihood estimate of θ.
Point Estimation 287
Time bubbles
[0, 50) 25
[50, 100) 19
[100, 150) 11
[150, 200) 8
[200, 250) 9
[250, 300) 7
[300, 450) 22
[450, 1050) 8
1 1−θ
f (x) = x θ , 0 < x < 1, 0<θ<∞
θ
(a) Derive the maximum likelihood estimator of θ for a random sample of size n.
(b) Derive the method of moments estimator of θ for a random sample of size n.
(c) Show that the maximum likelihood estimator is unbiased.
31. Given the density function
(a) What distribution has this density function? Be sure to specify the parameter.
(b) Find the maximum likelihood estimator of θ for random samples of size n.
(c) Find the asymptotic variance of the maximum likelihood estimator.
(d) Find the method of moments estimator of θ for a random sample of size n.
(e) Calculate the maximum likelihood and method of moments estimates of θ for the
sample {0.1, 0.7, 0.5, 0.85, 0.9}.
θ+1
1
f (x) = θ , x ≥ 1, θ > 1
x
(a) Find the maximum likelihood estimator of θ for a random sample of size n.
(b) Find the method of moments estimator of θ for a random sample of size n.
(c) Calculate the maximum likelihood and method of moments estimates of θ using the
sample values {2, 3, 2, 2.5, 1, 2, 2, 3, 1, 4, 6, 3, 4.4}.
(d) Find the mean of the distribution.
(e) Estimate the mean of the distribution using the maximum likelihood estimate of θ.
(a) Find the maximum likelihood estimator of θ for a random sample of size n.
(b) Show that the maximum likelihood estimator is unbiased and efficient.
(Hint: Denote the number of observations as n1 in the sample so that 0 < xi ≤ 1/2.)
(a) Set the seed equal to 201, and generate a random sample of size n = 500 with θ = 5.
(b) Find the sample mean and the sample variance of the random values generated in
(a).
(c) Graph the density function.
(d) Find the maximum likelihood estimate of θ.
(e) Plot the logarithm of the likelihood function versus θ.
36. Set the seed equal to 384, and generate 100 values from a β(α = 3, β = 2, A = 0, B =
1) distribution. Assume that these values are a random sample of size 100 from a β
distribution with unknown parameters. Use maximum likelihood techniques to obtain
estimates of α and β from this sample.
37. Given a random sample of size n from an exponential distribution with pdf
1 −x
f (x) = e θ x ≥ 0, θ > 0, (7.68)
θ
(a) Find the MLE of θ.
(b) Given the answer in part (a), what is the MLE of θ2 .
Chapter 8
Confidence Intervals
8.1 Introduction
In Chapter 7, techniques to find point estimators, such as the method of moments and
maximum likelihood, were introduced as well as were criteria to evaluate the “goodness” of
an estimator. However, even the most efficient unbiased estimator is not likely to estimate
the population parameter exactly. Further, a point estimate provides no information about
the precision or reliability of the estimate. Consequently, the construction of an interval
estimate or confidence interval (CI ), where the user can control the precision (width)
of the interval as well as the reliability (confidence) that the true parameter will be found
in the confidence interval, is desirable.
A (1 − α) confidence interval for a parameter θ, denoted CI 1−α (θ), is constructed by
first selecting a confidence level, denoted by (1 − α) and typically expressed as a percentage,
(1 − α) · 100%. The confidence level is simply a measure of the degree of reliability in the
procedure used to construct the confidence interval. Typical confidence levels are 90%, 95%,
or 99%. A confidence level of 99% implies that 99% of all samples would provide confidence
intervals that would contain θ. Clearly, it is desirable to have a high degree of reliability.
However, with increased reliability, the width of the confidence interval increases. So, the
goal is to construct a confidence interval with a width the practitioner finds useful while
maintaining a degree of reliability that is as high as possible. The relationship between the
width and confidence level in a confidence interval will become clearer once some actual
confidence interval formulas are examined. The confidence interval has two limits, a lower
limit denoted by L(X) and anupper limit denoted by U (X). The confidence level is
defined as P θ ∈ L(X), U (X) . That is, an interval should be constructed such that
P L(X) ≤ θ ≤ U (X) = 1 − α. (8.1)
It is important to note that the interval L(X), U (X) is a random interval since it de-
pends
on the random variables of X. However, after a sample is obtained and values for
L(X), U(X) are calculated,
the probability that the parameter θ will be included in the
interval L(x), U (x) is either 0 or 1, depending, of course, on whether θ is between the
lower limit L(x) and the upper limit U (x). Note that X changes to an x once there are
values, xi , for the random
variables,
Xi . The probability the parameter θ is contained in
the random interval L(X), U (X) from (8.1) is (1 − α). However, once the values for the
random variables are observed, (8.1) is written as
CI 1−α (θ) = L(x), U (x) , (8.2)
291
292 Probability and Statistics with R
in the interval L(x), U (x) .” However, the word confidence in such statements applies to
the procedure used to construct the interval, not the interval itself. That is, if there were
an infinite number of samples, (1 − α) · 100% of them
would contain θ.
Confidence intervals of the form L(x), U (x) are referred to as two-sided confidence
intervals. However, some applications will only require a single bound. For example, only
a lower confidence bound on the mean shear strength of an aluminum tube is required to
ensure the minimum design specification for a top tube of a bicycle is met. Likewise, only
an upper confidence bound on the mean level of NO3 in potable water is required to ensure
the maximum allowable limit is not exceeded. One-sided confidence intervals take the form
P L(X) ≤ θ = 1 − α or P θ ≤ U (X) = 1 − α,
depending on whether the confidence interval is a lower confidence interval, [L(x), ∞), or an
upper confidence interval, (−∞, L(x)], respectively. Unless otherwise specified, a confidence
interval will refer to a two-sided confidence interval.
There are several techniques used to obtain both one-sided and two-sided confidence
intervals. One of the more popular methods for constructing confidence intervals uses pivotal
quantities. A random variable Q(X; θ) is a pivotal quantity or pivot if the distribution of
Q is independent of the parameter θ. A method of constructing confidence intervals using
pivots is introduced in Section 8.2.1 and is used to derived most of the confidence interval
formulas in this chapter.
X−μ
Q(X; μ) = √ ∼ N (0, 1). (8.3)
σ/ n
Subtract X from both sides, multiply both sides by −1, and rearrange the inequalities, to
get
σ σ
P X − z1−α/2 √ ≤ μ ≤ X − zα/2 √ = 1 − α.
n n
Confidence Intervals 293
1−α
α/2 α/2
zα/2 z1−α/2
FIGURE 8.1: Standard normal distribution with an area of α/2 in each tail
Consequently, the (1−α) confidence interval for μ, when sampling from a normal distribution
with known variance, is given by
! "
σ σ
x̄ − z1−α/2 √ , x̄ − zα/2 √ ,
n n
or, equivalently, by recognizing that zα/2 = −z1−α/2 , write the standard form as
! "
σ σ
CI 1−α (μ) = x̄ − z1−α/2 √ , x̄ + z1−α/2 √ . (8.5)
n n
Note that X in the probability statement changes to x̄ in the confidence interval formula.
To obtain a one-sided (either upper or lower) confidence interval in a symmetric distri-
bution, proceed in a similar fashion. That is, write
X−μ X−μ
P −z1−α ≤ √ = 1 − α or P √ ≤ z1−α = 1 − α
σ/ n σ/ n
and rearrange the quantities inside the probability statements to obtain
σ σ
P μ ≤ X + z1−α √ = 1 − α or P X − z1−α √ ≤ μ = 1 − α
n n
Thus,
" !
σ σ
UCI 1−α (μ) = −∞, x̄ + z1−α √ or LCI 1−α = x̄ − z1−α √ , ∞
n n
Note that a one-sided confidence interval can be obtained from a two-sided confidence
interval by simply changing the z1−α/2 value to a z1−α value and replacing the lower or
upper limit with −∞ or ∞, respectively, depending on whether an upper or lower confidence
interval is desired.
Example 8.1 Generate 100 samples, each of size 500, from a N (0, 1) distribution. For
each of the 100 samples of size 500, calculate a 95% confidence interval for the population
mean. Finally, determine how many of the 100 intervals contain the population mean,
μ = 0. This number is the simulated confidence level.
294 Probability and Statistics with R
0.3
0.2
0.1
0.0
-0.1
-0.2
-0.3
0 20 40 60 80 100
FIGURE 8.2: Simulated confidence intervals for the population mean when sampling from
a normal distribution with known variance
In other words, one can be 95% confident that the mean, μ, will be found in the interval
CI 0.95 (μ) = [1.68, 3.46]. It is important to note that the sample mean (x̄ = 2.57) is the center
point of this interval; however, this will only be the case in symmetric distributions.
(a) Construct a 97% confidence interval for the true mean weekly grocery expenditure for
Watauga County households. Historical records indicate that the variance for grocery
expenditure per household in Watauga County is 900 dollars2.
(b) A grocery chain is considering building a new grocery story in Watauga County. How-
ever, it will only do so if it is 99% confident the average amount spent on groceries each
week is at least $105. Does a LCI 0.99 (μ) include $105? I f so, what does that imply?
(a) Before using (8.5), the confidence interval formula for μ with known σ on page 293, it
is necessary to verify that the assumption of normality is satisfied. To do this, create a
normal quantile-quantile plot using the qqnorm() function as follows:
> attach(Grocery)
> qqnorm(groceries)
> qqline(groceries)
120
100
80
60
−2 −1 0 1 2
Theoretical Quantiles
The resulting normal quantile-quantile plot is shown in Figure 8.3. Note that the plotted
values fall relatively close to the plotted line, indicating the assumption of normality is
reasonable. Consequently, one decides the assumption for using (8.5) on page 293 is satisfied
and continues by finding the sample mean:
n
xi 3619
x̄ = i=1 = = 120.63
n 30
Using the historical value of 900 for σ 2 , the 97% confidence interval is given by
! "
σ σ
CI 0.97 (μ) = x̄ − z0.985 √ , x̄ + z0.985 √
n n
# √ √ $
900 900
= 120.63 − (2.17) √ , 120.63 + (2.17) √ .
30 30
In other words, one can be 97% confident that the mean grocery spending will be found in
the interval [108.75, 132.52] dollars.
Confidence Intervals 297
Example 8.4 Determine the required sample size to estimate the true value of μ within
±0.02 with a confidence level of 95% when sampling from a normal distribution with σ = 0.1.
Solution: To determine the required sample size, use (8.7) as follows:
Example 8.5 Suppose a random sample of size n from a normal distribution with un-
known mean μ and standard deviation σ = 5 is taken. Calculate the minimum sample size
so that one can be 95% confident the interval [x̄ − 1, x̄ + 1] contains the true value of μ.
Solution: Given that the sample was taken from a normal distribution with known
variance, one can write
σ σ
P X − (1.96) √ ≤ μ ≤ X + (1.96) √ = 0.95.
n n
Since one needs to be 95% confident the interval [x̄ − 1, x̄ + 1] contains μ, write
P X − 1 ≤ μ ≤ X + 1 = 0.95,
σ
set 1.96 √ = 1, and solve for n given that σ = 5:
n
Since a sample of 96.04 is impossible, take the ceiling of n to make sure the confidence is
at or above the specified level. Consequently, the minimum sample size to be at least 95%
confident the interval [x̄ − 1, x̄ + 1] contains μ is n = 97 when σ = 5.
(a) Let X be the number of failures prior to the r = 100th success (defective container).
The distribution of X is NB(100, 0.03). The problem requests P(X = x|100, 0.03) ≥
0.95. That is, one must find the number x of non-defective containers to guarantee the
probability is at least 0.95 upon obtaining the 100th defective container. The following
S code indicates the total number of containers that must be manufactured to guarantee
100 are defective with a probability over 0.95 is 3891:
(b) Let the random variable X represent the number of defective containers. The distribu-
tion of X is Bin(n, 0.03). Consequently,
E [X ] = nπ = 0.03n and Var [X ] = nπ(1 − π) = 0.0291n.
If it is assumed n is sufficiently large and the production of each container is indepen-
dent, one can approximate the distribution of X using a normal distribution where
P(X ≥ 100) = 0.95.
Equivalently,
X − nπ 100 − nπ 100 − 0.03n
P ≥ =P Z≥ √ = 0.95.
nπ(1 − π) nπ(1 − π) 0.0291n
Note that P(Z ≥ −z1−α ) = 0.95 ⇒ −z1−α = −z0.95 = −1.64, and solve the equation
100 − 0.03n
√ = 1.64, (8.8)
0.0291n
which is equivalent to solving
0.0009n2 − 6.07826n + 1002 = 0. (8.9)
The solutions to (8.9) are n ≈ 3924 or 2832. However, the value 2832 is not acceptable
since it does not satisfy (8.8). Consequently, the number of containers the factory needs
to manufacture to be 95% confident of getting at least 100 defective containers is 3924.
(c) Let the random variable X again represent the number of defective containers. The
distribution of X is Bin(n, 0.03). To solve P(X ≥ 100) ≥ 0.95 with S, use code similar
to what follows and keep in mind that P(X ≥ 100) = 1 − P(X ≤ 99). The following S
code indicates the total number of containers that must be manufactured to guarantee
100 are defective with a probability over 0.95 is 3891 when using the binomial random
variable. This agrees with the answer that was found when modeling the number of
defective containers obtained with a negative binomial random variable.
The confidence intervals discussed in the remainder of this chapter are commonly used
confidence intervals based, for the most part, on the normal distribution. When constructing
confidence intervals, if historical evidence does not support normality or the text narrative
does not explicitly specify the sample information was collected from a normal distribution,
one should not blindly use techniques that require the normality assumption! Checking
normality assumptions graphically with normal quantile-quantile plots as discussed in Sec-
tion 4.3.7 on page 158 should become a habit.
8.2.2 Confidence Interval for the Population Mean when Sampling from
a Normal Distribution with Unknown Population Variance
Suppose a random sample of size n is taken from a normal distribution with unknown
mean μ and unknown variance σ 2 . To construct a confidence interval for μ, use the pivotal
quantity
√
n X−μ
Q(X; μ) = ∼ tn−1 .
S
Operating in a similar fashion to the derivation of the confidence interval for μ, using (8.3)
from Section 8.2.1, one obtains the interval
! "
s s
CI 1−α (μ) = x̄ − t1−α/2;n−1 √ , x̄ + t1−α/2;n−1 √ . (8.10)
n n
Example 8.7 A random sample of size 12 is taken from a population that follows a
N (μ, σ) distribution where the value for σ is unknown. Given:
12 12
xi = 61.9, and x2i = 450,
i=1 i=1
Example 8.8 Confidence Interval for μ: House Prices Estimate the mean
house price for three-bedroom/two-bath houses in Watauga County, North Carolina. A
random sample of 14 three-bedroom/two-bath houses was taken from the Watauga County
Multiple Listing Service real estate listings (2003), and the results are reported in Table 8.2
on the next page and stored in the data frame House. Calculate a 95% confidence interval
for the average price of a three bedroom/two bath house in this county.
Confidence Intervals 301
Table 8.2: House prices (in thousands of dollars) for three-bedroom/two-bath houses in
Watauga County, NC (House)
Neighborhood Price Neighborhood Price
Valley Crucis 184.9 Blowing Rock 279.5
Valley Crucis 160.0 Valley Crucis 294.9
Valley Crucis 298.0 Blowing Rock 324.5
Blowing Rock 269.9 Blowing Rock 226.0
Parkway 189.9 Valley Crucis 329.9
Blowing Rock 229.9 Green Valley 199.9
Cove Creek 175.0 Park Valley 133.9
Solution: Before using the confidence interval formula in (8.10), one needs to verify the
assumption of normality is satisfied. Consequently, a normal quantile-quantile plot for the
values reported in Table 8.2 was constructed with the S functions qqnorm() and qqline()
and is shown in Figure 8.4 on the next page. Since the points in Figure 8.4 fall relatively
close to the straight line, it is decided that the normality assumption for using (8.10) is
satisfied. Thus, continue by calculating the sample mean as
n
xi 3296.2
x̄ = i=1 = = 235.44
n 14
and the sample variance as
n 14
2 − x̄)2
i=1 (xi i=1 (xi − 235.44)2
s = = = 4084.4.
n−1 13
The sample standard deviation is s = 63.91, and a 95% confidence interval using (8.10) is
calculated as
! "
s s
CI 0.95 (μ) = x̄ − t0.975;n−1 √ , x̄ + t0.975;n−1 √
n n
! "
63.91 63.91
= 235.44 − (2.16) √ , 235.44 + (2.16) √ = [198.54, 272.34].
14 14
Thus, one is 95% confident the mean house price falls in [198.54, 272.34] thousands of
dollars.
> attach(House)
> MEAN<-mean(Price)
> CT<-qt(.975,13)
> ST<-sd(price) #stdev(Price) in S-PLUS
> round(c(MEAN-CT*ST/sqrt(14), MEAN+CT*ST/sqrt(14)),2)
[1] 198.54 272.34
Direct construction of the confidence interval is also possible using the internal function
[Link]() as shown next.
302 Probability and Statistics with R
300
Sample Quantiles
250
200
150
−1 0 1
Theoretical Quantiles
FIGURE 8.4: Quantile-quantile plot of the asking price for 14 randomly selected three-
bedroom/two-bath houses in Watauga County, North Carolina
> [Link](Price)$conf
[1] 198.5424 272.3433
attr(,"[Link]")
[1] 0.95
Note that the function sd(object) finds the standard deviation in R but will not work
with S-PLUS. The function to find the standard deviation with S-PLUS is stdev(object).
The default confidence level is 95% for both R and S-PLUS. To change the confidence level,
say to 90%, the argument [Link]=.90 is specified inside the [Link]() command as
[Link](object, [Link]=.90)$conf.
Example 8.9 Suppose independent random samples are taken from two normal distri-
nX
butions N (μX , σ= 3) and N (μY , σ = 3), respectively, such that nX = 15, i=1 xi = 60,
nY
nY = 22, and i=1 y i = 97. Calculate a 95% confidence interval for the difference in
population means (μX − μY ).
Solution: Since
nX nY
i=1 xi 60 i=1 yi 97
x̄ = = = 4 and ȳ = = = 4.41,
nX 15 nY 22
the 95% confidence interval for the difference in population means (μX − μY ) is calculated
using (8.11) as
⎡ . . ⎤
1 1 1 1
CI 0.95 (μX − μY ) = ⎣(4 − 4.41) − (1.96)(3) + , (4 − 4.41) + (1.96)(3) + ⎦
15 22 15 22
=[−2.38, 1.56].
Example 8.10 The hardness of a piece of fruit is a good indicator of the fruit’s ripeness.
An experiment was undertaken where 17 recently picked (fresh) apples were randomly
selected and measured for hardness. Seventeen apples were also randomly selected from
a warehouse where the apples had been stored for one week. Construct a 95% confidence
interval for the mean difference between the hardness of fresh apples and the hardness
for apples that were picked one week ago. Assume the distributions for both recently
picked apples and for apples picked one week ago have known and equal variances of 2.25
2 2
kg/meter . The data are provided in Table 8.3 on the following page and can be found
in the data frame Apple.
Solution: Before the confidence interval formula in (8.11) can be used, one needs to make
sure the assumption of normality is satisfied. Consequently, a normal quantile-quantile plot
for the values reported in Table 8.3 on the next page was constructed and is shown in Figure
8.5.
(Note that title and axis labels in Figure 8.5 are the R defaults and that a “Q-Q Normal
Plot” is equivalent to a normal quantile-quantile plot as discussed earlier.) Since the points
in Figure 8.5 fall relatively close to the straight lines, it is decided that the normality
assumptions for using (8.11) are satisfied.
304 Probability and Statistics with R
Fresh
Warehouse
9
Sample Quantiles
8
7
6
−2 −1 0 1 2
Theoretical Quantiles
FIGURE 8.5: Superimposed normal quantile-quantile plots of the hardness values for fresh
and warehoused apples
> attach(Apple)
> par(pty = "s")
> Altblue <- "#A9E2FF"
> Adkblue <- "#0080FF"
> fresh <- qqnorm(Fresh)
> old <- qqnorm(Warehouse)
> plot(fresh,type="n",ylab="Sample Quantiles",xlab="Theoretical Quantiles")
> qqline(Fresh, col = Altblue)
> qqline(Warehouse, col = Adkblue)
> points(fresh, col = Altblue, pch = 16, cex = 1.2)
> points(old, col = Adkblue, pch = 17)
> legend(-1.75, 9.45, c("Fresh", "Warehouse"), col = c(Altblue, Adkblue),
+ [Link]=c("black","black"),pch=c(16,17),lty=c(1,1),bg="gray95",cex=0.75)
> title("Q-Q Normal Plots")
Confidence Intervals 305
Thus, continue solving the problem by calculating the sample mean hardness for both the
fresh and warehoused apples as
nX nY
i=1 xi 123.25 yi 134.13
x̄ = = = 7.25 and ȳ = i=1 = = 7.89.
nX 17 nY 17
Using (8.11), the 95% confidence interval for μX − μY is
# .
1 1
CI 0.95 (μX − μY ) = (7.25 − 7.89) − (1.96)(1.5) + ,
17 17
. $
1 1
(7.25 − 7.89) + (1.96)(1.5) + = [−1.65, 0.37].
17 17
> attach(Apple)
> str(Apple) # Only works in R
‘[Link]’: 17 obs. of 2 variables:
$ Fresh : num 7.27 6.65 5.76 6.53 8.09 9.56 8.38 ...
$ Warehouse: num 7.79 7.11 6.27 7.22 8.83 10.5 9.17 ...
> [Link] <- mean(Fresh)
> [Link]
[1] 7.254118
> [Link] <- mean(Warehouse)
> [Link]
[1] 7.894706
> round(c([Link] - [Link] - qnorm(0.975)*1.5*sqrt(2/17),
+ [Link] - [Link] + qnorm(0.975)*1.5*sqrt(2/17)),2)
[1] -1.65 0.37
Thus, one is 95% confident that the difference in mean hardness for fresh and warehoused
apples falls in the interval [−1.65, 0.37] kg/meter2 . Since this interval contains zero, one can
say that there is essentially no difference between the hardnesses for fresh and warehoused
apples.
Note that no internal S functions such as [Link] that assume unknown variances to construct
the confidence interval reported in Example 8.10 were used.
Example 8.11 Suppose random samples of sizes nX = 50 and nY = 46 are drawn from
normal populations with standard deviations of 4.5 and 6, respectively, such that
nX nY
xi = 420 and yi = 405.
i=1 i=1
= [−2.76, 1.96].
Note that since zero is contained in the interval, one concludes μX is not significantly
different from μY . To construct the confidence interval with S, key in
> round(qnorm(0.985), 2)
[1] 2.17
> round(c((8.4 - 8.8) - qnorm(0.985)*sqrt((4.5)^2/50 + (6)^2/46),
+ (8.4 - 8.8) + qnorm(0.985)*sqrt((4.5)^2/50 + (6)^2/46)),2)
[1] -2.76 1.96
Solution: Before using the confidence interval formula in (8.12), one needs to make sure
the assumption of normality is satisfied. Consequently, a normal quantile-quantile plot
for the values reported in Table 8.4 on the facing page was constructed and is shown in
Figure 8.6 on the next page.
Confidence Intervals 307
Table 8.4: Mathematical assessment scores for students enrolled in a biostatistics course
(Calculus)
Y X
No Calculus Calculus
73 39 55 72 88 64 82 90 85 87 86 79
57 58 75 44 76 68 85 92 89 82 92 82
64 55 62 61 76 40 85 87 92 85 95 90
70
60
50
40
Yes Calculus
No Calculus
30
−2 −1 0 1 2
Theoretical Quantiles
Since the points in Figure 8.6 fall relatively close to the straight lines, one decides the
normality assumptions for using (8.12) are satisfied and continues by calculating the sam-
ple means for students who successfully completed calculus and those who have not yet
successfully completed calculus as
18 18
i=1 xi 1565 yi 1127
x̄ = = = 86.94 and ȳ = i=1 = = 62.61.
18 18 18 18
The 95% confidence interval for (μX − μY ) is constructed using (8.12) as
CI 0.95 (μX − μY ) =
⎡ . . ⎤
⎣(86.94 − 62.61) −(1.96) 25 144 25 144 ⎦
+ , (86.94 − 62.61) + (1.96) +
18 18 18 18
= [18.33, 30.34].
308 Probability and Statistics with R
Note, once again, that the internal S function [Link] was not used to construct the confidence
interval since [Link] assumes one is working with unknown variances; and in Example 8.12,
the variances are known. If σ is unknown, use (8.16) on page 310.
15 15
nX = 15, xi = 53, and x2i = 222.
i=1 i=1
Another random sample is taken from a N (μY , σ) population independent of the first sample
such that
11 11
nY = 11, yi = 77, and yi2 = 560.
i=1 i=1
Obtain a 95% confidence interval for μX − μY by assuming the true but unknown variances
are equal.
> round(qt(0.975,24), 2)
[1] 2.06
> sp <- round(sqrt((14*2.51+10*2.1)/24),2)
> sp
[1] 1.53
> round(c((3.53 - 7) - qt(0.975,24)*sp*sqrt(1/15 + 1/11),
+ (3.53 - 7) + qt(0.975,24)*sp*sqrt(1/15 + 1/11)),2)
[1] -4.72 -2.22
That is, one is 95% confident that the difference of means lies in [−4.72, −2.22].
Example 8.14 Given the information from Example 8.10 on page 303, construct a 95%
confidence interval for the difference in hardness between fresh and warehoused apples.
Assume the samples come from normal and independent distributions with unknown but
equal variances.
310 Probability and Statistics with R
Solution: According to the solution for Example 8.10, the sample means for fresh and
warehoused apples are x̄ = 7.25 and ȳ = 7.89, respectively. Next, calculate the respective
sample variances as
nX 17
i=1 (xi − x̄)2 i=1 (xi− 7.25)2
s2X = = = 1.51 and
nX − 1 16
nY 17
i=1 (yi − ȳ)2 i=1 (yi− 7.89)2
s2Y = = = 1.79.
nY − 1 16
Assuming the data frame Apple is attached, this confidence interval can be constructed
with S by keying in
So, one is 95% confident the difference in means for fresh and warehoused apple hardness
falls in [−1.54, 0.26] kg/meter2 .
The degrees of freedom, ν, for (8.16) are determined by (8.17). When ν does not give an
integer value, it is truncated to give a conservative approximation. “Conservative” means
Confidence Intervals 311
that the resulting confidence interval will have a confidence level of at least 1 − α.
2 2
sX s2Y
+
nX nY
ν= 2 (8.17)
(sX /nX )2 (s2 /nY )2
+ Y
nX − 1 nY − 1
The standardized test statistic in (8.18) is used to construct a confidence interval for μX −
μY . The sampling distribution of (8.18) is very complicated, but Welch’s approximation of
a tν -distribution provides adequate results and will be used in this text:
X − Y − (μX − μY ) q
9 ∼ tν (8.18)
SX2 SY2
+
nX nY
Example 8.15 Suppose a random sample is taken from a N (μX , σX ) population where
15 15
nX = 15, xi = 63, and x2i = 338.
i=1 i=1
A second random sample is taken from a N (μY , σY ) population independent from the first
sample such that
11 11
nY = 11, yi = 66.4, and yi2 = 486.
i=1 i=1
Construct a 95% confidence interval for μX − μY assuming the variances for the two
populations are unknown and unequal.
Solution: Start by calculating the sample means and sample variances for the respective
samples as well as ν, the value for the degrees of freedom:
nX
63 x2i − nX x̄2 338 − (15)(4.2)2
x̄ = = 4.2 s2X = i=1
= = 5.24
15 n −1 15 − 1
nY X2
66.4 y − nY ȳ 2 486 − (11)(6.04)2
ȳ = = 6.04 s2Y = i=1 i = = 8.47
11 nY − 1 11 − 1
Next, (8.17) is used with the sample variances and respective sample sizes to determine ν:
2
5.24 8.47
+
15 11
ν= 2 = 18.43 ≈ 18
(5.24/15) (8.47/11)2
+
14 10
The 95% confidence interval for μX − μY is constructed using (8.16) as
# .
5.24 8.47
CI 0.95 (μX − μY ) = (4.2 − 6.04) − t0.975;18 + ,
15 11
. $
5.24 8.47
(4.2 − 6.04) + t0.975;18 +
15 11
=[−1.84 − (2.01)(1.06), −1.84 + (2.01)(1.06)]
=[−4.06, 0.38].
312 Probability and Statistics with R
Example 8.16 Using the information from Example 8.12, which provided the mathemat-
ical assessment scores for students enrolled in a biostatistics course according to whether
they had completed a calculus course prior to enrolling in the biostatistics course, construct
a 95% confidence interval for μX − μY assuming the samples are taken from distributions
where the variances are unknown and unequal (σX 2
= σY2 ).
Solution: Recall from Example 8.12 that x̄ = 86.94 and ȳ = 62.61. Also recall that
the assumption of normality for these data seemed plausible based on the normal quantile-
quantile plot provided in Figure 8.6 on page 307. The respective sample variances are
n 18
i=1 (xi − x̄) (xi − 86.94)2
2
2
sX = = i=1 = 18.64 and
n−1 17
n 18
(yi − ȳ)2 (yi − 62.61)2
s2Y = i=1 = i=1 = 174.84.
n−1 17
Next, (8.17) on the previous page is used with the sample variances and respective sample
sizes to determine ν:
2
18.64 174.84
+
18 18
ν= = 20.58 ≈ 20
(18.64/18)2 (174.84/18)2
+
17 17
The 95% confidence interval for μX − μY is constructed using (8.16) as
# .
18.64 174.84
CI 0.95 (μX − μY ) = (86.94 − 62.61) − t0.975;20 + ,
18 18
. $
18.64 174.84
(86.94 − 62.61) + t0.975;20 +
18 18
=[24.33 − (2.09)(3.28), 24.33 + (2.09)(3.28)]
=[17.48, 31.19].
Assuming the data frame Calculus is attached, the confidence interval can be constructed
directly with
> [Link]([Link], [Link], [Link]=FALSE)$conf
[1] 17.50677 31.15990
attr(,"[Link]")
[1] 0.95
One is 95% confident that the difference of means lies in [17.48, 31.19]. Note that S can
compute quantiles in the t-distribution with non- integer degrees of freedom. In particular,
Confidence Intervals 313
S uses the exact value for ν from (8.17) to find the critical value t1−α/2;ν in its confidence
interval computation rather than truncating the value of ν. Consequently, the confidence
interval computed with 20 degrees of freedom is slightly wider than the confidence interval
S computes.
When working with normal distributions that have unknown variances, not pooling
the variances and using (8.16) is generally the better method when the sample sizes are
the same. It is also better when the sample sizes are unequal and the larger variance is
associated with the larger sample size. Pooling the variances and using (8.15) should only
be done if one is relatively confident that the variances are equal or if the larger variance is
associated with the smaller sample size. For a summary of these methods, see Table 8.5.
8.2.7 Confidence Interval for the Mean Difference when the Differences
Have a Normal Distribution
Information from two dependent distributions is often called paired or dependent
data. Paired samples have some common intrinsic features such as members of the same
family, animals from the same litter, etc. Data are also considered to be paired when
the same sample is observed at different times. For example, suppose one is interested in
evaluating the time undergraduate economic majors spend studying the first month of the
semester and how much time they spend studying the last month of the semester. To help
in the analysis, record the total time students spend studying the first and last months of
the semester. This information is considered paired data since there are two measurements
on each student. Scores recorded from a pre-test and post-test on the same group of people
are also considered to be a paired or a dependent sample. In general, when the researcher is
presented with paired samples, the standard approach is to analyze the differences between
the paired data. In other words, if the population of pairs is (X1 , Y1 ), (X2 , Y2 ), . . . , analyze
the paired differences D = (X1 − Y1 , X2 − Y2 , . . .). When there is a paired sample of size nD ,
denote the sample differences as d = (x1 − y1 , . . . , xnD − ynD ). Provided the distribution of
population differences is
D ∼ N (μD = μX − μY , σD ) , (8.19)
a confidence interval formula for μD when σD is unknown can be constructed using the
pivotal quantity
D − μD
Q(X; μD ) = √ ∼ tn−1 , (8.20)
SD / n D
where nD represents the number of pairs in the sample and SD is the standard deviation of
the differences. Using (8.20) as a pivot, a (1 − α) · 100% confidence interval for the difference
314 Probability and Statistics with R
Example 8.17 To compare the speed differences between two different brands of work-
stations (Sun and Digital), the times each brand took to complete complex simulations
were recorded. Five complex simulations were selected, and the five selected simulations
were run on both workstations. The resulting times in minutes for the five simulations are
given in Table 8.6 and stored in data frame Sundig. Construct a 95% confidence interval
for μD , the average time difference between SUN and DIGITAL workstations. Is one of the
workstations faster than the other?
Solution: Since each one of the five selected complex simulations was run on both work-
stations, these samples are dependent. The differences between the dependent samples are
d = (8, 5, 6, −1, 7), d¯ = 5 minutes, and sD = 3.53 minutes. Before using (8.21), one needs
to verify the distribution of differences is normal. To check the normality assumption, use
the functions qqnorm() and qqline() on the sample differences, d. The resulting normal
quantile-quantile plot is shown in Figure 8.7 on the next page. Based on Figure 8.7, it is
not immediately clear that the distribution of differences is normal due to the outlier in the
lower left of the plot. At this point, one should look at several normal quantile-quantile
plots for samples of size five using the ntester() function. The results of using the function
ntester() on the sample differences are shown in Figure 8.8 on the facing page. After using
the ntester() function on the differences and viewing the output in Figure 8.8, one can
conclude that it is not unreasonable to assume the distribution of differences between Sun
and Digital workstations follow a normal distribution and can use (8.21) to construct the
95% confidence interval for μD = μSUN − μDIG as follows:
! "
sD sD
CI 0.95 (μSUN − μDIG ) = d¯ − t0.975;nD −1 √ , d¯ + t0.975;nD −1 √
nD nD
! "
3.53 3.53
= 5 − (2.78) √ , 5 + (2.78) √ = [0.61, 9.39].
5 5
One is 95% confident μD lies in [0.61, 9.39] minutes. Since the confidence interval does not
contain zero, one can be 95% confident that μD = μSUN − μDIG > 0. This implies that
μSUN > μDIG , which means that the Digital workstation is faster than the Sun workstation.
Confidence Intervals 315
8
6
Sample Quantiles
4
2
0
−1.0 −0.5 0.0 0.5 1.0
Theoretical Quantiles
FIGURE 8.7: Normal quantile-quantile plot of the time differences between Sun and Digital
workstations to complete complex simulations
FIGURE 8.8: Quantile-quantile plot of the time differences between Sun and Digital
workstations to complete complex simulations shown in the middle with normal
quantile-quantile plots of random normal data depicted on the outside plots
To verify the value t0.975;4 and to calculate a 95% confidence interval for μD with S, enter
> round(qt(0.025,4), 2)
[1] -2.78
> attach(Sundig)
> [Link](SUN, DIGITAL, paired=TRUE)$conf
[1] 0.6100548 9.3899452
attr(,"[Link]")
[1] 0.95
316 Probability and Statistics with R
(n − 1)S 2
Q(X; σ 2 ) = ∼ χ2n−1 (8.22)
σ2
1−α
χ2α/2;6 χ21−α/2;6
FIGURE 8.9: Chi-square distribution with six degrees of freedom depicting the points χ2α/2;6
and χ21−α/2;6
The pivotal quantity (8.22) is not very robust with respect to the normality assumption.
Consequently, before constructing a confidence interval for σ 2 , one should always check the
sample for normality using a graphical procedure such as a normal quantile-quantile plot
(qqnorm()). Although Pearson’s χ2 distribution is not symmetric (see Figure 8.9), one can
use the sampling distribution of the statistic (n − 1)S 2 /σ 2 and the definition of percentiles
to obtain
(n − 1)S 2
P χ2α/2;n−1 ≤ ≤ χ 2
1−α/2;n−1 = 1 − α. (8.23)
σ2
To arrive at the standard confidence interval form for the variance, first take the recipro-
cal inside the probability statement of (8.23) as shown in (8.24). Then, multiply everything
inside the probability statement of (8.24) by (n − 1)S 2 to obtain the probability statement
shown in (8.25):
1 σ2 1
P ≥ ≥ 2 = 1 − α, (8.24)
χ2α/2;n−1 (n − 1)S 2 χ1−α/2;n−1
(n − 1)S 2 (n − 1)S 2
P 2 ≤ σ2 ≤ 2 = 1 − α. (8.25)
χ1−α/2;n−1 χα/2;n−1
Confidence Intervals 317
Using the probability statement (8.25) at a fixed confidence level of (1 − α), the standard
form for the confidence interval for σ2 is illustrated in (8.26). Note that the confidence
interval for the variance is not centered around the point estimate (the sample variance,
s2 ).
# $
2
(n − 1)s2 (n − 1)s2
CI 1−α σ = , . (8.26)
χ21−α/2;n−1 χ2α/2;n−1
2
Example 8.18 Construct an 80% confidence interval for σX using the information from
Example 8.15.
Solution: Recall that the underlying distribution in Example 8.15 was assumed to be
N (μX , σX ) and the sample information provided revealed that nX = 15, x̄ = 4.2, and
s2X = 5.24. Using (8.26), the 80% confidence interval for σX
2
is calculated as
# $ # $
2 (nX − 1)s2X (nX − 1)s2X 14(5.24) 14(5.24)
CI 0.8 (σX ) = , = ,
χ20.9;n−1 χ20.1;n−1 χ20.9;14 χ20.1;14
! "
73.36 73.36
= , = [3.48, 9.42].
21.06 7.79
To construct this confidence interval with S, type
> round(qchisq(0.1,14), 2)
[1] 7.79
> qchisq(0.9,14)
[1] 21.06
> round(c(14*5.24/qchisq(0.9,14), 14*5.24/qchisq(0.1,14)), 2)
[1] 3.48 9.42
Therefore, one is 80% confident the variance falls in [3.48, 9.42].
Example 8.19 The data frame barley is in the lattice package and contains yield,
variety, year, and site, giving barley yields (bushels/acre) in 1931 and 1932 for 10 varieties
of barley grown at six sites. The S-PLUS data frame barley is identical.
(a) Construct a 95% confidence interval for μ, the mean barley yield in 1932.
(b) Construct a 95% confidence interval for σ2 , the variance of barley yield in 1932.
Solution: Start by looking at the distribution of 1932 barley yield using the functions
qqnorm() and qqline() to create the normal quantile-quantile plot shown in Figure 8.10
on the following page. Since the values in Figure 8.10 on the next page are fairly linear, it
is decided the assumptions to use both (8.10) and (8.26) are satisfied.
! "
s s
CI 1−0.05 (μ) = x̄ − t1−0.05/2;n−1 √ , x̄ + t1−0.05/2;n−1 √
n n
! "
9.38 9.38
= 31, 76 − (2.00) √ , 31.76 + (2.00) √
60 60
= [29.34, 34.19] (8.27)
318 Probability and Statistics with R
50
Sample Quantiles
40
30
20
−2 −1 0 1 2
Theoretical Quantiles
> [Link](yield[year==1932])$conf
[1] 29.33907 34.18759
attr(, "[Link]"):
[1] 0.95
So, one is 95% confident that the mean barley yield (bushels/acre) lies in [29.34, 34.19].
To verify the previous values and construct this confidence interval with S, enter
> s2<- var(yield[year==1932])
> s2
[1] 88.06803
> ChiL <- qchisq(.025,59)
> ChiL
[1] 39.66186
> ChiU <- qchisq(.975,59)
> ChiU
[1] 82.1174
> n <- length(yield[year==1932])
> n
[1] 60
> round(c((n-1)*s2/ChiU, (n-1)*s2/ChiL),2)
[1] 63.28 131.01
One is 95% confident that the variance of barley yield lies in [63.28, 131.01] (bushels/acre)2 .
SY2 /σY2
2 /σ 2 ∼ FnY −1,nX −1 .
SX
(8.28)
X
By using (8.28), construct the (1 − α) probability statement shown in (8.29) and graphically
illustrated in Figure 8.11 on the next page for an F distribution with 10 and 10 degrees of
freedom:
S 2 /σ 2
P fα/2;nY −1,nX −1 ≤ 2Y Y2 ≤ f1−α/2;nY −1,nX −1 = 1 − α (8.29)
SX /σX
2
SX
After multiplying everything inside the probability statement given in (8.29) by 2
SY
, (8.30)
is used to derive the final confidence interval statement given in (8.31):
2 2 2
SX σX SX
P fα/2;nY −1,nX −1 2 ≤ 2 ≤ f1−α/2;nY −1,nX −1 2 = 1 − α (8.30)
SY σY SY
2
! "
σX s2 s2X
CI 1−α = fα/2;nY −1,nX −1 X , f 1−α/2;nY −1,nX −1 (8.31)
σY2 s2Y s2Y
320 Probability and Statistics with R
1−α
fα/2;10,10 f1−α/2;10,10
FIGURE 8.11: F distribution with ten and ten degrees of freedom depicting the points
fα/2;10,10 and f1−α/2;10,10
For sheer convenience, denote the larger sample variance as s2X when constructing a
confidence interval for the ratio of two population variances. Consequently, the numerator
for the ratio of the sample variances will always contain the larger of the two sample
variances. Many tables involving the F distribution only provide values for percentiles
in the right tail. However, this does not present a problem provided one remembers that
values in the left tail of the F distribution can be found from the values in the right tail of
an F distribution by using (8.32). Note that the order of the degrees of freedom changes in
the reciprocal.
1
fα/2;nY −1,nX −1 = . (8.32)
f1−α/2;nX −1,nY −1
Example 8.20 Using the information from Example 8.13 on page 309, construct a 90%
confidence interval for the ratio of variances.
Solution: In Example 8.13, the larger sample variance, s2X , was 2.51, nX = 15, and
the smaller sample variance, s2Y , was 2.1, nY = 11. Consequently, the 90% confidence
interval for the ratio of variances is constructed using (8.31) as shown in the following
where f0.05;10,14 = 0.35 and f0.95;10,14 = 2.60:
2 ! " ! "
σX s2X s2X 2.51 2.51
CI 0.9 = f 0.05;10,14 , f 0.95;10,14 = (0.35) , (2.60) = [0.42, 3.11]
σY2 s2Y s2Y 2.1 2.1
(8.33)
2
σX
To find f0.05;10,14 , f0.95;10,14 and a 95% confidence interval for 2
σY
with S, type
> round(qf(0.05,10,14), 2)
[1] 0.35
> round(qf(0.95,10,14), 2)
[1] 2.60
> round(c(qf(0.05,10,14)*(2.51/2.1), qf(0.95,10,14)*(2.51/2.1)),2)
[1] 0.42 3.11
So, one is 90% confident the ratio of the variance lies in [0.42, 3.11]. Note that this interval
includes 1, which indicates there is not evidence to suggest the variances are different.
Confidence Intervals 321
Example 8.21 Given the information in Table 8.3 on page 304, construct a 95% confi-
dence interval for the ratio of the variances.
Solution: According to Example 8.14, s2X = 1.51 and s2Y = 1.79. Also recall that in
the solution to Example 8.10, a normal quantile-quantile plot was created and illustrated in
Figure 8.5 on page 304 that justified the assumptions that both fresh and warehoused apples
follow a normal distribution. Consequently, the appropriate confidence interval formula for
the ratio of the variances is given in (8.31). However, since s2Y = 1.79 and s2X = 1.51,
reverse s2X for s2Y in the confidence interval formula provided in (8.31) to construct a 95%
confidence interval for the ratio of population variances:
2 ! "
σY s2Y s2Y
CI 0.95 2 = f 0.025;16,16 2 , f 0.975;16,16 2
σX sX sX (8.34)
= [(0.36)(1.19), (2.76)(1.19)] = [0.43, 3.27].
To verify the previous values and to construct a 95% confidence interval for the ratio of
variances with S, attach Apple and key in
> var(Warehouse)
[1] 1.790951
> var(Fresh)
[1] 1.510438
> round(var(Warehouse)/var(Fresh),2)
[1] 1.19
> round(qf(0.025,16,16), 2)
[1] 0.36
> round(qf(0.975,16,16), 2)
[1] 2.76
> [Link](Warehouse, Fresh)$conf
[1] 0.429396 3.274189
attr(, "[Link]"):
[1] 0.95
One is 95% confident that the ratio of variances falls in [0.43, 3.27], which indicates that a
pooled variance could be justified for confidence interval calculations regarding the means.
& '
θ̂ (X) ∼ N θ, In (θ)−1 . (8.35)
Using (8.35), one can construct asymptotic confidence intervals of the type given in (8.36).
Note that σθ̂(X) is the standard deviation of the estimator θ̂ (X). Specifically, in the multi-
parameter case, σθ̂(X) is the square root of the corresponding diagonal element of the inverse
322 Probability and Statistics with R
of the information matrix. When σθ̂(X) is unknown, the estimate σ̂θ̂(x) is used in place of
σθ̂(X) . Be sure to see that σ̂θ̂(x) is calculated from the data x.
% (
CI 1−α (θ) = θ̂(x) − z1−α/2 · σθ̂(X) , θ̂(x) + z1−α/2 · σθ̂(X) (8.36)
Example 8.22 Given a random sample of size 200 from an exponential distribution, find
a 90% confidence interval for θ if it is true that
200
xi = 400.
i=1
1 −1x
f (x, θ) = e θ , x ≥ 0, θ>0 (8.37)
θ
Solution: The reader should verify that the maximum likelihood estimator of θ is θ̂(X) =
2
X and the variance of X is θn . (Hint: See Example 7.6 on page 250.) Because X is the
2
maximum likelihood estimator of θ, it follows that the maximum likelihood estimator of θn
2
is Xn due to the invariance property of MLEs (property 2 on page 273). From the sample
information, calculate
2 x̄2
θ̂ (x) = x̄ = 2 and σ̂θ̂(x) = = 0.02.
n
Given that the confidence level is 0.9, z1−α/2 = z0.95 = 1.64, the 90% confidence interval
for θ is constructed using (8.36):
% √ √ (
CI 0.90 (θ) = 2 − 1.64 0.02, 2 + 1.64 0.02 = [1.77, 2.23]. (8.38)
So, one is 90% confident the exponential parameter θ falls in [1.77, 2.23].
π(1 − π)
In (π)−1 = (8.41)
n
Taking advantage of the asymptotic properties of MLE estimators allows one to write
.
π(1 − π)
π̂ (X) = P ∼ N π, as n → ∞;
n
⎡ . . ⎤
p(1 − p) p(1 − p) ⎦
CI 1−α (π) = ⎣p − z1−α/2 , p + z1−α/2 (8.42)
n n
A more accurate confidence interval for π can be obtained by solving for the values that
satisfy (8.43) instead of replacing σπ̂(X) with its MLE σ̂π̂(x) . Solving for the values that
satisfy (8.43) is slightly more involved but produces the confidence interval given in (8.44).
Recent research (Agresti and Coull, 1998) shows that the confidence interval in (8.44) can
be used for a wide range of parameters and sample sizes. Therefore, when working with
smaller sample sizes, the confidence interval formula in (8.44) is preferred over the confidence
interval formula (8.42) as it returns confidence intervals whose nominal confidence level is
closer to the user specified 1 − α level. If the sample size is large, z1−α/2
2
/2n is negligible
compared to p, z1−α/2 /4n under the square root is negligible compared to p(1 − p)/n, and
2 2
2
z1−α/2 /n is negligible compared to 1. If the negligible terms are ignored, the confidence
interval formula in (8.42) emerges.
. .
π(1 − π) π(1 − π)
P P − z1−α/2 ≤ π ≤ P − z1+α/2 =1−α (8.43)
n n
⎡ 2
2 2
z1−α/2
z1−α/2
⎢p + − z1−α/2 p(1−p) +
CI 1−α (π) = ⎢
⎣
2n
2
n 4n2
,
z
1 + 1−α/2
n
2 ⎤ (8.44)
2 2
z1−α/2
z1−α/2
p+ + z1−α/2 p(1−p) + ⎥
2n
n 4n2 ⎥
2
z1−α/2 ⎦
1+ n
When S uses (8.44) to construct confidence intervals, under certain conditions, it also
1
applies a Yates’ continuity correction to p so that the p used in the lower limit is pL = p− 2n
1
and the p used in the upper limit is pU = p + 2n .
324 Probability and Statistics with R
= [0.502, 0.798]
2 ⎤
2 z2
z1−α/2
p+ + z1−α/2 p(1−p) + 1−α/2 ⎥
2n
n 4n2 ⎥
2
z1−α/2 ⎦
1+ n
⎡ 2
1.962
0.65 + (2)(40) − 1.96 0.65(1−0.65) 1.962
+ (4)(40 2)
= ⎣ 2
40
,
1 + 1.96
40
2 ⎤
1.962
0.65 + (2)(40) + 1.96 0.65(1−0.65) 1.962
+ (4)(40 2)
2
40
⎦ = [0.495, 0.779]
1 + 1.96
40
Confidence Intervals 325
So, depending on which confidence interval the professor prefers, he can be 95% con-
fident that the proportion of students who pass lies in [0.502, 0.798], [0.495, 0.779], or
[0.483, 0.789].
Example 8.24 A computer firm would like to construct three confidence intervals for
the proportion of supermarkets that use a computerized database to manage their ware-
houses. Suppose 200 supermarkets are surveyed and 157 of the 200 supermarkets have
computerized inventories. Construct 90%, 95%, and 99% confidence intervals for the true
proportion of supermarkets that use a computerized database to manage the inventory of
their warehouses.
CI 1−α (π) =
⎡ . . ⎤
⎣0.785 − z1−α/2 (0.785)(0.215) (0.785)(0.215) ⎦
, 0.785 + z1−α/2 . (8.45)
200 200
326 Probability and Statistics with R
The computer firm is 90% confident the population proportion of supermarkets that use a
computerized database to manage their warehouses lies in [0.74, 0.83], 95% confident this
population proportion lies in [0.73, 0.84], and 99% confident this population proportion lies
in [0.71, 0.86]. Take special note that the widths of the confidence intervals increase as the
confidence level increases. To find these confidence intervals using the confidence interval
formula in (8.44) with S, enter
> round([Link](157, 200, [Link]=0.90, correct=FALSE)$conf,2)
[1] 0.73 0.83
> round([Link](157, 200, [Link]=0.95, correct=FALSE)$conf,2)
[1] 0.72 0.84
> round([Link](157, 200, [Link]=0.99, correct=FALSE)$conf,2)
[1] 0.70 0.85
Example 8.25 Confidence Interval and Sample Size for π The Department
of Agriculture wants to estimate the proportion of rural farm owners that are under 40 years
of age. They take a random sample of 2000 farms and find that 400 of the 2000 owners are
under the age of 40.
(a) Construct a 95% confidence interval for π using the asymptotic confidence interval for
π based on the MLE of σ̂π̂(x) given in (8.42).
(b) Determine the required sample size so that the maximum margin of error is within 0.015
of the true value of π for a 95% confidence level.
400
Solution: Note that p = 2000 = 0.20.
(a) A 95% confidence interval for π using (8.42) is
⎡ . . ⎤
p(1 − p) p(1 − p)
CI 0.95 (π) = ⎣p − z1−0.05/2 , p + z1−0.05/2 ⎦
n n
⎡ . . ⎤
0.2(1 − 0.2) 0.2(1 − 0.2)
= ⎣0.2 − (1.96) , 0.2 + (1.96) ⎦
2000 2000
= [0.182, 0.218].
(b) In order to construct a confidence interval such that the maximum margin of error does
not exceed 0.015, one needs to ensure that
.
p(1 − p)
(1.96) < 0.015. (8.46)
n
To maximize the margin of error, use p = 12 regardless of any prior information concerning
p. Using a value for p of 12 will ensure the margin of error is maximized at a given confidence
level. To see why this is true, consider plotting p × (1 − p) versus p. This can be done by
typing
Consequently, solving (8.46) for n yields 4268.4. To guarantee the maximum margin of
error is within 0.015 at a 95% confidence level, always take the ceiling of n (use the next
largest integer). In this case, a sample of size 4269 will guarantee the maximum margin of
error will be less than 0.015 at a 95% confidence level. That is,
9
1 1
2 2
(1.96) = 0.01499902 < 0.015.
4269
Since the sampling distributions of both PX and PY are approximately normal, the sampling
distribution for the difference between PX and PY will also be approximately normal.
Specifically,
⎛ 9 ⎞
q π (1 − π ) π (1 − π )
∼ N ⎝πX − πY , ⎠
X X Y Y
PX − PY + (8.47)
nX nY
⎡ 9
pX (1 − pX ) pY (1 − pY )
CI 1−α (πX − πY ) = ⎣(pX − pY ) − z1−α/2 + ,
nX nY
9 ⎤ (8.48)
pX (1 − pX ) pY (1 − pY ) ⎦
(pX − pY ) + z1−α/2 +
nX nY
& '
1 1 1
It is generally advisable to use the continuity correction 2 nX + nY with (8.48)
anytime
1 1 1
|pX − pY | > + . (8.49)
2 nX nY
The continuity correction is subtracted and added to the lower and upper confidence
limits of (8.48), respectively. The S function [Link]() automatically applies the con-
tinuity correction when (8.49) is satisfied provided the user does not issue the argument
correct=FALSE.
Example 8.26 A company wants to see if a certain change in the process for manufac-
turing component parts is beneficial. Samples are taken using both the existing and the
new procedure to determine if the new process results in an improvement. The first sample
is taken before the change has been implemented, and the second sample is taken once the
change has been implemented. If 70 of 1400 elements are found to be defective in the first
sample and 90 of 2000 elements are found to be defective from the second sample, find a
95% confidence interval for the true difference in the proportion of defective components
between the existing and the new processes.
70 90
Solution: The sample proportions of successes are pX = 1400 = 0.05 and pY = 2000 =
0.045. Using (8.48), the 95% confidence interval for the true difference in the proportion
of defective components between the existing and the new processes is given in (8.50).
Since the confidence interval contains 0, there is no reason to suspect the new procedure
significantly reduces the proportion of defective items.
CI 0.95 (πX − πY ) =
⎡ .
⎣(0.05 − 0.045) − 1.96 (0.05)(1 − 0.05) (0.045)(1 − 0.045)
+ ,
1400 2000
. ⎤
(0.05)(1 − 0.05) (0.045)(1 − 0.045) ⎦
(0.05 − 0.045) + 1.96 +
1400 2000
∂ 2 ln L(λ|X) − ni=1 xi
= (8.51)
∂λ2 λ2
Taking the expected value of (8.51) yields the following, from which the Fisher information,
In (λ)−1 = nλ , is obtained:
! "
∂ 2 ln L(λ|X) nλ n
−E = 2 = . (8.52)
∂λ2 λ λ
Taking advantage of the asymptotic properties of MLE estimators allows one to write
.
λ
λ̂ (X) = X ∼ N λ, as n → ∞.
n
One may then use (8.36), the confidence interval formula for MLEs, to construct
2 a (1 − α) ·
x̄
100% asymptotic confidence interval for λ as shown here where σ̂λ̂(x) = n :
# . . $
x̄ x̄
CI 1−α (λ) = x̄ − z1−α/2 , x̄ + z1−α/2 (8.53)
n n
330 Probability and Statistics with R
One could obtain a similar confidence interval by recognizing that X has a normal
distribution with parameters μ and √σn for large sample sizes according to the Central
Limit Theorem. Since
√ the mean for a Poisson is λ and the standard deviation of a Poisson
random variable is λ, it follows that
√
q λ
X Pois ∼ N λ, √ .
n
Example 8.27 Example 4.4 on page 122 provided evidence to suggest the number of
goals scored in the regulation 90 minute periods of World Cup soccer matches from 1990
to 2002 have a Poisson distribution. Use the information in column Goals of the data set
Soccer to construct a 90% confidence interval for the mean number of goals scored during
a 90 minute regulation period.
= [2.31, 2.65]
8.5 Problems
1. Is [x̄ − 3, x̄ + 3] a confidence interval for the population mean of a normal distribution?
Why or why not?
2. Explain how to construct a confidence interval for the population mean of a normal
distribution with a 95% confidence level.
3. Given a random sample {X1 , X2 , . . . , Xn } from a normal population N (μ, σ), where σ is
known:
(a) What is the confidence level for the interval x̄ ± 1.881 √σn ?
(b) What is the confidence level for the interval x̄ ± 1.175 √σn ?
(c) What is the value of the percentile zα/2 for a 92% confidence interval?
4. Given a random sample {X1 , X2 , . . . , Xn } from a normal population N (μ, σ), where σ is
known, consider the confidence interval x̄ ± z1−α/2 √σn for μ.
(a) Given a fixed sample size n, explain the relationship between the confidence level
and the precision of the confidence interval.
(b) Given a confidence level (1−α)%, explain how the precision of the confidence interval
changes with the sample size.
5. Given a normal population with known variance σ2 , by what factor must the sample size
be increased to reduce the length of a confidence interval for the mean by a factor of k?
6. A historic data set studied by R.A. Fisher is the measurements in centimeters of four
flower parts (sepal length, sepal width, petal length, and petal width) on 50 specimens
for each of three species of irises (Setosa, Versicolor, and Virginica). The data are named
iris in S-PLUS, and the same data can be found in R under the name iris3 (Fisher, 1936).
(a) Analyze the sepal lengths for Setosa, Versicolor, and Virginica irises, and comment
on the characteristics of their distributions. (Hint: Since the data in iris and iris3
are stored as arrays, type iris3[,1,1] if using R or iris[,1,1] if using S-PLUS to
isolate the sepal lengths for the Setosa irises.)
(b) Based on the analysis from part (a), construct an appropriate 95% confidence interval
for the mean sepal length of Setosa irises.
(a) Construct a quantile-quantile plot of the data. Does this plot rule out normality?
(b) Construct a 90% confidence interval for the mean salinity variation.
332 Probability and Statistics with R
8. The survival times in weeks for 20 male rats that were exposed to a high level of radiation
are
(a) Construct a quantile-quantile plot of the survival times. Based on the quantile-
quantile plot, can normality be ruled out?
(b) Construct a 97% confidence interval for the average survival time for male rats
exposed to high levels of radiation.
County1 County2
130 126 139 126 124 149 124 127 125 127 132 139 132 125
138 138 140 127 140 124 124 130 131 140 130 132 134 128
121 125 134 121 125 126 122 137 121 121 141 141 137 126
137 146 127 124 142 122 126 124 124 128 145 123 126 132
124 126 121 138 124 126 137 135 126 128 144 121 135 125
122 131 128 122 144 125 136 122 130 130
(a) Although the standard deviation for the Stanford-Binet IQ test is known, should it
be used? Justify.
(b) Be careful, the confidence interval formula that should be used in this situation has
not been explicitly covered yet. Construct a 90% confidence interval for the true
average IQ difference for gifted and talented students between the two counties.
10. A large company wants to estimate the proportion of its accounts that are paid on time.
(a) How large a sample is needed to estimate the true proportion within 2% with a 95%
confidence level?
(b) Suppose 650 out of 800 accounts are paid on time. Construct a 99% confidence
interval for the true proportion of accounts that paid on time.
11. In a study conducted at Appalachian State University, students used digital oral ther-
mometers to record their temperatures each day they came to class. A randomly selected
day of student temperatures is provided in the following table and in the data frame
StatTemps. Information is also provided with regard to subject gender and the hour of
the day when the students’ temperatures were measured.
Confidence Intervals 333
(a) Construct a 95% confidence interval for the true average temperature difference
between males and females. Does the interval contain the value zero? What does
this suggest about gender temperature differences?
(b) Construct a 95% confidence interval for the true average temperature difference
between students taking their temperatures at 8 a.m. and students taking their
temperatures at 9 a.m. Give a reason why one group appears to have a higher
temperature reading.
12. The Cosmed K4b2 is a portable metabolic system. A study at Appalachian State
University compared the metabolic values obtained from the Cosmed K4b2 to those
of a reference unit (Amatek) over a range of workloads from easy to maximal to test
the validity and reliability of the Cosmed K4b2 . A small portion of the results for
VO2 (ml/kg/min) measurements taken at a 150 watt workload are stored in data frame
CosAma and in the following table:
13. Let {X1 , . . . , X9 } and {Y1 , . . . , Y15 } be two random samples from a N (μX , σ) and a
N (μY , σ), respectively. Suppose that x̄ = 57.3, s2X = 8.3, ȳ = 65.6, and s2Y = 9.7. Find
a 96% confidence interval for μX , μY , and μX − μY .
14. The water consumption in liters per family per day in a given city is a normally dis-
tributed random variable with unknown variance. Consider the following confidence
intervals for the population mean obtained from a random sample of size n:
Compute a 95% confidence interval for the 2003 mean European TOE assuming the
amount of nuclear energy is normally distributed.
17. A group of engineers working with physicians in a research hospital is developing a new
device to measure blood glucose levels. Based on measurements taken from patients in
a previous study, the physicians assert that the new device provides blood glucose levels
slightly higher than those provided by the old device. To corroborate their suspicion, 15
diabetic patients were randomly selected, and their blood glucose levels were measured
with both the new and the old devices. The measurements, in mg/100 ml, appear in the
following table and are stored in the data frame glucose:
(b) If the blood glucose level is a normally distributed random variable, compute a 95%
confidence interval for the difference of the population means.
(c) Use the results in (b) to decide whether or not the two devices give the same results.
18. The European Union is developing new policies to promote research and development
investment. A random sample of 15 countries’ investments for the years 2002 and 2003 is
taken and the results (in millions of euros) are stored in the data frame EURD and shown
in the following table:
(a) Compute a 95% confidence interval for the difference between 2002 and 2003 invest-
ment means.
(b) Use (a) to decide if the new policies are increasing investments.
19. The “Wisconsin Card Sorting Test” is widely used by psychiatrists, neurologists, and
neuropsychologists with patients who have a brain injury, neurodegenerative disease,
or a mental illness such as schizophrenia. Patients with any sort of frontal lobe lesion
generally do poorly on the test. The data frame WCST and the following table contain the
test scores from a group of 50 patients from the Virgen del Camino Hospital (Pamplona,
Spain).
23 12 31 8 19 11 36 94 6 10 22 7 18 26 35 78 11
7 28 25 17 8 20 47 5 13 28 19 7 19 38 8 15 40
19 42 17 6 8 6 11 10 19 65 13 17 5 26 15 4
(a) Use the function EDA() from the PASWR package to explore the data and decide if
normality can be assumed.
336 Probability and Statistics with R
(b) What assumption(s) must be made to compute a 95% confidence interval for the
population mean?
(c) Compute the confidence interval from (b).
20. The following data were taken to measure the unknown pH values μ of a solution in a
chemical experiment:
8.01, 8.05, 7.96, 8.04, 8.03, 8.03, 8.02, 7.98, 8.05, 8.03.
If the pH meter has a systematic error, Δ, and a normally distributed random error,
ε ∼ N (0, σ2 ), then it can be assumed that the observations come from a normal random
variable, X ∼ N (μ + Δ, σ 2 ).
(a) Compute a 95% confidence interval for μ when Δ = 0 and σ = 0.05. Compute the
interval assuming that the variance is unknown.
(b) Repeat part (a) with Δ = 0.2.
21. When sampling from a normal distribution, what sample size will ensure that the interval
x̄ ± s attains at least a 95% confidence level?
22. Let {X1 , . . . , Xn } be a simple random sample from a normal distribution N (μ, σ), and
consider the following random variables:
(a) Set the seed value at 69, and generate m = 100 samples of size n = 5 from a normal
population N (μ = 5, σ = 2). Compute the number of intervals of the types [X, Y ]
containing the real value μ = 5. If the theoretical coverage of these intervals is
94% for a sample of size n = 5, do the empirical results agree with the theoretical
coverage?
(b) Set the seed value at 18, and generate m = 100 samples of size n = 5 from a normal
population
N (μ = 5, σ = 2). Compute
the confidence intervals of the type [X, Y ]
and X + z0.03 √σn , X + z0.97 √σn . Construct a plot with the length of both types of
intervals. Repeat the exercise with samples of size n = 50. Which type of confidence
interval is preferred? Why?
25.3 23.8 27.5 23.2 24.5 25.3 24.6 26.8 25.9 29.2,
(a) State the assumption(s) needed to construct a confidence interval for the population
variance.
(b) Assuming your assumption(s) in (a) are satisfied, construct a 95% confidence interval
for σ.
(c) Assuming that μ = 25, construct a 95% confidence interval for σ.
24. Schizophrenia is believed to cause changes in dopamine levels. Twenty-five patients with
schizophrenia were classified as psychotic or non-psychotic after being treated with an
antipsychotic drug. Samples of cerebral fluid were taken from each patient and assayed
for dopamine b-hydroxylase (DBH) activity. The dopamine measurements for the two
Confidence Intervals 337
groups are in nmol/(ml)(h)/(mg) of protein and are stored in the data frame Schizo as
well as in the following table (Sternberg et al., 1982).
(a) Construct side-by-side boxplots of the two groups. Based on the boxplots, comment
on the relative shapes of the two distributions.
(b) Construct quantile-quantile plots for the two groups, and comment on whether or
not the plots support the analysis in part (a).
(c) Construct a 95% confidence interval for the true ratio of psychotic to non-psychotic
variances.
(d) Based on the confidence interval for the ratio of variances, should the variances be
pooled to construct a 95% confidence interval for the true dopamine level difference
between psychotic and non-psychotic patients?
(e) Construct a 95% confidence interval for the true dopamine level difference between
psychotic and non-psychotic patients.
(f) Does the confidence interval contain zero? What does this say about the effectiveness
of the antipsychotic drug?
25. Assuming two independent random samples of sizes 22 and 45 with variance estimates
of s21 = 38.7 and s22 = 45.6, respectively, have been taken, construct a 95% confidence
interval for σ.
26. Those teams who win Formula 1 championships have pit crews who change tires as fast
as possible. The data frame Formula1 and the following table contain the times (in
seconds) that the pit crews of two different teams spent changing tires in 10 randomly
selected races.
Race 1 2 3 4 5 6 7 8 9 10
Team 1 5.613 6.130 5.422 5.947 5.514 5.322 5.690 5.243 5.920 5.859
Team 2 5.934 5.335 5.826 4.821 5.664 5.292 5.257 6.245 5.981 5.197
(a) Assuming that the times are normally distributed, compute a 95% confidence interval
for the variance ratio σ12 /σ22 . Are the population variances equal?
(b) Use the results in part (a) to compute a 95% confidence interval for the difference of
the population means μ2 − μ1 . What does the result mean?
27. Let {X1 , X2 , . . . , Xn } be a random sample from a normal population N (μ, σ), where μ
and σ are unknown. Find the value of the sample size n if (0.59s2 , 2s2 ) is to be at least
a 94% confidence interval for σ 2 .
338 Probability and Statistics with R
28. Use
√ a seed equal to 55, and simulate m = 100 samples of size n = 800 from a N (15, σ =
6 ). Calculate the confidence intervals for σ 2 at the 1 − α = 0.96 confidence level.
Plot the confidence intervals, and calculate the number of times the parameter is not
contained in the simulated confidence intervals.
29. Use a seed√ equal to 224, and simulate mx = 100 samples of size nx √ = 1500 from a
N (3, σ = 5 ) and my = 100 samples of size ny = 1500 from a N (6, σ = 7 ). Calculate
the confidence intervals for σx2 /σy2 with a 1 − α = 0.94 confidence level. Plot the intervals
and calculate the number of times the parameter ratio is not in the simulated confidence
interval.
30. The drug Sulfinpyrazone was studied for its efficacy in preventing death after myocardial
infarction. Construct a 90% confidence interval for the true proportion of deaths between
patients who have suffered a myocardial infarction who were administered Sulphinpyra-
zole and patients who were administered a placebo after myocardial infarctions. Based on
the confidence interval, does Sulphinpyrazole appear to reduce the proportion of deaths
among patients who have suffered a myocardial infarction?
31. From a random sample of 2000 Internet domains registered in a country during the
last few years, 300 were “.org” domains. Compute a 98% confidence interval for the
proportion of “.org” domains registered in that country during the last few years.
√
32. Use a seed equal to 10, and simulate 300 samples of size nx = 65 from a N (4, σx = 2 )
√ s2 /σ 2
distribution and 300 samples of size ny = 90 from a N (5, σy = 3 ). Check that x2 x2
sy /σy
follows an F64,89 distribution.
33. Use a seed equal to 95, and simulate m = 500 samples of size n = 1000 from a B(1, π =
0.4) distribution. Show that the sampling proportion is normally distributed.
34. How large a sample is needed to ensure the bound on the error of estimation for the
population proportion is no more than 2 percentage points for a 95% confidence interval?
35. A large company wants to estimate the proportion of its accounts that are paid on time.
(a) How large a sample is needed to estimate the true proportion within 5% with a 90%
confidence interval?
(b) Suppose 650 out of 800 accounts are paid on time. Construct a 99% confidence
interval for the true proportion of accounts that are paid on time.
36. A sociology research center conducts a survey to discern whether the proportion of
vegetarians is larger in urban or rural areas. Of the 180 people from urban areas, 32
were vegetarians. Of the 75 from rural areas, 17 were vegetarians. Construct a 98%
confidence interval for the difference between urban and rural vegetarian proportions.
37. Schizophrenia and other psychoses are complex and debilitating diseases, which affect
about 2% of the population. Two of the approaches used, as well as in other medical
diseases, to reduce clinical heterogeneity among psychoses are categorical and dimen-
sional. The first one assumes that there exist different subgroups within psychosis and
Confidence Intervals 339
the second one assumes that schizophrenia dimensions fall on a dimensional continuum
within psychosis. A sample of 660 consecutively admitted patients in Hospital Virgen del
Camino (Pamplona, Spain) is available with the following diagnoses: 358 schizophrenic
patients, 61 with schizophreniform disorder, 37 with schizoaffective disorder, 64 with
bipolar disorder, 24 with delusional disorder, 54 with brief psychotic disorder, and 32
with atypical psychosis. Compute a 95% confidence interval for the proportion of the
different types of patients (Cuesta et al., 2007).
Chapter 9
Hypothesis Testing
9.1 Introduction
A hypothesis test in the Neyman-Pearson paradigm is a decision criterion that allows
practitioners of statistics to select between two complementary hypotheses. Before conduct-
ing the hypothesis test, define the null hypothesis, H0 , which is assumed to be true prior
to conducting the hypothesis test. The null hypothesis is compared to another hypothesis,
called the alternative hypothesis, and denoted H1 . The alternative hypothesis is often
called the research hypothesis since the theory or what is believed to be true about the
parameter is specified in the alternative hypothesis. Both hypotheses define complementary
subsets of the parameter space Θ where the parameter θ is defined. The null hypothesis
defines the region [θ ∈ Θ0 ] and the alternative hypothesis defines the region [θ ∈ Θ1 ]. The
subsets Θ0 and Θ1 are mutually exclusive by definition, and they are complementary since
Θ0 ∪ Θ1 = Θ. When a hypothesis uniquely specifies the distribution of the population from
which the sample is taken, the hypothesis is said to be simple. For a simple hypothesis,
Θ0 is composed of a single element. Any hypothesis that is not a simple hypothesis is
called a composite hypothesis. A composite hypothesis does not completely specify the
population distribution. Of the various combinations of hypotheses that could be examined,
the case where the null hypothesis is simple and the alternative hypothesis is composite will
be the focus of this text. Hypothesis tests will generally take a form similar to those in
Table 9.1, where θ0 is a single numerical value. For alternative hypotheses (A) and (B),
which are lower one-sided and upper one-sided, respectively, the hypothesis test is called
a one-tailed test. For the alternative hypothesis in (C), a two-sided alternative, the
hypothesis test is called a two-tailed test.
Example 9.1 If H0 : π = 0.4 in a Bernoulli (π) distribution, the null hypothesis is simple
since the hypothesis H0 : π = 0.4 uniquely specifies the distribution as Bernoulli (0.4). If
H1 : π < 0.4, the hypothesis is composite since π can take any value in the interval [0, 0.4).
The goal in hypothesis testing is to decide which one of the two hypotheses (null and
alternative) is true. To this end, split the sample space into two mutually exclusive subsets
R and Rc . R is the rejection region and Rc is referred to as the acceptance region. The
341
342 Probability and Statistics with R
critical value is the number that splits Θ into R and Rc . To help decide between the two
hypotheses, calculate a test statistic based on the sample information from the experiment.
If the test statistic falls in the acceptance region, accept the null hypothesis. If the value
of the test statistic falls in the rejection region, reject the null hypothesis and accept the
alternative hypothesis.
There are two basic ways to think of a hypothesis test. First, one can think of it
as a two-decision problem where the researcher will choose one of two hypotheses to be
true. This is the historical approach due to Jerzy Neyman and Egon Pearson. The second
method, due to Ronald Fisher, determines how much evidence exists in the data against
the null hypothesis. The null hypothesis is never accepted but is merely a hypothesis of
“no difference.” The test will determine if the data that have been collected could be due
to chance alone if the null hypothesis were true; and if this is not likely, the researcher has
statistically significant evidence that the alternative hypothesis is true. A hypothesis test
where the null hypothesis is never accepted but merely “not rejected” is called a significance
test.
Example 9.2 The weight of a ball-bearing fluctuates between 1.5 g and 4.5 g. One wants
to test whether the distribution of the weight for the ball-bearing has a mean of either 2 g
(H0 : μ = 2) or 2.5 g (H1 : μ = 2.5). A random sample of size one is taken. If the weight
of the ball-bearing is greater than 2.3 g, the null hypothesis that the mean weight of the
ball-bearings is 2 g is rejected, and the alternative hypothesis that the mean weight of the
ball-bearings is 2.5 g is accepted. Specify the sample space, the rejection region, and the
acceptance region for this experiment.
Solution: The sample space is given by the interval [1.5, 4.5]. The rejection region is the
subinterval R = (2.3, 4.5], and the acceptance region is the subinterval Rc = [1.5, 2.3]. Note
that Rc ∪ R = [1.5, 2.3] ∪ (2.3, 4.5] = [1.5, 4.5].
Table 9.2: Possible outcomes and their consequences for a trial by jury
True State of the Defendant
(Reality)
H0 True H0 False
Jury’s Decision (innocent) (guilty)
Accept H0 (not guilty) A. correct B. error
Reject H0 (guilty) C. error D. correct
A. If the null hypothesis is true and the null hypothesis is accepted, the decision is correct.
In the legal example, if the defendant is innocent and the jury decides the defendant is
not guilty of the charge, the jury’s decision is correct.
B. If the null hypothesis is false and it is not rejected, the decision is incorrect. By failing
to reject a false null hypothesis, an error has been made. In statistics, this error is
called a type II error. The probability of committing a type II error is β. In the
legal scenario, a type II error is made when a guilty person is not convicted.
C. If the null hypothesis is true and it is rejected, the decision is incorrect. In other words,
by rejecting a true null hypothesis, an error has been made. In statistics, this type of
error is called a type I error. The probability of committing a type I error is α. In
the legal example, a type I error would be to convict an innocent defendant.
D. If the null hypothesis is false and it is rejected, the decision is correct. In the legal arena,
this translates into a jury convicting a guilty defendant.
The probability of committing a type I error (rejecting H0 when it is true) is called the
level of significance for a hypothesis test. The level of significance is also known as the
size of the test and is denoted by α, where
The relationship between type I and type II errors is shown in Table 9.3 on the next page.
If the researcher fails to reject the null hypothesis when the null hypothesis is true, note
that no error is committed. Specifically, the correct decision should be reached in roughly
(1 − α) × 100% of all trials. Using the same logic, approximately (1 − β) × 100% of the times
sample data are evaluated in a test of hypothesis, a false null hypothesis will be rejected.
Since a type I error is frequently considered to be more serious than a type II error and
the probability of a type I error is easier to control than the probability of a type II error, it
is common practice for researchers to specify a priori the largest probability of a type I error
they are willing to accept and subsequently to use this value as their level of significance to
make a decision when they conduct their hypothesis testing. The North American judicial
system certainly considers convicting an innocent person to be a worse error than allowing
a guilty person to walk free. However, a type I error is not always more critical than a
type II error. Suppose one is going to go sky diving. In this scenario, the null hypothesis
344 Probability and Statistics with R
is that the parachute will open and the alternative hypothesis is that the parachute will
not open. Certainly a type II error (concluding the parachute will open when it will not) is
more critical than a type I error (concluding the parachute will not open when it will).
Example 9.3 Given a normal distribution with unknown mean μ and known standard
deviation σ = 2, one wishes to test the null hypothesis H0 : μ = 1 versus the alternative
hypothesis H1 : μ = 4. A sample of size one is taken where the rejection region is considered
to be the interval (2, ∞). In other words, if the sample value is greater than 2, the null
hypothesis is rejected. On the other hand, if the sample value is less than or equal to two,
one fails to reject the null hypothesis. Determine α and β for this experiment.
Solution: Although there is no way to know if the decision made with regard to the null
hypothesis is correct, there is a reasonable criterion that allows the determination of the
probability of making type I and type II errors.
Note: S-PLUS returns the area to the left of a given value when using the function pnorm.
By default, R also returns the area to the left of a given value when using the function pnorm.
However, R also allows the user to find the area to the right of a given value by using the
argument [Link]=FALSE. Consequently, one might have used the [Link]=FALSE
argument with R’s pnorm function to find the answer.
-5 -2 1 2 4 7 10
FIGURE 9.1: Graphical representation of type I and type II errors when H0 : μ = 1 versus
H1 : μ = 4.
Since the probabilities of committing type I and type II errors for a fixed sample size
are dependent, it is usually impossible to make both type I and type II errors arbitrarily
small. However, out of convenience, the tests considered are restricted to only those tests
that control the type I error at a given significance level and subsequently select from these
tests the test with the most power. Researchers typically fix the probability of committing
a type I error at the 0.01, 0.05, or 0.1 significance level; however, these are merely values
that were tabled early in the history of statistics and have been used mainly for convenience
rather than through any actual merit. Since there are as many tests as there are partitions
of the sample space, the number of tests one may have to evaluate to decide between two
competing hypotheses might be huge. For this very reason, certain partitions will produce
results that are more appealing in the sense of supporting a specific hypothesis.
Power (θ) is a function of the parameter θ, which has for each value of θ in the alternative
hypothesis, θ ∈ Θ1 , the power that a simple alternative hypothesis would have for that
value of θ. When the null hypothesis is simple, θ = θ0 , the power of the test at θ0 is the
same as the significance level, that is, Power (θ0 ) = α.
(a) First, set up the integral to find the value of k that yields a significance level of 0.05:
k
α = P(X1 < k|H0 ) = 2e−2x1 dx1 = 1 − e−2k = 0.05
0
> qexp(0.05, 2)
[1] 0.02564665
Note that the answer clearly illustrates that it is not possible to obtain a single value for
the power of a composite alternative hypothesis since the answer itself is a function of θ. In
other words, for each value of the parameter θ compatible with the alternative hypothesis
(in this case θ > 2), a value for the power function is obtained that corresponds to that
simple hypothesis. As the parameter θ takes on values greater than two, the power function
approaches one.
Example 9.5 Achievement Test Test the null hypothesis that for a certain age
group the mean score on an achievement test (scores follow a normal distribution with
σ = 6) is equal to 40 against the alternative that it is not equal to 40.
(a) Find the probability of type I error for n = 9 if the null hypothesis is rejected when the
sample mean is less than 36 or greater than 44.
(b) Find the probability of type I error for n = 36 if the null hypothesis is rejected when
the sample mean is less than 38 or greater than 42.
(c) Plot the power functions for n = 9 and n = 36 for values of μ between 30 and 50.
Hypothesis Testing 347
(a) The probability of a type I error for n = 9 if the null hypothesis is rejected when the
sample mean is less than 36 or greater than 44 is
6 6
P(Type I error) = P X < 36N 40, √ + P X > 44N 40, √
9 9
36 − 40 44 − 40
=P Z < +P Z >
2 2
= P(Z < −2) + P(Z > 2) = 0.02275 + 0.02275 = 0.04550.
To compute the answer with S, key in
> pnorm(36,40,6/sqrt(9)) + 1 - pnorm(44,40,6/sqrt(9))
[1] 0.04550026
(b) The probability of type I error for n = 36 if the null hypothesis is rejected when the
sample mean is less than 38 or greater than 42 is
6 6
P(Type I error) = P X < 38N 40, √ + P X > 42N 40, √
36 36
38 − 40 42 − 40
=P Z < +P Z >
1 1
= P(Z < −2) + P(Z > 2) = 0.02275 + 0.02275 = 0.04550.
To compute the answer with S, enter
> pnorm(38,40,6/sqrt(36)) + 1 - pnorm(42,40,6/sqrt(36))
[1] 0.04550026
(c) The power function for n = 9 is
6 6
Power (μ) = P X < 36N μ, √ + P X > 44N μ, √
9 9
The power function for n = 36 is
6 6
Power (μ) = P X < 38 N μ, √ + P X > 42 N μ, √
36 36
To produce a plot similar to the one in Figure 9.2 on the next page with R, use the following
code:
> mu <- seq(30,50,.01)
> power9 <- 1-pnorm(44, mu,6/sqrt(9)) + pnorm(36, mu,6/sqrt(9))
> power36 <- 1-pnorm(42, mu,6/sqrt(36)) + pnorm(38, mu,6/sqrt(36))
> plot(mu,power9,type="l", ylab=expression(Power(mu)), xlab=expression(mu),
+ ylim=c(0,1))
> lines(mu, power36, type="l")
> arrows(32, 0.6 , 34.2, .78, lwd=2, length=0.05)
> arrows(32, 0.35 , 37, .78, lwd=2, length=0.05)
> arrows(40, 0.4 , 40, 0.06, lwd=2, length=0.05)
> text(32,0.58, expression(n==9))
> text(32.3,0.33, expression(n==36))
> text(40,0.45, expression(alpha==0.045))
348 Probability and Statistics with R
1.0
0.8
Power (μ)
0.6
n=9
α=0.045
0.4
n = 36
0.2
0.0
30 35 40 45 50
μ
FIGURE 9.2: Graphical representation of the power function, Power (μ), for both scenarios
in Example 9.5 on page 346.
Note that Power (μ0 ) = α for both power functions depicted in Figure 9.2. In general,
as the true μ is farther from the hypothesized μ in H0 , the power of a test will increase.
Additionally, the power function approaches 1 faster for larger n as the true μ moves farther
from the hypothesized μ in H0 .
Example 9.6 Given a N (μ, 1) population from which one takes a simple random sample
of size 1, test the null hypothesis H0 : μ = 1 versus the alternative hypothesis H1 : μ = 2.
Determine the significance level and the power of the test for the following rejection regions:
(a) (2.036, ∞)
and the power of the test is 1 − β = 1 − 0.514 = 0.486. See Figure 9.3 on the facing page
for a graphical representation of the type I and type II errors.
Hypothesis Testing 349
-2 -1 0 1 2.036 3 4 5
FIGURE 9.3: Graphical representation of type I and type II errors when H0 : μ = 1 versus
H1 : μ = 2 with rejection region (2.036, ∞).
(b) Since the rejection region is (1.100, 1.300) ∪ (2.461, ∞), the probability of committing
a type I error is
α = P 1.100 < X < 1.300|N (1, 1) + P X > 2.461|N (1, 1)
1.100 − 1 1.300 − 1 X −1 2.461 − 1
=P <Z< +P >
1 1 1 1
= P(0.100 < Z < 0.300) + P(Z > 1.461)
= P(Z < 0.300) − P(Z < 0.100) + P(Z > 1.461) = 0.618 − 0.540 + 0.072 = 0.150,
β = P X ≤ 1.100|N (2, 1) + P 1.300 ≤ X ≤ 2.461|N (2, 1)
X −2 1.100 − 2 1.300 − 2 X −2 2.461 − 2
=P ≤ +P ≤ ≤
1 1 1 1 1
= P(Z ≤ −0.900) + P(−0.700 ≤ Z ≤ 0.461)
= P(Z ≤ −0.900) + P(Z ≤ 0.461) − P(Z ≤ −0.700)
= 0.184 + 0.678 − 0.242 = 0.620.
It follows that the power of the test is 1 − β = 1 − 0.620 = 0.380. A graphical representation
of the type I and type II errors is provided in Figure 9.4 on the next page. To find α and
β with S, type
It is clear to see from the previous example that, with the same level of significance
(0.150), the power obtained for the test with a rejection region of (1.100, 1.300) ∪ (2.461, ∞)
has less power than the test that uses a rejection region of (2.036, ∞). The probabilities of
committing type I and type II errors for the rejection regions (2.036, ∞) and (1.100, 1.300) ∪
(2.461, ∞) are shown in Figures 9.3 and 9.4, respectively. In general, it is possible to have a
350 Probability and Statistics with R
P(Type II error) = 0.62 P(Type I error) = 0.15
-2 -1 0 1 2 3 4 5
FIGURE 9.4: Graphical representation of type I and type II errors when H0 : μ = 1 versus
H1 : μ = 2 with rejection region (1.100, 1.300) ∪ (2.461, ∞)
test that is “better” in the sense of having more power than another test even though both
tests have the same significance level. So, the researcher wants to find a uniformly most
powerful test that has more power than all other tests that have the correct significance
level, α, if such a test exists. To be complete, it is important to note that uniformly most
powerful tests do not always exist. A generalization that can be made from Example 9.6
is that one-sided tests with the same sample size as two-sided tests will always have more
power for the same α level.
It is important to note that the ℘-value is not fixed a priori, but rather is determined
after the sample is taken. A small ℘-value indicates that observing differences as large or
larger than the one found in the sample is rare, and thus do not occur by chance alone. A
small ℘-value lends support to H1 ; so, given a fixed significance level α, reject H0 whenever
the ℘-value < α. In Fisher’s paradigm, hypothesis tests are tests of significance, where
Hypothesis Testing 351
Step 2: Test Statistic — Select an appropriate test statistic and determine the sampling
distribution of the test statistic or the standardized test statistic under the assump-
tion that the null hypothesis is true.
Choose a test statistic, θ̂, generally one such that the expected value of the test
statistic is equal to the parameter in H0 . For example, if testing μ, θ̂ = X; or, if
testing π, θ̂ = P .
A common standardized test statistic will take the form
θ̂(X) − θ0
T = t(X) = 2 .
Var θ̂(X)
Other test statistics will present themselves when testing hypotheses regarding
variances.
Step 4: Statistical Conclusion — If a rejection region was not computed in step 3, calcu-
late the ℘-value. The procedure for calculating the ℘-value is found in Section 9.5
on the facing page.
Use the rejection region or the ℘-value to determine if the evidence warrants
rejecting the null hypothesis. If tobs falls into the rejection region, reject H0 ; if
not, fail to reject H0 . If the ℘-value is less than α, reject H0 ; if not, fail to reject
H0 .
352 Probability and Statistics with R
Step 5: English Conclusion — State in plain English what the conclusion reached in
step 4 means. This statement will always be about the alternative hypothesis.
That is, the evidence will either warrant concluding the alternative hypothesis or
the evidence will not be sufficient to conclude the alternative hypothesis is true.
There are two distributions that occur frequently in hypothesis testing involving means:
a standard normal distribution and a t-distribution. When the standardized test statistic
follows a standard normal distribution, the hypothesis test will typically be called a one-
sample z -test or a two-sample z -test, depending on whether there are one or two
samples. Likewise, if the standardized test statistic follows a t-distribution, the test will be
a one-sample t-test, a two-sample t-test, or a paired t-test. The general form for a
z-test statistic is
statistic − μstatistic
(9.2)
σstatistic
while the general form of a t-test statistic is
statistic − μstatistic
. (9.3)
σ̂statistic
In testing hypotheses, when the standardized test statistic has the same form as the pivot
used to construct a confidence interval, namely tobs = θ̂(x)−θ 0
σθ̂(X) , and the confidence intervals
and the acceptance region for the null hypothesis are based on the same distribution, there
exists a duality between (1−α)·100% confidence intervals and α-level hypothesis tests. That
is, when θ0 is in the confidence interval, H0 : θ = θ0 is not rejected. This is summarized in
general in Table 9.5.
Table 9.5: Duality of (1 − α) · 100% confidence intervals and α-level tests of significance
Alternative Fail to Reject (1 − α) · 100%
Hypothesis H0 Region Confidence Interval
& (
H1 : θ < θ 0 tobs ≥ tα −∞, θ̂(x) − tα · σθ̂(X)
% '
H1 : θ > θ 0 tobs ≤ t1−α θ̂(x) − t1−α · σθ̂(X) , ∞
%
H1 : θ = θ0 tα/2 ≤ tobs ≤ t1−α/2 θ̂(x) + tα/2 · σθ̂(X) ,
(
θ̂(x) + t1−α/2 · σθ̂(X)
Hypothesis Testing 353
X − μ0
Z= √ ∼ N (0, 1).
σ/ n
The formula to calculate its observed value as well as the three possible alternative hy-
potheses and their rejection regions are described in Table 9.6.
Table 9.6: Summary for testing the mean when sampling from a normal distribution with
known variance (one-sample z-test)
Standardized
x̄−μ
√0
Null Hypothesis — H0 : μ = μ0 Test Statistic’s — zobs = σ/ n
Value
Graphical
Representation of
Rejection Region zα z1−α zα/2 z1−α/2
Step 2: Test Statistic — The test
statistic chosen is X because E X = μ. The value of
n
x i
this test statistic is x̄ = i=1
n = 56
30 = 1.867. The standardized test statistic and
√ 0 ∼ N (0, 1).
its distribution under the assumption H0 is true are Z = X−μ
σ/ n
I. From the rejection region, fail to reject H0 because 0.183 is not greater than
1.64.
II. From the ℘-value, fail to reject H0 because the ℘-value = 0.427 is greater than
0.05.
Fail to reject H0 .
Step 5: English Conclusion — There is not evidence to suggest that the mean is greater
than 1.8.
To use S to find z0.95 , the ℘-value for a zobs value of 0.183 for a right tail alternative
hypothesis, key in
9.7.2 Test for the Population Mean when Sampling from a Normal
Distribution with Unknown Population Variance
The null hypothesis is still H0 : μ = μ0 when working with data from a normal
distribution with unknown variance. However, the standardized test statistic under the
assumption that H0 is true is now
X − μ0
T = √ ∼ tn−1 .
S/ n
The formula to calculate its observed value as well as the three possible alternative hypothe-
ses and their rejection regions are described in Table 9.7 on the next page. The computation
of β and Power with the t-test is not nearly as easy as with the standard normal distribution.
This is due to the fact that when the null hypothesis is false, the random variable X−μ √0
S/ n
has what is known as a non-central t-distribution with non-centrality parameter
μ1 − μ0
γ= ,
√σ
n
denoted tn−1;γ , where μ1 is the true value of μ. Currently, S-PLUS does not have a non-
central t-distribution. However, S-PLUS does have a non-central F distribution that can
be used to compute the power for a two-tailed alternative hypothesis involving the t-test
using the relationship in (9.4). R has both a non-central t-distribution and a non-central F
356 Probability and Statistics with R
distribution. To compute the power for a t-test, one must provide some estimate of σ for
the non-centrality parameter:
P (tn−1;γ < tα/2;n−1 ) ∪ (tn−1;γ > t1−α/2;n−1 ) = P F1,n−1;γ 2 > (t1−α/2;n−1 )2 (9.4)
Table 9.7: Summary for testing the mean when sampling from a normal distribution with
unknown variance (one-sample t-test)
Standardized
x̄−μ
√0
Null Hypothesis — H0 : μ = μ0 Test Statistic’s — tobs = s/ n
Value
Alternative
H1 : μ < μ0 H1 : μ > μ0 H1 : μ = μ0
Hypothesis
Rejection Region tobs < tα;n−1 tobs > t1−α;n−1 |tobs | > t1−α/2;n−1
Graphical
Representation of
Rejection Region t1−α tα/2 t1−α/2
tα
(a) Test the null hypothesis H0 : μ = 2.5 versus the alternative hypothesis H1 : μ = 2.5 at
the α = 0.05 significance level.
(c) Use S to simulate a t24;γ=3 distribution, and use it to compute the simulated power of
the test in (b).
I. From the rejection region, reject H0 because tobs = 2.595 is greater than 2.06.
II. From the ℘-value, reject H0 because the ℘-value = 0.016 is less than 0.05.
Reject H0 .
Step 5: English Conclusion — There is evidence to suggest that the mean is not equal
to 2.5.
To use S to find t0.975,24 and the ℘-value for a tobs value of 2.595 for a two-tailed alternative
hypothesis, type
(b) Before computing Power (μ1 = 4), first determine the non-centrality parameter:
μ1 − μ0 4.0 − 2.5
γ= = = 3.0.
√σ 2.5
√
n 25
X−μ
√0.
Let T = t(X) = S/ n
Then
A graphical representation of the Power (μ1 = 4) is depicted in Figure 9.5 on the following
page. Find the Power (μ1 = 4) using the non-central t-distribution and the non-central F
distribution in R. Note that if one is using S-PLUS, one can only solve the problem using
the non-central F distribution. Further, the non-central
F distribution cannot be used to
find power for directional hypotheses. To find P (t24;3 < t0.025;24 ) ∪ (t24;3 > t0.975,24 ) with
R, enter
> pt(qt(0.025,24),24,3)+(1-pt(qt(0.975,24),24,3))
[1] 0.8207219
358 Probability and Statistics with R
Using the relationship between t-distributions and F distributions given in (9.4), write
P (t24;3 < t0.025;24 ) ∪ (t24;3 > t0.975;24 ) = P F1,24;γ=32 > (t1−α/2;n−1 )2
= P F1,24;γ=32 > (t0.975;24 )2
= P F1,24;γ=32 > (2.06)2 = 4.26
= 0.82.
> 1-pf(qt(.975,24)^2,1,24,9)
[1] 0.8207219
t24 t24;γ=3
−4 −2 0 2 4 6 8
One can also use the R function [Link]()to compute the answer as follows:
n = 25
delta = 1.5
sd = 2.5
[Link] = 0.05
power = 0.8207213
alternative = [Link]
(c) The following S code computes the simulated power and produces the graph in Figure 9.6
on the next page:
> [Link](13)
> nvar <- rnorm(25 * 20000, 4, 2.5)
> nvarmat <- matrix(nvar, 20000, 25) # 20000 by 25 Matrix
> xbar <- apply(nvarmat, 1, mean)
> S <- apply(nvarmat, 1, sd) # Change sd to stdev for S-PLUS
> tstar <- (xbar - 2.5)/(S/5)
> hist(tstar, xlim = c(-4, 8), nclass = "Scott", col = 13,
+ xlab = "", probability = T, ylim = c(0, 0.4),
+ main = "Central and Simulated Non-Central t-Distributions")
> [Link] <- qt(0.975, 24)
Hypothesis Testing 359
-4 -2 0 2 4 6 8
2.5 3.0 3.1 4.0 1.2 5.0 4.1 3.9 3.2 3.3 2.8 4.1 2.7 2.9 3.7
Solution: To solve this problem, start by verifying the normality assumption of the data
using exploratory data analysis (EDA()). The results from applying the function EDA() to
the wheat yields per plot are provided in Figure 9.7 on the following page. Based on the
graphical output from the function EDA(), it is not unreasonable to assume that wheat yield
follows a normal distribution. Now, proceed with the five-step procedure.
H0 : μ = 2 versus H1 : μ > 2
360 Probability and Statistics with R
FIGURE 9.7: Exploratory data analysis of the wheat yield per plot values
Step 2: Test Statistic — The test
statistic chosen is X because E X = μ. The value of
n
i x
this test statistic is x̄ = i=1
n = 49.5
15 = 3.3. The standardized test statistic under
√ 0 ∼ t15−1 .
the assumption that H0 is true and its distribution are X−μ
S/ n
I. From the rejection region, reject H0 because tobs = 5.64 is greater than 1.76.
II. From the ℘-value, reject H0 because the ℘-value ≈ 0 is less than 0.05.
Reject H0 .
Step 5: English Conclusion — There is evidence to suggest that the mean yield with the
new fertilizer is greater than two bushels per plot.
To compute the value of the standardized test statistic and its corresponding ℘-value with
S, type
data: x
t = 5.6443, df = 14, p-value = 3.026e-05
alternative hypothesis: true mean is greater than 2
95 percent confidence interval:
2.894334 Inf
sample estimates:
mean of x
3.3
Note that the upper limit of the confidence interval in the R output is Inf (S-PLUS uses
NA instead of Inf), indicating that the limit on the right side of the confidence interval is
∞. Also, the calculation of the lower limit uses (8.10) on page 300 modified for a one-sided
confidence interval.
9.7.3 Test for the Difference in Population Means when Sampling from
Independent Normal Distributions with Known Variances
When sampling from two normal distributions with known variances, the null hypothesis
for testing the difference between two means is H0 : μX − μY = δ0 , and the standardized
test statistic under the assumption that H0 is true is
X − Y − δ0
Z= 9 ∼ N (0, 1).
2
σX σY2
+
nX nY
The formulas to calculate its observed value as well as the three possible alternative hy-
potheses and their rejection regions are described in Table 9.8 on the following page. Note
that testing the equality of two means (H0 : μX = μY ) is the same as specifying δ0 = 0 in
the null hypothesis H0 : μX − μY = δ0 .
(a) Test the null hypothesis of equality of means at the 10% significance level (α = 0.1)
knowing the difference in sample means is 20.
(b) Find the power of the test in part (a) if μ1 (X, Y ) = μX − μY = 40. (Note that
μ0 (X, Y ) = 0 from H0 .)
362 Probability and Statistics with R
Table 9.8: Summary for test for differences in means when taking independent samples from
normal distributions with known variances (two-sample z-test)
Null Hypothesis — H0 : μX − μY = δ0
Standardized x̄ − ȳ − δ0
Test Statistic’s — zobs = 2 2 2
σX σY
Value nX + nY
Step 1: Hypotheses — To test if engineering majors have a higher average math SAT
score than English majors, the hypotheses are
H0 : μX − μY = 0 versus H1 : μX − μY > 0
Step 2: Test Statistic — The test statistic chosen is X−Y because E X − Y = μX −μY .
The value of this test statistic is 20 according to the problem. The standardized
test statistic under the assumption that H0 is true and its distribution are
X − Y − δ0
2 2 2
∼ N (0, 1).
σX σY
nX + nY
Step 5: English Conclusion — There is evidence to suggest that the difference between
the average math SAT score for engineering majors and that of the average math
SAT score for English majors is greater than zero; therefore, the evidence suggests
engineering majors have a higher average math SAT score.
Hypothesis Testing 363
β μ1 (X, Y ) = P(Fail to Reject H0 |H1 )
X − Y − μ0 (X, Y )
=P
≤ z0.9 H1
σX−Y
X−Y −0
=P
≤ z0.9 H1
σX−Y
= P X − Y ≤ z0.9 σX−Y H1
X − Y − μ1 (X, Y ) z0.9 σX−Y − μ1 (X, Y )
=P ≤ H1
σX−Y σX−Y
(1.282)(15.403) − 40
=P Z≤
15.403
= P(Z ≤ −1.315) = 0.094
9.7.4 Test for the Difference in Means when Sampling from Indepen-
dent Normal Distributions with Variances That Are Unknown
but Assumed Equal
Recall that when random samples of size nX and nY , respectively, are taken from two
normal distributions N (μX , σ) and N (μY , σ), where σ is unknown, the random variable
X − Y − (μX − μY )
T = . & ' ∼ tnX +nY −2
2 1 1
Sp nX + nY
364 Probability and Statistics with R
(n −1)S 2 +(n −1)S 2
by Theorem 6.4 on page 237, where Sp2 = X nXX+nY −2 Y Y
. The null hypothesis used
to test for a difference of means between two normal distributions where the variances are
assumed to be unknown but equal is H0 : μX − μY = δ0 . When H0 is false, the random
variable T has a non-central t-distribution with non-centrality parameter
μ1 (X, Y ) − μ0 (X, Y )
γ=
σX−Y
where μ1 (X, Y ) is the value of μX − μY under H1 and μ0 (X, Y ) = δ0 .
This distribution is denoted tnX +nY −2; γ . The value of the standardized test statistic is
written
x̄ − ȳ − δ0 x̄ − ȳ − δ0
tobs = 2 2 = 2 .
(nX −1)sX +(nY −1)sY
2 2
1 1 1 1
nX +nY −2 nX + nY
s p nX + nY
The three possible alternative hypotheses and the corresponding rejection regions are in
Table 9.9.
Table 9.9: Summary for test for differences in means when taking independent samples
from normal distributions with unknown but assumed equal variances (two-sample pooled
t-test)
Null Hypothesis — H0 : μX − μY = δ0
Use of the pooled t-test should only be undertaken when the variances of X and Y are
almost certainly equal.
100
State School X
State School Y
90
90
80
80
70
70
60
60
50
State School X State School Y
-2 -1 0 1 2
Normal Distribution
FIGURE 9.8: Side-by-side boxplots and normal quantile-quantile plots of the satisfaction
level for graduates from State School X and State School Y .
Five-Step Procedure:
Step 1: Hypotheses — Since the problem gives no reason to suspect graduates from School
X are any more satisfied than graduates from School Y, use a two-tailed alternative
hypothesis:
H0 : μX − μY = 0 versus H1 : μX − μY = 0
Step 2: Test Statistic — The test statistic chosen is X−Y because E X − Y = μX −μY .
The value of this test statistic is 83.45 − 76 = 7.45. The standardized test statistic
under the assumption that H0 is true and its distribution are
% (
X − Y − δ0
. & ' ∼ tnX +nY −2 .
Sp2 n1X + n1Y
I. From the rejection region, reject H0 because |tobs | = 2.08 is greater than 2.06.
II. From the ℘-value, reject H0 because the ℘-value = 0.048 is less than 0.05.
Reject H0 .
366 Probability and Statistics with R
Step 5: English Conclusion — There is evidence to suggest the average satisfaction levels
between State School X and State School Y are different.
To compute the value of the standardized test statistic and its corresponding ℘-value with
S, key in
data: X and Y
t = 2.0798, df = 24, p-value = 0.0484
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
0.05691592 14.85217499
sample estimates:
mean of x mean of y
83.45455 76
The confidence interval is calculated by S using (8.15) on page 308 and does not include 0.
Thus, a conclusion based on this interval would be identical to that in step 5 of the five-step
procedure used to solve this problem.
(b) Before computing Power (μ1 (X, Y ) = 10), first determine the non-centrality parameter:
μ1 (X, Y ) − μ0 (X, Y ) 10 − 0 10 10
γ= = 2 2 =2 = = 2.80.
σX−Y σX 2
σY 92 92 3.573
nX + nY 11 + 15
X − Y − μ1 (X, Y )
Let T = t(X, Y) = 2 . Then
Sp n1X + n1Y
Hypothesis Testing 367
Find the Power (μ1 (X, Y ) = 10) using the non-central t-distribution and the non-central F
distribution in R. Note that if one is using S-PLUS, one can only solve the problem
using
the non-central F distribution. To calculate the quantity P (t24;3 < t0.025;24 ) + P (t24;3 >
t0.975,24 ) with R, enter
> pt(qt(0.025,24),24,2.8)+(1-pt(qt(0.975,24),24,2.8))
[1] 0.7662468
Using the relationship between t-distributions and F distributions given in (9.4), write
P (t24;2.8 < t0.025;24 ) ∪ (t24;2.8 > t0.975;24 ) = P F1,24;γ=2.82 > (t1−α/2;n−1 )2
= P F1,24;γ=2.82 > (t0.975;24 )2
= P F1,24;γ=2.82 > (2.06)2 = 4.26
= 0.766.
To find P (F1,24;9 > 4.26) = 1 − P (F1,24;7.84 < 4.26) with S, key in
> 1-pf(qt(.975,24)^2,1,24,7.84)
[1] 0.7662468
In real problems, the values of the population variances are seldom known. Further, the
random variable
X − Y − (μX − μY )
.& ' , (9.5)
SX2 2
SY
nX + nY
does not have a known distribution. However, the random variable in (9.5) can be approx-
imated with a t-distribution with ν degrees of freedom, where
2 2
sX s2
+ Y
nX nY
ν= 2 2 . (9.6)
(sX /nX ) (s2 /nY )2
+ Y
nX − 1 nY − 1
368 Probability and Statistics with R
The approximation of the random variable (9.5) with a tν is known as the Welch-
Satterthwaite method. Output from S using this technique is simply labeled Welch.
The null hypothesis used to test for a difference of means between two independent
normal distributions where the variances are unknown and unequal is H0 : μX − μY = δ0 .
The value of the standardized test statistic using the Welch-Satterthwaite method is written
x̄ − ȳ − δ0
tobs = 2 2 .
sX s2Y
nX + nY
The three possible alternative hypothesis and the corresponding rejection regions are in
Table 9.10.
Table 9.10: Summary for test for differences in mean when taking independent samples
from normal distributions with unknown and unequal variances (Welch test)
Null Hypothesis — H0 : μX − μY = δ0
Standardized Test x̄ − ȳ − δ0
— tobs = 9
Statistic’s Value s2X s2
+ Y
nX nY
Example 9.12 A bottled water company acquires its water from two independent sources,
X and Y. The company suspects that the sodium content in the water from source X is
less than the sodium content for water from source Y. An independent agency measures the
sodium content in 20 samples from source X and 10 samples from source Y and stores them
in data frame Water. Is there statistical evidence to suggest the average sodium content in
the water from source X is less than the average sodium content in the water from source
Y? The measurements for the sodium values are mg/L. Use an α level of 0.05 to test the
appropriate hypotheses.
Source X: 84 73 92 84 95 74 80 86 80 77
86 72 62 54 77 63 85 59 66 79
Source Y: 78 79 84 82 80 85 81 83 79 81
Solution: To solve this problem, start by verifying the reasonableness of the normality
assumption. The side-by-side boxplots and normal quantile-quantile plots depicted in
Figure 9.9 on the facing page suggest it is reasonable to assume the sodium values for
both sources follow normal distributions; however, it is clear from the boxplot that the
variances are very different. Now, proceed with the five-step procedure.
Hypothesis Testing 369
Source X
90
90 Source Y
80
80
70
70
60
60
50
Source X Source Y -2 -1 0 1 2
Normal Distribution
FIGURE 9.9: Side-by-side boxplots and normal quantile-quantile plots of the sodium
content for source X and source Y.
Step 1: Hypotheses — Since the problem wants to test to see if the mean sodium content
from source X is less than the mean sodium content from source Y, use a lower
one-sided alternative hypothesis.
H0 : μX − μY = 0 versus H1 : μX − μY < 0
Step 2: Test Statistic — The test statistic chosen is X−Y because E X − Y = μX −μY .
The value of this test statistic is 76.4 − 81.2 = −4.8. The standardized test statistic
under the assumption that H0 is true and its approximate distribution are
% (
X − Y − δ0 q
.& ' ∼ tν .
2
SX 2
SY
nX + nY
Step 5: English Conclusion — There is evidence to suggest the average sodium content
for source X is less than the average sodium content for source Y.
370 Probability and Statistics with R
To compute the value of the standardized test statistic and its corresponding ℘-value with
S, type
data: X and Y
t = -1.8589, df = 22.069, p-value = 0.0382
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
NA -0.3665724
sample estimates:
mean of x mean of y
76.4 81.2
The confidence interval S calculates agrees with the one from (8.16) on page 310 modified
for a one-sided confidence interval. Note that the values included in the confidence interval
are all less than zero, which would give a conclusion identical to that found in step 5 of the
five-step procedure.
9.7.6 Test for the Mean Difference when the Differences Have a Normal
Distribution
If one wants to test whether there has been some change in a single group of subjects
or if there exists some difference between two dependent samples, one can compute the net
change from one condition to the next and do a paired t-test provided certain normality
assumptions are satisfied. Recall from Section 8.2.7 on page 313 that when a researcher is
presented with paired samples, the standard approach is to analyze the differences between
the paired data. Provided the distribution of population differences is
D ∼ N (μD = μX − μY , σD ) ,
The null hypothesis for testing a difference of means with dependent samples is H0 : μD =
μX − μY = δ0 , and the value of the standardized test statistic is written
d¯ − δ0
tobs = √ .
sD / n D
The three alternative hypotheses and the rejection regions for H0 are in Table 9.11. The
paired t-test has a smaller variance than does an independent two-sample t-test when the
data are dependent and is a special case of the experimental design known as the randomized
block design. The matched differences are known as blocks. Blocks should be used any
time the differences within a block are relatively homogeneous compared to the differences
within the particular treatment. When blocks are used appropriately, differences noted in
the paired observations can subsequently be attributed to differences in treatments.
Table 9.11: Summary for testing the mean of the differences between two dependent samples
when the differences follow a normal distribution with unknown variance (paired t-test)
Null Hypothesis — H0 : μD = μX − μY = δ0
Standardized d¯ − δ0
Test Statistic’s — tobs = √
sD / n D
Value
Example 9.13 The data frame barley in S-PLUS or in the lattice package lists barley
yield in bushels per acre for the years 1931 and 1932 for ten varieties of barley grown at
six sites. Is there evidence to suggest the average barley yield in 1932 for the Morris site
is greater than the average barley yield in 1932 for the Crookston site? Use the five-step
procedure to test the appropriate hypotheses using an α = 0.05 significance level.
Solution: Note that the same ten varieties are grown at both the Morris and the Crook-
ston site. Consequently, the yields at the two sites are dependent on the varieties. That is,
variety acts as a block. It stands to reason that one can expect less variability between two
similar plots growing the same variety than the variability within each of the plots growing
different varieties. Start the analysis by verifying the normality assumption required to use
a paired t-test. The results from applying the function EDA() to the differences between
the 1932 barley yields from the Morris and Crookston sites are provided in Figure 9.10
on the following page. Based on the graphical output from the function EDA(), it is not
unreasonable to assume the differences between the 1932 barley yields from the Morris and
Crookston sites follow a normal distribution. Now, proceed with the five-step procedure.
Step 1: Hypotheses — To test if the average 1932 barley yield from Morris is greater
than the average 1932 barley yield from Crookston, the hypotheses are
H0 : μD = 0 versus H1 : μD > 0
372 Probability and Statistics with R
FIGURE 9.10: Exploratory data analysis of the differences between 1932 barley yields from
the Morris and Crookston sites.
Step 2: Test Statistic — The test statistic chosen is D because E D = μD . The value of
this test statistic is d¯ = 10.33. The standardized test statistic under the assumption
that H0 is true and its distribution are SDD−δ/ nD ∼ t10−1 .
√0
I. From the rejection region, reject H0 because tobs = 6.29 is greater than 1.83.
II. From the ℘-value, reject H0 because the ℘-value ≈ 0 is less than 0.05.
Reject H0 .
Step 5: English Conclusion — There is evidence to suggest that the 1932 mean barley
yield for Morris is greater than the 1932 mean barley yield for Crookston.
To compute the rejection region, value of the standardized test statistic, and its correspond-
ing ℘-value with S, enter
One-sample t-Test
data: d
t = 6.2924, df = 9, p-value = 0.0001
alternative hypothesis: true mean is greater than 0
95 percent confidence interval:
7.323012 NA
sample estimates:
mean of x
10.33333
Note that the confidence interval calculated by S is using (8.21) on page 314 modified for
one-sided confidence intervals. The interval calculated agrees with our conclusion from step
5 because it contains values that are exclusively greater than zero.
9.8.1 Test for the Population Variance when Sampling from a Normal
Distribution
The tests for population means presented up to this point have assumed the sampling
distributions for their corresponding statistics follow a normal distribution. However, the
tests for means are fairly robust to violations in normality assumptions. In contrast, the
normality assumption for testing a hypothesis about variance is not robust to departures
from normality. Consequently, one should proceed with caution when testing a hypothesis
about the variance especially since non-normality is difficult to detect when working with
small to moderate size samples. As a minimum, one should look at a normal quantile-
quantile plot to make sure normality is plausible before testing a hypothesis concerning the
population variance.
Provided X1 , X2 , . . . , Xn is a random sample from a N (μ, σ) distribution, the random
variable
(n − 1)S 2
∼ χ2n−1 .
σ2
The null hypothesis for testing the population variance is H0 : σ 2 = σ02 , and the value for
the test statistic is
(n − 1)s2
χ2obs = .
σ02
The three alternative hypotheses and the rejection regions for H0 are in Table 9.12 on the
following page.
374 Probability and Statistics with R
Table 9.12: Summary for testing the population variance when sampling from a normal
distribution
Alternative
H1 : σ 2 < σ02 H1 : σ 2 > σ02 H1 : σ 2 = σ02
Hypothesis
χ2obs < χ2α/2;n−1 ∪
Rejection χ2obs < χ2α;n−1 χ2obs > χ21−α;n−1
χ2obs > χ21−α/2;n−1
Region
Graphical
Representation
of Rejection
Region χ2α χ21−α χ2α/2 χ21−α/2
Note that the degrees of freedom for all the χ2 values are n − 1.
Example 9.14 The quality control office of a large hardware manufacturer received
more than twice the number of complaints it usually receives in reference to the diameter
variability of its 4 cm washers. In light of the complaints, the quality control manager
wants to ascertain whether or not there has been an increase in the diameter variability of
the company’s washers manufactured this month versus last month, where the variance was
0.004 cm2 . The manager takes a random sample of 20 washers manufactured this month.
The results are recorded in Table 9.13 and stored in the data frame Washer. Conduct an
appropriate hypothesis test using a significance level of α = 0.05.
Step 1: Hypotheses — The null and alternative hypotheses to test whether the diameter
variability of the companies washers manufactured this month is greater than the
variability last month, where the variance was 0.004 cm2 , are
Step 2: Test Statistic — The test statistic chosen is S 2 because E S 2 = σ 2 . The value
of this test statistic is s2 = 0.005318684. The standardized2 test statistic under the
assumption that H0 is true and its distribution are (n−1)S
σ2
∼ χ2n−1 .
0
Hypothesis Testing 375
Step 5: English Conclusion — There is insufficient evidence to suggest the variance for
washers manufactured this month increased from the variance of washers manufac-
tured last month.
To compute the critical value, the standardized test statistic’s value, and the corresponding
℘-value with S, use the variable washers, which contains the variance of last month’s
washers’ diameters.
> qchisq(0.95,19) # Critical Value
[1] 30.14351
> attach(Washer)
> s2 <- var(diameters)
> s2
[1] 0.005318684
> ChiObs <- 19*s2/0.004 # Standardized Test Statistic’s Value
> ChiObs
[1] 25.26375
> 1-pchisq(ChiObs,19) # P-value
[1] 0.1520425
376 Probability and Statistics with R
2
The null hypothesis for testing the equality of two population variances is H0 : σX = σY2 ,
2 2
which is equivalent to testing H0 : σX /σY = 1. The value for the test statistic when the
variances are assumed equal is
s2
fobs = X .
s2Y
The three alternative hypotheses and the rejection regions for H0 are in Table 9.14.
Table 9.14: Summary for test for equality of variances when sampling from independent
normal distributions
Standardized Test s2
2
Null Hypothesis — H0 : σX = σY2 — fobs = X
Statistic’s Value s2Y
Graphical
Representation
of Rejection
Region f1−α fα/2 f1−α/2
fα
data frame Bac are the sorted blood alcohol content values reported with breathalyzers from
company X and company Y. Test the appropriate hypotheses using a 5% significance level.
(Note: The units of measurement for blood alcohol content, BAC, are grams of alcohol per
liter of blood, g/L .)
Company X: 0.08 0.09 0.09 0.10 0.10 0.10 0.10 0.11 0.11 0.12
Company Y: 0.00 0.03 0.04 0.04 0.05 0.05 0.06 0.07 0.08 0.08
Solution: Prior to using a test that is very sensitive to departures in normality, the func-
tion EDA() is applied to the ten blood alcohol readings using breathalyzers from company
X and the ten blood alcohol readings recorded using breathalyzers from company Y. Based
on the results displayed in Figure 9.12, it seems reasonable to assume the blood alcohol
values breathalyzers report from both companies X and Y follow normal distributions.
Although the blood alcohol values reported with company Y analyzers are slightly skewed
to the left, one must remember that only ten values were used in the construction of the
graphs, and that graphs constructed with small numbers even when sampling from normal
distributions will often appear skewed. When working with small sample sizes, one may
want to test formally the hypothesis of normality with a function like [Link](),
which is explained more fully in Section 10.7.3 of Chapter 10. The Shapiro-Wilk Normality
Test also indicates normality is plausible based on the relatively large ℘-value (0.5489).
Therefore, proceed with the five-step procedure.
> attach(Bac)
> [Link](Y)
data: Y
W = 0.9396, p-value = 0.5489
FIGURE 9.12: Exploratory data analysis for the blood alcohol values using the
breathalyzers from company X and company Y on two volunteers after drinking four beers.
Step 1: Hypotheses — The null and alternative hypotheses to test whether the variability
in blood alcohol values using company X’s breathalyzers is less than the variability
378 Probability and Statistics with R
Step 5: English Conclusion — The evidence suggests the variability of blood alcohol
values using breathalyzers from company X is less than the variance for blood
alcohol values using breathalyzers from company Y.
To compute the critical value, the standardized test statistic’s value, and the corresponding
℘-value with S, enter
data: X and Y
F = 0.2222, num df = 9, denom df = 9, p-value = 0.01764
alternative hypothesis: true ratio of variances is less than 1
95 percent confidence interval:
0.0000000 0.7064207
sample estimates:
ratio of variances
0.2222222
Hypothesis Testing 379
The confidence interval is calculated with (8.31) on page 319, modified for a one-sided
confidence interval. Note that the interval agrees with our step 5 conclusion as it contains
σ2
values that are exclusively less than 1, implying σX
2 < 1.
Y
Table
9.15: Summary for testing the proportion of successes in a binomial experiment
number of successes is Y ∼ Bin(n, π)
Null Hypothesis — H0 : π = π0
n n n−i
H1 : π > π0 P (Y ≥ yobs |H0 ) = i π0i (1 − π0 )
i=yobs
n n−i
H1 : π = π0 I P(Y = i) ≤ P(Y = yobs ) · ni π0i (1 − π0 )
i=0
It is also possible to compute an exact confidence interval for π. However, due to the
discrete nature of Y , the actual confidence level (coverage probability) of the interval is often
considerably higher than the stated confidence level. An exact (1 − α) · 100% confidence
interval for π requires each one-sided ℘-value in an exact binomial test to exceed α/2.
Except when y = 0 and the lower bound is zero, and when y = n and the upper bound is
380 Probability and Statistics with R
1, the lower and upper endpoints for an exact (1 − α) · 100% confidence interval for π are
the solutions in π0 to the equations
y n
n k α n k α
π0 (1 − π0 )n−k ≥ and π0 (1 − π0 )n−k ≥ . (9.7)
k 2 k 2
k=0 k=y
For values of y = 1, 2, . . . , n − 1, it can be shown that the solutions to (9.7) yield the
lower and upper endpoint expressions for the confidence interval given in
CI 1−α (π) =
⎡ ⎤
−1 −1
⎣ 1+ n − y obs + 1 n − y obs ⎦
, 1+
yobs Fα/2;2yobs ,2(n−yobs +1) (yobs + 1)F1−α/2;2(yobs +1),2(n−yobs )
(9.8)
Both S-PLUS and R perform an exact binomial test using the function [Link]().
However, at the time of writing, the S-PLUS [Link]() did not provide a corresponding
confidence interval and uses a different criterion to compute its ℘-value for two-sided
alternatives than the one presented in Table 9.15 on the previous page. The R [Link]()
uses the criterion in Table 9.15 on the preceding page, called the likelihood method,
to compute its ℘-values for two-sided alternatives, while the S-PLUS [Link]() uses
a tail-balancing criterion; see Blaker (2000). Using the tail-balancing approach, the
℘-value is the minimum of the two-tailed probabilities P (Y ≥ yobs ) and P (Y ≤ yobs ) plus
an attainable probability in the other tail that is as close as possible to, but not greater
than, that one-tailed probability. The criteria both S-PLUS and R use to compute ℘-values
with [Link]() for two-sided alternatives differ from the general criterion of
2 min P (Y ≤ yobs |H0 ) , P (Y ≥ yobs |H0 ) ,
(a) Is there statistical evidence to refute the claim at the α = 0.05 level?
(b) Compute an exact 95% confidence interval for the true proportion of college graduates
that find work in their chosen field of study.
Step 1: Hypotheses — The null and alternative hypotheses to test whether or not 20%
of college graduates find work in their chosen field are
Step 2: Test Statistic — The test statistic chosen is Y , where Y is the number of college
graduates finding work in their chosen field. Provided H0 is true, Y ∼ Bin(n, π0 ).
The value of the test statistic is yobs = 90.
The S code to compute this ℘-value and the R output from using [Link]()
are
Tail-Balancing Method: To compute the ℘-value, first find P (Y ≤ yobs |H0 ) and
P (Y ≥ yobs |H0 ) :
yobs
n i n−i
P (Y ≤ yobs |H0 ) = π0 (1 − π0 )
i=0
i
90
500 500−i
= 0.20i (1 − 0.20)
i=0
i
=0.1437028
n
n i
P (Y ≥ yobs |H0 ) = π0 (1 − π0 )n−i
i=y
i
obs
500
500 500−i
= 0.20i (1 − 0.20)
i=90
i
=0.8807233
Fail to reject H0 .
Step 5: English Conclusion — There is not sufficient evidence to suggest the proportion
of college graduates finding work in their chosen fields of study is something other
than 20%.
Hypothesis Testing 383
# −1
500 − 90 + 1
CI 1−0.05 (π) = 1 + ,
(90)F0.05/2;2(90),2(500−90+1)
⎤
−1
500 − 90 ⎦
1+
(90 + 1)F1−0.05/2;2(90+1),2(500−90)
⎡ ⎤
−1 −1
411 410
=⎣ 1+ , 1+ ⎦
(90)(0.7888743) (91)(1.245244)
= [0.1473006, 0.2165364]
One is 95% confident that the true proportion of college graduates finding work in their
chosen fields of study lies in [0.147, 0.216]. Note that this confidence interval, calculated in
step 4 by R, contains the hypothesized value of 0.20, corroborating the decision to fail to
reject the null hypothesis.
The formula to calculate the test statistic’s observed value as well as the three possible
alternative hypotheses and their rejection regions are described in Table 9.16 on the next
page.
When |p − π0 | > 2n1
, many statisticians advocate using a continuity correction when
calculating confidence intervals and standardized test statistics’ values. A continuity cor-
rection of ± 2n
1
is automatically applied when using the S function [Link](); however,
384 Probability and Statistics with R
Table 9.16: Summary for testing the proportion of successes in a binomial experiment
(normal approximation)
Null Hypothesis — H0 : π = π0
not all statisticians recommend the use of a continuity correction with this test, and using
one does lead to a more conservative test. The continuity corrections that are applied, as
well as the standardized test statistic calculations, can be found in Table 9.17.
1 p − π0 + 1
p − π0 < 0 + zobs = 2 2n
2n π0 (1−π0 )
n
Step 2: Test Statistic — The test statistic chosen is P , where P is the proportion of
college graduates finding work in their chosen field. Provided H0 is true,
.
q π0 (1 − π0 )
P ∼ N π0 ,
n
Step 3: Rejection Region Calculations — Because the standardized test statistic has
an approximate N (0, 1) distribution, and H1 is a two-sided hypothesis, the rejection
region is |zobs | > z0.975 = 1.96. The value of the standardized test statistic is
p − π0 p − π0 + 1
zobs = 2 zobs = 2 2n
π0 (1−π0 ) π0 (1−π0 )
n n
90
− 0.2 − 0.2 +
90 1
=2 500 OR = 2
500 1000
(0.2)(1−0.2) (0.2)(1−0.2)
500 500
= −1.1180 = −1.0621
> Y <- 90
> n <- 500
> p <- Y/n
> PI <- 0.2
> zobs <- (p - PI)/sqrt((PI * (1 - PI))/n)
> pval <- 2 * pnorm(zobs)
> zobsC <- (p - PI + 1/(2 * n))/sqrt((PI * (1 - PI))/n)
> pvalC <- 2 * pnorm(zobsC)
> round(c(zobs, pval, zobsC, pvalC), 4)
[1] -1.1180 0.2636 -1.0621 0.2882
386 Probability and Statistics with R
Step 5: English Conclusion — There is not sufficient evidence to suggest the proportion
of college graduates finding work in their chosen fields of study is something other
than 20%.
(b) An approximate 95% confidence interval is [0.1488049, 0.2160747] without a continuity
correction and [0.1478847, 0.2171388] with a continuity correction. One is 95% confident
that the true proportion of college graduates finding work in their chosen fields of study
lies in [0.1488049, 0.2160747]. Note that this confidence interval contains the hypothesized
value of 0.20, corroborating the decision to fail to reject the null hypothesis.
The calculation of 95% confidence intervals as well as verifications of the calculated ℘-values
from step 4 are computed with [Link]() both without and with continuity corrections
being used:
> [Link](x=90, n=500, p=.2, correct=FALSE)
Note that the output for [Link]() does not give a zobs -value; rather it reports a
χ2obs -value, denoted X-squared in the S output, with one degree of freedom. Provided one
uses the relationship Z 2 = χ21 , it is possible to see that the zobs -values reported in step 3
correspond to the X-squared values given in the S output from using [Link]() without
and with continuity correction. That is, −1.1180342 = 1.25 and −1.0621322 = 1.1281.
Although the approximation procedures presented in this section lead to the same
conclusion as the exact test in the previous section when applied to Example 9.16 on
page 380, the approximation procedures of this section are only valid when applied to large
samples. In contrast, the exact test presented in the last section will work for both large
and small samples and is generally preferred over large sample approximation procedures
when the user has access to a computer.
Hypothesis Testing 387
One of the most common ways to present numerical data is in a table. When presented
with a 2 × 2 table, where 2 × 2 refers to the dimensions of the number of internal cells, if the
sample size is small, equality of proportions should be tested with Fisher’s exact test.
That is, H0 : πX = πY , where X ∼ Bin(m, πX ) and Y ∼ Bin(n, πY ) are the numbers of
successes observed from two independent binomial random variables. To compute Fisher’s
exact test, let N = m + n be the total sample size and k = x + y be the total number of
observed successes. Table 9.18 shows the general form of such a table.
Fisher’s exact test uses the number of successes from the X sample as its test statistic,
namely X. The observed value of X is denoted x. In performing the exact test, the total
number of successes is considered fixed. That is, x + y = k is a fixed quantity in the
derivation of the test. Specifically,
m n
i
P(X = i |X + Y = k) = Nk−i
, where i = max{0, k − n}, . . . , min{m, k}. (9.10)
k
Note that (9.10) is a hypergeometric distribution, Hyper (m, n, k), where the parameters
are m, n, and k. As in Section 9.9.1, the three possible alternative hypotheses and the
respective ℘-value calculation formulas are presented in Table 9.19 on the following page.
Since the distribution of the statistic is obtained by constructing all possible 2 × 2 tables,
the test has historically been used with small samples. With the advent of inexpensive
computing power, it is now feasible to use Fisher’s exact test on relatively large samples
with fixed marginals.
A statistic that measures how associated X and Y are is the odds ratio. It is frequently
used in biomedical and sociological studies to measure the association between two variables.
The odds ratio is defined as
πX /(1 − πX )
θ= . (9.11)
πY /(1 − πY )
An odds ratio other than 1 indicates there is a relationship between X and Y , while an
odds ratio of exactly 1 indicates that X and Y are independent. If the odds ratio is larger
than 1, πX is greater than πY ; and if smaller, πX is less than πY .
Only R computes a (1 − α) · 100% confidence interval for the odds ratio. R’s procedure
uses maximum likelihood techniques with the non-central hypergeometric distribution to
compute the confidence interval. The procedure is beyond the scope of this text. When
R’s (1 − α) · 100% confidence interval for the odds ratio upper bound is less than 1, one
can be (1 − α) · 100% confident that πX is less than πY . Likewise, when the lower bound
of a (1 − α) · 100% confidence interval for the odds ratio is greater than 1, one can be
(1 − α) · 100% confident that πX is greater than πY . R is capable of performing this
test for one-sided alternative hypotheses; however, at the time of writing, S-PLUS was
388 Probability and Statistics with R
Table 9.19: Summary for testing the proportion of successes with Fisher’s exact test
Null Hypothesis — H0 : πX = πY
Test Statistic’s
— x = number of observed successes from X sample
Value
not. Additionally, the current version of S-PLUS does not compute a confidence interval
associated with [Link]().
Table 9.20: Juveniles who failed a vision test classified by delinquency and glasses wearing
(Weindling et al., 1986)
Wear Do Not Wear
Glasses Glasses Totals
Juvenile Delinquents 1 8 9
Non-delinquents 5 2 7
Totals 6 10 16
Solution: To solve this problem, use Fisher’s exact test and the five-step procedure.
Step 1: Hypotheses — The null and alternative hypotheses to test whether the proportion
of non-delinquents who wear glasses is different from the proportion of juvenile
delinquents who wear glasses are
H0 : πX = πY versus H1 : πX = πY .
In this case, the random variable X will represent the number of juvenile delinquents
who wear glasses, and the random variable Y will represent the number of non-
delinquents who wear glasses.
Hypothesis Testing 389
Step 2: Test Statistic — The test statistic chosen is X, where X is the number of
juvenile delinquents who wear glasses. The observed value of the test statistic
is x = 1. Provided H0 is true, and conditioning on the fact that X + Y = k,
X ∼ Hyper (m, n, k).
For such a small sample, the seven possible 2 × 2 tables that can be constructed
where k = 6, m = 9, and n = 7 and their respective ℘-values are shown in
Table 9.21. Since the ℘-value is 0.035, one rejects H0 because 0.035 is less than
0.05.
Reject H0 .
Table 9.21: Seven possible 2 × 2 tables that can be constructed where k = 6, m = 9, and
n = 7, with their associated probabilities
Table Probability Table Probability Table Probability Table Probability
0 9 1 8 2 7 3 6
0.00087 0.0236 0.15734 0.36713
6 1 5 2 4 3 3 4
4 5 5 4 6 3
0.33042 0.11014 0.0104
2 5 1 6 0 7
> round(p,5)
[1] 0.00087 0.02360 0.15734 0.36713 0.33042 0.11014 0.01049
> pobs <- dhyper(1,9,7,6)
> pval <- sum(p[p<=pobs])
> pval
[1] 0.03496503
> [Link](JD) # Output is for R, S-PLUS is slightly different.
data: JD
p-value = 0.03497
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
0.0009525702 0.9912282442
sample estimates:
odds ratio
0.06464255
Note that values of θ farther from 1.0 in a given direction represent stronger levels of
association (0 < θ < ∞). In this case, θ = 0.06 means that the odds ratio for non-
delinquents wearing glasses is 1/0.06 = 15.5 times the odds ratio for delinquents wearing
glasses. This is a very strong association.
Table 9.22: Observed heart attacks for those physicians taking aspirin and a placebo
(Hennekens, 1988)
Heart No Heart
Attack Attack Totals
Aspirin 104 10,933 11,037 = m
Placebo 189 10,845 11,034 =n
Totals 293 = k 21,778 22,071 = N
Solution: To solve this problem, use Fisher’s exact test and the five-step procedure.
Step 1: Hypotheses — The null and alternative hypotheses to test whether the proportion
of physicians who suffer heart attacks while taking aspirin is less than the proportion
of physicians who suffer heart attacks while taking a placebo are
H0 : πX = πY versus H1 : πX < πY .
Hypothesis Testing 391
In this case, let the random variable X represent the number of physicians who had
a heart attack while taking aspirin, and let the random variable Y represent the
number of physicians who had a heart attack while taking a placebo.
Step 2: Test Statistic — The test statistic chosen is X, where X is the number of
physicians who had a heart attack while taking aspirin. Provided H0 is true, and
conditioning on the fact that X + Y = k, X ∼ Hyper (m, n, k). The observed value
of the test statistic is x = 104. To enter the data from Table 9.22 on the preceding
page into S, type the following code:
Note that the limits on the sum are typically the max{0, x − n} and x. In this case
x − n = 104 − 11, 034 = −10, 930, so the lower limit of the sum will be zero. This
calculation should be done with a computer, so the S code to do so follows. Note
that the data from the table must have been entered as shown in step 2.
data: HA
p-value = 3.253e-07
alternative hypothesis: true odds ratio is less than 1
95 percent confidence interval:
0.0000000 0.6721508
sample estimates:
odds ratio
0.5458537
Note that the odds ratio for physicians having a heart attack taking a placebo is 1/0.546 =
1.83 times the odds ratio for physicians who take aspirin.
In Section 9.9.3, Fisher’s exact test was presented for testing the equality of proportions
for two independent random samples taken from Bernoulli populations of sizes m and n,
respectively. Once the sample sizes become large for Fisher’s exact test, even computers
begin to have difficulties. Thus, there exists a procedure for approximating the distribution
of PX −PY that will lead to a test that does not have nearly the computational requirements
of Fisher’s exact test. In Section 8.4.2, it was argued that
.
q πX (1 − πX ) πY (1 − πY )
PX − PY ∼ N πX − πY , + (9.12)
m n
when taking independent random samples of sizes m and n from Bernoulli (πX ) and
Bernoulli (πY ) populations, respectively. Using (9.12),
(PX − PY ) − (πX − πY ) q
Z= 2 ∼ N (0, 1). (9.13)
πX (1−πX ) πY (1−πY )
m + n
Unfortunately, the values of πX and πY are unknown. In Section 8.4.2, πX and πY were
replaced with their maximum likelihood estimators, π̂X = PX and π̂Y = PY , respectively,
to create the asymptotic confidence interval in (8.48). To create a standardized test statistic
with an approximate normal distribution, the same approach will be used. That is,
(PX − PY ) − δ0 q
Z= 2 ∼ N (0, 1) (9.14)
PX (1−PX ) PY (1−PY )
m + n
can be used to test the null hypothesis H0 : πX − πY = δ0 . It is often the case that δ0
is zero. In this case, it is standard practice to create a pooled estimate of the population
proportions such that πX = πY = π. The pooled estimate of π, denoted P , is
X +Y
P = (9.15)
m+n
which is simply an estimate of the total proportion of successes. When this estimate is
used, the standardized test statistic becomes
(PX − PY ) q
Z=2 1 ∼ N (0, 1). (9.16)
P (1 − P ) m + n
1
There are advantages and disadvantages to both (9.14) and (9.16) as test statistics. The
S function [Link]() bases its confidence interval construction on (9.14) and uses (9.16)
for testing hypotheses. Table 9.23 on the facing page uses the standardized test statistic in
(9.16) and provides the rejection regions for the three possible alternative hypotheses.
Hypothesis Testing 393
Table 9.23: Summary for testing the differences of the proportions of successes in two
binomial experiments (large sample approximation)
Null Hypothesis — H0 : πX = πY
1
When |pX − pY | > 12 m + n1 , some statisticians advocate using a continuity correction
when calculatingconfidence intervals and standardized test statistics’ values. A continuity
correction of ± 21 m1
+ n1 is automatically applied when using the S function [Link]()
on two samples. The continuity corrections that are applied, as well as the standardized test
statistic calculations, can be found in Table 9.24. When applying the continuity correction
to (8.48) on page 328, recall that the continuity correction is subtracted and added to the
lower and upper confidence limits, respectively.
1
Table 9.24: Correction factors when |pX − pY | > 1
2 m + 1
n
Example 9.20 Large Sample Test of πX = πY : Heart Attacks Use the data
from Table 9.22 on page 390 to test whether physicians who take aspirin are less likely to
suffer heart attacks than those who take a placebo at an α level of 0.05. Base the test on
the large sample approximation procedures found in Table 9.23.
Step 1: Hypotheses — The null and alternative hypotheses to test whether the proportion
of physicians who suffer heart attacks while taking aspirin is less than the proportion
394 Probability and Statistics with R
H0 : πX = πY versus H1 : πX < πY .
In this case, let the random variable X represent the number of physicians who had
a heart attack while taking aspirin, and let the random variable Y represent the
number of physicians who had a heart attack while taking a placebo.
Step 2: Test Statistic — The test statistic chosen is PX −PY since E[PX −PY ] = πX −πY .
The standardized test statistic under the assumption that H0 is true is
PX − PY
Z=2 1
P (1 − P ) m + n1
Step 3: Rejection Region Calculations — Because the standardized test statistic has
an approximate N (0, 1) distribution and H1 is a lower one-sided hypothesis, the
rejection region is zobs < z0.05 = −1.645. The pooled estimate of π is p = m+n
x+y
=
293
22071 . The value of the standardized test statistic is
pX − pY
zobs = 2 1
p(1 − p) m + n1
104
− 11034
189
= 2
11037
293
22071 1− 293
22071
1
11037 + 1
11034
= −5.01139
OR
With Continuity Correction
1
pX − pY + 12 m + n1
zobs = 2 1
p(1 − p) m + n1
1
11037 − 11034 + 2 11037 + 11034
104 189 1 1
= 2 1
22071 1 − 22071
293 293 1
11037 + 11034
= −4.94258
Step 4: Statistical Conclusion — The ℘-value is P(Z ≤ zobs ) and is approximately 0 for
both cases. This is less than 0.05, so reject H0 .
Reject H0 .
Notice that if zobs is squared, it will be equal to the values of X-squared in the S output.
396 Probability and Statistics with R
9.10 Problems
1. Define α and β for a test of hypothesis. What is the quantity 1 − β called?
2. How can β be made small in a given hypothesis test with fixed α?
3. Using a 5% significance level, what is the power of the test H0 : μ = 100 versus H1 : μ =
100 if the true standard deviation is σ = 50 grams?
4. An experiment was conducted to investigate how the resistance of rubber to abrasion
is affected by the hardness of the rubber and its tensile strength. The data come from
Hand et al. (1994, Data Set #6 Abrasion Loss) and are stored in the data frame Rubber
of the MASS package. The abrasion loss is measured in grams/hour; the hardness, in
degrees shore; and the tensile strength, in kg/cm2 . Use the five-step procedure to test
whether H0 : μ = 170 versus H1 : μ < 170 for abrasion loss (loss).
5. An apartment appraiser in Vitoria, Spain, feels confident in his appraisals of 90m2 or
larger pisos (apartments) provided his variability is less than 60,000e 2 . Due to constant
movement in the housing market, the regional housing authority suspects the appraiser’s
variability may be greater than 60,000 e 2 . Is there evidence to support the suspicions of
the regional housing authority? Test the appropriate hypothesis at the 5% significance
level using the five-step procedure. The appraised values of apartments in Vitoria are
stored in the variable totalprice of the vit2005 data frame.
6. The Hubble Space Telescope was put into orbit on April 25, 1990. Unfortunately, on
June 25, 1990, a spherical aberration was discovered in Hubble’s primary mirror. To
correct this, astronauts had to work in space. To prepare for the mission, two teams
of astronauts practiced making repairs under simulated space conditions. Each team of
astronauts went through 15 identical scenarios. The times to complete each scenario were
recorded in days. Is one team better than the other? If not, can both teams complete
the mission in less than 3 days? Use a 5% significance level for all tests. The data are
stored in the data frame Hubble.
7. The research and development department of an appliance company suspects the energy
consumption required of their 18 cubic foot refrigerator can be reduced by a slight
modification to the current motor. Thirty 18 cubic foot refrigerators were randomly
selected from the company’s warehouse. The first 15 had their motors modified while
the last 15 were left intact. The energy consumption (kilowatts) for a 24 hour period
for each refrigerator was recorded and stored in the data frame Refrigerator. The
refrigerators with the design modification are stored in the variable modelA and those
without the design modification are stored in the variable modelB. Is there evidence that
the design modification reduces the refrigerators’ average energy consumption?
8. The Yonalasee tennis club has two systems to measure the speed of a tennis ball. The
local tennis pros suspects one system (Speed1) consistently records faster speeds. To
test her suspicions, she sets up both systems and records the speeds of 12 serves (three
serves from each side of the court). The values are stored in the data frame Tennis in
the variables Speed1 and Speed2. The recorded speeds are in kilometers per hour. Does
the evidence support the tennis pro’s suspicion? Use α = 0.10.
9. An advertising agency is interested in targeting the appropriate gender for a new “low-
fat” yogurt. In a national survey of 1200 women, 825 picked the “low-fat” yogurt over
Hypothesis Testing 397
a regular yogurt. Meanwhile, 525 out of 1150 men picked the “low-fat” yogurt over the
regular yogurt. Given these results, should the advertisements be targeted at a specific
gender? Test the appropriate hypothesis at the α = 0.01 level.
10. A plastics manufacturer makes two sizes of milk containers: half-gallon and gallon
sizes. The time required for each size to dry is recorded in seconds in the data frame
MilkCarton. Test to see if there are differences in average drying times between the
container sizes.
11. A multinational conglomerate has two textile centers in two different cities. In order to
make a profit, each location must produce more than 1000 kilograms of refined wool per
day. A random sample of the wool production in kilograms on five different days over the
last year for the two locations was taken. The results are stored in the data frame Wool.
Based on the collected data, does the evidence suggest the locations are profitable? Is
one location superior to the other?
12. Use the data frame Fertilize, which contains the height in inches for self-fertilized
plants in the variable self to
(a) Test if the data suggest that the average height of self-fertilized plants is more than
17 inches. (Use α = 0.05.)
(b) Compute a one-sided 95% confidence interval for the average height of self-fertilized
plants (H1 : μ > 17).
(c) Compute the required sample size to obtain a power of 0.90 if μ1 = 18 inches
assuming that σ = s.
(d) What is the power of the test in part (a) if σ = s and μ1 = 18.
13. A manufacturer of lithium batteries has two production facilities. One facility manu-
factures a battery with an advertised life of 180 hours (facilityA), while the second
facility manufactures a battery with an advertised life of 200 hours (facilityB). Both
facilities are trying to reduce the variance in their products’ lifetimes. Is the variability
in battery life equivalent, or does the evidence suggest the facility producing 180 hour
batteries has smaller variability than the facility producing 200 hour batteries? Use the
data frame Battery with α = 0.05 to test the appropriate hypothesis.
14. In the construction of a safety strobe, a particular manufacturer can purchase LED
diodes from one of two suppliers. It is critical that the purchased diodes conform to
their stated specifications with respect to diameter since they must be mated with a
fixed width cable. The diameter in millimeters for a random sample of 15 diodes from
each of the two suppliers is stored in the data frame Leddiode. Based on the data, is
there evidence to suggest a difference in variabilities between the two suppliers? Use an
α level of 0.01.
15. The technology at a certain computer manufacturing plant allows silicon sheets to be
split into chips using two different techniques. In an effort to decide which technique is
superior, 28 silicon sheets are randomly selected from the warehouse. The two techniques
of splitting the chips are randomly assigned to the 28 sheets so that each technique is
applied to 14 sheets. The results from the experiment are stored in the data frame Chips.
Use α = 0.05, and test the appropriate hypothesis to see if there are differences between
the two techniques. The values recorded in Chips are the number of usable chips from
each silicon sheet.
398 Probability and Statistics with R
(a) Normal patients have a Q10 reading of 0.69 μM. Using the variable Q10.2, is there
evidence that the mean value of Q10 in patients diagnosed with PKU is less than
0.69 μM? (Use α = 0.01.)
(b) Patients diagnosed with PKU are placed on strict vegetarian diets. Some have
speculated that patients diagnosed with PKU have low Q10 readings because meats
are rich in Q10. Is there evidence that the patients’ Q10 level decreases over time?
Construct a 99% confidence interval for the means of the Q10 levels using Q10.1 and
Q10.4
17. According to the Pamplona, Spain, registration, 0.4% of immigrants in 2002 were from
Bolivia. In June of 2005, a sample of 3740 registered foreigners was randomly selected.
Of these, 87 were Bolivians. Is there evidence to suggest immigration from Bolivia has
increased? (Use α = 0.05.)
18. Find the power for the hypothesis H0 : μ = 65 versus H1 : μ > 65 if μ1 = 70 at the
α = 0.01 level assuming σ = s for the variable hard in the data frame Rubber of the
MASS package.
19. The director of urban housing in Vitoria, Spain, claims that at least 50% of all apartments
have more than one bathroom and that at least 75% of all apartments have an elevator.
(a) Can the director’s claim about bathrooms be contradicted? Test the appropriate
hypothesis using α = 0.10. Note that the number of bathrooms is stored in the
variable toilets in the data frame vit2005.
(b) Can the director’s claim about elevators be substantiated using an α level of 0.10?
Use both an approximate method as well as an exact method to reach a conclusion.
Are the methods in agreement?
(c) Test whether the proportion of apartments built prior to 1980 without garages have
a higher proportion with elevators than without elevators.
20. A rule of thumb used by realtors in Vitoria, Spain, is that each square meter will cost
roughly e 3000. However, there is some suspicion that this figure is high for apartments
in the 55 to 66 m2 range. Use a 5 m2 bracket, that is, [55, 60) and [60, 65), to see if
evidence exists that the average difference between the larger and smaller apartment
sizes is less than e 15,000.
(a) Use the data frame vit2005 and the variables totalprice and area to test the
appropriate hypothesis at a 5% significance level.
(b) Are the assumptions for using a t-test satisfied? Explain.
(c) Does the answer for (b) differ if the variances are assumed to be equal? Can the
hypothesis of equal variances be rejected?
Hypothesis Testing 399
21. A survey was administered during the first trimester of 2005 in the Spanish province of
Navarra. The numbers of unemployed people according to urban and rural areas and
gender follow.
Unemployment in Navarra, Spain, in 2005
Male Female Totals
Urban 4734 6161 10895
Rural 3259 4033 7292
Totals 4933 10194 18127
(a) Test to see if there is evidence to suggest that π[Link] < π[Link] at α = 0.05.
(b) Use an exact test to see if the evidence suggests π[Link] > 0.55.
(c) Is there evidence to suggest the unemployment rate for rural females is greater than
50%? Use α = 0.05 with an exact test to reach a conclusion.
(d) Does evidence suggest that π[Link] > π[Link] ?
22. The owner of a transportation fleet is evaluating insurance policies for transporting
hazardous waste. The owner has narrowed his possibility of insurers to two companies
(A and B). Insurance company A claims to have the least expensive policies on the
market while insurer B disputes the claim. To evaluate company A’s claim, the owner
requests the last 100 policies issued by each insurer. The means and standard deviations
are e 325 and e 85 for company A and e 340 and e 75 for company B. Based on these
summary statistics, the owner was not convinced that company A actually had less
expensive rates. Consequently, a representative from company A was sent to speak to
the owner. The representative from company A convinced the owner to take another
look at the numbers. This time, insurance quotes were sought from both insurers for
the next 15 transportation of hazardous waste jobs. Results are given in the data frame
InsurQuotes. Analyze these data. How is it possible the owner changed his mind with
a sample of size 15 versus the results based on a sample of size 100?
23. The data frame vit2005 contains housing information for the Spanish city Vitoria
collected in 2005. Use the variables age and garage to see if the proportion of abodes
with garages has increased since 1980. Use α = 0.05 to reach a conclusion.
24. Environmental monitoring is done in many fashions, including tracking levels of different
chemicals in the air, underground water, soil, fish, milk, and so on. It is believed that
milk cows eating in pastures where gamma radiation from iodine exceeds 0.3 μGy/h in
turn leads to milk with iodine concentrations in excess of 3.7 MBq/m3 . Assuming the
distribution of iodine in pastures follows a normal distribution with a standard deviation
of 0.015 μGy/h, determine the required sample size to detect a 2% increase in baseline
gamma radiation (0.3μGy/h) using an α = 0.05 significance level with probability 0.99
or more.
400 Probability and Statistics with R
25. A local farmer packages and freezes his spinach. He claims that the packages weigh 340
grams and have a standard deviation of no more than σ = 15 grams. The manager of
a local organic supermarket is somewhat skeptical of the farmer’s claim and decides to
test the claim using a random sample of 10 frozen spinach packages.
(a) Find the critical region of the test if α = 0.05.
(b) Find the power of the test if σ = 10.
26. A cell phone provider has estimated that it needs revenues of e 2 million per day in
order to make a profit and remain in the market. If revenues are less than e 2 million
per day, the company will go bankrupt. Likewise, revenues greater than e 2 million per
day cannot be handled without increasing staff. Assume that revenues follow a normal
distribution with σ = e 0.5 million and a mean of μ.
(a) Graphically depict the power function for testing H0 : μ = 2 versus H1 : μ = 2 if
n = 150 and α = 0.05 for values of μ ranging from 1.8 to 2.2.
(b) Graphically depict the power for testing H0 : μ = 2 versus H1 : μ = 2 when μ1 = 2.1
and n = 150 for values of α ranging from 0.01 to 0.5.
(c) Graphically depict the power for testing H0 : μ = 2 versus H1 : μ = 2 when μ1 = 2.1
and α = 0.05 for values of n ranging from 1 to 365.
(d) Generalize what is seen in the graphs for (a), (b), and (c).
27. Use simulation to compute the empirical significance level by generating 10,000 samples
of size n from a N (100, 28) population using α = 0.05 to test the alternative hypothesis
H1 : μ = 100. Use the command [Link](33) so the answers can be reproduced.
(a) Use samples of size n = 49.
(b) Use samples of size n = 196.
(c) Use samples of size n = 1936.
(d) Does increasing the sample size affect the significance level?
28. Use simulation to compute the empirical power for testing H0 : μ = 100 versus H1 : μ >
100 when μ = 108 and sampling from a N (100, 28) distribution. Use 10,000 samples with
n = 49 in the simulation and [Link](14) so that the results will be reproducible.
(a) Use a significance level of α = 0.05.
(b) Use a significance level of α = 0.20.
(c) Compute the theoretical power for scenarios (a) and (b). How do these values
compare to those from the simulations.
(d) What happens to the empirical power as α increases?
29. Test the null hypothesis that the mean life for a certain brand of 19 mm tubular tires
is 1000 miles against the alternative hypothesis that it is less than 1000 miles. Assume
that tubular tire life follows a normal distribution with σ = 100 miles.
(a) Find the probability of a type I error for n = 16 if the null hypothesis is rejected
when the sample mean is less than or equal to 960 miles.
(b) Plot the power function for n = 16 for values of μ between 900 and 1000 miles.
30. Given a normal population with unknown mean and a known variance of σ 2 = 4, test
the hypothesis H0 : μ = 10 versus H1 : μ < 10 at the α = 0.05 significance level.
Hypothesis Testing 401
(a) Use the command [Link](28) to generate 10,000 samples of size n = 16 from a
N (10, 2) population. Is the empirical significance level close to 5%?
(b) Compute a 95% confidence interval for α when simulating 10,000 samples of size
n = 16 from a N (10, 2) population.
(c) What is the theoretical power if μ1 = 9.5 for the given hypothesis test?
(d) Graphically represent the power for testing H0 : μ = 10 versus H1 : μ < 10 for
samples of size n = 16 from a N (10, 2) population when α = 0.05 for values of μ
from 8 to 10.
(e) Graphically represent the power for testing H0 : μ = 10 versus H1 : μ < 10 for
samples of size n = 16 when μ1 = 9.5 for values of α ranging from 0.01 to 0.3.
31. Generate 10,000 samples of size nx = 20 from X ∼ N (8, 2) and 10,000 samples of size
ny = 20 from Y ∼ N (6, 2). Use [Link](59) so that the answers are reproducible.
Assuming X and Y are independent and a 5% significance level,
(a) What type of distribution does the statistic s2x /s2y follow?
(b) Create a density histogram of the 10,000 values of s2x /s2y . Superimpose the theoretical
sampling distribution of s2x /s2y on the density histogram.
(c) Compute the empirical significance level for testing H0 : σx2 /σy2 = 1 versus H1 :
σx2 /σy2 = 1.
(d) Plot the power function for testing H0 : σx2 /σy2 = 1 versus H1 : σx2 /σy2 = 1 for ratios
of σx2 /σy2 from 0.25 to 4.
(e) Repeat (d) for nx = ny = 100.
(f) Repeat (d) for nx = ny = 200.
(g) Put the graphs from (d), (e), and (f) on the same graph.
(h) Plot the power function for testing H0 : σx2 /σy2 = 1 versus H1 : σx2 /σy2 = 1 for α
values between 0.01 and 0.5 if σx2 /σy2 = 2 and
(1) nx = ny = 20,
(2) nx = ny = 100, and
(3) nx = ny = 200.
(i) Simulate the power for testing H0 : σx2 /σy2 = 1 versus H1 : σx2 /σy2 = 1 at the α = 0.5
level when σx2 /σy2 = 2 and
(1) nx = ny = 20,
(2) nx = ny = 100, and
(3) nx = ny = 200.
(4) Compute the theoretical power for the three previous scenarios.
Chapter 10
Nonparametric Methods
10.1 Introduction
The statistical inference techniques presented in Chapter 8 and Chapter 9 are based on
complete satisfaction of all of the assumptions made in the derivations of their sampling
distributions. Indeed, many of the techniques in Chapters 8 and 9 are commonly referred to
as parametric techniques since not only was the form of the underlying population (generally
normal) stated, but so was one or more of the underlying distribution’s parameters. This
chapter introduces both distribution free tests as well as nonparametric tests. The collection
of inferential techniques known as distribution free are based on functions of the sample
observations whose corresponding random variable has a distribution that is independent
of the specific distribution function from which the sample was drawn. Consequently,
assumptions with respect to the underlying population are not required. Nonparametric
tests involve tests of a hypothesis where there is no statement about the distribution’s
parameters. However, it is common practice to refer collectively to both distribution free
tests and nonparametric tests simply as nonparametric methods.
When there are analogous parametric and nonparametric tests, comparisons between
tests can be made based on power. The power efficiency of a test A relative to a test
B is the ratio of nb /na , where na is the number of observations required by test A for
the power of test A to equal the power of test B when nb observations are used. Since the
power value is conditional on the type of alternative hypothesis and on the significance level,
power efficiency can be difficult to interpret. One way to avoid this problem is to use the
asymptotic relative efficiency (ARE ) (a limiting power efficiency) of consistent tests.
A test is consistent for a specified alternative if the power of the test when that alternative
is true approaches 1 as the sample size approaches infinity (Gibbons, 1997). Provided that
A and B are consistent tests of a null hypothesis H0 and alternative hypothesis H1 at
significance level α, the asymptotic relative efficiency of test A to test B is the limiting
ratio nb /na , where na is the number of observations required by test A for the power of
test A to equal the power of test B based on nb observations while simultaneously nb → ∞
and H1 → H0 . Although the ARE considers infinite (hence not obtainable) sample sizes,
the ARE provides a good approximation to the relative efficiency for many situations of
practical interest. When a nonparametric technique has a parametric analog, the ARE will
be used to compare the two techniques.
403
404 Probability and Statistics with R
for testing hypotheses concerning the population median is introduced. This procedure is
known as the sign test. A corresponding confidence interval formula for the median will
also be derived.
To use the sign test, assume X1 , X2 , . . . , Xn is a random sample of n observations drawn
from a continuous population with unknown median ψ. The sign test statistic, S, is defined
as the number of positive differences among the X1 − ψ0 , X2 − ψ0 , . . . , Xn − ψ0 , where ψ0
is the median from the null hypothesis H0 : ψ = ψ0 . The distribution of S when H0 is true
is S ∼ Bin(n, π = 0.5).
The sign test may also be used for testing whether the median difference (ψD ) between
two dependent populations (X and Y ) is equal to some value, H0 : ψD = ψ0 . It is important
to point out that ψD is not usually equal to ψX −ψY . The only instance where ψD is equal to
ψX − ψY is when X and Y are symmetric populations. For dependent samples, S is defined
as the number of positive differences among the X1 −Y1 −ψ0 , X2 −Y2 −ψ0 , . . . , Xn −Yn −ψ0 .
Since the assumption of a continuous population is a requirement for using the sign test,
theoretically, there should not be any values that are exactly equal to the parameter being
tested in the sample. However, due to rounding or crude measurements, it is not uncommon
to observe sample values equal to ψ0 , the value of ψ or ψD under the null hypothesis. There
are several strategies one can pursue in dealing with values that are equal to the parameter
being tested. Some of these include randomization, midranks, average statistic, average
probability, least favorable statistic, range of probability, and omission of tied observations.
The book by Pratt and Gibbons (1981) gives a more complete discussion of these various
techniques. The final approach, omission of tied observations, consists of eliminating
the value(s) in the sample that are equal to the parameter being tested. This is the approach
that will be used in this text. This method leads to some loss of information; however, if
the number of observations to be omitted is small compared to the sample size, this loss is
usually acceptable. Generally, omission of tied observations decreases the power of the test.
Due to the discrete nature of S, it is generally not possible to define a rejection region
that results in a test whose size is exactly equal to a prescribed α. Consequently, the
approach presented for this test relies on ℘-values rather than on defining rejection regions
for the statistical conclusion. The three possible alternative hypotheses and their associated
℘-value calculation formulas are presented in Table 10.1 on the facing page. If one assumes
a normal population, the asymptotic relative efficiency (ARE ) of the sign test relative to
the t-test is π2 ≈ 0.637. The ARE of the sign test relative to the t-test is also quite poor
(1/3) for the uniform distribution (short tails). However, for the Laplace distribution (long
tails), the ARE of the sign test in relation to the t-test is 2.
Clearly, k must be a positive integer since it is the subscript of an order statistic. Using
(10.1) will seldom produce typical confidence levels such as 90%, 95%, or 99% exactly. Many
texts provide charts to find k for the construction of confidence intervals at these typical
confidence levels that are based either on always attaining a level of at least (1 − α) × 100%
confidence or by providing the value of k such that the achieved confidence level is as close
to (1 − α) × 100% as possible. The first approach will always return confidence intervals
with a confidence level of (1 − α) × 100% or more. Roughly 50% of the confidence intervals
computed with the second approach will return confidence intervals of less than the reported
confidence.
The function [Link]() provided in the PASWR package returns two confidence in-
tervals with exact confidence levels closest to the (1 − α) × 100% level specified by the
user. One of these intervals has a confidence level lower than the specified level and the
other has a higher confidence level than the specified level. Finally, the function uses linear
interpolation between these first two intervals to give a confidence interval with the level
specified by the user.
S − n(0.5) q
Z= ∼ N (0, 1), (10.2)
n(0.5)2
where S is defined as the number of positive differences among the X1 −ψ0 , X2 −ψ0 , . . . , Xn −
ψ0 . See Figure 10.1 on the next page for a graph of a Bin(20, 0.5) superimposed with a
normal distribution with μS = 20(0.5) = 10 and σS = 20(0.5)2 = 3.16.
The formula for calculating the observed value of the standardized test statistic as well
as the three possible alternative hypotheses and their rejection regions are described in
Table 10.2 on the following page.
406 Probability and Statistics with R
0.20
0.15
P(S = s)
0.10
0.05
0.00
0 5 10 15 20
Table 10.2: Summary for testing the median — approximation to the sign test
Null Hypothesis — H0 : ψ = ψ0
Standardized Test √
s±0.5−n(0.5)
— zobs =
Statistic’s Value n(0.5)2
A corresponding confidence interval for the median based on (10.2) is formed with the
k th and (n − k + 1)st order statistics of the sample, where
√
n + 1 + zα/2 × n
k= . (10.3)
2
For a one-sided confidence interval, replace zα/2 with zα and solve for k. Since k is generally
not an integer, it can be either rounded or truncated. To obtain a conservative estimate,
one should truncate.
Nonparametric Methods 407
Example 10.1 Sign Test: Telephone Call Times Table 10.3 and the variable
[Link] in the data frame Phone contain the times in minutes of long distance telephone
calls during a one month period for a small business.
(a) Use an exact test with α = 0.05 to see if 2.1 minutes is a representative measure of
center for the telephone call lengths.
(b) Construct a 95% confidence interval for the population median.
(c) Use a normal approximation for the test used in part (a) to test if 2.1 minutes is a
representative measure of center for the telephone call lengths.
(d) Construct a 95% confidence interval for the population median using (10.3).
Solution: First, use the function EDA() to assess the general shape of telephone call times.
The four graphs in Figure 10.2 on the following page all lead one to the conclusion that the
distribution of the long distance telephone call times is positively skewed (skewed right).
Consequently, the median is a more representative measure of center than is the mean for
these data.
(a) Use the five-step procedure to test if 2.1 minutes is a representative measure of center.
Step 1: Hypotheses — The null and alternative hypotheses to test whether or not 2.1
minutes is a representative measure of the center of telephone call times are
H0 : ψ = 2.1 versus H1 : ψ = 2.1.
Step 2: Test Statistic — The test statistic chosen is S, where S is the number of positive
differences among X1 − 2.1, X2 − 2.1, . . . , Xn − 2.1. Here, s = 11. Also note that
since there are two instances where xi = ψ0 , n is reduced from 23 to 21.
FIGURE 10.2: Graphical representation of the data in [Link] with the function EDA()
Fail to reject H0 .
Step 5: English Conclusion — There is not sufficient evidence to suggest the median
length of long distance telephone calls is not 2.1 minutes.
(b) To construct a 95% confidence interval for the population median, start by finding the
largest and smallest values of k such that 1−2×P(S < k) > 0.95 and 1−2×P(S < k) < 0.95,
respectively. To find these values, use S, and type 1 - 2 * pbinom(0:23,23,0.5):
> round(1-2*pbinom(5:8,23,0.5),3)
[1] 0.989 0.965 0.907 0.790
and
1 − 2 × P(S ≤ 7) = 1 − 2 × P(S < 8) = 0.907.
So, use the k = 7 with the n − k + 1 = 23 − 7 + 1 = 17th order statistics to form the 96.5%
th
confidence interval, CI 0.965 (ψ) = [0.7, 5.6], and the k = 8th with the n − k + 1 = 23 − 8 + 1 =
16th order statistics to form the 90.7% confidence interval, CI 0.907 (ψ) = [0.8, 5.2]. Thus,
the 95% interpolated confidence interval, [L, U ] is calculated such that
Using the function [Link]() on the variable ([Link]) gives the following output:
> attach(Phone)
> [Link]([Link], md=2.1)
One-sample Sign-Test
data: [Link]
s = 11, p-value = 1
alternative hypothesis: true median is not equal to 2.1
95 percent confidence interval:
0.7261939 5.4952244
sample estimates:
median of x
2.1
(c) Use the five-step procedure using the normal approximation to the sign test to test if
2.1 minutes is a representative measure of center.
Step 1: Hypotheses — The null and alternative hypotheses to test whether or not 2.1
minutes is a representative measure of the center of telephone call times are
Step 2: Test Statistic — The test statistic chosen is S, where S is the number of positive
differences among X1 − 2.1, X2 − 2.1, . . . , Xn − 2.1. Here, s = 11. Also note that
since there are two instances where xi = ψ0 , n is reduced from 23 to 21.
I. From the rejection region, do not reject H0 because |zobs | = 0 is not larger
than 1.96.
II. From the ℘-value, do not reject H0 because the ℘-value = 1 is larger than
0.05.
Fail to reject H0 .
Step 5: English Conclusion — There is not sufficient evidence to suggest the median
length of long distance telephone calls is not 2.1 minutes.
410 Probability and Statistics with R
(d) To construct a confidence interval for the population median using (10.3), solve
√
n + 1 + zα/2 × n
k=
2 √
23 + 1 − 1.96 × 23
= = 7.3.
2
By truncating k, k = 7. The approximate 95% confidence interval for ψ is then
[x(k) , x(n−k+1) ] = [x(7) , x(23−7+1) ] = [0.7, 5.6].
In summary, for large n, when testing location for symmetric populations, it is generally
better to use the sign test with Laplace populations and the Wilcoxon signed-rank test
for all other non-normal symmetric distributions. It can be shown that the ARE of the
Wilcoxon signed-rank test relative to the t-test is never less than 0.864 for any continuous
distribution and is ∞ for some distributions. For small n, it is not so clear which test will
be better.
Given a random sample X1 , X2 , . . . , Xn taken from a continuous population that is
symmetric with respect to its median ψ, under the null hypothesis H0 : ψ = ψ0 , the
differences, Di = Xi − ψ0 , are symmetrically distributed about zero. Further, positive and
negative differences of the same magnitude have the same probability of occurring. As with
the sign test, if any of the di s are zero, they are removed from the sample before the ranks
are computed, and the value of n is reduced accordingly.
To compute the Wilcoxon signed-rank statistic,
Step B: Assign the ranks to the n values from step A. If there are ties, use the midranks.
The midrank is defined as the average rank of the tied observations.
Step D: Sum the positive quantities in step C. The result is denoted t+ . The random
variable (test statistic) T + is defined as the sum of the positive signed ranks and
the random variable T − is defined as the sum of negative signed ranks.
Provided the null hypothesis is true, E(T + ) = E(T − ). When T + is either sufficiently
small or sufficiently large, the null hypothesis is rejected. The test statistic T + takes
on values between 0 and n(n + 1)/2, and has a mean and variance of n(n + 1)/4 and
n(n + 1)(2n + 1)/24, respectively. The distribution of T + is known as the Wilcoxon
signed-rank distribution. Although conceptually easy to understand, one needs access
to extensive tables or statistical software to compute exact ℘-values. Further, tabled values
for T + are generally published only when there are no ties in the absolute values of the di s,
|di | for i = 1, . . . , n. When there are ties in the |di |s, the S function [Link]() uses
a normal approximation to compute the ℘-values. It is possible to calculate exact ℘-values
when testing hypotheses about the median as well as to construct exact confidence intervals
for the median even in the presence of ties using the function [Link]() from the
PASWR package. The function is rather primitive and should only be used for problems with
fewer than 19 observations as the memory requirements are rather large.
Solution: Since there are three values (n = 3) that must be ranked and each di may have
either a positive or negative sign, there are a total of 2n = 23 = 8 possible sets of signs
associated with the three possible ranks (1, 2, 3). Under the null hypothesis, each of the
sets of signs is equally likely to occur, and thus each has a probability of 1/8 of occurring.
Table 10.5 on the following page lists the eight possible sets of signs and Table 10.6 on the
next page provides the probability distribution (pdf) for T + .
412 Probability and Statistics with R
Table 10.5: Possible sign and rank combinations for Example 10.2 on the preceding page
−1 +1 −1 +1 −1 +1 −1 +1
−2 −2 +2 +2 −2 −2 +2 +2
−3 −3 −3 −3 +3 +3 +3 +3
+
t 0 1 2 3 3 4 5 6
R can compute quantiles (qsignrank()), the density function (dsignrank()), the dis-
tribution function (psignrank()), and random numbers (rsignrank()) from the Wilcoxon
signed-rank distribution. For example, the probabilities in Table 10.6 can be generated with
dsignrank(0:6,3). To obtain further help, type ?dsignrank at the R prompt. S-PLUS
has the function psignrank(), but it was not documented at the time of writing. Also,
S-PLUS did not have the functions dsignrank(), qsignrank(), nor rsignrank().
Due to the discrete nature of T + , it is generally not possible to define a rejection region
that results in a test whose size is exactly equal to the prescribed α. Consequently, the
approach presented for this test relies on ℘-values rather than on defining rejection regions
for the statistical conclusion. The three possible alternative hypotheses and their associated
℘-value calculation formulas are presented in Table 10.7 on page 415. The ℘-value formulas
given in Table 10.7 can be used to calculate exact ℘-values with S’s psignrank() function
when there are no ties among the non-zero |di |s. In the presence of ties, the S function
[Link]() uses Table 10.9 on page 419 with a correction factor. The formulas in
Table 10.7 on page 415 are still valid when there are ties in the non-zero |di |s; however,
the exact conditional distribution of T + when ties are present is not a base function of S.
Example 10.3 shows how S can be used to compute the exact ℘-value for the conditional
distribution of T + (the distribution of T + with ties in the non-zero |di |s).
Solution: If the data are symmetric, a Wilcoxon signed-rank test may be appropriate.
A visual inspection of the pH measurements reveals they are symmetric around 7.3. The
creation of a density plot to verify this assumption is left to the reader. The steps for
Nonparametric Methods 413
carrying out the Wilcoxon signed-rank test are to, first, create the di values that equal
xi − ψ0 for i = 1, . . . , n. Next, take the absolute value of the n di s and assign the ranks to
the n values. If there are ties, use the midranks. Then, multiply the values of the ranks
of the |di |s by the sign of the original di s. Finally, sum the resulting positive quantities to
obtain t+ .
After the matrix listing the locations of the positive ranks with 1s and the locations of
negative ranks with 0s is created (signs), matrix multiplication is used to sum the positive
ranks to get the distribution of T + , where mat contains the ranks of the absolute values of
the di s:
414 Probability and Statistics with R
Since H1 is an upper one-sided hypothesis, the ℘-value is the sum of the values of the
distribution of T + that are greater than or equal to the value of our test statistic t+ . In
this case, the t+ was 8, so the ℘-value is
This ℘-value can also be found using the function [Link]() from the PASWR package.
Note that the function [Link]() cannot be used because it cannot compute exact
℘-values when there are ties in the data.
data: PH
t+ = 8, p-value = 0.25
alternative hypothesis: true median is greater than 7.25
93.75 percent confidence interval:
7.25 Inf
sample estimates:
(pseudo)median
7.3
The Wilcoxon signed-rank test may also be used for testing whether the median differ-
ence (ψ0 ) between two dependent populations (X and Y ) is equal to some value, H0 : ψD =
ψ0 . For dependent samples, Di = Xi − Yi − ψ0 instead of Di = Xi − ψ0 . The computation
of T + for dependent samples follows the same steps as those for a single sample.
Table 10.7: Summary for testing the median — Wilcoxon signed-rank test
Null Hypothesis — H0 : ψ = ψ0
H1 : ψ = ψ0 2 × min {P (T + ≤ t+ ) , 1 − P (T + ≤ t+ − 1) , 0.5}
subscript of an order statistic. The exact confidence level is 1 − 2P(T + < k) for a two-sided
confidence interval and 1 − P(T + < k) for a one-sided confidence interval.
When there are no di s (xi − ψ0 ) that equal zero, as well as no xi s that equal zero, testing
H0 : ψ = ψ0 with the procedures described in Section 10.3 yields an equivalent acceptance
region to that produced by the confidence interval based on the Walsh averages. If this is
not the case, the regions are no longer equivalent.
For the dependent case, use the n(n + 1)/2 dependent Walsh averages x − yij = (xi −
yi ) + (xj − yj ) /2. In this case, di = xi − yi − ψ0 . Here the equivalence between the
acceptance region of the hypothesis test and the confidence interval created based on the
Walsh averages exists only when di = 0 and xi − yi = 0, i = 1, . . . , n.
(a) Test to see if his median waiting time is less than 6 minutes.
Solution: Before using the Wilcoxon signed-rank test, a quick check on the assumption
of symmetry is made with a boxplot in Figure 10.3 on the next page. Since the boxplot
does appear symmetric, it is legitimate to proceed with a Wilcoxon signed-rank test.
(a) Use the five-step procedure to test if the median waiting time is less than 6 minutes.
Step 1: Hypotheses — The null and alternative hypotheses to test if the median waiting
time is less than 6 minutes are
H0 : ψ = 6 versus H1 : ψ < 6.
Step 2: Test Statistic — The test statistic chosen is T + , where T + is the Wilcoxon
signed-rank statistic. Here, the observed value of T + is t+ = 28.
2 4 6 8
Step 4: Statistical Conclusion — The ℘-value is P(T + ≤ 28) = 0.03649902, which can
be obtained by typing psignrank(28,15).
Reject H0 .
Step 5: English Conclusion — There is sufficient evidence to suggest the median waiting
time is less than 6 minutes.
Nonparametric Methods 417
> attach(Wait)
> [Link](wt, mu=6, alternative="less")
data: wt
V = 28, p-value = 0.0365
alternative hypothesis: true location is less than 6
(b) To compute an upper 95% confidence interval for ψ, first determine the n(n + 1)/2 =
15(15 + 1)/2 = 120 Walsh averages. To ease the drudgery of 120 calculations of means, use
S:
Next, find the largest value k such that P(T + < k) ≤ 0.05. This can be accomplished in
two ways:
> qsignrank(0.05,15)
[1] 31
(2) Visually inspect psignrank(0:n*(n+1)/2, n) for the largest value k such that P(T + <
k) ≤ α. Note that the first pair (k − 1, P(T + < k)) of the output shown is (28, 0.036),
and the pair that gives the answer is (30, 0.047), which implies k − 1 = 30 or k = 31.
Either (1) or (2) can be used with R while (2) must be used with S-PLUS. Note that if
method (1) is used, that the exact confidence level will be 1 - psignrank(30,15) for an
upper one-sided confidence interval. The 95.27% confidence interval where k = 31 is then
& (
−∞, x̄( n(n+1) −k+1) = −∞, x̄(90) = (−∞, 5.8].
2
This may be done directly with the argument [Link]=TRUE in the [Link]() func-
tion if one is using R:
418 Probability and Statistics with R
data: wt
V = 28, p-value = 0.0365
alternative hypothesis: true location is less than 6
95 percent confidence interval:
-Inf 5.8
sample estimates:
(pseudo)median
4.625
The answer can also be computed with the function [Link] from the PASWR package:
> [Link](wt, mu=6, alternative="less")
data: wt
t+ = 28, p-value = 0.0365
alternative hypothesis: true median is less than 6
95.26978 percent confidence interval:
-Inf 5.8
sample estimates:
(pseudo)median
4.625
See Figure 10.4 on the facing page for a graph of the Wilcoxon signed-rank distribution
for
n = 15 superimposed by a normal distribution with μ = n(n + 1)/4 = 60 and σ =
n(n + 1)(2n + 1)/24 = 17.61.
The formula for calculating the observed value of the standardized test statistic as well
as the three possible alternative hypotheses and their rejection regions are described in
Table 10.9 on the next page. If there are ties in the |di |s, the variance of T + is reduced to
g
n(n + 1)(2n + 1) j=1 tj (tj − 1)(tj + 1)
− (10.5)
24 48
where g denotes the number of tied groups of non-zero |di |s and tj is the size of tied group
j. In (10.5), an untied observation is considered to be a tied group of size one. In the event
that no ties exist, g = n and tj = 1 for j = 1, . . . , n, which produces a correction factor of
zero.
Nonparametric Methods 419
0 20 40 60 80 100 120
+
t
Table 10.9: Summary for testing the median — normal approximation to the Wilcoxon
signed-rank test
Null Hypothesis — H0 : ψ = ψ0
Standardized Test
— zobs = √ t ±0.5−n(n+1)/4
+
g
Correction Factor — CF = tj (tj − 1)(tj + 1)/48
j=1
A corresponding two-sided confidence interval for the median based on (10.4) are the
k th and (n(n + 1)/2 − k + 1)st ordered Walsh averages, where
.
n(n + 1) n(n + 1)(2n + 1)
k = 0.5 + + zα/2 . (10.6)
4 24
For a one-sided confidence interval, replace zα/2 with zα . Since k is generally not an
integer, it can be either rounded or truncated. To obtain a conservative estimate, one
should truncate.
420 Probability and Statistics with R
The data that were collected are presented in Table 10.10 on the facing page and stored
in data frame Aggression, where xi represents aggression test scores for the children who
watched violent programming (violence) and yi represents aggression test scores for the
children who watched non-violent television programs (noviolence).
(b) Test whether the median difference for aggression test scores for pairs of children is
greater than zero using a significance level of α = 0.05 with the normal approximation
to the Wilcoxon signed-rank test.
(c) Use the function [Link]() to report the exact ℘-value and the lower one-sided
confidence interval for the hypothesis in (b).
(d) Construct a lower one-sided confidence interval with confidence level of at least 95%
using the normal approximation to find k.
(a) Before using the Wilcoxon signed-rank test, a quick check on the assumption of sym-
metry is made with a boxplot in Figure 10.5. Since the boxplot does appear symmetric, it
is legitimate to proceed with a Wilcoxon signed-rank test.
0 5 10 15
(b) Use the five-step procedure to test if the median difference for aggression scores for pairs
of children is greater than zero.
Nonparametric Methods 421
Step 1: Hypotheses — The null and alternative hypotheses to test if the median difference
for aggression scores for pairs of children is greater than zero are
H0 : ψD = 0 versus H1 : ψD > 0.
Step 2: Test Statistic — The test statistic chosen is T + , where T + is the Wilcoxon
signed-rank statistic. Here, the observed value of T + is t+ = 118.5.
3
CF = tj (tj − 1)(tj + 1)/48
j=1
I. From the rejection region, reject H0 because zobs = 2.591 is more than 1.645.
II. From the ℘-value, reject H0 because the ℘-value = 0.0048 is less than 0.05.
Reject H0 .
Step 5: English Conclusion — There is sufficient evidence to suggest that children who
view violent television programs have higher aggression test scores than children
who view non-violent television programs.
S commands to compute the test follow. Note that S automatically uses a normal approx-
imation to the distribution of T + when there are ties in the |di |s as well as a correction
factor for the variance of T + .
> attach(Aggression)
> [Link](violence, noviolence, paired=TRUE, alternative="greater")
Warning message:
In [Link](violence, noviolence, paired = TRUE,
alternative = "greater") : cannot compute exact p-value with ties
(c) From the output of [Link](), the ℘-value is 0.003265 and the lower 95.21%
confidence interval is [2, ∞):
(d) The paired differences are stored in PD and the sorted Walsh averages are in SWA. Using
(10.6), k is calculated to be 36, and the k th Walsh average is determined to be 2. Therefore,
the 95% confidence interval for ψD is [2, ∞).
Another way to achieve the same result is with the function outer(), which applies the
third argument ("+") to the first two vectors in an element-wise manner to create an array,
and ![Link], which returns the values of the upper triangular matrix containing double
the Walsh averages. Finally, the upper triangular matrix is divided by two, and then sorted
to calculate the values for the sorted Walsh averages.
Table 10.11: Summary for testing equality of medians — Wilcoxon rank-sum test
Null Hypothesis — H0 : ψX − ψY = δ0
H1 : ψX − ψY = δ0 2 × min {P (W ≤ w) , 1 − P (W ≤ w − 1) , 0.5}
Null Hypothesis — H0 : ψX − ψY = δ0
H1 : ψX − ψY = δ0 2 × min {P (U ≤ u) , 1 − P (U ≤ u − 1) , 0.5}
Solution: Start by reading the values of x and y into vectors labeled x and y, respectively:
Note that the values of W are between n(n + 1)/2 = 2(2 + 1)/2 = 3 and n(2N − n + 1)/2 =
2[(2)(5) − 2 + 1]/2 = 9.
426 Probability and Statistics with R
> table(W)/choose(8,4)
W
12 15 18 21 24
0.08571429 0.22857143 0.37142857 0.22857143 0.08571429
Since H1 is a two-sided hypothesis, the ℘-value is
2 × min{P(W ≤ w), 1 − P(W ≤ w − 1), 0.5}.
In this case, w was 12, so P(W ≤ 12) = 0.0857 and 1 − P(W ≤ 11) = 1. It follows that the
℘-value is 2 × 0.0857 = 0.1714.
> [Link] <- 2*(sum(W <= w)/choose(N, n))
> [Link]
[1] 0.1714286
Nonparametric Methods 427
data: x and y
w = 12, p-value = 0.1714
alternative hypothesis: true median is not equal to 0
82.85714 percent confidence interval:
-0.2 0.0
sample estimates:
difference in location
-0.1
B B
Diets
Diets
A A
2 4 6 8 2 4 6 8
FIGURE 10.6: Side-by-side boxplots as well as comparative dotplots for pig weights for
diets A and B
To find the largest k such that P(U < k) ≤ α/2, use the command pwilcox(). Since R and
S-PLUS use different definitions for pwilcox, pay close attention to the code that follows and
recall the relationship U = W − n(n + 1)/2.
By visual inspection, one realizes the largest value k such that P(U < k) ≤ 0.05 is k =
2. That is, the pair (2, 0.05714286) implies a confidence level of 1 − (2)(0.02857143) =
0.9428571. As an alternative to visual inspection, the appropriate value of k can be found
as
> pwil <- pwilcox(1:(n*m), n, m) # For R
> which(pwil >= 0.05)[1]
[1] 2
Next, the nm differences are generated using the S command outer() and the k th and
(nm−k+1)st order statistics from the nm differences are identified. Consequently, a 94.28%
confidence interval for the difference in medians is CI 0.9428 (ψA − ψB ) = [−7.1, −1.4].
> k <- 2
> diffs <- matrix(sort(outer(A, B,"-")), byrow=FALSE, nrow=4)
> diffs
[,1] [,2] [,3] [,4]
[1,] -7.4 -4.6 -3.8 -2.2
[2,] -7.1 -4.5 -3.4 -1.8
[3,] -6.3 -4.3 -3.3 -1.4
[4,] -4.9 -4.2 -3.0 -0.2
Nonparametric Methods 429
data: A and B
W = 0, p-value = 0.02857
alternative hypothesis: true location shift is not equal to 0
90 percent confidence interval:
-7.1 -1.4
sample estimates:
difference in location
-4
Recall that the achieved confidence level is actually 94.28%. The achieved confidence level
is reflected in the output for the function [Link](). Also note that the statistic w
in [Link]() is the observed Wilcoxon rank-sum statistic not the Mann-Whitney U
statistic reported by R’s [Link](). The S-PLUS function [Link]() displays a
U statistic that is the Wilcoxon rank-sum statistic.
> [Link](A, B)
data: A and B
w = 10, p-value = 0.02857
alternative hypothesis: true median is not equal to 0
94.28571 percent confidence interval:
-7.1 -1.4
sample estimates:
difference in location
-4
See Figure 10.7 for a graph of the Wilcoxon rank-sum distribution for
n = m = 10 super-
imposed by a normal distribution with μ = n(N + 1)/2 = 105 and σ = nm(N + 1)/12 =
13.22876.
0.030
P(W = w)
0.020
0.010
0.000
The formula for calculating the observed value of the standardized test statistic as well
as the three possible alternative hypotheses and their rejection regions are described in
Table 10.13 on the next page. If there are tied ranks, the variance of W is reduced to
g
nm(N + 1) nm
− tj (tj − 1)(tj + 1) (10.8)
12 12N (N − 1) j=1
where g denotes the number of tied groups and tj is the size of tied group j. In (10.8), an
untied observation is considered to be a tied group of size one. In the event that no ties
exist, g = N and tj = 1 for j = 1, . . . , N , which produces a correction factor of zero.
The sampling distribution of U can likewise be reasonably approximated
with a normal
distribution that has a mean of nm/2 and a standard deviation of nm(N + 1)/12. The
standardized test statistic under the assumption that H0 : ψX − ψY = δ0 is true is
U− nm
− δ0 q
Z= 2 2
∼ N (0, 1). (10.9)
nm(N +1)
12
The formula for calculating the observed value of the standardized test statistic as well
as the three possible alternative hypotheses and their rejection regions are described in
Table 10.14 on the facing page.
A corresponding two-sided confidence interval for the shift in distribution based on (10.9)
are the k th and (nm − k + 1)st ordered differences, where
.
nm nm(N + 1)
k = 0.5 + + zα/2 . (10.10)
2 12
For a one-sided confidence interval, replace zα/2 with zα . Since k is generally not an
integer, it can be either rounded or truncated. To obtain a conservative estimate, one
should truncate.
Nonparametric Methods 431
Table 10.13: Summary for testing the difference in two medians — normal approximation
to the Wilcoxon rank-sum test
Null Hypothesis — H0 : ψX − ψY = δ0
Standardized Test √
— zobs = w±0.5−n(N +1)/2−δ0
Statistic’s Value nm(N +1)/12−CF
g
Correction Factor — CF = nm
12N (N −1) tj (tj − 1)(tj + 1)
j=1
Table 10.14: Summary for testing the difference in two medians — normal approximation
to the Mann-Whitney U -Test
Null Hypothesis — H0 : ψX − ψY = δ0
Standardized Test
— zobs = √u±0.5−nm/2−δ0
Statistic’s Value nm(N +1)/12−CF
g
Correction Factor — CF = nm
12N (N −1) tj (tj − 1)(tj + 1)
j=1
(a) Verify that the time improvement distributions are similar in shape.
(b) Test whether the median difference for improvements in swim times is different from zero
using a significance level of α = 0.10 with the normal approximation to the Wilcoxon
rank-sum test and the normal approximation to the Mann-Whitney U -test.
(c) Use the function wilcox test() from the coin package, which can be downloaded
from your nearest CRAN mirror at [Link] to report
the exact ℘-value and the 90% confidence interval for the hypothesis in (b). According
to the documentation, this function computes exact conditional (on the data) ℘-values
and quantiles using the shift-algorithm by Streitberg and Röhmel for both tied and
untied samples.
(d) Construct a confidence interval with confidence level of at least 90% using the normal
approximation to find k.
(a) To use the Wilcoxon rank-sum test, the time improvement distributions must be similar
in shape. A comparative boxplot of time improvements for low-fat and high fat diets is found
in Figure 10.8 on the facing page. Since the comparative boxplot does appear to show the
same underlying distribution for time improvements for swimmers eating both diets, it is
legitimate to proceed with a Wilcoxon rank-sum test or the Mann-Whitney U -test.
(b) Use the five-step procedure to test if the median difference for improvements in swim
times for high and low-fat diets is different from zero.
Step 1: Hypotheses — The null and alternative hypotheses to test if the median difference
for improvements in swim times for high and low-fat diets is different from zero are
H0 : ψX − ψY = 0 versus H1 : ψX − ψY = 0.
Table 10.15: Sorted improvements in swim times in seconds for high (x) and low (y) fat
diets, where rank refers to the rank of the data point in the combined sample of x and y
data points (Swimtimes)
yi rank(yi ) xi rank(xi )
Tied Rank 0.18 8.5 0.18 8.5
−0.79 2.0 0.38 10.0
−0.49 3.0 0.56 11.0
−0.37 4.0 0.65 12.0
−0.20 5.0 0.84 13.0
−0.15 6.0 1.58 20.0
0.02 7.0 0.89 16.0
−0.87 1.0 1.18 18.0
Tied Rank 0.87 14.5 0.87 14.5
0.98 17.0 2.03 22.0
1.42 19.0 3.53 27.0
1.71 21.0 4.33 28.0
3.52 26.0
Tied Ranks 2.66 24.0 2.66 24.0
2.66 24.0
w = 248
Time Improvement
4
3
Seconds
2
1
0
−1
Lowfat Highfat
Diets
FIGURE 10.8: Comparative boxplot for improvements in swim times for high and low-fat
diets
434 Probability and Statistics with R
Step 3: Rejection Region Calculations —Because both standardized test statistics are
distributed approximately N (0, 1) and H1 is a two-sided hypothesis, the rejection
region is |zobs | > z1−0.10/2 = 1.645.
Because there are three groups of ties (g = 3), where the sizes of the tied groups
are 2, 2, and 3, the correction factor is
3
nm
CF = tj (tj − 1)(tj + 1)
12N (N − 1) j=1
(14)(14)
= × 2(2 − 1)(2 + 1)
12(28)(28 − 1)
+ 2(2 − 1)(2 + 1) + 3(3 − 1)(3 + 1)
7
= [6 + 6 + 24]
324
= 7/9
I. From the rejection region, reject H0 because zobs = 2.046353 is more than
1.645.
II. From the ℘-value, reject H0 because the ℘-value = 0.04072 is less than 0.10.
Reject H0 .
Step 5: English Conclusion — There is sufficient evidence to suggest that the median
time improvements are different for swimmers eating high fat and low-fat diets.
Output for R:
> attach(Swimtimes)
> [Link](highfat, lowfat)
Warning message:
In [Link](highfat, lowfat) :
cannot compute exact p-value with ties
Output for S-PLUS:
(c) From the output of wilcox test(), the ℘-value is 0.03818 and the 90% confidence
interval for the difference in medians is CI 0.90 (ψX − ψY ) = [0.31, 1.68].
Note that the reported standardized test statistic computed with wilcox test() does not
use a continuity correction.
(d) The xi − yj differences are stored in diffs. Using (10.10) on page 430, k is calculated
to be 62. The k th difference is determined to be 0.31 and the nm − k + 1st difference is 1.68.
Therefore, the 90% confidence interval, CI 0.90 (ψX − ψY ), is [0.31, 1.68].
436 Probability and Statistics with R
H H
H = = r (10.14)
fc j=1 t3j − tj
1−
−N
N3
where tj is the number of times a given rank was tied in the combined sample of size N and
r is the number of ranks in the combined sample of size N that were tied. Provided each
Nonparametric Methods 437
ni ≥ 5, the sampling distributions of Hobs and H are both approximately chi-square random
variables with a − 1 degrees of freedom (χ2a−1 ). More specifically, when H0 is true, the
statistic H has, as min(n1 , . . . , na ) tends to infinity, an asymptotic χ2a−1 distribution. The
arguments for [Link]() differ for R and S-PLUS, yet the test statistic is computed
according to (10.13) and (10.14) when ties are present in the data.
Solution: The five-step procedure is used and explained to determine if differences exist
among teaching methods. Before proceeding, first examine side-by-side boxplots for free
throws made grouped by teaching method. Based on the boxplots and the density plots in
Figure 10.9, it seems reasonable to assume that all a populations are similar in shape.
Method4
0.6
0.5
0.4
Method4 0.3
0.2
0.1
0.0
Method3
0.6
0.5
0.4
Method3 0.3
0.2
0.1
Density
0.0
Method2
0.6
0.5
Method2 0.4
0.3
0.2
0.1
0.0
Method1
0.6
0.5
Method1 0.4
0.3
0.2
0.1
0.0
0 2 4 6 8 0 2 4 6 8
Free Throws Free Throws
FIGURE 10.9: Boxplots and density plots of free throw teaching results
438 Probability and Statistics with R
Step 2: Test Statistic — The test statistic Ri is used to evaluate the null hypothesis.
Under the assumption that H0 is true, the standardized test statistic
a 2
12 ni R i − R • q
H= i=1
∼ χ2a−1
N (N + 1)
Step 3: Rejection Region Calculations — The rejection region is Hobs > χ2.95;3 = 7.815.
The number of free throws completed in each of the methods is combined into a
single population and ranked among the 80 observations. Table 10.17 on the next
page shows the actual free throws with their ranks among the 80 observations. The
value of Hobs is calculated as
a 2
12 ni R i − R •
i=1
Hobs =
N (N + 1)
2 2
12 20 × (35.05 − 40.50) + 20 × (42.875 − 40.50) +
= × 2 2
(80 × 81) 20 × (50.825 − 40.50) + 20 × (33.250 − 40.50)
= 7.20412
Nonparametric Methods 439
Table 10.17: Actual free throws with ranks among all free throws
Meth1 RankM1 Meth2 RankM2 Meth3 RankM3 Meth4 RankM4 Total
6 76.5 3 63.5 2 45.5 2 45.5
1 18.0 2 45.5 1 18.0 1 18.0
2 45.5 1 18.0 2 45.5 1 18.0
0 2.0 2 45.5 3 63.5 3 63.5
0 2.0 1 18.0 2 45.5 1 18.0
1 18.0 6 76.5 2 45.5 2 45.5
1 18.0 2 45.5 4 70.0 1 18.0
3 63.5 1 18.0 3 63.5 6 76.5
1 18.0 1 18.0 2 45.5 1 18.0
2 45.5 2 45.5 3 63.5 1 18.0
1 18.0 1 18.0 2 45.5 0 2.0
2 45.5 1 18.0 5 73.0 1 18.0
4 70.0 2 45.5 1 18.0 1 18.0
2 45.5 3 63.5 1 18.0 1 18.0
1 18.0 2 45.5 3 63.5 1 18.0
1 18.0 2 45.5 7 79.5 2 45.5
1 18.0 3 63.5 6 76.5 2 45.5
3 63.5 2 45.5 2 45.5 1 18.0
7 79.5 5 73.0 2 45.5 5 73.0
1 18.0 2 45.5 2 45.5 4 70.0
Means: 35.050 42.875 50.825 33.250 40.500
The adjusted test statistic Hobs is calculated as
Hobs Hobs
Hobs = = r 3
fc j=1 tj − tj
1−
N3 − N
7.20412
= , -
(33 −3)+(293 −29)+(263 −26)+(103 −10)+(33 −3)+(33 −3)+(43 −4)+(23 −2)
1− 803 −80
7.204120
= = 7.865376
0.9159283
Step 4: Statistical Conclusion — The ℘-value for the standardized test statistic without
adjustment for ties(H) and the standardized test
2 statistic adjusted for ties (H )
are calculated as P χ3 ≥ 7.204 = 0.0656 and P χ3 ≥ 7.86 = 0.0488, respectively.
2
℘-values such as 0.065 and 0.0488 indicate that observing values as extreme or more
than 7.20 or 7.86 when the null hypothesis is true are fairly unlikely.
I. From the rejection region, reject H0 since Hobs = 7.86 > χ2.95;3 = 7.81.
II. From the ℘-value, reject H0 because the ℘-value = 0.0488 is less than 0.05.
Reject H0 .
To compute the rejection region, the value of the standardized test statistic (Hobs), and the
value of the standardized test statistic corrected for ties (Hc) with S, enter
To find the standardized test statistic corrected for ties and its corresponding ℘-value with
the function [Link](), enter
Nonparametric Methods 441
data: NumberFT by g
Kruskal-Wallis chi-squared = 7.8654, df = 3,
p-value = 0.04888
Ri − N +1
q
Zi = 2 2
∼ N (0, 1) (10.15)
(N +1)(N −ni )
12ni
q
by the Central Limit Theorem. It then follows that Zi2 ∼ χ21 . Although the Zi s are not
independent, when H0 is true, the statistic
a a 2
N − ni 2 12ni R i − N2+1
H= Zi = (10.16)
i=1
N i=1
N (N + 1)
and declaring treatments i and j significantly different when Zij obs > Z1−α/[a(a−1)] . By
dividing α/2 by a(a − 1)/2, the number of pairwise comparisons, the overall significance
level is appropriately adjusted.
Example 10.10 on page 437 rejected the null hypothesis of equal distributions and
concluded that at least two of the four methods have different distributions. The next
step is to decide which one of the four methods the gym instructor should use in teaching
students to shoot free throws. Using (10.17) with an α = 0.20, methods 1 and 4 are declared
to be significantly different from method 3 since Z13 obs = 2.15 > Z1−α/(a(a−1)) = 2.13 and
Z34obs = 2.39 > Z1−α/(a(a−1)) = 2.13. In this case, the probability that all the statements
442 Probability and Statistics with R
are correct is 1 − α = 0.8. The gym instructor should stop using methods 1 and 4. If the
instructor had to pick only one method to use, and all other factors were the same, he/she
should use method 3 since it is statistically better than method 1 and method 4. Although
there is no statistical difference between methods 2 and 3, method 2 is not statistically
better than method 1 or method 4. Code to compute the multiple comparisons according
to (10.17) is
> a <- 4 # Four methods
> Method1 <- c(6,1,2,0,0,1,1,3,1,2,1,2,4,2,1,1,1,3,7,1)
> Method2 <- c(3,2,1,2,1,6,2,1,1,2,1,1,2,3,2,2,3,2,5,2)
> Method3 <- c(2,1,2,3,2,2,4,3,2,3,2,5,1,1,3,7,6,2,2,2)
> Method4 <- c(2,1,1,3,1,2,1,6,1,1,0,1,1,1,1,2,2,1,5,4)
> n1 <- length(Method1); n2 <- length(Method2)
> n3 <- length(Method3); n4 <- length(Method4)
> NumberFT <- c(Method1, Method2, Method3, Method4)
> N <- length(NumberFT)
> RKs <- rank(NumberFT) # Ranks for all
> MRKs <- unstack(RKs, RKs~g) # Only R not S-PLUS
> RK <- apply(MRKs,2, mean) # Treatment ranks
> names(RK) <- c("MRKT1","MRKT2","MRKT3","MRKT4")
> alpha <- 0.20
> Z12 <- abs(RK[1]-RK[2])/sqrt((N*(N+1)/12)*(1/n1 + 1/n2))
> Z13 <- abs(RK[1]-RK[3])/sqrt((N*(N+1)/12)*(1/n1 + 1/n3))
> Z14 <- abs(RK[1]-RK[4])/sqrt((N*(N+1)/12)*(1/n1 + 1/n4))
> Z23 <- abs(RK[2]-RK[3])/sqrt((N*(N+1)/12)*(1/n2 + 1/n3))
> Z24 <- abs(RK[2]-RK[4])/sqrt((N*(N+1)/12)*(1/n2 + 1/n4))
> Z34 <- abs(RK[3]-RK[4])/sqrt((N*(N+1)/12)*(1/n3 + 1/n4))
> Zij <- round(c(Z12, Z13, Z14, Z23, Z24, Z34),2)
> names(Zij) <- c("Z12","Z13","Z14","Z23","Z24","Z34")
> CV <- round(qnorm(1- alpha/(a*(a-1))),2)
> Zij
Z12 Z13 Z14 Z23 Z24 Z34
1.06 2.15 0.24 1.08 1.31 2.39
> CV
[1] 2.13
> which(Zij > CV)
Z13 Z34
2 6
a block is to reduce the error of the experiment as much as possible by grouping similar
units so that the remaining differences will be largely due to the treatments. The use of
“blocks” comes from some of the earliest experimental designs in agriculture where fields
where divided in “blocks.”
In a randomized complete block design (RCBD), experimental units are assigned to
blocks, and then treatments are randomly assigned to the units within the blocks. To
analyze a RCBD with Friedman’s test, ranks are assigned to the observations within each
block. The ranked observations are denoted Rij , i = 1, . . . , b, j = 1, . . . , k. A representation
of the ranked data from a RCBD is shown in Table 10.18.
Table 10.18: A representation of the ranked data from a randomized complete block design
1 2 k Row Totals
1 R11 R12 · · · R1k k(k + 1)/2
2 R21 R22 · · · R2k k(k + 1)/2
Blocks . .. .. ..
.. . . .
b Rb1 Rb2 · · · Rbk k(k + 1)/2
Column Totals: R1 R2 · · · Rk bk(k + 1)/2
The assumptions required to apply Friedman’s test are the same as those required for the
Kruskal-Wallis test; namely, all populations sampled are continuous and identical, except
possibly for location. The null hypothesis is that the populations all have the same location.
Typically, the null hypothesis of no difference among the k treatments is written in terms
of the medians as H0 : ψ1 = ψ2 = · · · = ψk . Although the distribution under the null
hypothesis could be enumerated, it is not practical to do so as there are a total of (k!)b
distinguishable sets of entries in a b × k table. The Friedman statistic S is
⎡ ⎤
k
12
S=⎣ Rj2 ⎦ − 3b(k + 1), (10.18)
bk(k + 1) j=1
where Rj is the sum of ranks for each treatment, where ranks were assigned within each
block. The statistic S has an asymptotic χ2k−1 distribution as b tends to infinity. For
b > 7, numerical comparisons have shown χ2k−1 to be a reasonable approximation to the
distribution of S (Gibbons and Chakraborti, 2003). When ties are present in the ranks, S
is replaced with the quantity S :
k
12 Rj2 − 3b2 k(k + 1)2
S =
j=1
, - (10.19)
1 b gi
bk(k + 1) − k−1 i=1 j=1 tij − k
3
where gi denotes the number of tied groups in the ith block and tij is the size of the j th
tied
group)inthe ith block.* Note that when there are no ties in the blocks, the quantity
b gi
j=1 tij − k = 0 and S reduces to S. The null hypothesis is rejected at
1 3
k−1 i=1
the α level of significance whenever Sobs > χ21−α;k−1 . When the null hypothesis is rejected,
one can declare treatments i and j significantly different when ZRij obs > Zα/[k(k−1)] , where
444 Probability and Statistics with R
Typical values of α when performing multiple comparisons are often as large as 0.20 due to
the large number of comparisons.
Example 10.11 Friedman Test: Body Fat The body fat of 78 high school
wrestlers was measured using three separate techniques and the results are stored in the
data frame HSwrestler. The techniques used were hydrostatic weighing (HWFAT), skin fold
measurements (SKFAT), and the Tanita body fat scale (TANFAT). Do the three methods of
recording body fat have equal medians? Use a significance level of α = 0.05 to reach your
conclusion. If the null hypothesis of equal medians is rejected, determine which treatments
are significantly different using an overall experiment-wise error rate of α = 0.20.
Solution: Each wrestler in this scenario acts as a block. This particular design structure
is also known as a repeated measures design. Before testing the null hypothesis of equal
medians, a few graphs are created to verify the assumption of equal shaped populations.
> attach(HSwrestler)
> HSwrestler[1:5,]
AGE HT WT ABS TRICEPS SUBSCAP HWFAT TANFAT SKFAT
1 18 65.75 133.6 8 6 10.5 10.71 11.9 9.80
2 15 65.50 129.0 10 8 9.0 8.53 10.0 10.56
3 17 64.00 120.8 6 6 8.0 6.78 8.3 8.43
4 17 72.00 145.0 11 10 10.0 9.32 8.2 11.77
5 17 69.50 299.2 54 42 37.0 41.89 41.6 41.09
> FAT <- c(HWFAT, TANFAT, SKFAT)
> GROUP <- factor(rep(c("HWFAT","TANFAT","SKFAT"), rep(78,3)))
> BLOCK <- factor(rep(1:78,3)) # used later
> library(lattice)
> A <- bwplot(GROUP~FAT, xlab="% Fat")
> B <- densityplot(~FAT|GROUP, layout=c(1,3), xlab="% Fat")
> print(A, split=c(1,1,2,1), more=TRUE)
> print(B, split=c(2,1,2,1), more=FALSE)
Based on the boxplots and the density plots in Figure 10.10 on the next page, it seems
reasonable to assume that the distributions of body fat for the three treatment groups are
similar in shape.
Step 1: Hypotheses — The hypotheses to test no difference among the k treatments are
H0 : ψ1 = ψ2 = · · · = ψk versus H1 : ψi = ψj for at least one pair (i, j).
Step 2: Test Statistic — The test statistic S is used to evaluate the null hypothesis.
Under the assumption that H0 is true, the standardized test statistic
⎡ ⎤
k
12 q
S=⎣ R2 ⎦ − 3b(k + 1) ∼ χ2b−1
bk(k + 1) j=1 j
Nonparametric Methods 445
TANFAT
0.10
TANFAT
0.05
0.00
SKFAT
0.10
Density
SKFAT
0.05
0.00
HWFAT
0.10
HWFAT
0.05
0.00
10 20 30 40 0 10 20 30 40 50
% Fat % Fat
FIGURE 10.10: Comparative boxplots and density plots for hydrostatic weighing (HWFAT),
skin fold measurements (SKFAT), and the Tanita body fat scale (TANFAT)
Step 3: Rejection Region Calculations — The rejection region is Sobs > χ2.95;2 = 5.99.
The first six wrestlers’ body fat as measured by the three techniques and their
corresponding ranks are shown in Table 10.19.
Table 10.19: The first six wrestlers’ body fat as measured by the three techniques and their
corresponding ranks
Measurement Rank
Wrestler HWFAT TANFAT SKFAT HWFAT TANFAT SKFAT
1 10.71 11.9 9.80 2 3 1
2 8.53 10.0 10.56 1 2 3
3 6.78 8.3 8.43 1 2 3
4 9.32 8.2 11.77 2 1 3
5 41.89 41.6 41.09 3 2 1
6 34.03 29.9 29.45 3 2 1
.. .. .. .. .. .. ..
. . . . . . .
R1 = 128 R2 = 187 R3 = 153
I. From the rejection region, reject H0 since Sobs = 22.48718 > χ2.95;2 = 5.99.
II. From the ℘-value, reject H0 because the ℘-value = 1.309095 × 10−5 is less
than 0.05.
Reject H0 .
To compute the rejection region, the value of the standardized test statistic, and its corre-
sponding value with S, enter
> cfat <- cbind(HWFAT, TANFAT, SKFAT)
> RK <- t(apply(cfat,1, rank))
> OBSandRK <- cbind(cfat, RK)
> OBSandRK[1:5,]
HWFAT TANFAT SKFAT HWFAT TANFAT SKFAT
[1,] 10.71 11.9 9.80 2 3 1
[2,] 8.53 10.0 10.56 1 2 3
[3,] 6.78 8.3 8.43 1 2 3
[4,] 9.32 8.2 11.77 2 1 3
[5,] 41.89 41.6 41.09 3 2 1
> Rj <- apply(RK,2, sum)
> b <- length(HWFAT)
> k <- dim(cfat)[2]
> S <- (12/(b*k*(k+1)))*sum(Rj^2)-3*b*(k+1)
> S
[1] 22.48718
> pval <- 1-pchisq(S, k-1)
> pval
[1] 1.309095e-05
To find the standardized test statistic and its corresponding ℘-value with the function
[Link](), enter
> [Link](FAT~GROUP|BLOCK) # R syntax
neither of the other two methods is an acceptable substitute for measuring body fat for
high school wrestlers.
where χ2obs is the sum of the squared deviations between what is observed (Ok ) and what
is expected (Ek ) in each of the k categories divided by what is expected in each of the k
categories. Large values of χ2obs occur when the observed data are inconsistent with the
null hypothesis and thus lead to rejection of the null hypothesis. The exact distribution
of χ2obs is very complicated; however, for large n, provided all expected categories are at
least 5, χ2obs is distributed approximately χ2 with k − 1 degrees of freedom. When the
null hypothesis is composite, that is, not all of the parameters are specified, the degrees of
freedom for the random variable χ2obs are reduced by one for each parameter that must be
estimated.
Example 10.12 Soccer Goodness-of-Fit Test the hypothesis that the number
of goals scored during regulation time for the 232 soccer matches stored in the data frame
Soccer has a Poisson cdf with λ = 2.5 with the chi-square goodness-of-fit test and an α
level of 0.05. Produce a histogram showing the number of observed goals scored during
regulation time and superimpose on the histogram the number of goals that are expected
to be made when the distribution of goals follows a Poisson distribution with λ = 2.5.
Solution: Since the number of categories for a Poisson distribution is theoretically infinite,
a table is first constructed of the observed number of goals to get an idea of reasonable
categories.
> attach(Soccer)
> table(Goals)
Goals
0 1 2 3 4 5 6 7 8
19 49 60 47 32 18 3 3 1
Since there were a total of n = 232 soccer games, the expected number of goals for the six
categories is simply 232 × PX:
Step 1: Hypotheses — The null and alternative hypotheses for using the chi-square
goodness-of-fit test to test the hypothesis that the number of goals scored during
regulation time for the 232 soccer matches stored in the data frame Soccer has a
Poisson cdf with λ = 2.5 are
Step 3: Rejection Region Calculations — Reject if χ2obs > χ21−α;k−1 . χ2obs is computed
with (10.21):
?
1.391940 = χ2obs > χ2.95;6 = 12.59.
I. Since χ2obs = 1.391940 is not greater than χ2.95;6 = 12.59, fail to reject H0 .
II. Since the ℘-value = 0.9663469 is greater than 0.05, fail to reject H0 .
Fail to reject H0 .
Step 5: English Conclusion — There is no evidence to suggest that the true cdf does not
equal the Poisson distribution with λ = 2.5 for at least one x.
450 Probability and Statistics with R
R and S-PLUS use different functions to perform a chi-square goodness-of-fit test. The R
function to implement a chi-square goodness-of-fit test is [Link](), and the S-PLUS
function is [Link](). Although S-PLUS has the function [Link](), it will not
accept all of the arguments that the R function [Link]() does. The reader should
refer to Table A.6 on page 663 or the respective help files for more information on each
function.
Code and output for R:
data: OB
X-squared = 1.3919, df = 6, p-value = 0.9663
Code and output for S-PLUS follow. The argument [Link] specifies the categories
with an open left interval and closed right interval, that is, (lower, upper]. By specifying
right=TRUE, the categories become left closed and right open.
data: Goals
Chi-square = 1.3919, df = 6, p-value = 0.9663
alternative hypothesis:
True cdf does not equal the poisson Distn. for at least
one sample point.
Note that the histogram does not reflect the category ≥ 6, but rather depicts the observed
categories of 6, 7, and 8.
60
50
40
30
20
10
0
0 2 4 6 8
Goals
FIGURE 10.11: Histogram of observed goals for Soccer with a superimposed Poisson
distribution with λ = 2.5 (vertical lines)
Although the chi-square goodness-of-fit test is primarily designed for discrete distri-
butions, it can also be used with a continuous distribution if appropriate categories are
defined.
Example 10.13 Goodness-of-Fit for SAT Scores Use the chi-square goodness-
of-fit test with α = 0.05 to test the hypothesis that the SAT scores stored in the data frame
Grades have a normal cdf. Use categories, (−∞, μ− 2σ], (μ− 2σ, μ− σ], (μ− σ, μ], (μ, μ+ σ],
(μ + σ, μ + 2σ], and (μ + 2σ, ∞]. Produce a histogram using the categories specified and
superimpose on the histogram the expected number of SAT scores in each category when
F0 (x) ∼ N (μ = x̄, σ = s).
Step 1: Hypotheses — The null and alternative hypotheses for using the chi-square
goodness-of-fit test to test the hypothesis that the SAT scores stored in the data
frame Grades have a Normal cdf are
Step 2: Test Statistic — Since the mean and standard deviation are unknown, the first
step is to estimate the unknown parameters μ and σ using x̄ = 11334.65 and
s = 145.61:
> attach(grades)
> mu <- mean(sat)
452 Probability and Statistics with R
Step 4: Statistical Conclusion — In this problem, two parameters were estimated, and
as a consequence, the degrees of freedom are computed as 6−2−1 = 3. The ℘-value
is 0.2433129.
I. Since χ2obs = 4.173654 is not greater than χ2.95;3 = 7.81, fail to reject H0 .
II. Since the ℘-value = 0.2433129 is greater than 0.05, fail to reject H0 .
Fail to reject H0 .
Step 5: English Conclusion — There is no evidence to suggest that the true cdf of SAT
scores is not a normal distribution.
If one uses the R function [Link](), the degrees of freedom and the subsequent ℘-value
will be incorrect, as illustrated next:
The S-PLUS function [Link]() computes the degrees of freedom and the corresponding
℘-value correctly, provided the argument [Link]= is correctly specified:
80
60
40
20
0
sat
FIGURE 10.12: Histogram of SAT scores in Grades superimposed with the expected
number of SAT scores for the categories (−∞, μ − 2σ], (μ − 2σ, μ − σ], (μ − σ, μ], (μ, μ + σ],
(μ + σ, μ + 2σ], and (μ + 2σ, ∞] (vertical lines)
The statistic Dn does not depend on F0 (x) as long as F (x) is continuous. The deriva-
tion of the sampling distribution of Dn is beyond the scope of this text. The curious
reader can refer to Gibbons and Chakraborti (2003), page 114, for the derivation of the
sampling distribution of Dn . The statistic and sampling distribution of Dn should only
be used with simple hypotheses. When the null hypothesis is composite, the critical
values for the Kolmogorov-Smirnov test (based on the sampling distribution of Dn ) are
extremely conservative. The Kolmogorov-Smirnov test can be used to assess normality
provided the distribution is completely specified. In a test of normality where the null
hypothesis is not completely specified, the statistic Dn can still be used
by estimating
the unknown parameters of F0 (x) using maximum likelihood F̂0 (x) and substituting
F̂0 (x) for F0 (x) in (10.22). However, this further complicates the sampling distribution
of Dn . When testing
a composite
normal hypothesis with unknown μ and σ, the test that
uses Dn = supx F̂n (x) − F̂0 (x) is called Lilliefors’s normality test (explained more fully
starting on page 458). Lilliefors used simulation to study the sampling distribution of Dn
for composite hypotheses and subsequently to publish critical values for using Dn with
composite hypotheses. Simulation will be used to show the differences in the distribution
of Dn for a simple null hypothesis versus the distribution of Dn with a composite null
hypothesis.
Nonparametric Methods 455
where
, -
Mi = max F̂n X(i) − F0 X(i) , F0 X(i) − F̂n X(i−1) (10.25)
i
Since F̂n X(i) = and F̂n X(i−1) = i−1
n n , (10.25) can be expressed as
3 4
i i − 1
Mi = max − F0 X(i) = Di+ , F0 X(i) − = D− (10.26)
n n i
Stated formally, the null and alternative hypotheses for the Kolmogorov-Smirnov test
for goodness-of-fit are
H0 : F (x) = F0 (x) for all x versus H1 : F (x) = F0 (x) for some x. (10.27)
The null hypothesis is rejected when Dn > Dn;1−α or when the test’s ℘-value is less than the
largest acceptable α value. Since S will compute the ℘-value for the Kolmogorov-Smirnov
test, critical values for various n and α are not presented. R uses the function [Link](x,
y, ...), where x is a numeric vector of observations and y is either a numeric vector of
data values or a character string naming a distribution function. S-PLUS uses the function
[Link](x, distribution = "normal", ...), where x is a numeric vector of observations.
The examples will illustrate the use of both functions.
The computation of the Komolgorov-Smirnov statistic Dn and its ℘-value with R and S-
PLUS (given that the previous S code has been entered) follow.
R code:
data: x
D = 0.2556, p-value = 0.827
alternative hypothesis: [Link]
Nonparametric Methods 457
S-PLUS code:
> [Link](x, dist="normal", mean=mu, sd=sig)
data: x
ks = 0.2556, p-value = 0.8269
alternative hypothesis:
True cdf is not the normal distn. with the specified parameters
The Komolgorov-Smirnov statistic is labeled D in R and ks in S-PLUS. Both R and S-PLUS
return the value Dn = 0.2556 with a corresponding ℘-value √ of 0.827, which provides no
evidence to reject the null hypothesis that F0 (x) ∼ N 6.5, 2 . Figure 10.13 provides a
graphical illustration of the vertical deviations used to compute the statistic Dn for this
problem.
1.0
D+
D−
0.8
F (x)
F̂n (x)
0.6
0.4
0.2
0.0
4 5 6 7 8 9 10
FIGURE 10.13: Graphical illustration of the vertical deviations used to compute the
statistic Dn for Example 10.14 on page 455
In Example 10.14 on page 455, the statistic Dn = 0.2556 was computed, and both R
and S-PLUS returned a ℘-value of 0.827. To visualize the sampling distribution of Dn and
to find simulated critical values, one can use R code similar to the following:
2
1
0
Simulated critical value = 0.567 for n = 5 when the alpha value = 0.05
The function ksLdist() allows the user to choose the number of samples with the argu-
ment sims= and easily to verify the results given by Lilliefors (1967). Dallal and Wilkinson
(1986) duplicated the work by Lilliefors (1967) using much larger samples
as well as deriving
an analytic approximation for the upper tail ℘-values for Dn = supx F̂n (x) − F̂0 (x). For
℘-values less than 0.100 and sample sizes ranging from 5 to 100, the Dallal-Wilkinson
approximation is
+ = exp(−7.01256 · D2 · (n + 2.78019)
℘-value n
√ √
+ 2.99587 · Dn · n + 2.78019 − 0.122119 + 0.974598/ n + 1.67997/n) (10.28)
The estimated densities from running ksLdist(sims=10000, n=10) are shown in Fig-
ure 10.15 on the following page, which highlights how much less variability is present in
the sampling distribution of Dn when the null hypothesis is composite. To correctly test a
composite hypothesis of normality, one should use the R function [Link]() available
in the R package nortest. That is, one should not use the R function [Link](). However,
both simple and composite hypotheses of normality can be tested using the S-PLUS function
[Link](). The R function [Link]() from the nortest package produces virtually
the same result as the S-PLUS function [Link](), with the exception that the ℘-value is
not set to 0.5 when the Dallal-Wilkinson approximation yields a ℘-value greater than 0.1
when testing a composite hypothesis of normality.
Example 10.15 Long Distance Phone Calls Calculate the ℘-value and state
the English conclusion for testing whether the times spent on long distance phone calls
([Link]) in the data frame Phone have a normal distribution using the R function
[Link] from the nortest package as well as using the S-PLUS function [Link]().
Verify the reported ℘-values using (10.28).
Solution: The results after attaching the data frame Phone are presented first for R, and
then for S-PLUS. Note that R labels the statistic Dn with D and S-PLUS with ks. Both
functions return Dn = 0.1910237 with a ℘-value = 0.0291.
460 Probability and Statistics with R
10
Simple Hypothesis
Composite Hypothesis
8
6
Density
4
2
0
Simulated critical value = 0.405 (simple hypothesis) and 0.262 (composite hypothesis)
for n = 10 when the alpha value = 0.05
FIGURE 10.15: Estimated densities for simple and composite hypotheses from running
ksLdist(sims=10000, n=10)
For R:
> attach(Phone)
> library(nortest)
> [Link]([Link])
data: [Link]
D = 0.191, p-value = 0.0291
For S-PLUS:
> attach(Phone)
> [Link]([Link], distribution="normal")
data: [Link]
ks = 0.191, p-value = 0.0291
alternative hypothesis:
True cdf is not the normal distn. with estimated parameters
sample estimates:
mean of x standard deviation of x
3.686957 3.623698
To compute the ℘-value using (10.28), a small function DWA is written that returns an
estimated ℘-value of 0.0291:
Nonparametric Methods 461
With a ℘-value of 0.0291, the null hypothesis is rejected. There is evidence that phone call
length is not normally distributed.
b2
W = , (10.29)
nSu2
n2 ? @
where Su2 is the uncorrected sample variance, b = i=1 an−i+1 (x(n−i+1) − x(i) ), and n2
is the integer part of n2 . The coefficients an−i+1 that are calculated automatically by S are
tabulated in Table 6 of Shapiro and Wilk (1965).
The critical region of the test is given by
P(W ≤ K|H0 ) = α,
where α is the significance level. The critical values K can be found in Shapiro and Wilk
(1965, Table 5), but they are not displayed in the S output for this test. The vector of
weights a = (a1 , . . . , an ), where ai = −an−i+1 , is calculated as
w V−1
a= , (10.30)
w V−1 V−1 w
where the elements of the vector w are wi = E x(i) and V is the covariance matrix of the
order statistics x(1) , x(2) · · · , x(n) .
Example 10.16 Shapiro-Wilk Normality Test Use the Shapiro-Wilk test with
the random sample {47, 50, 57, 54, 52, 54, 53, 65, 62, 67, 69, 74, 51, 57, 57, 59} to test for nor-
mality using α = 0.05.
462 Probability and Statistics with R
47 ≤ 50 ≤ 51 ≤ 52 ≤ 53 ≤ 54 = 54 ≤ 57 = 57 = 57 ≤ 59 ≤ 62 ≤ 65 ≤ 67 ≤ 69 ≤ 74.
? @
Next, calculate the differences x(n−i+1) − x(i) for i = 1, 2, . . . , n2 = 8:
Looking at Table 6 from Shapiro and Wilk (1965) (n = 16 and i = 1, . . . , 8), one obtains
b2 808.7881
W = 2
= = 0.9471.
nSu 854
The critical value K with α = 0.05 and n = 16 is 0.887. As Wobs = 0.9471 > 0.887, one
fails to reject the null hypothesis of normality.
In S:
> x <- c(47,50,57,54,52,54,53,65,62,67,69,74,51,57,57,59)
> [Link](x)
receives), 400 male patients with mild dementia were randomly divided into two groups of
200. One group was given a placebo over three months while the second group received
an experimental drug for three months. At the end of the three months, the physicians
(all psychiatrists) classified the 400 patients into one of three categories: improved, no
change, or worse. Are the proportions in the three status categories the same for the two
treatments?
The two scenarios illustrate two different sampling schemes that both result in I × J
contingency tables. In the first scenario, there is a single population (Americans) and
individuals are sampled from this single population and classified into one of the IJ cells
of the I × J contingency table based on the I = 2 SEX categories and the J = 3 HAPPY
categories. The format of an I ×J contingency table when sampling from a single population
is shown in Table 10.23. The number of observations from the ith row classified into the
j th column is denoted by nij . It follows that the number of observations in the j th column
(1 ≤ j ≤ J) is n•j = n1j + n2j + · · · + nIj , while the number of observations in the ith row
(1 ≤ i ≤ I) is ni• = ni1 + ni2 + · · · + niJ .
The true population proportion of individuals in cell (i, j) will be denoted πij . Under
the assumption of independence between row and column variables (SEX and HAPPY
J I
in this example), πij = πi• × π•j , where πi• = j=1 πij and π•j = i=1 πij . That is,
πi• is the proportion of observations in the population classified in category i of the row
variable and π•j is the proportion of observations in the population classified in category j
of the column variable. Since πi• and π•j are marginal population proportions, it follows
n
that π̂i• = pi• = nni• and π̂•j = p•j = n•j , where n is the sample size. Under the
assumption of independence the expected count for cell (i, j) is μij = nπij = nπi• π•j and
n n n
μ̂ij = nπ̂ij = nπ̂i• π̂•j = n nni• n•j = i•n •j .
In the second scenario, there are two distinct populations from which samples are taken.
The first population is the group of all patients receiving the experimental drug while the
second population is the group of all patients receiving a placebo. In this scenario, there
are I = 2 separate populations and J = 3 categories for the I = 2 populations. Individuals
sampled from the I = 2 distinct populations are classified into one of the J = 3 status
categories. This scenario has fixed row totals whereas the first scenario does not. In the
first scenario, only the total sample size, n, is fixed. That is, neither the row nor the column
totals are fixed. This is in contrast to scenario two, where the number of patients in each
treatment group (row) was fixed. The notation used for an I × J contingency table when
I samples from I distinct populations differs slightly from the notation used in Table 10.23
on the previous page with a contingency table from a single sample.
Since the sample sizes of the I distinct populations are denoted ni• , the total for all I
samples is denoted by n•• rather than the notation n used for a single sample in Table 10.23
on the preceding page. Table 10.24 shows the general form and notation used for an I × J
contingency table when sampling from I distinct populations. Each observation in each
sample is classified into one of J categories. If ni• denotes the number of observations in
the ith sample (1 ≤ i ≤ I) and nij denotes the number of observations from the ith sample
classified into the j th category (1 ≤ j ≤ J), it follows that the number of observations in
the j th column is n•j = n1j + n2j + · · · + nIj , while the number of observations in the ith
row is ni• = ni1 + ni2 + · · · + niJ .
Table 10.24: General form and notation used for an I × J contingency table when sampling
from I distinct populations
The null and alternative hypotheses to test for independence between row and column
variables is written H0 : πij = πi• π•j versus H1 : πij = πi• π•j . The test statistic is
I J
(Oij − Eij )2
χ2obs = . (10.31)
i=1 j=1
Eij
It compares the observed frequencies in the table with the expected frequencies when H0
is true. Under the assumption of independence, and when the observations in the cells
I J (n −μ̂ )2 q
are sufficiently large (usually greater than 5), χ2obs = i=1 j=1 ijμ̂ij ij ∼ χ2(I−1)(J−1) ,
n n
where μ̂ij = i•n •j = Eij and nij = Oij . The null hypothesis of independence is rejected
when χobs > χ21−α;(I−1)(J−1) .
2
The chi-squared approximation is generally satisfactory if the Eij s (μ̂ij s) in the test
statistic are not too small. Various rules of thumb exist for what might be considered
too small. A very conservative rule is to require all Eij s to be 5 or more. This can
be accomplished by combining cells with small Eij s and reducing the overall degrees of
freedom. At times, it may be permissible to let the Eij of a cell be as low as 0.5.
Step 1: Hypotheses — H0 : πij = πi• π•j (Row and column variables are independent.)
versus H1 : πij = πi• π•j for at least one i, j (Row and column variables are
dependent.)
> E
Very Happy Pretty Happy Not To Happy
Male 123.6280 262.2 51.17202
Female 149.3720 316.8 61.82798
I. From the rejection region, since χ2obs = 4.32 < χ0.95;2 = 5.99, fail to reject the
null hypothesis of independence.
II. Since the ℘-value = .115 is greater than 0.05, fail to reject the null hypothesis
of independence.
Fail to reject H0 .
Step 5: English Conclusion — There is not sufficient evidence to suggest the variables
gender and happiness are statistically dependent.
Both R and S-PLUS have the function [Link](), which can be used to test the null
hypothesis of independence by computing the observed test statistic and its corresponding
℘-value:
> [Link](HAT)
data: HAT
X-squared = 4.3215, df = 2, p-value = 0.1152
H0 : π1j = π2j = · · · = πIj for all j versus H1 : πij = πi+1,j for some (i, j). (10.32)
Nonparametric Methods 467
Expressed in words, the null hypothesis is that the I populations are homogeneous with
respect to the J categories versus the I populations are not homogeneous with respect to the
J categories. An equivalent interpretation is that for each population j = 1, 2, . . . , J, the
proportion of people in the j th category is the same. When H0 is true, π1j = π2j = · · · = πIj
n•j n n•j
for all j. Under the null hypothesis, μij = ni• πij , π̂ij = pij = n•• , and μ̂ij = i•
n•• = Eij .
When H0 is true, all the probabilities in the j th column are equal, and a pooled estimate of
πij is obtained by adding all the frequencies in the j th column (n•j ) and dividing the total
by n•• . The statistic used in this type of problem has the same form as the one used for
the test of independence in (10.31). Substituting the homogeneity expressions for Oij and
Eij , the statistic is expressed as
I J
(nij − ni• n•j /n•• )2 q 2
χ2obs = ∼ χ(I−1)(J−1) .
i=1 j=1
ni• n•j /n••
Step 1: Hypotheses — H0 : π1j = π2j for all j versus H1 : πi, j = πi+1, j for some (i, j).
That is, all the probabilities in the same column are equal to each other versus at
least two of the probabilities in the same column are not equal to each other.
The value of the test statistic is χ2obs = 6.7584. This can be done with code by
entering
?
6.76 = χ2obs > χ2.95,2 = 5.99.
I. From the rejection region, since χ2obs = 6.76 > χ.95;2 = 5.99, reject the null
hypothesis of homogeneity.
II. Since the ℘-value = .034 is less than 0.05, reject the null hypothesis of
homogeneity.
Reject H0 .
Step 5: English Conclusion — There is sufficient evidence to suggest that not all of the
probabilities for the i = 2 populations with respect to each of the J categories are
equal.
Both R and S-PLUS have the function [Link](), which can be used to test the null
hypothesis of homogeneity by computing the observed test statistic and its corresponding
℘-value:
> [Link](DTT)
data: DTT
X-squared = 6.7584, df = 2, p-value = 0.03408
Nonparametric Methods 469
These values are used to calculate an estimate of the statistic of interest, s(x) = θ̂. This s(x)
is not necessarily the plug-in estimate of θ, θ̂ = t(F̂ ), where F̂ is the empirical probability
distribution function. It is, however, the function applied to the bootstrap sample x∗ that
creates a bootstrap estimate of θ denoted θ̂∗ or t∗ . That is,
where G is the cumulative distribution function of the distribution of θ̂. Generally, the
bootstrap estimate of the parameter of interest is not computed directly, but it is instead
estimated from B bootstrap samples.
The process of creating a bootstrap sample x∗ and a bootstrap estimate θ̂∗ of the
parameter of interest is repeated B times (typically 999 or more). The B bootstrap estimates
of θ, the θ̂∗ s, are subsequently used to estimate specific properties of the bootstrap sampling
distribution of θ̂∗ . Note that B values of θ̂∗ are used to estimate specific
properties of
the bootstrap sampling distribution of θ̂∗ . There are a total of 2n−1 n distinct bootstrap
∗ =
samples. Yet, a reasonable estimate of the standard error of θ̂ , σ̂θ̂∗ ≡ SEB , can be achieved
with only B = 200 bootstrap replications in most problems. For confidence intervals and
quantile estimation, B generally should be at least 999.
The general procedure for estimating the standard error of θ̂∗ is
(1) Generate B independent bootstrap samples {x∗1 , x∗2 , . . . , x∗B }, each consisting of n values
drawn with replacement from x.
(3) Estimate the standard error of θ̂ SEF (θ̂) ≡ σθ̂ by computing the sample standard
deviation of the bootstrap replications of θ̂b∗ , b = 1, 2, . . . , B:
⎡ & '2 ⎤ 12
B θ̂b∗ − θ̂∗ B
θ̂b∗
= B ≡ σ̂ ∗ = ⎢
σθ̂ ≈ SE ⎣
⎥
⎦ , where θ̂∗ = . (10.35)
θ̂
B−1 B
b=1 b=1
The bootstrap algorithm for estimating the standard error of a statistic θ̂ = s(x) is
graphically depicted in Figure 10.16.
Nonparametric Methods 471
Bootstrap Bootstrap
Empirical Samples of Replications
Distribution Size n of θ̂
⎫
x∗1 θ̂1∗ = s(x∗1 ) ⎪
⎪
*
⎪
x∗ θ̂2∗ = s(x∗2 )
⎪
⎪
⎪
⎪
1
2∗ ⎪
θ̂3∗ = s(x∗3 ) ⎪
⎪
x3
:
⎪
⎪
⎪
⎪
.. .. ⎬
F̂
XX . .
H XXX ⎪
⎪
⎪
HH XXX ⎪
HH z
X x∗ θ̂b∗ ⎪
s(x∗b )
⎪
b = ⎪
⎪
HH .. .. ⎪
⎪
⎪
⎪
H .∗
H
j . ⎪
xB θ̂B = s(xB ) ⎪
∗ ∗ ⎭
⎡ & '2 ⎤ 12
B θ̂b∗ − θ̂∗ ⎥
=B = ⎢
SE ⎣ ⎦
B−1
b=1
FIGURE 10.16: Graphical representation of the bootstrap based on Efron and Tibshirani
(1993, Figure 6.1)
For most statistics, bootstrap distributions approximate the shape, spread, and bias of
the actual sampling distribution; however, bootstrap distributions differ from the actual
sampling distribution in the locations of their centers. The bootstrap distribution having a
similar shape is clear. A similar spread means
Var s(X)|F ≈ Var s(X)|F̂ .
That is, the variance of the estimator s(X) under the unknown distribution F is approx-
imately the same as the variance of s(X) under the bootstrap distribution obtained by
replacing F with F̂ . The variance of s(X) under F̂ is not computed, but rather estimated
from the B bootstrap samples and is
& '2
B θ̂b∗ − θ̂∗
+ B s(X)|F̂ =
Var s(X)|F̂ ≈ Var .
B−1
b=1
The sampling distribution of a statistic s(X) used to estimate the parameter θ = t(F ) is
centered at the parameter θ plus any bias, while the bootstrap distribution is centered at
θ̂ plus any bias. Recall that the bias of a statistic θ̂ is E(θ̂) − θ. Consequently, the bias of
s(X) = θ̂ is expressed as
Bias s(X)|F = EF s(X) − t(F ),
The result is an estimated bootstrap bias of s(X) based on B bootstrap samples denoted
With estimates of the standard error (standard deviation) and bias of some statistic
of interest, various types of confidence intervals for the parameter θ can be constructed.
Although exact confidence intervals for specific problems can be computed, most confidence
intervals are approximate. The most common confidence interval for a parameter θ when θ̂
follows either a normal or approximately normal distribution is
% (
+ B (θ̂) − z1−α/2 · SE
CI 1−α (θ) = θ̂ − Bias = B , θ̂ − Bias
+ B (θ̂) + z1−α/2 · SE
=B (10.38)
∗
The basic bootstrap confidence interval is based on the idea that theq θ̂ − θ̂
quantity
∗
has roughly the same distribution as θ̂ − θ. Since (10.39) has θ̂ − θ̂ ∼ θ̂ − θ , (10.40)
follows. To get (10.41) from (10.40), subtract θ̂ inside the probability statement and divide
by −1:
% (
∗
P θ̂((B+1)·α/2) − θ̂ ≤ θ̂∗ − θ̂ ≤ θ̂((B+1)·(1−α/2))
∗
− θ̂ ≈ 1 − α, (10.39)
% (
∗ ∗
P θ̂((B+1)·α/2) − θ̂ ≤ θ̂ − θ ≤ θ̂((B+1)·(1−α/2)) − θ̂ ≈ 1 − α (10.40)
% (
∗ ∗
P 2θ̂ − θ̂((B+1)·(1−α/2)) ≤ θ ≤ 2θ̂ − θ̂((B+1)·α/2) ≈1−α (10.41)
Equations (10.39) to (10.41) lead to the basic bootstrap confidence interval given in (10.42):
% (
∗ ∗
CI 1−α (θ) = 2θ̂ − θ̂((B+1)·(1−α/2)) , 2θ̂ − θ̂((B+1)·α/2) (10.42)
The percentile confidence interval is based on the quantiles of the B bootstrap replica-
tions of s(X). Specifically, the (1 − α) percentile confidence interval of θ uses the α/2 and
the 1 − α/2 quantiles of the θ̂∗ values to create a (1 − α) · 100% confidence interval for θ:
% (
∗ ∗
CI 1−α (θ) = θ̂((B+1)·α/2) , θ̂((B+1)·(1−α/2)) (10.43)
∗
The notation θ̂(Integer) is used to denote the (Integer)th θ̂∗ of the B sorted θ̂∗ values. The
values of B and α are generally chosen so that (B + 1) · α/2 is an integer. In cases where
(B + 1) · α/2 is not an integer, interpolation can be used. (Note that different programs use
different interpolation techniques.)
Nonparametric Methods 473
∗
One may have noticed that the percentile confidence interval uses θ̂((B+1)·α/2) to con-
struct the lower endpoint of the confidence interval while the basic bootstrap interval uses
∗
θ̂((B+1)·α/2) in the construction of the upper endpoint of its confidence interval. Is one of
the methods backwards? If not, does one method work better than the other? In fact,
neither method is backward and neither method is uniformly superior to the other. At
this point, a reasonable question might be which confidence interval is recommended for
general usage since the normal confidence interval is based on large sample properties and
the percentile and basic bootstrap confidence interval formulas give different answers when
the distribution of θ̂∗ is skewed. In fact, the answer is to use none of the confidence intervals
discussed thus far. The bootstrap confidence interval procedure recommended for general
usage is the BCa method, which stands for bias-corrected and accelerated. The first three
methods discussed (normal, percentile, and basic bootstrap) have first-order accuracy, while
the BCa method is second-order accurate. Accuracy in this context simply refers to the
coverage errors. The bottom line is that there are theoretical reasons to prefer the BCa
confidence interval over the normal, percentile, and basic % bootstrap
( confidence intervals.
∗ ∗
To compute a BCa interval for θ, CI 1−α (θ) = θ̂lower , θ̂upper , first compute the bias
factor, z, where ⎡ B , -⎤
∗
b=1 I θ̂ b < θ̂
z = Φ−1 ⎣ ⎦. (10.44)
B
Recall the definition of Φ−1 on page 153. Provided the estimated bootstrap B
distribution,
b=1 I{θ̂b <θ̂ }
∗
∗ ∗
s(x ) = θ̂ , is symmetric with respect to θ̂, and if θ̂ is unbiased, then B will be
close to 0.5, and the bias correction factor z will be close to zero since Φ−1 (0.5) = 0, with
S, qnorm(.5)=0. Next, compute the skewness correction factor:
n & '3
i=1 θ̂ (−i) − θ̂(−i)
a= ! (10.45)
n & '2 " 32
6 i=1 θ̂ (−i) − θ̂(−i)
where θ̂(−i) is the value of θ̂ = s(X) when the ith value is deleted from the sample of n
n θ̂
values and θ̂ (−i) = i=1 (−i)
n . Using z and a, compute
! " ! "
z + zα/2 z + z1−α/2
a1 = Φ z + and a2 = Φ z + (10.46)
1 − a(z + zα/2 ) 1 − a(z + z1−α/2 )
Now, lower = (B + 1) · a1 and upper = (B + 1) · a2 . When either lower or upper is not
an integer, interpolation can be used to obtain the lower and upper endpoints of the BCa
confidence interval: % (
∗ ∗
CI 1−α (θ) = θ̂lower , θ̂upper . (10.47)
Let k = (B + 1) · ai for i = 1, 2. Then the appropriate interpolation is
& '
Φ−1 (ai ) − Φ−1 B+1k & '
∗ ∗ & ' & ∗
' · θ̂(k+1) ∗
θ̂((B+1)·ai)
= θ̂(k) + − θ̂(k) for i = 1, 2. (10.48)
Φ−1 B+1
k+1
− Φ−1 B+1k
(b) Calculate bootstrap confidence intervals for the mean and standard deviation of those
times using the function [Link]() from the boot package. Specifically, use the
arguments norm, basic, perc, and bca with the function [Link]() to create normal
approximation, basic, percentile, and BCa confidence intervals.
(c) Verify the resulting confidence intervals for the mean using the appropriate equations.
(a) It appears that the conditions for an approximate Poisson process are satisfied. The esti-
mated parameter, λ, for the Poisson process, λ̂, is 0.1282051 cars per second. Consequently,
the waiting time until the next car follows an exponential distribution with mean 1/λ . In this
case, the estimated mean of the exponential distribution (waiting time) is 7.8 seconds/car.
A density histogram of the interarrival times with a superimposed Exp(λ = 0.1282051)
suggests this distribution is reasonable. In addition, both the mean and the standard
deviation of Times are roughly equal, as they should be with an exponential distribution.
> attach(SDS4)
> TT <- max(cumsum(Times)) # Time period (seconds)
> n <- length(lag(Times)) # Number of Cars
> Elamb <- n/TT # Cars/Time period
> EMeanExp <- 1/Elamb # Time period/Cars
> ans <- c(TT, n, Elamb, EMeanExp)
> names(ans) <- c("Total Time", "# of Cars", "Est. lambda", "Est. Mean")
> ans
Total Time # of Cars Est. lambda Est. Mean
312.0000000 40.0000000 0.1282051 7.8000000
> hist(Times, prob=TRUE)
> curve(dexp(x, Elamb), 0, 35, add=TRUE) # Only R
> c(mean(Times), sd(Times)) # Distribution Check
[1] 7.800000 7.871402
Histogram of Times
0.10
0.08
Density
0.06
0.04
0.02
0.00
0 5 10 15 20 25 30 35
Times
(b) The four bootstrap confidence intervals are now constructed for the mean using both the
function boot() and the formulas. Use the function [Link]() with the boot package
to compute the mean:
> library(boot)
> [Link] <- function(data, i)
+ {
+ d <- data[i]
+ M <- mean(d)
+ M
+ }
Set the number of bootstrap replications B to 9999 and generate the bootstrapped distri-
bution of X denoted by t∗ when using the boot package. Store the results in [Link]. Note
that R in boot is the number of bootstrap replications, which is denoted B in this text, so
R is set equal to B. A random seed value of 10 (in R) is used so the reader can reproduce
the results in the text:
> [Link](10)
> B <- 9999
> [Link] <- boot(Times, [Link], R=B)
> [Link]
Call:
boot(data = Times, statistic = [Link], R = B)
Bootstrap Statistics :
original bias std. error
t1* 7.8 -0.012494 1.215
Examine the graph (Figure 10.18) of t∗ . Note that the histogram and normal quantile-
quantile plot of t∗ indicate the distribution is slightly skewed to the right. Hence, there are
small differences between the confidence intervals created with the percentile method and
the basic bootstrap.
Next, use the function [Link]() on the object [Link] to create the four types of boot-
strapped confidence intervals:
CALL :
[Link]([Link] = [Link], type = c("norm", "basic", "perc", "bca"))
Intervals :
Level Normal Basic
95% ( 5.431, 10.194 ) ( 5.275, 10.050 )
476 Probability and Statistics with R
12
0.30
Sample Quantiles
10
0.20
Density
8
0.10
6
0.00
4
4 6 8 10 12 −4 −2 0 2 4
* Theoretical Quantiles
t
(c) To verify that these confidence intervals match the appropriate equations, start by setting
TO = tobs , Tstar = t∗ values, and BIAS = estimated bootstrapped bias from (10.36). A
95% confidence level matches the default confidence level for the function [Link]().
The normal confidence interval is calculated with (10.38). Note that the command sd()
should be replaced with stdev() if using S-PLUS.
> c( (T0-BIAS)-qnorm(1-alpha/2)*sd(Tstar),
+ (T0-BIAS)+qnorm(1-alpha/2)*sd(Tstar) )
[1] 5.4312 10.1938
The BCa confidence interval is figured with (10.47), including the bias factor z from (10.44),
the a value in (10.45), and the a1 and a2 values in (10.46):
Thus, all four intervals are verified to match the confidence intervals computed with
[Link]() by using their respective equations.
For the confidence intervals for the standard deviation, simply use [Link](). First
write the function [Link]() to compute standard deviation. Then, proceed with the
calculation as before.
> [Link]
Call:
boot(data = Times, statistic = [Link], R = B)
Bootstrap Statistics :
original bias std. error
t1* 7.8714 -0.22009 1.2813
Construct a histogram and quantile-quantile plot of the t∗ values. Note the slight negative
skew in the distribution of t∗ in Figure 10.19. This slight skewness will make the BCa
interval preferred over the other three types.
10
Sample Quantiles
0.20
Density
8
0.10
6
4
0.00
4 6 8 10 12 −4 −2 0 2 4
t* Theoretical Quantiles
CALL :
[Link]([Link] = [Link], type = c("norm", "basic", "perc", "bca"))
Intervals :
Level Normal Basic
95% ( 5.580, 10.603 ) ( 5.702, 10.659 )
Nonparametric Methods 479
Step 1: Choose a test statistic θ̂ that measures the effect under study. Note that certain
statistics will have more power to detect the effect of interest than others.
Step 2: Create the sampling distribution that the test statistic in step 1 would have if the
effect is not present in the population.
Step 3: Find the “observed test statistic” in the sampling distribution from step 2. Ob-
served values in the extremes of the sampling distribution suggest that the effect
under study is “real.” In contrast, observed values in the main body of the sampling
distribution imply that the effect is likely to have occurred by chance.
Step 4: Calculate the ℘-value based on the observed test statistic. This may be
P(|θ̂| ≥ |θ̂obs |)
P(θ̂ ≥ θ̂obs )
P(θ̂ ≤ θ̂obs )
Example 10.18 Permutation Test The data set used in this problem (Ratbp)
is originally from Ott and Mendenhall (1985, problem 8.17). Researchers wanted to know
whether a drug was able to reduce the blood pressure of rats. Twelve rats were chosen and
the drug was administered to six rats, the treatment group, chosen at random. The other
six rats, the control group, received a placebo. The drops in blood pressure (mmHg) for the
treatment group (with probability distribution F ) and the control group (with probability
distribution G) are stored in the variables Treat(z) and Cont(y), respectively. Note that
positive numbers indicate blood pressure decreased while negative numbers indicate that it
rose. Under the null hypothesis, H0 : F = G, the data come from a single population. The
question of interest is, “How likely are differences as extreme as those observed between the
treatment and control groups to be seen if the null hypothesis is correct?” Use θ̂ = z̄ − ȳ as
the statistic of interest and compute:
(a) The exact permutation ℘-value,
(b) An estimated permutation ℘-value based on 499 permutation replications, and
(c) An estimated bootstrap ℘-value based on 499 bootstrap replications.
Solution: The test statistic of interest (step 1) has been specified to be θ̂ = z̄ − ȳ. Finding
the ℘-values requires the creation of sampling distributions with different methods. First,
load the boot package and attach Ratbp. Combine the treatment and control data in a
single variable called Blood.
> library(boot)
> B <- 999
> attach(Ratbp)
> Ratbp
Treat Cont
1 69.0 9.0
2 24.0 12.0
3 63.0 36.0
4 87.5 77.5
5 77.5 -7.5
6 40.0 32.5
> Blood <- c(Treat, Cont)
Nonparametric Methods 481
This puts the mean of (69, 24, 87.5, 12, -7.5, 32.5) = 36.25 minus the mean of (63,
40, 9, 36, 77.5) = 50.5, which is −14.25 in Theta[751]. The for loop calculates
77.5,
all 12
6 = 924 values of θ̂ in a similar manner. [Link] is the actual observed value of
θ̂ = 60.167 − 26.583 = 33.583, the mean blood pressure drop of the treatment group minus
the mean blood pressure drop of the control group. The exact ℘-value is the number of
values in Theta greater than or equal to 33.583 divided by 924. This value is 0.031385, the
exact permutation ℘-value.
0.020
0.015
Density
θ^obs
0.010
0.005
0.000
θ^
(b) Estimated Permutation Method based on B=499 resamples of Blood without re-
placement. After assigning 499 to B, the function [Link]() is created to compute
the mean difference between the first six values and the last six values in a vector of
length twelve. Note that the object data is what will be resampled. To resample without
replacement, the argument sim="permutation" is used with boot(). The ℘-value is
computed according to (10.49) and the estimated permutation ℘-value based on B=499
permutation replications is 0.03. A histogram and a quantile-quantile plot of θ̂∗ = z̄ ∗ − ȳ ∗
when sampling without replacement are shown in Figure 10.21 on the facing page.
> [Link] <- function(data, i)
+ {
+ d <- data[i]
+ MD <-mean(d[1:6]) - mean(d[7:12])
+ MD
+ }
> [Link](13)
> B <- 499
> [Link] <- boot(Blood, [Link], R=B, sim="permutation")
> plot([Link])
> [Link] <- (sum([Link]$t >= [Link]$t0)+1)/(B+1)
> [Link]
[1] 0.03
(c) Estimated Bootstrap Method based on B=499 resamples of Blood with replacement.
After assigning 499 to B, the function [Link]() is used with boot() to create an
estimated bootstrap distribution based on B=499 resamples. Note that the argument
sim="ordinary" is used with boot() to sample with replacement. The ℘-value is computed
according to (10.49) and the estimated bootstrap ℘-value based on B=499 replications is
0.038. A histogram and a quantile-quantile plot of θ̂∗ = z̄ ∗ − ȳ ∗ when sampling with
replacement are shown in Figure 10.22 on the next page.
Nonparametric Methods 483
*
Histogram of θ^ Normal Q−Q Plot
40
0.020
Sample Quantiles
20
Density
θ^obs
0
0.010
−20
−40
0.000
−40 −20 0 20 40 −3 −2 −1 0 1 2 3
*
θ^ Theoretical Quantiles
FIGURE 10.21: Histogram and quantile-quantile plot of θ̂∗ = z̄ ∗ −ȳ ∗ when sampling without
replacement (permutation)
> [Link](13)
> B <- 499
> [Link].b <- boot(Blood, [Link], R=B, sim="ordinary")
> plot([Link].b)
> [Link].b <- (sum([Link].b$t >= [Link].b$t0)+1)/(B+1)
> [Link].b
[1] 0.038
*
Histogram of θ^ Normal Q−Q Plot
40
0.020
Sample Quantiles
20
Density
θ^obs
0.010
0
−20
−40
0.000
−40 −20 0 20 40 60 −3 −2 −1 0 1 2 3
*
θ^ Theoretical Quantiles
FIGURE 10.22: Histogram and quantile-quantile plot of θ̂∗ = z̄ ∗ − ȳ ∗ when sampling with
replacement (bootstrap)
484 Probability and Statistics with R
10.11 Problems
1. Provide a brief explanation of the pros and cons of using a nonparametric test.
2. What are the assumptions made with respect to the distribution from which the data
come when
3. When testing the median difference (ψD ) of two dependent samples, does the sign test
or the Wilcoxon signed-rank test have more power? For the recommended test, what
assumption(s) must be made?
Scores: 6 10 8 3 6 2 8 9 10 10 2
Service departments where the company has evidence that the median rating is more
than 7 will receive a bonus. Perform the appropriate hypothesis test to determine if this
service department should be awarded a bonus.
7. A Mendebaldea real estate agent claims Mendebaldea, Spain, has larger apartments than
those in San Jorge, Spain. A San Jorge real estate agent disputes this claim. To resolve
the issue, two random samples of the total area of several apartments (given in m2 ) are
taken from each community in 2002 and stored in the data frame AptSize.
8. To study the retained carbon of trees, a sample of 41 plots has been drawn in different
mountainous regions of Navarra (Spain). In these plots, the carbon retained by leaves
has been measured in kg/ha, depending on the forest classification: Areas with 90%
Nonparametric Methods 485
or more beech trees (Fagus Sylvatica) are labeled monospecific, while areas with many
species of trees are labeled multispecific. The data are stored in the data frame fagus.
Is there evidence that leaves from different forest classifications retain the same amount
of carbon?
SOURCE: Data come from Gobierno de Navarra and Gestión Ambiental de Viveros y
Repoblaciones de Navarra, 2006. The data were obtained within the European Project
FORSEE.
9. The R data set USJudgeRatings provides 43 lawyers’ ratings of state judges serving in
the U.S. Superior Court. Use help(USJudgeRatings) to obtain a detailed view of the
file. Suppose the variables integrity (INTG) and demeanor (DMNR) are chosen.
(a) Test whether lawyers are more likely to give a judge high integrity ratings rather
than high demeanor ratings.
(b) Find a confidence interval for the median difference (integrity−demeanor) with a
confidence level of at least 0.90.
10. A company manager is studying the possibility of giving 20 minutes of rest to her
employees in a resting room. To check the viability of this proposal, she analyzed 12
days of productivity where employees took 20 minutes of rest and 12 days where they
did not. The employee productivity scores are given in the following table where higher
scores represent greater productivity.
With Rest 9 8 8 7 6 7 8 9 7 7 7 6
Without Rest 7 9 5 6 7 3 9 9 4 5 6 4
Is there evidence to suggest that taking a rest produces an increase in median employee
productivity?
11. A Japanese company and an American company each claims that it has developed new
technology to increase network transmission speeds. The marketing managers of both
companies simultaneously announce that they can transmit 1 terabyte per second. To
substantiate their claims, each company submits trial data (in seconds) to transmit one
terabyte with the new technologies:
Is there evidence to suggest the transmission speed using the technology developed
by the American company is superior to the transmission speed using the technology
developed by the Japanese company? Compute the ℘-value to answer the question with
the following techniques:
(a) Enumerate all possible combinations with the function SRS() to find the ℘-value for
a permutation test.
(b) Use the function oneway test() from the coin package to calculate an appropriate
℘-value. Does this ℘-value match the one in part (a)?
(c) Obtain an estimated permutation ℘-value using the boot() function from the boot
package.
(d) What conclusion do the ℘-values support?
486 Probability and Statistics with R
12. The R data frame sleep shows the increase or decrease in hours of sleep for two groups
of patients when compared with a control group. Both groups were provided with a
different soporific drug. Is there evidence to suggest that one drug is superior (induces
more sleep) than the other drug?
13. In 1876, Charles Darwin had his book, The Effect of Cross and Self-Fertilization in
the Vegetable Kingdom, published. Darwin planted two seeds, one obtained by cross-
fertilization and the other by auto-fertilization in two opposite but separate locations of
a pot. Self-fertilization, also called autogamy or selfing, is the fertilization of a plant with
its own pollen. Cross-fertilization, or allogamy, is the fertilization with pollen of another
plant, usually of the same species. Darwin recorded the plants’ heights in inches. The
data frame Fertilize from the PASWR package contains the data from this experiment.
14. Salaries for graduates of three engineering universities ten years after graduation are
provided in the data frame Engineer of the PASWR package. Seventeen graduates were
randomly selected from each university, and their salaries in thousands of dollars were
recorded. Is there any evidence to suggest graduates earn different salaries based on the
university from which they graduated?
15. An engineering team is studying four different circuits that regulate the light intensity
of a conference room. An accelerated life test was used to estimate the lifetime of
each circuit. The results (lifetimes in thousands of hours) are stored in the data frame
CircuitDesigns and are
(a) Do an exploratory analysis of the data and decide if normality can be assumed.
(b) Choose an appropriate test to decide if there exist significant differences among the
circuit designs. Use α = 0.05.
16. The R data frame airquality shows daily air quality measurements in New York City,
NY, from May to September 1973.
(a) Attach the airquality data, and read the definition of the variables with the
command help(airquality). What does the symbol NA mean?
(b) Create a boxplot and a density plot of Month versus ozone as in Example 10.10 on
page 437. Do the graphs for each month exhibit similar shapes?
(c) Is it reasonable to assume that each month has a normally distributed ozone level?
(d) Is there evidence that differences exist in the ozone levels during the year?
17. The R data frame warpbreaks gives the number of warp breaks per loom, where a loom
corresponds to a fixed length of yarn.
Nonparametric Methods 487
(a) Attach the warpbreaks data and use the function xtabs() to create a contingency
table containing the number of warp breaks classified by wool and tension.
(b) Is there an association between wool type and tension level?
18. The music industry wants to know if the musical style on a CD influences how many
illegal copies of it are sold. To achieve this purpose, the company chooses six cities
randomly and writes down the number of illegal CDs available on the street categorized
by music type: classic music, flamenco, heavy-metal, and pop-rock. The data are shown
in the following table.
Musical Style
City Classical Flamenco Heavy-Metal Pop-Rock
City 1 4 1 6 9
City 2 3 4 5 10
City 3 2 1 8 14
City 4 5 3 2 7
City 5 2 3 6 14
City 6 9 1 2 6
(a) Use lattice/Trellis functions to create a box and whiskers plot and a density plot of
the number of illegal CDs available for each music style.
(b) Are the distribution shapes similar?
(c) Are there significant differences in the numbers of CDs available according to musical
style?
Bank
Province BBVA CM LC BS
Navarra 47 8 54 43
Álava 31 5 21 17
Guipuzcoa 64 4 43 43
Vizcaya 134 11 104 66
Is there evidence that an association exists between region and number of branches?
20. The data frame Depend from the PASWR package shows the number of dependent children
(number) for 50 families (count). Use a goodness-of-fit test to see if a Poisson distribution
with λ = 2 can reasonably be used to model the number of dependent children.
21. Is it reasonable to assume that the time variable from the data frame Phone in the PASWR
package follows an exponential distribution?
22. The data frame TestScores in the PASWR package gives the test grades of 29 students
taking a basic statistics course.
(a) Use the function EDA() on the data. Can normality be assumed?
488 Probability and Statistics with R
23. A government grant is funding a study to calculate how long it takes for the average
consumer to establish an Internet connection. A random sample of 20 Internet users’
connection times is collected. The connections times in seconds are 0.03, 0.48, 0.49, 0.52,
0.66, 0.69, 0.70, 0.76, 0.82, 1.20, 1.22, 1.39, 1.62, 1.85, 1.97, 2.25, 2.84, 3.44, 3.48, and
4.02.
24. Perform a simulation study to determine the power of both Kolmogorov-Smirnov’s and
Shapiro-Wilk’s normality tests.
(a) Set the seed equal to 897, and simulate m = 10, 000 samples of size n = 10, 20, 30,
and 40 from a χ21 distribution. Compute the simulated power for both tests using
α = 0.05.
(b) Set the seed equal to 897, and simulate m = 10, 000 samples of size n = 10, 20, 30,
and 40 from a Unif (0, 1) distribution. Compute the simulated power for both tests
using α = 0.05.
(c) Set the seed equal to 897, and simulate m = 10, 000 samples of size n = 10, 20, 30,
and 40 from a β(8, 3) distribution. Compute the simulated power for both tests
using α = 0.05.
(d) Set the seed equal to 897, and simulate m = 10, 000 samples of size n = 10, 20, 30,
and 40 from a N (0, 1) distribution. Compute the simulated power for both tests
using α = 0.05.
(e) Generalize your findings from (a) through (d).
25. The R data frame HairEyeColor contains classifications of 592 students by gender, hair
color, and eye color.
26. The sinking of the Titanic occurred on the 15th of April in 1912. The data frame
titanic3 contains information regarding class, gender, and survival as well as several
other variables.
27. Mental inpatients in the Virgen del Camino Hospital (Pamplona, Spain) are interviewed
by expert psychiatrists to diagnose their illnesses. An important aspect in diagnosis
is determining the severity of any delusions a patient might suffer. A new questioning
technique has been developed to detect the presence of delusions. The technique assigns
a score from 0 to 5, where 5 indicates the presence of strong delusions and a 0 indicates
no delusions. The psychiatrists wish to know if the new technique actually results in high
scores for patients who have previously been diagnosed as suffering from severe delusions.
The scores that follow were obtained from randomly selected patients who were known
to suffer from delusions and those who were known not to suffer with delusions:
Score
Delusions Present 5 5 4 5 4 5 5
Delusions Absent 1 0 5 0 4 4 0
Do the data provide evidence that the new test yields higher scores for those patients
who are known to suffer from delusions than for those who do not suffer from delusions?
28. It is believed by conservative psychiatrists that the use of illegal drugs can produce
persistent hallucinations, even after drug use stops. Some more liberal psychiatrists
dispute this assertion. The following data rate the severity of hallucinations suffered by
randomly selected mental inpatients from the Virgen del Camino Hospital (Pamplona,
Spain), where a 5 indicates severe hallucinations and a 0 indicates no hallucinations.
The patients are divided by whether or not they consumed illegal drugs before being
admitted to the hospital.
Score
Illegal Drugs Not Consumed 2 0 0 5 5 2 4 0
Illegal Drugs Consumed 0 4 5 5 4 5 5 2
Is there evidence that the severity of hallucinations in patients who have consumed illegal
drugs is greater than the severity of hallucinations in patients who have not consumed
illegal drugs?
29. Generate 10 values from a N (0, 1) distribution with the seed set at 10. Calculate a
bootstrap estimation of the standard error of X using B = 200 replications. Repeat the
experiment generating a sample of size 100 from the standard normal. What conclusions
can be drawn?
Chapter 11
Experimental Design
11.1 Introduction
This chapter deals with designed experiments where the experimenter follows a specific
protocol established before the experiment starts. This protocol should dictate how ran-
domization is performed and how measurements are taken. As a consequence of adhering
to an established protocol, designed experiments allow the user to make strong inferences
about the nature of observed differences.
Experiments are generally conducted to compare groups in terms of some response of
interest. The methods considered in this chapter assume the response variable is continuous.
The factors, independent variables whose levels are set by the experimenter, are categories
or continuous variables that have been categorized into a fixed number of discrete levels.
The treatments of an experiment are applied to experimental units, and measurements
on the response variable are taken where the objective of the experiment is to compare the
observed responses. When the combinations of the levels of two or more factors form the
treatments of interest, the experiment is known as a factorial design.
For example, an agricultural researcher may be interested in determining which of three
different fertilizers produces the greatest soybean yield. In this example, the three fertilizers
correspond to three treatments the experimenter wants to compare, and the three fertilizers
collectively constitute a factor. In the event a second factor, such as two different methods
of watering the soybeans, is of interest, the experiment will consists of 3 × 2 = 6 different
treatment combinations and is called a factorial design.
Suppose an agronomist is interested in determining which of three types of wheat
(Triticum aestivum, Triticum durum, or Triticum spelta) produces the greatest yield for
a particular geographical location. Available to the agronomist are six plots of equal size,
all in the same geographical area. In this setting, the factor of interest is wheat, and the
treatments are the three types of wheat. When the plots are homogeneous in their physical
characteristics, distinguishing differences in treatments becomes easier if differences exist.
Since there are likely to be some differences in the plots, the researcher will want to assign
the wheat types to the six plots randomly in order to minimize any possible bias due to plots.
By randomizing the assignment of treatments, the possibility of confounding differences due
to types with differences due to plots is minimized. When the assignment of treatments
is done in a completely random fashion, the design is known as a completely randomized
design (CRD). When experimental units are similar (homogeneous) with respect to some
characteristic, they can be grouped together into blocks. In the wheat study, some of the
plots may be exposed to more sun than other plots, or some plots may receive more water
than other plots. When the experimental units are more homogeneous within a block than
they are between blocks, treatments are assigned to experimental units within each block to
reduce variability. Such a design is known as a randomized complete block design (RCBD).
See Figures 11.1 and 11.2 on the next page for possible assignments of treatments for a
CRD and RCBD, respectively.
491
492 Probability and Statistics with R
C C A
B A B
Block 1 C A B
Block 2 B A C
Before proceeding further, some of the more important experimental design concepts
are defined:
• Treatments are levels of a factor or combinations of factor levels the experimenter
wants to compare.
• Experimental units are anything to which treatments are applied, for example,
animals, plots, plants, or people.
• Responses are outcomes observed after the application of a treatment to an experi-
mental unit.
• Experimental error is random variation present in the experiment not under the
control of the experimenter. Experimental error may be due to many things, including
but not limited to: measurement error, different responses from measuring the same
quantity in different trials, and different responses from experimental units given the
same treatment.
• Treatment structure specifies the set of factors the experimenter has selected to
study or compare.
• Design structure defines how experimental units are assigned to treatment groups.
• Randomization is the use of some well-defined probabilistic mechanism to assign
treatments to experimental units. Randomization reduces the possibility of bias and
confounding. Randomization should also be used, if possible, with any variable not
under the direct control of the experimenter that may influence the measured response.
Experimental Design 493
When using statistical models, it is important to keep in mind that a model is simply
a mathematical expression of how the researcher believes the response is explained using
the independent variables of the experiment (predictors). Models are expressed in S with
the syntax response ∼ predictors, where ∼ means that the response is modeled by the
predictors. There may be several plausible models for a particular experiment. Finding
an adequate model is an iterative process that starts by:
1. Identifying an appropriate model based on the treatment and design structure of the
experiment.
2. Validating the model’s assumptions using diagnostic plots.
3. Selecting a different model or transforming the response variable when the model’s
assumptions are not satisfied until a plausible model is found.
Once a model has been validated, formal inference to test for no treatment effects (equality
of treatment means) and estimation of the model’s parameters can be undertaken. In the
event formal inference suggests differences in treatments, multiple comparisons are used to
determine which treatments are significantly different from one another.
Run Treatment
1 1 A
2 2 D
3 3 B
4 4 C
5 5 B
In particular, this randomization would assign tire tread A to the first run, D to the second
run, and so on.
It is always a good idea to examine experimental data graphically before initiating any
formal inferential procedure. Side-by-side boxplots are often a good starting point when
comparing several treatments. When the number of observations in each treatment group
is relatively small, dotplots will often prove more helpful than boxplots. The function
[Link]() from the PASWR package is used to create Figure 11.3.
440
440
A
420
420
B C
D
StopDist
StopDist
B
400
400
C
380
380
A
D
360
360
360 380 400 420 440 fac1
A B C D
StopDist
fac1 Factors
From the boxplots and dotplots shown in Figure 11.3, it appears that there are differences
in stopping distances based on different tire treads. At this point, it would be nice to
formalize the last sentencewith
aninferential procedure. It is tempting to many to perform
pairwise t-tests on all six 42 = 6 of the pairwise differences; however, this should not be
done! If the probability of correctly accepting the null hypothesis is 1 − α = 0.90, then
the probability of correctly accepting the null hypothesis for all six pairwise tests assuming
independence among tests would be (.90)6 = 0.7350919. The type I error rate is not 10%
but 26.5% in this case. Of course, the more treatments that are compared, the more likely
one is to make a type I error. What would the type I error rate be if the individual error rate
for a single comparison is 5% and seven treatments were compared? (Answer: 0.66) The
appropriate procedure for testing the equality of several means is the analysis of variance,
which is introduced in the context of a completely randomized design.
Completely Randomized Desgin The simplest randomized design for comparing sev-
eral treatments is the completely randomized design (CRD). CRDs have a ≥ 2 treatments
to compare and N experimental units. Each treatment is applied to ni (i = 1, 2, . . . , a)
experimental units, where n1 + n2 + · · · + na = N . In order to conduct the experiment,
the researcher randomly assigns treatments to the experimental units (design structure).
Although the sizes of the a samples need not be identical, the power of the test is maximized
Experimental Design 495
Table 11.2: Model, parameters, and estimators for fixed effects, one-way CRD
Model Parameter Estimator
Yij = μ + τi + ij μ Y ••
μi Y i•
τi Y i• − Y ••
εij Yij − Y i•
a ni
(Yij − Y i• )2
σ2
i=1 j=1
N −a
Although estimating the parameters for model (11.1) is important, the goal of the
experimenter is generally to discern whether or not the a treatment means are equal, and
if they are not equal, which treatments are better (for example, have a higher mean).
Specifically, the null hypotheses of interest are
H0 : μ1 = μ2 = · · · = μa versus H1 : μi = μj for some (i, j).
When the null hypothesis is true, all treatments have a common mean μ and an equivalent
statement of the null hypothesis can be written in terms of the treatment effects as
H0 : τ1 = τ2 = · · · = τa = 0 versus H1 : τi = 0 for some i.
Experimental Design 497
Consequently, testing the equality of treatment means is equivalent to testing that the
treatment effects are all zero. As mentioned earlier, the appropriate procedure for testing
the null hypothesis of equal treatment means is the analysis of variance, which is simply a
decomposition of the total variability into its component parts, which is shown next.
which partitions the deviation of any observation from the grand mean into two parts. The
first part, (Y i• − Y •• ), is the deviation of the ith treatment mean from the grand mean.
The second part is the deviation of the observation from the ith treatment mean. Squaring
and summing both sides of (11.2) produces
a ni a ni
2
(Yij − Y •• )2 = (Y i• − Y •• ) + (Yij − Y i• )
i=1 j=1 i=1 j=1
a a ni
= ni (Y i• − Y •• )2 + (Yij − Y i• )2
i=1 i=1 j=1
a ni
+2 (Y i• − Y •• )(Yij − Y i• ) (11.3)
i=1 j=1
Consequently,
a ni a a ni
(Yij − Y •• ) =2
ni (Y i• − Y •• ) +2
(Yij − Y i• )2 (11.4)
i=1 j=1 i=1 i=1 j=1
which
a says the total2 variability in the data can be partitioned into two parts. The quantity
i=1 ni (Y i• −Y •• ) measures the difference between the observed treatment means and the
grand mean. Specifically, it is a measure of variability due to the treatments
a niand is denoted2
SS Treatment (sum of squares due to treatments). The quantity i=1 j=1 (Yij − Y i• )
measures the differences of observations within a treatment from the treatment mean, which
must be due to error and is referred to as SS Error (sum of squares due to error). The quantity
on the left-hand side of the equals sign in (11.4) is called the total sum of squares corrected
for the mean and is denoted SS Total . The symbolic representation of (11.4) is
has a − 1 degrees of freedom since there are a treatment means and SS Error has N − a
degrees of freedom. To adjust for the number of treatments, SS Treatment is divided by its
degrees of freedom, a−1. The resulting quantity is known as the mean square treatment
MS Treatment = SSdfTreatment and is also called the between treatments error variance. In order
Treatment
Combining the sample variances, a single estimate of the population variance emerges as
a %ni (
j=1 (Yij − Y i• )
2
(n1 − 1)S12 + (n2 − 1)S22 + · · · + (na − 1)Sa2 i=1
= a
(n1 − 1) + (n2 − 1) + · · · + (na − 1) i=1 (ni − 1)
SS Error
= = MS Error
N −a
The pooled estimate of the variance from the two-sample t-test in Section 9.7.4 has now
been generalized for a different samples.
If there are no differences among the a treatment means, MS Treatment is an unbiased
estimate of σ 2 , and the ratio of MS Treatment/MS Error will be close to 1. If differences
actually exist among the a treatment means, then the ratio, MS Treatment /MS Error should
be larger than 1. In fact, it can be shown that
a
ni τi2
E(MS Error ) = σ 2 and E(MS Treatment) = σ 2 +
i=1
a−1
implying that when H0 is false, E(MS Treatment ) > E(MS Error ) since some τi = 0. When
H0 is true, τi = 0 for all i and E(MS Treatment ) = E(MS Error ) = σ 2 . With a little effort, it
can be shown that
MS Error χ2dfError χ2
∼ = N −a
σ 2 dfError N −a
regardless of whether H0 is true or not, and that
MS Treatment χ2dfTreatment χ2a−1
∼ =
σ2 dfTreatment a−1
when H0 is true independently of MS Error . Consequently, using Definition 6.2 on page 238,
when H0 is true, the ratio MS Treatment /MS Error ∼ Fa−1; N −a . Thus, H0 is rejected in an
α-level test if Fobs > f1−α; a−1, N −a , where Fobs = MS Treatment/MS Error . The S function
aov() used with summary() returns a table similar to Table 11.3 on the facing page.
Example 11.1 Tire ANOVA Table Use the data frame Tire and compute the
values for the ANOVA table using both the formulas and the S function summary(aov())
to test the null hypothesis that all the tire treads have identical mean stopping distances
versus the alternative hypothesis that there is at least one mean that is different.
Experimental Design 499
SS Error = MS Error =
Error N −a a ni
(Yij − Y i• )2 SS Error
i=1 j=1 N −a
SS Total =
Total N −1 a ni
(Yij − Y •• )2
i=1 j=1
a
SS Treatment = ni (Y i• − Y •• )2
i=1
= 6 · (379.6667 − 404.2083)2 + 6 · (405.1667 − 404.2083)2
+ 6 · (421.6667 − 404.2083)2 + 6 · (410.3333 − 404.2083)2 = 5673.12
a ni
SS Total = (Yij − Y •• )2
i=1 j=1
The ℘-value for the test is P(F3, 30 ≥ 5.33) = 0.007. Based on the small ℘-value, the null
hypothesis of no tire tread effect (τi = 0 for all i) is rejected. This suggests at least one
tire tread effect is not zero. Thus, the question then becomes, “Which tire tread has the
shortest mean stopping distance?” The statistical conclusion as well as the validity of any
500 Probability and Statistics with R
multiple comparison procedures used to detect individual differences between tire treads
assume the one-way model (11.1) is sound. Checking model assumptions for the one-way
CRD is discussed in Section 11.5, followed by multiple comparison procedures in Section
11.7.
The values for the ANOVA Table can be computed with S by entering
> attach(Tire)
> TreatmentMean <- tapply(StopDist, tire, mean)
> TreatmentMean
A B C D
379.6667 405.1667 421.6667 410.3333
> a <- length(TreatmentMean)
> N <- length(StopDist)
> dft <- a - 1
> dfe <- N - a
> GrandMean <- mean(StopDist)
> GrandMean
[1] 404.2083
> SStreat <- 6*sum((TreatmentMean - GrandMean)^2)
> SStreat
[1] 5673.125
> SStotal <- sum((StopDist - GrandMean)^2)
> SStotal
[1] 12771.96
> SSerror <- SStotal - SStreat
> SSerror
[1] 7098.833
> MStreat <- SStreat/dft
> MStreat
[1] 1891.042
> MSerror <- SSerror/dfe
> MSerror
[1] 354.9417
> Fobs <- MStreat/MSerror
> Fobs
[1] 5.327753
> pvalue <- 1-pf(Fobs, 3, 20)
> pvalue
[1] 0.007315521
Experimental Design 501
> summary(aov(StopDist~tire))
Df Sum Sq Mean Sq F value Pr(>F)
tire 3 5673.1 1891.0 5.3278 0.007316 **
Residuals 20 7098.8 354.9
---
Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
The treatment means and the grand mean can also be computed using the function
[Link]():
404.2083
tire
tire
A B C D
379.7 405.2 421.7 410.3
> detach(Tire)
The reader should verify that the right-hand side of (11.8) is the same as the expression
on
nthe left of the ≡. Two facts that should be kept 2 inmind during the verification are
i ni
Y
j=1 ij = Y n
i• i for i = 1, 2 and (n 1 + n 2 )Y •• = i=1 j=1 ij . Rewriting the right side
Y
of (11.8) gives
⎡ ⎤2
( Y 1• − Y 2• )2 ( Y − Y )
F = & ' =⎣ 2 1• 2• ⎦
= [t]2 . (11.9)
2 1 1 1 1
Sp n1 + n2 Sp n1 + n2
One verifies that the pooled t-test from Section 9.7 is simply a special case of the F -test used
in ANOVA when a = 2. It is important to emphasize that the equivalence of the pooled
502 Probability and Statistics with R
t-test and the F -test used in ANOVA for a = 2 groups applies only to the non-directional
hypothesis H1 : μ1 = μ2 because
2
t1−α/2; dfError = f1−α; 1, dfError , but (11.10)
2
[t1−α; dfError ] = f1−α; 1, dfError , as would be required for a directional hypothesis. In Section
9.7, the non-centrality parameter γ for the pooled t-test was defined as
μ1 (X, Y ) − μ0 (X, Y )
γ= .
σX−Y
and (11.11). Specifically, the quantity in (11.12) is used to measure the statistical differences
between the sample means. In a similar fashion, (11.11) is used to measure the statistical
differences between the population means. Rewriting (11.12) and (11.11), one notes
& '−1
( Y 1• − Y 2• )2 1
n1 + 1
n2 MS Treatment
F = t2 = =
Sp2 MS Error
and
& '−1
(μ1 − μ2 )2 1
n1 + 1
n2 SS Hypothesis (population)
λ = γ2 = =
σ2 σ2
where SS Hypothesis (population) is the sum of squares for treatments obtained by replacing
Y 1• with μ1 , Y 2• with μ2 , and Y •• with n1 μn11 +n2 μ2
+n2 . By defining the non-centrality parameter
λ as the ratio of SS Hypothesis (population) to σ 2 , it becomes easy to calculate λ using
statistical software. The SS Hypothesis (population) will always be the sum of squares formula
for the H0 being tested, thus this method of computing λ extends to whatever hypothesis
the user would like to test. It is not limited merely to the equality of treatment means.
a
i=1 ni (μi• − μ̄•• ) ,
2
For any completely
a randomizeda design, SS Hypothesis (population) =
where μ̄•• = ( i=1 ni μi• ) / ( i=1 ni ). Recall that power is the probability that the null
hypothesis will be rejected when it is false. In this case,
Power (λ) = P [Fa−1; N −a, λ > f1−α; a−1; N −a; λ=0 ] . (11.13)
Power (λ) is maximized when all a groups have an equal number of observations; how-
ever, using SS Hypothesis to compute the non-centrality parameter adjusts for experiments
with different sample sizes. R has the function [Link](), which can be used
to determine the sample size for the a samples when resources are allocated such that each
group has the same size.
Experimental Design 503
Example 11.2 Tires’ Stopping Distance Suppose the tire manufacturer be-
lieves the true mean stopping distance for tread patterns A, B, C, and D to be 390, 405,
415, and 410 feet, respectively, with a common standard deviation that could be as high as
20 feet or as small as 10 feet. Assume sets of tires are put on the car (a single car is used
for all tests to reduce variability) in random order.
(a) Suppose the manufacturer wants to test H0 : μB − μA = 0 versus H1 : μB − μA > 0
using α = 0.05, assuming σ = 10. Determine the power of the test if six sets of tires
with each tread are available.
(b) Determine the probability that differences among the means will be detected using
α = 0.05 assuming σ = 20 feet if six sets of tires with each tread are available. Simulate
the non-central F distribution and compute the power by simulation. How does the
simulation compare to the theoretical answer?
(c) Determine the probability that differences among the means will be detected using
α = 0.05 if six sets of tires with each tread are available and assuming σ = 10 feet.
(d) Assuming the stopping distance standard deviation for all tire sets is σ = 20 feet, what
is the minimum number of tire sets that need to be used to ensure the probability of
detecting tire tread differences is at least 80%?
(e) Given 6 sets of tires with tread A, 6 sets of tires with tread B, 12 sets of tires with
tread C, and 12 sets of tires with tread D, what is the probability of detecting tire tread
differences if the true stopping standard deviation for all tire tread sets is σ = 14 feet?
Solution: The answers are as follows:
(a)
a
i=1 ni (μi• − μ̄•• ) 6 · (405 − 397.5)2 + 6 · (390 − 397.5)2
2
SS Hypothesis
λ= 2
= 2
= = 6.75
σ σ 102
√
Power (γ = λ = 2.598) = P(t10; γ=2.598 > t0.95; 10 = 1.81) = 0.78. This can be computed
with R using the following code:
n = 6
delta = 15
sd = 10
[Link] = 0.05
power = 0.7798662
alternative = [Link]
Note that the alternative hypothesis is directional and the F distribution cannot be used to
answer the question. The answer is obtained with a non-central t-distribution. A graphical
representation of the power is given in Figure 11.4.
t10 t0.95; 10
t10;γ=2.6
Power (γ = 2.6)
−4 −2 0 2 4 6 8
FIGURE 11.4: Power for the directional alternative hypothesis H1 : μB − μA > 0 when
γ = 2.6 at the α = 0.05 level
> #####################################################################
> [Link] <- sum(Fobs > qf(1 - alpha, [Link], [Link]))/sims
> [Link]
[1] 0.3984
0.10
0.05
0.00
0 2 4 6 8 10 12
Fobs
FIGURE 11.5: Histogram of simulated F3, 20; λ=5.25 superimposed by the theoretical
distribution
a
i=1 ni (μi• − μ̄•• )
2
SS Hypothesis
λ= =
σ2 σ2
6 · (390 − 405) + 6 · (405 − 405)2 + 6 · (415 − 405)2 + 6 · (410 − 405)2
2
2100
= = = 5.25
202 202
Power (λ = 5.25) = P(F3, 20; λ=5.25 > f0.95; 3, 20 = 3.098) = 0.386.
Using S:
F3, 20
Power (λ = 5.25)
f0.95; 3, 20
0 2 4 6 8 10 12
FIGURE 11.6: Power for detecting treatment differences when λ = 5.25 at the α = 0.05
level
Since sample sizes are equal in the a groups, the R function [Link]() can be
used to solve the problem:
groups = 4
n = 6
[Link] = 116.6667
[Link] = 400
[Link] = 0.05
power = 0.3862415
(c)
SSHypothesis
λ= 2
a σ
ni (μi• − μ̄•• )2
= i=1
σ2
6 · (390 − 405)2 + 6 · (405 − 405)2 + 6 · (415 − 405)2 + 6 · (410 − 405)2
=
20
2100
= = 21
102
Power (λ = 21) = P(F3, 20; λ=21 > f0.95; 3, 20 = 3.098) = 0.95.
Using S:
Again, because treatment groups have equal ns, an answer is possible using R’s function
[Link]():
> SD <- 10
> [Link](groups=a, n=n, [Link]=var(MEANS), [Link]=SD^2)
groups = 4
n = 6
[Link] = 116.6667
[Link] = 100
[Link] = 0.05
power = 0.9501649
(d) Since λ is a function of sample size, one solution is to find n such that P(Fa−1, a·n−a,λ >
f0.95, a−1, a·n−a ) ≥ 0.80. The following code uses a loop to find the value of n such that
Experimental Design 509
the power is at least 80%. Power is maximized with a total of N sets when each of the a
treatments receives n sets of tires such that N = a · n. That is, power is maximized with
equal treatment sizes.
> SD <- 20
> Powerr <- 0
> nr <- 1
> MEANS <- c(390, 405, 415, 410)
> a <- length(MEANS)
> while(Powerr < .80)
+ {
+ nr <- nr + 1
+ Nr <- a*nr
+ alpha <- .05
+ Yr <- rep(MEANS, rep(nr, a))
+ treatr <- factor(rep(1:a, rep(nr, a)))
+ SStreatr <- summary(aov(Yr~treatr))[[1]][1, 2] # R
+ # SStreatr <- summary(aov(Yr~treatr))[1, 2] # S-PLUS
+ lambdar <- SStreatr/SD^2
+ cvr <- qf(1 - alpha, a - 1, Nr - a)
+ Powerr <- 1 - pf(cvr, a - 1, Nr - a, ncp=lambdar)
+ }
> c(nr, lambdar, Powerr)
[1] 14.0000000 12.2500000 0.8176811
From the output, note that when n = 14, λ = 12.25, which returns a power of 81.7%. Since
the problem permits equal n per treatment group, the R function [Link]() can
also be used:
groups = 4
n = 13.47806
[Link] = 116.6667
[Link] = 400
[Link] = 0.05
power = 0.8
where the τi s are fixed but unknown numbers and the εij s are independent normals with a
mean of zero and constant variance. Consequently, the three basic assumptions concerning
the errors:
1) independence,
3) constant variance
should be investigated. Since the actual errors are unknown quantities, they will never be
observed; however, it is possible to use estimates (or predictors) of the errors, the residuals.
Recall from Chapter 2 that a residual is the difference between what is observed and what
is predicted (ε̂ij = Yij − Y:ij ). For model (11.14), Y:ij = Y •• + Y i• − Y •• = Y i• . While
(11.14) may be a reasonable approximation to some real-life phenomena, real-life data are
never exactly normal. The real question is whether the assumptions have been violated
to such an extent that the inferences based on the particular model in question would be
invalidated. Although a few formal tests are presented, most of the material that follows
deals with visual diagnostics for the three basic assumptions concerning errors.
The most important assumption for (11.14) to be valid and the most challenging as-
sumption to correct if it fails is the assumption of independence. The material in this text
will not address how to deal with dependent data, which is the topic of a more advanced
Experimental Design 511
course. One of the easier dependencies to detect is a dependence in time. When values are
either very similar (positive dependence) or very different (negative dependence) to each
other in time, the assumption of independence becomes untenable. An easy way visually
to inspect data for dependence is to plot the residuals on the vertical axis versus a time
sequence on the horizontal axis. Naturally, if there is no time component to the data, this
graph will not reveal any useful information not found in other residual plots.
It is often helpful to standardize the residuals so they have unit variance. Many books
define standardized residuals as
ε̂ij
rij = √ ,
MS Error
√
however, the standard deviation of the ij th residual is actually σ · 1 − hii , where the
hii s are the diagonal elements of the hat matrix (discussed in more detail in Chapter 12:
Regression).
√ For model (11.14), the hii values are simply 1/ni . By estimating σ with the
MS Error , the standardized residuals (rij ) are computed as
ε̂ij ε̂ij
rij = 2 = √ √ (11.15)
+ ij )
Var(ε̂ MS Error · 1 − hii
The function stdres() in the MASS package computes standardized residuals according to
(11.15).
Modifications to the following code used with the Tire data set from the motivational
problem (Example 11.1) at the beginning of the chapter can be used to help the user assess
the assumption of independence among the errors. Based on Figure 11.7 on the next page,
no discernible pattern is seen that might threaten the assumption of independent errors.
> attach(Tire)
> par(pty="s")
> [Link] <- aov(StopDist~tire)
> library(MASS)
> r <- stdres([Link])
> n <- length(StopDist)
> plot(1:n, r, ylab="Standardized Residual", xlab="Ordered Value")
> detach(Tire)
The quantile-quantile plot is a graphical procedure for assessing normality. The quantile-
quantile plot can be performed on either the residuals or the standardized residuals. If
standardized residuals are used, the plotted observations should follow a straight line with
an intercept of zero and a slope of one.
a Reading quantile-quantile plots, especially when
the total number of residuals (N = i=1 ni ) is small, requires a high degree of skill. A
formal test of normality can be obtained with the function [Link](). Modifications
to the following code used with the Tire data set from the motivational problem (Example
11.1) can be used to help the user assess the normality of errors assumption. Figure 11.8 on
page 513 shows a quantile-quantile plot of the standardized residuals with a superimposed
line with a zero intercept and a slope of one indicating the assumption of normal errors
is reasonable. The ℘-value (0.7584) from the Shapiro-Wilk normality test provides further
corroboration that the normality assumption of the errors is reasonable.
512 Probability and Statistics with R
2
Standardized Residual
1
0
−1
−2 5 10 15 20
Ordered Value
FIGURE 11.7: Standardized residuals versus order for [Link] using the Tire data set
> attach(Tire)
> [Link] <- aov(StopDist~tire)
> library(MASS)
> r <- stdres([Link])
> par(pty="s")
> qqnorm(r)
> abline(a=0, b=1)
> [Link](r)
data: r
W = 0.9737, p-value = 0.7584
> detach(Tire)
2
1
Sample Quantiles
0
−1
−2
−2 −1 0 1 2
Theoretical Quantiles
particular modification to Levene’s test, which uses the absolute deviations from the group
medians, is relatively insensitive to non-normality and is easily implemented with S. It is
also a preprogrammed function [Link]() in the car package. Modifications to the
following code used with the Tire data set from Example 11.1 can be used to help the
user assess the homogeneity of variance with respect to the errors assumption. Figure 11.9
on the following page shows a plot of the standardized residuals versus the fitted values of
(11.14), indicating that there are no serious departures in homogeneity of variance. The
fitted values Y:ij of an aov object can be obtained by using the fitted() on an aov
object. The ℘-value (0.4224) from the modified Levene test provides further corroboration
that the homogeneity of variance assumption of the errors is reasonable.
> attach(Tire)
> [Link] <- aov(StopDist~tire)
> library(MASS)
> r <- stdres([Link])
> tm <- fitted([Link])
> plot(tm, r, xlab="Fitted Value", ylab="Standardized Residual")
> med <- tapply(StopDist, tire, median)
> ZIJ <- abs(StopDist - med[tire])
> summary(aov(ZIJ~tire))
Df Sum Sq Mean Sq F value Pr(>F)
tire 3 388.79 129.60 0.9789 0.4224
Residuals 20 2647.83 132.39
> [Link]([Link])
> detach(Tire)
The function [Link]() from the PASWR package creates the three graphs dis-
cussed in Sections 11.5.1, 11.5.2, and 11.5.3 that assess independence, normality, and
constant variance, respectively. The graphs from using [Link]() with [Link]
are shown in Figure 11.10 on the next page.
514 Probability and Statistics with R
2
Standardized Residual
1
0
−1
−2
380 390 400 410 420
Fitted Value
FIGURE 11.9: Plot of the standardized residuals versus the fitted values for [Link] using
the Tire data set
Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus
ordered values for [Link] residuals from [Link] fitted values for [Link]
3 22 3 22 3 22
2
2
standardized residuals
standardized residuals
standardized residuals
1
1
0
0
−1
−1
−1
−2
−2
−2
20 20 20
the chosen model and needs to be evaluated closely. The balance of fixing problems will
center on how to deal with 1) non-normal errors and 2) non-constant variance.
11.6.1 Non-Normality
When a quantile-quantile plot of the residuals indicates skewness (typically to the right)
a transformation on the response variable will often alleviate the problem of non-normal
errors. Finding a meaningful and appropriate transformation is often challenging. One
technique that searches computationally for an appropriate transformation of the response
variable that directly addresses normality is the Box-Cox method. The Box-Cox method
estimates the parameter λ for the transformation Y = Y λ , where
⎧ λ
⎨Y − 1
for λ = 0,
Y = λ (11.16)
⎩ln Y for λ = 0,
by the method of maximum likelihood. Figure 11.11 shows transformations in common use.
The function boxcox() of the MASS package produces a plot of the log-likelihood against the
10
λ=2
8
Transformed Data
λ=1
4
λ = 0.5
λ=0
2
λ = −1
0
0 2 4 6 8 10
Original Data
FIGURE 11.11: Transformations in common use with the Box-Cox method: The long
dashed line shows data transformed by squaring; the solid, by doing nothing; the dot-dashed,
by taking the square root; the dotted, by taking the natural log; and the dashed, by taking
the reciprocal
that maximizes the log-likelihood function may turn out to be 0.53; but if there is a possible
explanation for taking the square root of the response, the transformation applied should
be λ = 0.5 and not the value that maximizes the log-likelihood function.
Observations that do not fit the pattern of the rest of the data in the quantile-quantile
plot (outliers) can distort an analysis, and one should consider removing the outlier(s)
and performing the analysis without the offending point(s). Oftentimes, outliers are simply
poorly transcribed experimental results such as an incorrectly placed decimal or a misplaced
label. However, just because a value is an outlier does not mean it should be eliminated
from the data; rather, outliers imply that the model being used is incorrect. Does this
suggest that if the values in a quantile-quantile plot are not exactly linear, then there are
problems? Fortunately not! With equal treatment sizes, the F -test used with ANOVA is
quite robust to non-normal errors when the homogeneity of variance assumption is satisfied.
The reader should perform their own simulations to verify that sampling distribution for
MS Treatment/MS Error when sampling from non-normal distributions is quite close to the
F distribution. Unfortunately, subsequent inference on individual parameters using one-
sided confidence intervals is sensitive to the normality assumption and can result in poor
conclusions when the errors do not follow a normal distribution.
where
3% & '(2 4
a
a 3 1 − wi / a /(n i − 1)
i=1 i=1 wi
ni i=1 wi Y i
wi = , YE = a , and Λ = .
s2i i=1 wi a2 − 1
Example 11.3 Fat Cats In a weight loss study on obese cats, overweight cats
were randomly assigned to one of three groups and boarded in a kennel. In each of the three
groups, the cats’ total caloric intake was strictly controlled (1 cup of generic cat food) and
monitored for 10 days. The difference between the groups was that group A was given 1/4
of a cup of cat food every 6 hours, group B was given 1/3 a cup of cat food every 8 hours,
and group C was given 1/2 a cup of cat food every 12 hours. The weights of the cats at
the beginning and end of the study were recorded and the differences in weights (grams)
are stored in the variable Weight of the data frame FCD. Are there mean weight differences
among the three treatments?
Experimental Design 517
Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus
ordered values for [Link] residuals from [Link] fitted values for [Link]
3
3
32 32 32
22 22 22
2
2
standardized residuals
standardized residuals
standardized residuals
1
1
0
0
−1
−1
−1
−2
−2
−2
28 28 28
−3
−3
−3
> attach(FCD)
> ni <- tapply(Weight, Diet, length)
> a <- length(ni)
> si2 <- tapply(Weight, Diet, var)
> wi <- ni/si2
> yb <- tapply(Weight, Diet, mean)
> ytild <- sum(wi*yb)/sum(wi)
> wlamb <- 3*sum((1 - (wi/sum(wi)))^2 / (ni - 1) )/(a^2 - 1)
> dfn <- (a - 1)
> dfd <- 1/wlamb
> W <- sum(wi*(yb - ytild)^2/(3 - 1)) / (1 + 2/3*(3 - 2)*wlamb)
> W
[1] 1.451544
518 Probability and Statistics with R
−188
−189
log−Likelihood
95%
−190
−191
−192
−2 −1 0 1 2
FIGURE 11.13: Box-Cox transformation graph for the model [Link] (aov(Weight ∼
Diet)) with the FCD data frame
Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus
ordered values for [Link] residuals from [Link] fitted values for [Link]
3
3
32 32 32
22 22 22
2
2
standardized residuals
standardized residuals
standardized residuals
1
1
0
0
−1
−1
−1
−2
−2
−2
−3
−3
28 28 −3 28
> detach(FCD)
how they differ. To do this, several tests are required; however, as noted earlier, repeated
application of a test drastically increases type I errors.
Suppose a set of K null hypotheses H01 , H02 , . . . , H0K are to be tested where the overall
hypothesis H0 is true if all of the H0i s for i = 1, 2, . . . , K are true:
H0 : H01 ∩ H02 ∩ · · · ∩ H0K (11.18)
Note that H0 is rejected if any of the H0i s is rejected. The comparison-wise error rate is
the probability of rejecting a particular H0i in a single test when H0i is true. Controlling the
comparison-wise error rate at the αc level means that the expected proportion of individual
tests that reject H0i when H0i is true is αc . This is the only error rate considered thus far
and has previously been denoted as merely α. It is simply the risk one is willing to take of
making a type I error in a single test. In contrast to the comparison-wise error rate, the
experiment-wise error rate is the probability of rejecting at least one of the H0i s in a
series of tests when all of the H0i s are true, and is denoted αe . It is the risk of making
at least one type I error among the family of comparisons in (11.18). The experiment-wise
error rate, αe , can be evaluated for a family of independent tests. Although a set of tests
that might be of interest, such as all pairwise differences of a means, are not independent
tests, an upper limiton αe can be established by assuming the tests are independent. There
are a total of ma = a2 = a(a − 1)/2 tests needed to evaluate all pairwise differences among
a means.
The probability of a type I error for any single test is αc and the probability of a correct
decision is 1 − αc . If it is assumed that the ma tests are independent, then the random
variable X = number of type I errors has a binomial distribution:
X ∼ Bin(n = ma , π = αc ).
Since αe is the probability of making at least one type I error in the family of tests (ma ),
ma 0
αe = P(X ≥ 1) = 1 − P(X = 0) = 1 − αc (1 − αc )ma = 1 − (1 − αc )ma
0
Glancing at Table 11.5, one sees very clearly that for fixed αc , as K increases, αe tends
to 1. In other words, the probability of making at least one type I error in a series of tests
approaches 1 as the number of tests increases. Consequently, multiple comparisons will
generally attempt to control αe , the experiment-wise error rate. To obtain a rough idea
of the value of αe , one can use the Bonferroni inequality αe ≤ K · αc . Likewise, a rough
estimate of αc is αe /K .
of interest. This procedure, despite its appearance, controls neither αc nor αe and is not
a recommended testing procedure. It is included here for pedagogical reasons only. For
pairwise comparisons, group means are considered different if
9
1 1
Y i• − Y j• > t1− αc ; df · MS Error · + (11.19)
Error
2
ni nj
F GH I
LSD
The (1 − αc ) · 100% confidence interval on the difference of means based on the LSD is
# 9
1 1
CI 1−αc (μi − μj ) = (Y i• − Y j• ) − t1−αc /2; dfError MS Error + ,
ni nj
9 ⎤ (11.20)
1 1 ⎦
(Y i• − Y j• ) + t1−αc /2; dfError MS Error +
ni nj
When the number of comparisons is small (K ≤ 5), the problem of an increasing αe for
using Fisher’s LSD can be addressed with the Bonferroni method.
The Bonferroni method divides αc by the total number (K) of comparisons. Means are
considered different if the difference of sample means is greater than Bonferroni’s significant
difference (BSD):
9
1 1
Y i• − Y j• > t1− αc ; dfError · MS Error · + (11.21)
2·K ni nj
F GH I
BSD
The (1 − αe ) · 100% confidence interval on the difference of means based on the BSD is
# 9
1 1
CI 1−αe (μi − μj ) = (Y i• − Y j• ) − t1− 2K
αc
; dfError MS Error + ,
ni nj
9 ⎤ (11.22)
1 1 ⎦
(Y i• − Y j• ) + t1− 2K
αc
; dfError MS Error +
ni nj
The experiment-wise error rate using αc /K can be much less than αe , thus this method is
very conservative and has correspondingly low power.
Y max − Y min
Q= √ (11.23)
σ̂/ n
Experimental Design 521
The distribution of Q depends on the number of treatments (a) and the degrees of freedom
for σ̂ (MS Error ), denoted by ν. In the one-way CRD, dfError = N −a. The notation q1−α; a, ν
denotes the studentized range value with 1 − α area to the left with a and ν degrees of
freedom, respectively. The S function qtukey() returns values from the studentized range
distribution. For example, q0.95; 4, 20 = 3.958 is obtained by entering qtukey(0.95, 4,
20).
The HSD method rejects any pairwise null hypothesis H0 : μi = μj at the αe level if
√
Y i• − Y j• > q1−αe ; a, ν · MS
√ Error (11.24)
n
F GH I
HSD
Note that
Y i• − Y j• q1−αe ; a, ν
√ 2 = |t| > √
MS Error n1 + 1 2
n
which implies a confidence interval for μi − μj at the 1 − αe level using the studentized
range statistic is written as
# .
q1−αe ; a, ν 1 1
CI 1−αe (μi − μj ) = (Y i• − Y j• ) − √ MS Error + ,
2 n n
. ⎤ (11.25)
q1−αe ; a, ν 1 1 ⎦
(Y i• − Y j• ) + √ MS Error +
2 n n
Strictly speaking, HSD is only applicable to the equal sample size problem. For unequal
sample sizes, HSD can be approximated as
9
q1−αe ; a, ν 1 1
HSD ≈ √ · MS Error · +
2 ni nj
If consecutive groups are not significantly different, use a single line segment under all of
such groups. Suppose there are four treatments being studied, which are labeled A, B, C,
and D. The diagram
A D C B
indicates that A and D are not distinguishable from each other, nor are D, C, and B
distinguishable from each other. Only A can be distinguished from C and B.
μA + μB μC + μD
H0 : = .
2 2
a a
Any linear combination of means C= i=1 ci μi , where i=1 ci = 0, is called a contrast.
a
An estimate of the contrast C = i=1 ci μi can be obtained from the observed data and
: = a ci Y i• . A contrast of observed means is an unbiased estimate of the
expressed as C i=1
corresponding true treatment means:
& a ' a
E C:= ci Y i• = ci μi (11.26)
i=1 i=1
Since the treatment means are independent, the variance of the observed contrast is
& a ' a
c2i
Var C:= ci Y i• = σ 2 . (11.27)
i=1 i=1
ni
a
i=1 ci Y i• − δ
t= √ 2 (11.28)
a c2i
MS Error · i=1 ni
Experimental Design 523
⎡ /
0
a a 0 a
c2i
CI 1−α ci μi =⎣ ci Y i• − t1− α2 ; N −a · MS Error · 1 ,
i=1 i=1
n
i=1 i
/ ⎤ (11.29)
0 a 2
a 0 ci ⎦
ci Y i• + t1− α2 ; N −a · MS Error · 1
i=1
n
i=1 i
Thesum of squares can also be computed for a contrast. In particular, the sum of squares
a
for i=1 ci Y i• is
& '2
a
i=1 c i Y i•
SS C: = a c2i (11.30)
i=1 ni
a
which has 1 degree of freedom. To test if the contrast C = i=1 ci μi is zero the ratio
SS C: / is formed, which follows an F when H is true.
MS Error 1, dfError 0
Orthogonal contrasts are independent of one another and partition the treatment sum of
squares. That is, if one computes the sum of squares for a full set of orthogonal contrasts
(a − 1 contrasts for a treatments), adding up the a − 1 orthogonal contrasts will equal the
treatment sum of squares (SS Treatment). Unfortunately, the construction of a complete set
of meaningful contrasts is not an easy proposition. Contrasts should be used to answer
scientific questions of interest rather than because a complete set of orthogonal contrasts
can be computed.
Example 11.4 Drosophila The data set Drosophila contains per diem fecundity
(number of eggs laid per female per day for the first 14 days of life) for 25 females from
each of three lines of Drosophila melanogaster. The three lines are Nonselected (control),
Resistant, and Susceptible. The original measurements are from an experiment conducted
by R. R. Sokal (Sokal and Rohlf, 1994, p. 237). Test if there are
(b) Differences in fecundity between the Resistant and the Susceptible lines versus the
Nonselected line, and
(c) Fecundity differences between the Resistant and the Susceptible lines.
Solution: The first question (a) seeks to answer if there are differences in the treatment
means. In this case, the hypothesis of interest is H0 : μNonselected = μResistant = μSusceptible.
The second question is typical of experiments with two new treatments and a control. The
524 Probability and Statistics with R
null hypothesis for question (b) is equality between the Nonselected line (control) and the
Resistant and the Susceptible lines (the two new treatments), written
μResistant + μSusceptible
H0 : μNonselected = .
2
The hypothesis needed to answer question (c) of whether the two treatments (Resistant and
Susceptible) are different is written
H0 : μResistant = μSusceptible.
> attach(Drosophila)
> summary(aov(Fecundity~Line))
Df Sum Sq Mean Sq F value Pr(>F)
Line 2 1362.2 681.1 8.6657 0.0004244 ***
Residuals 72 5659.0 78.6
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Table 11.6: ANOVA table for model Fecundity ∼ Line using Drosophila data
Source df SS MS F ℘-value
Before answering (b) and (c), the residuals are examined (not shown) for the model
Fecundity ∼ Line with the function [Link](). No problems are noted, so the
second and third questions can be answered using the orthogonal contrasts
μResistant + μSusceptible
C1 = μNonselected −
2
which has coefficients ci = (1, −0.5, −0.5), and
C2 = μResistant − μSusceptible
which has coefficients di = (0, 1, −1). Contrasts C1 and C2 are orthogonal because
a
ci · di 1 × 0 −0.5 × 1 −0.5 × −1
= + + = 0.
i=1
ni 25 25 25
Experimental Design 525
Since there are a = 3 treatments, there are two degrees of freedom for a set of orthogonal
contrasts. The sum of squares for the first contrast is 1329.0817 and the sum of squares
for the second contrast is 33.1298. The sum of squares for treatments is 1362.2115, which
equals the sum of the sum of squares for the two orthogonal contrasts: 1329.0817 + 33.1298.
The ℘-value for the first contrast (℘-value = 0.0001) provides strong evidence to suggest
μ +μ
μNonselected = Resistant 2 Susceptible . The ℘-value for the second contrast (℘-value = 0.518)
provides insufficient evidence to reject the null hypothesis μResistant = μSusceptible.
:1 and C
The sums of squares for C :2 using (11.30) are computed as
2
(1 × 33.372) + (−0.5 × 25.256) + (−0.5 × 23.628)
SS C: 1 = (−.5)2 (−.5)2
= 1329.08
12
25 + 25 + 25
and 2
(0 × 33.372) + (1 × 25.256) + (−1 × 23.628)
SS C: 2 = (−1)2
= 33.13
02 12
25 + 25 + 25
Note the ℘-values in Table 11.7 are individual ℘-values. That is, they are not simultaneously
correct ℘-values. To obtain ℘-values adjusted for simultaneous inference or simultaneous
confidence intervals, one should use the R package multcomp.
The S commands to calculate the values used in the ANOVA table for the contrasts are
There are several ways to obtain contrasts with S by changing the type of contrasts S
uses. Contrasts settings for S include [Link], [Link], [Link], and
[Link]. R also has the contrast [Link], which is not directly available
in S-PLUS. The interested reader should refer to the help documentation by typing
?[Link] for more explanation. R uses [Link] for unordered factors
which is not strictly a contrast in its default options. S-PLUS, on the other hand, uses
[Link] as its default contrast. The option [Link] produces Helmert
contrasts, which are orthogonal contrasts when there are an equal number of observations
at each of the factor levels. For example, R default contrasts for Line are
To compute the sum of squares for the contrasts used in parts (b) and (c), key in
Note that using coefficients (2, −1, −1) is equivalent to using (1, −0.5, −0.5), since the first
set of coefficients is simply a linear combination of the second set of coefficients:
To obtain simultaneous ℘-values and confidence intervals, the following R code should be
used:
> library(multcomp)
> summary(glht(aov(Fecundity~Line), linfct = mcp(Line = t(CO))))
Linear Hypotheses:
Estimate Std. Error t value p value
Contrast 1 == 0 17.860 4.343 4.112 0.000205 ***
Contrast 2 == 0 1.628 2.508 0.649 0.766699
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> CI
Linear Hypotheses:
Estimate lwr upr
Contrast 1 == 0 17.8600 7.9456 27.7744
Contrast 2 == 0 1.6280 -4.0961 7.3521
Using the R function barplot2() from the gregmisc package, barplots for the mean of the
various lines and contrasts are created with superimposed 95% confidence intervals as well
as a graph of the 95% simultaneous confidence intervals from the multcomp package with
the command plot(CI) and shown in Figure 11.15.
30
10
10
0
^
μ ^
Resistant + μSelected
Nonselected Resistant Susceptible ^
μ Nonselected 2
( )
30
Contrast 2
10
( )
0
−5 0 5 10 15 20 25
^
μ ^
μ
Resistant Susceptible
Linear Hypotheses
FIGURE 11.15: These graphs give barplots showing the mean fecundity by line, by contrast
1, and by contrast 2 with individual 95% confidence intervals. The bottom right graph
displays the simultaneous 95% confidence intervals for contrast 1 and contrast 2.
Experimental Design 529
⎡ /
2 0
a a
0 a
c2i
CI 1−αe ci μi =⎣ ci Y i• − (a − 1)f1−αe ; a−1, ν · 1MS Error ,
i=1 i=1 i=1
ni
/ ⎤ (11.31)
2 0
a
0 a
c2i ⎦
ci Y i• + (a − 1)f1−αe ; a−1, ν · 1MS Error
i=1 i=1
ni
The Scheffé confidence intervals have simultaneous 1 − αe coverage over any set of contrasts.
Solution: Each of the methods provides a cutoff value for considering a difference of
means significant. The estimated means are μ̂A = 379.67, μ̂B = 405.16, μ̂C = 421.67, and
μ̂D = 410.33. The estimated mean differences with which these values will be compared are
I. μ̂B − μ̂A = Y 2• − Y 1• = 25.50
II. μ̂C − μ̂A = Y 3• − Y 1• = 42.00
III. μ̂D − μ̂A = Y 4• − Y 1• = 30.67
IV. μ̂C − μ̂B = Y 3• − Y 2• = 16.50
V. μ̂D − μ̂B = Y 4• − Y 2• = 5.16
VI. μ̂D − μ̂C = Y 4• − Y 3• = −11.33
(a) Fisher’s LSD considers group means significantly different if
9
1 1
Y i• − Y j• > t1− αc ; df · MS Error · +
Error
2
ni nj
F GH I
LSD
.
√ 1 1
LSD = 2.085 · 354.94 · + = 22.68
6 6
Comparing this value with the statistics from I–VI indicates that μA is significantly different
from μB , μD , and μC :
A B D C
A B D C
(c) Tukey’s honestly significant difference considers group means significantly different if
√
Y i• − Y j• > q1−αe ; a, ν · MS
√ Error
n
F GH I
HSD
.
√ 1 1
HSD = 2.799 · 354.94 · + = 30.445
6 6
Comparing this value with the statistics from I–VI indicates that μA is significantly different
from μD and μC :
A B D C
$tire
diff lwr upr p adj
B-A 25.500000 -4.9446409 55.94464 0.1213153
C-A 42.000000 11.5553591 72.44464 0.0049515
D-A 30.666667 0.2220258 61.11131 0.0479540
C-B 16.500000 -13.9446409 46.94464 0.4464584
D-B 5.166667 -25.2779742 35.61131 0.9637307
D-C -11.333333 -41.7779742 19.11131 0.7273681
Figure 11.16 on the following page shows a graphical representation of the Tukey’s HSD
confidence intervals calculated above. A slightly different graphical representation of signif-
icant differences between group means using Tukey’s HSD can be created with the following
R code:
> library(multcompView)
> multcompBoxplot(StopDist~tire, data=Tire)
B−A
C−A
D−A
C−B
D−B
D−C
−40 −20 0 20 40 60
FIGURE 11.16: Graphical representation of confidence intervals based on Tukey’s HSD for
the model StopDist ∼ tire using the data frame Tire
a
A
a b
B
D
b
C
b
360 380 400 420 440
FIGURE 11.17: Multiple comparison boxplot with multcompTs differentiating means based
on Tukey’s HSD for the model StopDist ∼ tire using the data frame Tire
In this case,
/ .
2 0
0 a
c2i 2
(a − 1)f1−αe ; a−1, ν · 1MS Error = (4 − 1) · 3.098 · 354.94 · = 33.162.
i=1
ni 6
Experimental Design 533
Comparing this value with the statistics from I–VI indicates that μA is significantly different
from μC :
A B D C
The S-PLUS command to generate Scheffé’s significant difference pairwise confidence inter-
vals is multicomp(aov(StopDist~tire), method="scheffe").
S code that can be used to calculate the LSD, BSD, HSD, and Scheffé statistics as well as
the pairwise mean differences is
> library(gregmisc)
> NS <- tapply(StopDist, tire, length)
> SE <- sqrt(MSE)/sqrt(NS)
> t.v <- qt(.975, dfe)
> ci.l <- ybari - t.v*SE
534 Probability and Statistics with R
400
300
200
100
0
A B C D
FIGURE 11.18: Barplot of mean stopping distance by tire type with superimposed
individual 95% confidence intervals for the Tire data frame
(2) τi ∼ N ID(0, στ ).
Because of assumption number (3), the variance of any observation is σY2 ij = στ2i + σ 2 .
In the random effects model, one is interested in estimating variance components, not in
testing treatment means. The reason for this is that the means will vary due to the random
nature of selecting the a treatments from the entire population of possible treatments. The
partitioning of the sum of squares employed with the fixed effects model is still valid with
the random effects model; however, the hypotheses of interest are now
which are tested using the ANOVA procedure outlined for the fixed effects model. If the
null hypothesis cannot be rejected, στ2 = 0, it is concluded that there are no treatment
differences. On the other hand, if the alternative hypothesis is supported, στ2 > 0, the
conclusion is that variability exists among treatments.
The test statistic for testing στ2 = 0 is MS Treatment/MS Error , which follows an Fa−1,N −a
distribution when the null hypothesis is true. Although the same ANOVA table is used for
fixed effects and random effects models, the interpretations are different. The conclusions
from a random effects model are not limited to the a treatments used in the computation
of the test statistic but rather apply to the entire population of treatments. Estimators for
the two variance components when the a treatments have equal sample size n are
MS Treatment − MS Error
σ̂ 2 = MS Error and σ̂τ2 = (11.32)
n
When treatment sample sizes are unequal, the n in (11.32) is replaced with n , where
a a
1 n2i
n = ni − i=1
a (11.33)
a−1 i=1 i=1 ni
Example 11.6 Frozen Carrots A food processing company that uses many
hundreds of freezers is studying the variability of its freezers on the texture of frozen carrots.
The shear measured in kN on frozen carrots from four randomly selected freezers is shown in
Table 11.8 and available in the food data frame. The company would like all of its freezers
to be homogeneous in order to control the taste of the frozen carrots.
(a) The company in this problem is ultimately interested in reducing freezer variability
and wants to know if there is more variability in their frozen carrots due to the carrots
themselves or due to the numerous freezers used in freezing the carrots.
From Table 11.9, one can see that there is strong evidence to suggest στ2 > 0 (℘-value <
0.0002). In other words, there is more variability due to the freezers than variability due to
the carrots.
S Commands:
> attach(food)
> summary(aov(shear~freezer))
Df Sum Sq Mean Sq F value Pr(>F)
freezer 3 0.035675 0.011892 15.681 0.0001878 ***
Residuals 12 0.009100 0.000758
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
where μ is the grand mean; τi is the ith treatment effect, which is the difference between
the mean response of the ith treatment over all blocks and the grand mean; βj is the jth
block effect, which is the difference between the mean response of the jth block over all
treatments and the grand mean; and εij are the N ID(0, σ) error terms. Treatment and
block
a effects are considered
b fixed effects, defined as deviations from the grand mean so that
i=1 τi = 0 and j=1 β j = 0 . Note that model (11.34) is a completely additive model,
which assumes blocks and treatments do not interact. That is, if treatment one causes
the expected response to increase by 3 units (τ1 = 3), and if the first block decreases the
expected response by 1 unit (β1 = −1), then the expected response for both treatment and
block one is E(Y11 ) = μ + τ1 + β1 = μ + 3 − 1 = μ + 2. A RCBD is really a design with two
factors, where only one factor (the one measuring the treatment effect) is of interest. The
other factor (called a block) is used to reduce the experiment’s variability and to enhance its
ability to detect treatment differences for the factor of interest. Analysis of the RCBD differs
from a two-factor design because the blocking factor is not randomized. This dependence in
the blocking factor means there is no theoretical justification for a test of blocks. However,
one will often look at the ratio MS Blocks /MS Error to get an idea if blocking was beneficial.
Just keep in mind that the ratio MS Blocks /MS Error does not truly follow an F distribution,
as does the ratio MS Treatment /MS Error . One must remember that blocks should only be used
when doing so reduces the overall design variability. To do otherwise reduces the power of
the test.
The least squares estimators for the parameters in (11.34) are
μ̂ = Y •• (11.35)
τ̂i = Y i• − Y •• (11.36)
β̂j = Y •j − Y •• (11.37)
538 Probability and Statistics with R
Each Yij from (11.34) can be decomposed into four parts by substituting the least squares
estimates of μ, τi , βj , and εij for the parameters’ values:
a b a b
2
(Yij − Y •• )2 = (Y i• − Y •• ) + (Y •j − Y •• ) + (Yij − Y i• − Y •j + Y •• )
i=1 j=1 i=1 j=1
(11.40)
When the right side of (11.40) is expanded, all three cross products sum to zero (which is
left to the reader to verify), giving
a b a b a b
(Yij − Y •• )2 = (Y i• − Y •• )2 + (Y •j − Y •• )2
i=1 j=1 i=1 j=1 i=1 j=1
F GH I F GH I F GH I
SS Total SS Treatment SS Block
a b
+ (Yij − Y i• − Y •j + Y •• )2 (11.41)
i=1 j=1
F GH I
SS Error
The mean squares are computed as with the completely randomized design model by
dividing each sum of squares by its corresponding degrees of freedom. The expected value
of the mean squares, if treatments and blocks are fixed, can be shown to be
a
2 b · i=1 τi2
E MS Treatments = σ +
a−1
b
2
a · 2
i=1 βj
E MS Blocks = σ +
b−1
E MS Error = σ 2
Consequently, to test for no treatment effect, one uses the ratio MS Treatment /MS Error , which
has an F distribution with (a − 1) and (a − 1)(b − 1) degrees of freedom when H0 is
Experimental Design 539
true. There is no formal test for blocks; however, examining the ratio MS Blocks /MS Error ,
and comparing it to an F distribution with (b − 1) and (a − 1)(b − 1) degrees of freedom
will give an indication of whether blocking is appropriate. If blocking is not appropriate,
then it should be eliminated in future experiments. The ANOVA table for the randomized
complete block design is given in Table 11.10.
Table 11.10: ANOVA table for the randomized complete block design
Source of Degrees of
Variation Freedom Sum of Squares Mean Square
(Source) (df ) (SS ) (MS ) F
a
SS Treatment = b · τ̂i2 ≡
i=1
MS Treatment =
Treatments a−1 MS Treatment
a b SS Treatment MS Error
(Y i• − Y •• )2 a−1
i=1 j=1
b
SS Blocks = a · β̂j2 ≡
j=1
MS Block =
Blocks b−1
a b SS Block
(Y •j − Y •• )2 b−1
i=1 j=1
a b
SS Error = ε̂2ij ≡
MS Error =
i=1 j=1
Error (a − 1)(b − 1) SS Error
a b
(Yij −Y i• −Y •j +Y •• )2 (a − 1)(b − 1)
i=1 j=1
SS Total =
Total a·b−1 a b
(Yij − Y •• )2
i=1 j=1
all cars. Consider the completely random assignment of tire models to cars given in the S
output stored in the variable tireCRD. Note that tire model D is never used with Car1, tire
model C is never used with Car2, and tire model A is never used with Car3. Further, any
variation in model A may simply be due to Car1, Car2, and Car4. Although the completely
randomized design averaged out the car effects, it did not eliminate the variance among
cars. The randomized complete block design does remove the variability due to cars. One
possible assignment of tire models within cars is given under the variable tireCRBD:
> car <- rep(c("Car1", "Car2", "Car3", "Car4"), c(4, 4, 4, 4))
> tire <- rep(LETTERS[1:4], c(4, 4, 4, 4))
> tireCRD <- sample(tire)
> tireCRBD <- c(sample(LETTERS[1:4]), sample(LETTERS[1:4]),
+ sample(LETTERS[1:4]), sample(LETTERS[1:4]))
> Designs <- cbind(car, tire, tireCRD, tireCRBD)
> Designs
car tire tireCRD tireCRBD
[1,] "Car1" "A" "C" "B"
[2,] "Car1" "A" "A" "D"
[3,] "Car1" "A" "B" "C"
[4,] "Car1" "A" "A" "A"
[5,] "Car2" "B" "D" "A"
[6,] "Car2" "B" "A" "B"
[7,] "Car2" "B" "D" "C"
[8,] "Car2" "B" "B" "D"
[9,] "Car3" "C" "C" "A"
[10,] "Car3" "C" "C" "B"
[11,] "Car3" "C" "D" "D"
[12,] "Car3" "C" "B" "C"
[13,] "Car4" "D" "C" "B"
[14,] "Car4" "D" "D" "D"
[15,] "Car4" "D" "A" "A"
[16,] "Car4" "D" "B" "C"
Example 11.7 Tire Wear The data frame TireWear contains measurements for
the amount of tread loss after 10,000 miles of driving in thousandths of an inch. The tread
loss from the TireWear data frame is presented in tabular form in Table 11.11 on the next
page along with the order the tires were assigned to the car in parentheses. Use the values
in Table 11.11 to test for treatment (tire model) effects using an additive RCBD.
(a) Verify that an additive model is appropriate.
(b) Compute the ANOVA table to test H0 : τi = 0 for all i versus H1 : τi = 0 for some i.
(c) Represent the Yij values using (11.39).
(d) Verify graphically that εij ∼ N (0, σ).
(e) Determine which tires are different (have the least tread loss) using Tukey’s HSD at
αe = 0.05.
(a) The RCBD is completely additive, and the function [Link]() is used to
verify the reasonableness of the additivity assumption before computing any sums of squares.
Experimental Design 541
Table 11.11: The tread loss from the TireWear data frame
Car1 Car2 Car3 Car4
A 10 (4) 8 (1) 7 (1) 7 (3)
B 9 (1) 8 (2) 7 (2) 5 (1)
C 8 (3) 7 (3) 5 (4) 3 (4)
D 6 (2) 5(4) 3 (3) 3 (2)
Interaction plots show the relative size of main effects and interaction. The pairs (i, Y ij )
for all j are plotted, and points in the same block are connected. The roles of blocks and
treatments can be reversed, and it is often informative to do so with interaction plots.
Parallel lines are indicative of additive designs. Lines that cross should be investigated
further. Figure 11.19 on the following page does not suggest any problems with the RCBD’s
assumption of additivity; however, since graphs are often misleading and their interpretation
is subjective, other means of analyzing and evaluating interaction should also be explored.
> attach(TireWear)
> par(mfrow=c(1, 2), cex=.8)
> [Link](Treat, Block, Wear, type="b", legend=FALSE)
> [Link](Block, Treat, Wear, type="b", legend=FALSE)
> par(mfrow=c(1, 1), cex=1)
The interaction plots suggest both a treatment and a block effect. Another graph that
is helpful when there is only one observation per treatment/block combination is the strip
plot. Results from using the lattice/Trellis function stripplot() are shown in Figure 11.20
on the next page. One can see that tire wear increases with tire models in the order D, C,
B, and then A. In a similar fashion, one notes that tire wear in cars increases in the order
Car4, Car3, Car2, and then Car1. The graph showing tire wear means due to treatments
and blocks using the function [Link]() is shown in Figure 11.21 on page 543.
> library(lattice) # R
> A <- stripplot(Treat~Wear|Block, layout=c(4, 1))
> B <- stripplot(Block~Wear|Treat, layout=c(4, 1))
> print(A, split=c(1, 1, 1, 2), more=TRUE)
> print(B, split=c(1, 2, 1, 2), more=FALSE)
> [Link](Wear~Treat+Block)
(b) Using the values from Table 11.12 on the previous page, the values for the ANOVA
542 Probability and Statistics with R
10
10
1 1
1 2
9
2 2 1 3 1
2
8
mean of Wear
mean of Wear
3
4 3 2 3 1
2 1
7
6 1 4
6
4 3 2 4 3 2
5
5
4
4
4 3
4 4 3
4
3
3
A B C D Car1 Car2 Car3 Car4
Treat Block
FIGURE 11.19: Left graph shows an interaction plot of blocks and treatments for the
response Wear where the four blocks, Car1, Car2, Car3, and Car4, are denoted with the
numbers 1, 2, 3, and 4, respectively, and the treatments shown along the x-axis are A, B, C,
and D, respectively. The right graph shows an interaction plot of treatments and blocks for
the response Wear where the four treatments, A, B, C, and D, are denoted with the numbers
1, 2, 3, and 4, respectively, and the blocks shown along the x-axis are Car1, Car2, Car3,
and Car4, respectively.
4 6 8 10 4 6 8 10
4 6 8 10 4 6 8 10
Wear
4 6 8 10 4 6 8 10
A B C D
Car4
Car3
Car2
Car1
4 6 8 10 4 6 8 10
Wear
FIGURE 11.20: The graph resulting from the lattice/Trellis function stripplot() for
Example 11.7
table are
a
SS Treatment = b · τ̂i2 = 4 1.68752 + 0.93752 + (−0.5625)2 + (−2.06252)
i=1
= 33.1875
b
SS Block = a β̂j2 = 4 1.93752 + 0.68752 + (−0.8125)2 + (−1.8125)2
j=1
= 32.6875
Experimental Design 543
Car1
A
8
B
Car2
7
mean of Wear
6
C
5 Car3
Car4
D
Treat Block
Factors
FIGURE 11.21: Tire wear means due to treatments and blocks using the function
[Link]() for Example 11.7
a b
2
SS Total = Yij − Y ••
i=1 j=1
Note that the computer treats the blocking factor as if it were assigned at random and
computes a ℘-value for the blocking factor. The small ℘-value suggests that blocking is
appropriate. Since the ℘-value = 0.000068, the null hypothesis (H0 : τi = 0) of no treatment
effect is rejected.
544 Probability and Statistics with R
> residm
[,1] [,2] [,3] [,4]
[1,] 0.0625 -0.6875 -0.1875 0.8125
[2,] -0.1875 0.0625 0.5625 -0.4375
[3,] 0.3125 0.5625 0.0625 -0.9375
[4,] -0.1875 0.0625 -0.4375 0.5625
> GM+treatm+blockm+residm
[,1] [,2] [,3] [,4]
[1,] 10 8 7 7
[2,] 9 8 7 5
[3,] 8 7 5 3
[4,] 6 5 3 3
The values used in the matrices can also be obtained from using the S function proj()
(proj([Link])).
(d) The residuals from the model [Link] are graphed in Figure 11.22 with the function
[Link]() from the PASWR package. The first graph in Figure 11.22 suggests that
there is no problem with the independence of errors assumption. The middle graph in
Figure 11.22 suggests the errors follow a normal distribution, while the last graph suggests
homogeneity of variance is reasonable.
Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus
ordered values for [Link] residuals from [Link] fitted values for [Link]
13 13 13
2
2
standardized residuals
standardized residuals
standardized residuals
1
1
0
0
−1
−1
−1
5 5 5
−2
−2
−2
15 15 15
5 10 15 −2 −1 0 1 2 4 6 8 10
(e) The following R code was used to create simultaneous 95% mean pairwise confidence
intervals using Tukey’s HSD. The confidence intervals are depicted in Figure 11.23 on the
next page.
B−A
C−A
D−A
C−B
D−B
D−C
−5 −4 −3 −2 −1 0
FIGURE 11.23: Simultaneous 95% mean pairwise confidence intervals using Tukey’s HSD
from Example 11.7
$Treat
diff lwr upr p adj
B-A -0.75 -2.138820 0.6388204 0.3838264
C-A -2.25 -3.638820 -0.8611796 0.0031175
D-A -3.75 -5.138820 -2.3611796 0.0000699
C-B -1.50 -2.888820 -0.1111796 0.0343452
D-B -3.00 -4.388820 -1.6111796 0.0003981
D-C -1.50 -2.888820 -0.1111796 0.0343452
Tire D is significantly better (less wear) than tires C, B, and A. Tire C is significantly better
than tires B and A, and tires B and A are not significantly different from one another.
Figure 11.24 shows a barplot of the mean wear by tire with superimposed individual 95%
confidence intervals.
A B C D
FIGURE 11.24: Barplot of the mean wear by tire with superimposed individual 95%
confidence intervals from Example 11.7
Experimental Design 547
Factor B
1 2 ··· b
1 Y111 , Y112 , . . . , Y11n Y121 , Y122 , . . . , Y12n · · · Y1b1 , Y1b2 , . . . , Y1bn Y 1••
2 Y211 , Y212 , . . . , Y21n Y221 , Y222 , . . . , Y22n · · · Y2b1 , Y2b2 , . . . , Y2bn Y 2••
Factor A .. .. .. .. ..
. . . . .
a Ya11 , Ya12 , . . . , Ya1n Ya21 , Ya22 , . . . , Ya2n · · · Yab1 , Yab2 , . . . , Yabn Y a••
Y •1• Y •2• ··· Y •b• Y •••
The observations from a two-factor factorial design are described by the linear model
where μ is the overall mean effect, αi is the effect of the ith row factor A, βj is the effect
of the j th column factor B, αβij is the effect of the interaction between αi and βj , and εijk
is a random error. Note that αβ is not α · β but rather a single term. Both αi and βj are
assumed to be fixed with the constraints
a b a b
αi = 0; βj = 0; αβij = αβij = 0. (11.43)
i=1 j=1 i=1 j=1
That is, the treatment effects are defined as deviations from the overall mean. Given these
assumptions, the least squares estimators for the parameters in the two-factor factorial
design are
Sums of Squares Each Yijk from (11.42) can be decomposed into five parts by substi-
tuting the least squares estimates of μ, αi , βj , αβij , and εijk for the parameters’ values:
That is,
SS Total = SS A + SS B + SS AB + SS Error (11.47)
The corresponding degrees of freedom are
F GH− 1I = a
abn − 1I + bF GH
F GH − 1I + (a − 1)(b − 1) + ab(n − 1).
F GH I F GH I
(11.48)
total df A df B df AB interaction df Error df
The mean squares are computed by dividing each sum of squares by its degrees of
freedom. The expected value of the mean squares, with fixed factors A and B, can be
shown to be
a b
2 bn i=1 α2i 2
an j=1 βj2
E(MS A ) = σ + E(MS B ) = σ +
a−1 b−1
a b 2
n i=1 j=1 αβij
E(MS AB ) = σ 2 + E(MS Error ) = σ 2
(a − 1)(b − 1)
Consequently, to test for A and B main effects as well as the interaction between A and
B, the corresponding mean square is divided by the MS Error . The ANOVA table for a
two-factor design is given in Table 11.15 on the facing page. The formal hypotheses for
testing for factor A treatment effects, factor B treatment effects, and the interaction between
factor A and factor B are written, respectively, as
Factor A Factor B Interaction
H0 : αi = 0 for all i H0 : βj = 0 for all j H0 : αβij = 0 for all (i, j)
H1 : αi = 0 for some i H1 : βj = 0 for some j H1 : αβij = 0 for some (i, j)
Experimental Design 549
b
MS B
B b−1 SS B = an (Y •j• − Y ••• )2 MS B = SS B
b−1
j=1
MS Error
a b
MS AB
AB (a − 1)(b − 1) SS AB = n (Y ij• − Y i•• MS AB = SS AB
(a−1)(b−1)
i=1 j=1
MS Error
−Y •j• + Y ••• )2
a b n
Error ab(n − 1) SS Error = (Yijk − Y ij• )2 MS Error = SS Error
ab(n−1)
i=1 j=1 k=1
a b n
Total abn − 1 SS Total = (Yijk − Y ••• )2
i=1 j=1 k=1
Example 11.8 Television Tube Screen Brightness The data in Table 11.16
are taken from Hicks (1956) where an experiment was designed to study the effect of glass
type and phosphor type on the brightness of a television tube screen. The measured variable
was the current in microamperes (μA) necessary to produce a certain level of brightness.
The higher the μA required to produce a given brightness, the poorer are the tube screen
characteristics. That is, optimal characteristics are obtained when the response (μA) is
small. Analyze the data using a two-factor factorial design.
Phosphor
A B C
I 280, 290, 285 300, 310, 295 270, 285, 290
Glass
II 230, 235, 240 260, 240, 235 220, 225, 230
(c) Fill in the missing values to complete Table 11.17 on the following page.
(d) Create a two-way ANOVA table using the information from Table 11.17 on the next
page and verify your answers using the function anova().
(e) Analyze the residuals and comment on whether the model from (11.42) fits the data.
(f) Is there significant interaction between glass type and phosphor type?
550 Probability and Statistics with R
Table 11.17: Two-factor factorial design table to complete for (c) of Example 11.8
α̂i =
Phosphor A Phosphor B Phosphor C Y i•• Y i•• − Y •••
Y 11• = Y 12• = Y 13• = Y 1•• = α̂1 =
Glass I
α:β 11 = α:β 12 = α:β 13 =
− Y •••
(g) Using αe = 0.05, compute Tukey’s HSD 95% confidence intervals to determine which
combination of glass type and phosphor type require the least μA. Create a graph of
the resulting confidence intervals as well as a barplot of the individual means for the
six treatment combinations with superimposed 95% individual confidence intervals.
(a) The numbers from Table 11.16 on the preceding page are read into the variable
Microamps and the factors Glass and Phosphor are created as follows:
> Microamps <- c(280, 290, 285, 300, 310, 295, 270, 285, 290, 230,
+ 235, 240, 260, 240, 235, 220, 225, 230)
> Glass <- factor(c(rep("Glass I", 9), rep("Glass II", 9)))
> Phosphor <- factor(rep(rep(c(rep("Phosphor A", 3),
+ rep("Phosphor B", 3), rep("Phosphor C", 3)), 2)))
(c) The values to fill in Table 11.17 are computed using the S function [Link]() as
follows:
262.2222
Experimental Design 551
Glass
Glass I Glass II
289.44 235.00
Phosphor
Phosphor A Phosphor B Phosphor C
260.00 273.33 253.33
Glass:Phosphor
Phosphor
Glass Phosphor A Phosphor B Phosphor C
Glass I 285.00 301.67 281.67
Glass II 235.00 245.00 225.00
> [Link]([Link], type="effects")
Tables of effects
Glass
Glass I Glass II
27.222 -27.222
Phosphor
Phosphor A Phosphor B Phosphor C
-2.222 11.111 -8.889
Glass:Phosphor
Phosphor
Glass Phosphor A Phosphor B Phosphor C
Glass I -2.2222 1.1111 1.1111
Glass II 2.2222 -1.1111 -1.1111
Glass Phosphor
300
300
Microamps
Microamps
260
260
220
220
Interaction
300
300
2
Microamps
Microamps
Glass I 1
3
Phosphor B
260
260
Phosphor A
Phosphor C
2
Glass II 1
220
220
3
fac1 fac2
Glass I Glass II
Factors Glass
Table 11.18: Two-factor factorial design table COMPLETED for (c) of Example 11.8
β̂j = Y •j• β̂1 = −2.2222 β̂2 = 11.1111 β̂3 = −8.889 Y ••• = 262.22
− Y •••
(d) Using the results from (c), the sums of squares for the ANOVA table are computed and
displayed in Table 11.19 on the next page.
a a
SS A = bn (Y i•• − Y ••• )2 = bn α̂2i
i=1 i=1
= 3 · 3 · 27.2222 + (−27.222)2 = 13338.9
b b
SS B = an (Y •j• − Y ••• )2 = an β̂j2
j=1 j=1
= 2 · 3 · (−2.2222)2 + (11.1111)2 + (−8.889)2 = 1244.4
a b a b
SS AB = n (Y ij• − Y i•• − Y •j• + Y ••• )2 = n 2
αβij
i=1 j=1 i=1 j=1
= 3 · (−2.2222) + (1.1111) + · · · + (−1.1111)2 = 44.4
2 2
a b n
SS Error = (Yijk − Y ij• )2
i=1 j=1 k=1
= (280 − 285)2 + (290 − 285)2 + · · · + (230 − 225)2 = 833.3
a b n
SS Total = (Yijk − Y ••• )2
i=1 j=1 k=1
= (280 − 262.2222)2 + (290 − 262.2222)2 + · · · + (230 − 262.2222)2 = 15461.11
The values for Table 11.19 on the facing page are verified with the S function anova():
Experimental Design 553
Table 11.19: ANOVA table for two-factor factorial design for Example 11.8
Source df SS MS F
Glass 1 13338.9 13338.9 192.08
Phosphor 2 1244.4 622.2 8.96
Glass:Phosphor 2 44.4 22.2 0.32
Residuals 12 833.3 69.4
Total 17 15461.0
> anova([Link])
Analysis of Variance Table
Response: Microamps
Df Sum Sq Mean Sq F value Pr(>F)
Glass 1 13338.9 13338.9 192.08 9.568e-09 ***
Phosphor 2 1244.4 622.2 8.96 0.004162 **
Glass:Phosphor 2 44.4 22.2 0.32 0.732158
Residuals 12 833.3 69.4
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(e) The residuals from fitting the data to model (11.42) are analyzed using the function
[Link]() and shown in Figure 11.26. The first graph in Figure 11.26 is not
relevant because no time component is present in the data, the second graph suggests
normality is reasonable and the third graph indicates homogeneity of variance is plausible.
Consequently, a two-factor factorial model seems to be a reasonable model for the data on
hand.
Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus
ordered values for [Link] residuals from [Link] fitted values for [Link]
13 13 13
2
2
standardized residuals
standardized residuals
standardized residuals
1
1
0
0
−1
−1
−1
15 15 15
−2
−2
−2
7 7 7
FIGURE 11.26: Graphs resulting from using [Link]() on the model [Link]
from Example 11.8
(f) To assess possible interaction between the factors glass and phosphor, two interaction
plots of the same data are created with the following R code and shown in Figure 11.27 on
the following page. Since the lines in Figure 11.27 are roughly parallel in both plots, it is
554 Probability and Statistics with R
reasonable to assume the two factors, glass and phosphor, do not interact.
300
2 1
1 1
mean of Microamps
mean of Microamps
280
280
3 1
260
260
2 2
240
240
1 2
3 2
Glass Phosphor
FIGURE 11.27: Left graph shows an interaction plot of glass and phosphor for the response
μA where the three types of phosphor, Phosphor A, Phosphor B, and Phosphor C, are
denoted with the numbers 1, 2, and 3, respectively, and the glasses depicted along the
x-axis are Glass I and Glass II, respectively. The right graph shows an interaction plot
of phosphor and glass for the response μA where the two types of glass, Glass I, and Glass
II, are denoted with the numbers 1 and 2, respectively, and the phosphors depicted along
the x-axis are Phosphor A, Phosphor B, and Phosphor C, respectively.
(g) Tukey 95% confidence intervals are computed for the 62 = 15 pairwise differences
in mean treatment combinations of glass type and phosphor type using the R function
TukeyHSD(). The code and output follow with a graph of the confidence intervals shown in
Figure 11.28 on the next page. Based on the Tukey HSD confidence intervals, Glass II with
either Phosphor A or Phosphor C should be used in the construction of television picture
tubes since these combinations require the least μA. Note that the mean for Glass II
with Phosphor C is less than the mean for Glass II with Phosphor A but not statistically
different.
The function barplot2() from the gregmisc package is used to create a barplot showing the
six treatment combination means with individual superimposed 95% confidence intervals
using the following R code, with the results displayed in Figure 11.29 on the facing page:
Experimental Design 555
−100 −50 0 50
FIGURE 11.28: Tukey HSD 95% family-wise confidence intervals for the model [Link]
> library(gregmisc)
> meanM <- tapply(Microamps, list(Glass, Phosphor), mean)
> nsM <- tapply(Microamps, list(Glass, Phosphor), length)
> MSE <- anova([Link])[4, 3]
> t.c <- qt(.975, 12)
> lci <- meanM - t.c*sqrt(MSE/nsM)
> uci <- meanM + t.c*sqrt(MSE/nsM)
> barplot2(meanM, beside=TRUE, legend=TRUE, ylim=c(0, 400),
+ [Link]=TRUE, ci.l=lci, ci.u=uci, [Link]=2, col=c("#A9E2FF", "#0080FF"))
> title(main="Treatment Means with Individual 95% CIs")
Glass I
Glass II
300
200
100
0
FIGURE 11.29: Barplot of the means for the six treatment combinations of factors Glass
and Phosphor with individual superimposed 95% confidence intervals
556 Probability and Statistics with R
11.13 Problems
1. Develop a randomization scheme to assign three treatments A, B, and C to 15 exper-
imental units, numbered from 1 to 15. Use the command sample to assign them.
2. Develop a randomization scheme for a complete block design that has 4 blocks, 3
treatments, and 12 experimental units.
3. Provide a randomized assignment for a two-factor factorial design with 36 experimen-
tal units, 4 levels for the first factor, 3 levels for the second factor, and 3 experimental
units for every combination of factor levels.
4. An economic study in a particular city desires to discover the monthly expenses of
consumers, based on their level of education. The survey has drawn data in three
different boroughs: I, II, and III. The educational levels corresponds to low, medium
low, medium high, and high. The expenses have been recorded in thousands of dollars,
and the analysis of variance provides the following information:
A (ASPEN) 3 2 4 3
B (ERIKA) 2 3 4 4
C (SULTANE) 7 6 5 6
Assume the treatments (types of barley) were assigned at random to the 12 plots.
Experimental Design 557
7. Car and Driver (July 1995) conducted tests of five cars from five different countries:
Japan’s Acura NSXT, Italy’s Ferrari F355, Great Britain’s Lotus Esprit S4S, Ger-
many’s Porche 911 Turbo, and the United States’ Dodge Viper RT/10. The maximum
speeds the cars obtained in miles per hour using as much distance as necessary without
exceeding the engine’s redline are given:
8. The data frame barley from the lattice package lists barley yield in bushels per
acre for the years 1931 and 1932 for ten varieties of barley grown at six sites. Is there
evidence to suggest the average barley yield in 1931 for the Waseca site is different
from the average barley yield in 1931 for the Duluth site?
(a) Use the five-step procedure to test the appropriate hypotheses using an α = 0.05
significance level.
(b) Solve the same problem using a RCBD.
(c) Generalize your findings about the relationship between (a) and (b).
9. The following data were obtained from an experiment that investigated the effects
of four bleaching chemicals (randomly selected from a large population of potential
bleaching agents) on pulp brightness. The brightness of pulp is measured as the ability
of a pulp sheet to reflect light directed at it. Brightness is affected by both the light
558 Probability and Statistics with R
(a) Create side-by-side boxplots of the four chemicals. Interpret the resulting graph.
(b) Specify an appropriate model to test if the chemicals have an influence on pulp
brightness. Conduct an analysis on the specified model using α = 0.05.
(c) Estimate the component of variance for the chemicals.
(d) Estimate the total variability in the data.
(e) Construct a confidence interval for the ratio of the variability due to chemicals with
MS Treatment /(5στ2 +σ2 )
respect to the total variability given that MS Error /σ2 ∼ F3,21 . Interpret
your interval.
10. The household appliances section of a well-known store does research to satisfy the
clients’ demands for information about its products. In particular, clients are increas-
ingly asking if the average washing times of the different brands of washing machines
are the same. To discover this, the household appliances section has done the following
experiment: They measured the washing time of five machines of different brands in
four types of cycles (prewash, short, medium, long). The results, in minutes, are
displayed in the following table:
Washing Cycle
Machines Prewash Short Medium Long
Machine 1 15.45 19.95 23.10 25.35
Machine 2 3.15 6.30 13.80 17.70
Machine 3 20.10 22.05 32.10 33.30
Machine 4 25.20 27.15 33.15 38.55
Machine 5 13.65 16.35 19.80 21.75
(j) Is the mean washing time of machines 2, 3, and 4 significantly different from the
mean washing time of machine 5?
(k) Use a barplot to show the mean washing times by machine. Superimpose 95%
confidence intervals over the appropriate bars.
11. An insurance company wants to know how its resources are being used with respect
to time spent issuing travel insurance policies. The company randomly selects three
moments during a day and records the time required to issue a travel insurance policy
to three randomly selected clients who take out a travel policy over the phone, over
the Internet, and in person. The data obtained (in minutes) are
1 2 3
telephone 3.49 2.38 2.09
Internet 4.38 6.68 5.37
in person 7.91 8.70 8.54
12. The Environmental Protection Agency (EPA) is interested in the fuel consumption
of older vehicles. An experiment is designed where the gallons of gasoline consumed
by vehicles over six years old are measured when the same driver travels 162.78 miles
from Boone, NC, to Durham, NC, in 35 different vehicles. Seven vehicles are randomly
selected from each category to be tested. The categories are compact, station wagon,
minivan, van, and full size pickup truck. The data obtained (gallons consumed) are
given in the following table:
(a) Based on the described randomization, what type of design structure did the EPA
use?
(b) Propose a statistical model to analyze these data.
(c) Are the model’s assumptions specified in part (b) satisfied?
560 Probability and Statistics with R
(d) Are there significant differences between the fuel consumption for the five types
of vehicles?
(e) Estimate the model’s error variance.
(f) What conclusions can be drawn from the data?
13. A health conscious pizza parlor is attempting to specify the added calories for each
ingredient of its medium size pizza. Specifically, the pizza parlor wants to know if
there is more variability in an olive topping due to olive suppliers or due to the olives
themselves. From numerous suppliers, four are selected randomly and the calories
for a pizza topping of olives are recorded for five randomly selected pizzas. The data
obtained are given in the following table:
1 2 3 4 5
Supplier 1 133 136 142 135 134
Supplier 2 124 137 125 132 131
Supplier 3 127 126 130 120 123
Supplier 4 150 141 155 150 157
(a) Analyze these data using a two-factor factorial design (model (11.42)).
(b) Looking at the results of the two-way ANOVA table, is there significant interaction
between acid treatment and hole shape? Use α = 0.05.
(c) Create a graphical display of the interactions. Does this display corroborate the
numerical results?
(d) Analyze the residuals and comment on whether model (11.42) fits the data.
Experimental Design 561
(e) If the interaction term is not significant, reanalyze a model where the interaction
term is pooled with the model’s error.
(f) Provide estimates of the parameters αi , i = 1, 2 and βj , j = 1, . . . , 4. of the new
model.
(g) Analyze the residuals.
(h) Use Levene’s test to check if the homogeneity of variances for acid treatment levels
and for hole shape levels are reasonable assumptions.
(i) Are the effects of acid treatment and hole shape statistically significant?
(j) Using an experiment-wise error rate of αe = 0.05, what shape has the highest
quantity of resin collected?
15. The data stored in Cows were extracted from a Canadian record book of purebred diary
cattle. Random samples of 10 mature (five-year-old and older) and 10 two-year-old
cows were taken from each of five breeds. The average butterfat percentage of these
100 cows is stored in the variable butterfat, with the type of cow stored in the
variable breed and the age of the cow stored in the variable age.
16. Quantifying the effect of the loss of leaf area (defoliation) on sunflower (Helianthus
annuus L.) yield caused by hail, pests, and diseases is important in the management
of this crop both from a technical and economic point of view. The effect of defoliation
depends, however, on the foliar surface eliminated and on the growth stage at which
this takes place. The aim of this case study is to determine the response of sunflower
cultivation to several levels of defoliation (defoli) that took place at different growth
stages. An overall of 72 field trials were conducted by applying four defoliation
treatments (non-defoliated control, 33%, 66% and 100%) at different growth stages
(stage) ranging from pre-flowering (1) to physiological maturity (5) in four different
locations (location) of Navarra, Spain: Carcastillo (1), Mélida (2), Murillo (3), and
Unciti (4). There are two response variables: yield in kg/ha of the sunflower and
numseed, the number of seeds per sunflower head. Data are stored in the data frame
sunflower.
(ii) Construct a table to display yield for every level of defoli, location, and
stage. (Hint: Use the functions xtabs() and ftable().)
Note that the function ftable() places 0s where there are no observations in a
level combination.
(b) How many observations are there for every combination of stage and defoli?
(c) Is the design complete or incomplete?
(d) Is the design balanced or unbalanced?
(e) Use side-by-side boxplots to display the variable yield for every level of stage.
(f) Use side-by-side boxplots to display the variable yield for every level of defoli.
(g) Construct an interaction plot for stage and defoli on yield. Comment on the
results.
(h) If an insurance company compensates the yield loss only when there is a 100%
defoliation, can statistical differences between this level and the rest of the defo-
liation levels be found? (Hint: Use orthogonal contrasts.)
(i) To illustrate the final results, provide two graphs: a boxplot and a barplot of
yield by levels of stage. Calculate the numerical values of the yield means and
the standard errors.
(j) To illustrate the final results, redefine two levels for defoli: The first level
combines the original levels 100 and 66 and the second new level groups the
original levels 0 and 33 into a single level. Construct a boxplot and a barplot of
yield for these two new levels. Provide a table for the corresponding means and
standard errors.
Chapter 12
Regression
12.1 Introduction
The central theme of this chapter is modeling associations among variables. Under-
standing these associations can be important for many reasons, including:
The primary tool used to model associations among variables in this chapter is regression.
Regression analysis is used for modeling the relationship between a single variable Y , called
the response or dependent variable, and one or more predictor(s) or independent
variable(s), x1 , x2 , . . . , xp−1 . The response variable must be a continuous variable, but the
predictor variables can be either continuous, discrete, or categorical. The word “regression”
is due to Sir Francis Galton, who demonstrated that offspring do not tend toward the size of
the parents; rather, offspring size tends toward the mean of the population. That is, there
is a “regression toward mediocrity.” The following examples illustrate scenarios where it is
important to understand the associations among response and predictor variables.
563
564 Probability and Statistics with R
Yi = β0 + β1 xi + εi for i = 1, . . . , n (12.2)
Model (12.2) is said to be simple, linear in the parameters (β0 and β1 ), and linear in the
predictor variables (xi ). It is simple because there is only one predictor; it is linear in the
parameters because no parameter appears as an exponent nor is multiplied or divided by
another parameter; and it is linear in the predictor variable since the predictor variable is
raised only to the first power. When the predictor variable is raised to a power, this power
is called the order of the model.
The models
Yi = β0 + β1 ln(xi ) + εi
Yi = β0 + β1 x1i x2i + εi
Yi = β0 exp(β1 xi ) + εi
β0
Yi = + εi
1 + eβ1 xi
are not statistical linear models since the Yi s are not linearly related to the parameters
β0 and β1 . Thus, a “linear model” is characterized by a linear relationship between
the dependent variable and the parameters, not necessarily by a linear relationship with
the independent variables. The random error term represents the absence of an exact
relationship between Y and x. When the variance for all error terms is constant, the errors
are said to be homoscedastic. Typically, Var(εi ) = σ 2 . Furthermore, the random variability
is independent of x. The expected value of Y given x is written
The distribution of Y given x when εi follows a normal distribution with a mean of zero
and a standard deviation of σ is depicted in Figure 12.1 on page 566. Since the random
variable Y is a linear combination of the xs, it follows that σ 2 is not truly the variance of Y
but rather the variance of Y given x. As seen in Figure 12.1, σ 2 = Var(ε) = Var(Y |x). Up
to this point, normally distributed random variables have been denoted as N (μ, σ), where
σ is the standard deviation. To simplify matrix expressions, the variance will take the place
of the standard deviation in normal distributions from this point forward. For example,
the distribution of the error terms in a simple linear regression model will be expressed
N (0, σ 2 I) rather than saying each of the n errors has a N (0, σ) distribution.
Regression 565
The slope, β1 , represents the expected change in Y when a one-unit change is present
in x. If β1 = 0, Y does not depend linearly on x. When β1 < 0, x and Y have a negative
linear relationship, which means that as x increases, Y decreases. Likewise, when β1 > 0,
x and Y have a positive linear relationship, where, as x increases, so does Y .
Yi = β0 + β1 xi + εi (12.4)
where
Yi is the value of the response variable for the ith trial
β0 and β1 are parameters
Y1 = β0 + β1 x1 + ε1
Y2 = β0 + β1 x2 + ε2
.. . . .
. = .. + .. + ..
Yn = β0 + β1 xn + εn
Y = X β + ε (12.5)
n×1 n×2 2×1 n×1
⎡ ⎤ ⎡ ⎤ ⎡ ⎤
Y1 1 x1 ε1
⎢ Y2 ⎥ ⎢1 x2 ⎥ ! " ⎢ ε2 ⎥
⎢ ⎥ ⎢ ⎥ β0 ⎢ ⎥
where Y = ⎢ . ⎥, X = ⎢ . .. ⎥, β = β , and ε = ⎢ .. ⎥.
⎣ .. ⎦ ⎣ .. . ⎦ 1 ⎣ . ⎦
Yn 1 xn εn
Note that ε ∼ N (0, σ 2 I), where σ 2 I is the variance-covariance matrix of the vector of errors.
E(Y |x3 ) = β0 + β1 x3
E(Y |x2 ) = β0 + β1 x2
E(Y |x) = β0 + β1 x
E(Y |x1 ) = β0 + β1 x1
x1 x2 x3
FIGURE 12.1: Graphical representation of simple linear regression model depicting the
distribution of Y given x
of βj values to multiply by a matrix of xij values. The multiple linear regression model is
written
Y = X β + ε (12.7)
n×1 n×p p×1 n×1
⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤
Y1 1 x11 . . . x1(p−1) β0 ε1
⎢ Y2 ⎥ ⎢1 x21 . . . x2(p−1) ⎥ ⎢ β1 ⎥ ⎢ ε2 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
where Y = ⎢ . ⎥, X = ⎢ . .. .. ⎥, β = ⎢ .. ⎥, and ε = ⎢ .. ⎥.
⎣ .. ⎦ ⎣ .. . . ⎦ ⎣ . ⎦ ⎣ . ⎦
Yn 1 xn1 . . . xn(p−1) βp−1 εn
Each column of X contains the values for a particular independent variable. The values of
X are assumed to be known constants. The vectors Y and ε are random vectors whose
elements are random variables. The vector β is a vector of unknown constants that are
estimated from the data. Each βj for j = 0, 1, . . . , p − 1 indicates the change E[Y |xij ] for
a fixed i when xij is increased by one unit and all the other predictors are held constant.
When ε is assumed N (0, σ 2 I), model (12.7) is referred to as the normal error model.
In the normal error model, X and β are assumed to be constants. Consequently, Y is a
random vector that is the sum of a constant vector Xβ and the random vector ε. Since ε
is assumed N (0, σ2 I), it follows that Y ∼ N (Xβ, σ 2 I). The tests and confidence intervals
developed in later sections are based on the assumption that ε ∼ N (0, σ 2 I).
Assuming that there is no error in the measurement of the xij values, one can proceed
with either of the two most widely used techniques used to estimate parameters (βj s) in a
regression model: ordinary least squares or the method of maximum likelihood.
Regression 567
εi = Yi − E(Yi )
is the ith deviation (error). For the simple linear regression model, εi = Yi − (β0 + β1 xi ).
The estimates β̂0 of β0 and β̂1 of β1 are calculated by minimizing the quantity Q (the sum
of the squared residuals) found in (12.8):
n n
2
Q= ε2i = Yi − (β0 + β1 xi ) , (12.8)
i=1 i=1
The values of β0 and β1 that minimize Q are found by differentiating Q with respect to β0
and β1 and setting the partial derivatives equal to zero. The resulting equations are known
as the normal equations:
n
δQ
=2 (Yi − β0 − β1 xi )(−1)
δβ0 i=1
n
= −2 (Yi − β0 − β1 xi ) (12.10)
i=1
n
δQ
=2 (Yi − β0 − β1 xi )(−xi )
δβ1 i=1
n
= −2 (Yi − β0 − β1 xi )(xi ) (12.11)
i=1
After setting each of these partial derivatives equal to zero, the normal equations for the
simple linear regression model simplify to
n n
Yi = nβ̂0 + β̂1 xi (12.12)
i=1 i=1
n n n
Yi xi = β̂0 xi + β̂1 x2i (12.13)
i=1 i=1 i=1
Note that the βj s are replaced with β̂j s as their values are estimates once the partial
derivatives are set equal to zero. These equations are now solved for β̂0 and β̂1 .
568 Probability and Statistics with R
In solving for β̂1 , two quantities appear that require simplification. The first quantity is
n n n
Yi i=1 xi
Yi xi − i=1
. (12.15)
i=1
n
n n n n n
Yi i=1 xi
Yi xi − i=1
= Yi xi − Y xi
i=1
n i=1 i=1
n n n n
n
= Yi xi − Y xi − Y xi + Y xi
i=1 i=1 i=1
n i=1
n n n n
Yi
= Yi xi − Y xi − i=1 xi + nY x̄
i=1 i=1
n i=1
n n n
= Yi xi − Y xi − x̄ Yi + nY x̄
i=1 i=1 i=1
n
= (xi − x̄)(Yi − Y )
i=1
n n 2 n
2
n
( i=1 xi ) xi
x2i − = x2i −n i=1
i=1
n i=1
n
n
= x2i − nx̄2
i=1
n
= x2i − nx̄2 − nx̄2 + nx̄2
i=1
n
= (xi − x̄)2 (12.17)
i=1
Regression 569
n n n
Yi xi = β̂0 xi + β̂1 x2i
i=1 i=1 i=1
n n n
Yi xi = (Y − β̂1 x̄) xi + β̂1 x2i
i=1 i=1 i=1
n
n n n
n
Yi x i
Yi xi = i=1
− β̂1 i=1
xi + β̂1 x2i
i=1
n n i=1 i=1
n n n 2 n
i=1 Yi i=1 xi ( ni=1 xi )
Yi xi = − β̂1 + β̂1 x2i
i=1
n n i=1
n n n n n 2
i=1 Yi xi ( i=1 xi )
Yi xi − i=1
= β̂1 x2i − β̂1
i=1
n i=1
n
n n n n n 2
Yi xi ( i=1 xi )
Yi xi − i=1 i=1
= β̂1 xi −
2
i=1
n i=1
n
n
(x − x̄)(Yi − Y )
β̂1 = i=1 n i (12.18)
i=1 (xi − x̄)
2
After β̂0 and β̂1 have been found, it must be shown that these values will give a minimum
value for the sum of squared errors.
Proof ( ni=1 ε̂2i is a Minimum): If the matrix of partial derivatives of Q as found in (12.8)
n then our β̂ values do give the minimumδQvalue forQ.
is positive definite,
n
Recall from (12.10)
δQ
that δβ 0
= −2 (Y
i=1 i −β 0 −β x
1 i ) and from (12.11) that δβ1 = −2 i=1 (Yi −β0 −β1 xi )(xi ).
This implies that the second-order partials are
n
δ2 Q
= −2 (−1) = 2n
δβ02 i=1
n n
δ2 Q
= −2 (−xi )(xi ) = 2 x2i
δβ12 i=1 i=1
n n
δ2Q
= −2 (−xi ) = 2 xi
δβ0 δβ1 i=1 i=1
2
The determinant of this matrix is 4n ni=1 x2i − 4 ( ni=1 xi ) . It must be shown that this
quantity is always positive to prove that β̂0 and β̂1 as given provide a minimum value for
Q. Note that n is assumed to be greater than zero:
570 Probability and Statistics with R
n
n
2
?
4n x2i − 4 xi >0
i=1 i=1
n 2
( ni=1 xi ) ?
xi −
2
>0
i=1
n
n
(xi − x̄)2 > 0 from (12.17)
i=1
Therefore, the β̂0 and β̂1 calculated do give the minimum value for Q.
Now that the β values that will minimize Q are computed, the fitted regression line is
written
Y:i = β̂0 + β̂1 xi (12.20)
Note that all five of these properties follow from the least squares normal (12.12) and (12.13).
ε̂i = Yi − Y:i
ε̂i = Yi − (β̂0 + β̂1 xi )
n n
ε̂i = (Yi − β̂0 − β̂1 xi )
i=1 i=1
n n n
ε̂i = Yi − nβ̂0 − β̂1 xi = 0 by (12.12)
i=1 i=1 i=1
Regression 571
Proof (Property 5): Given the regression line Y:i = β̂0 + β̂1 xi , if xi = x̄, then
Y:i = β̂0 + β̂1 x̄
Y:i = Y − β̂1 x̄ + β̂1 x̄ using (12.14)
⇒ Y:i = Y
This is simplified first and then differentiated with respect to β. Then, the result is set
equal to 0 to solve for β̂:
Q = Y Y − β X Y − Y Xβ + β X Xβ
Q = Y Y − 2Y Xβ + β X Xβ.
δQ
The expression for δβ can now be calculated:
δQ δ δ δ
= (Y Y) − (2(X Y) β) − (β X Xβ)
δβ δβ δβ δβ
= 0 − 2X Y − [X Xβ + (X X) β]
by Rules for Differentiation 1 and 3 on page 671
= −2X Y − 2X Xβ (12.22)
the normal equations expressed in matrix notation. The worked out solutions for the matrix
form of the simple linear regression model are presented next.
⎡ ⎤
1 x1
⎢ 1 x2 ⎥
⎢ ⎥
Recall that, for the simple linear regression model, X = ⎢ . . ⎥, so
⎣ .. .. ⎦
1 xn
# n $
n i=1 xi
XX= n n 2
. (12.24)
i=1 xi i=1 xi
! " ! "
ab d −b
Also recall that the inverse of a matrix A = is A−1 = 1
, where det A =
cd det A −c a
ad − bc. Then
n & n '2 n
det(X X) = n x2i − xi =n (xi − x̄)2 . (12.25)
i=1 i=1 i=1
So,
# n n $
1 2
i=1 xi − i=1 xi
(X X)−1 = n n . (12.26)
n i=1 (xi − x̄ )2 − i=1 xi n
Likewise,
# n $
i=1 Yi
XY= n . (12.27)
i=1 xi Yi
Regression 573
Next, show β̂0 from (12.14) is equal to the first entry of β̂:
n n n n
2
i=1 xi Yi − i=1 xi i=1 xi Yi ?
n
i=1
= β̂0
n i=1 (xi − x̄ )2
2 2
n
n
n
n
n
n xi Yi xi Yi
n
n
x2i Yi − i=1
n n
i=1
+ i=1
xi Yi i=1
− xi
?
i=1 i=1
n i=1 i=1
= β̂0
n i=1 (xi − x̄ )2
!n " ! n n "
n 2 ( ni=1 xi )2 n
Yi xi − n − x Y
i i − Y xi xi
?
i=1 i=1
n i=1 i=1 i=1
= β̂0
n i=1 (xi − x̄ )2
n n n n
i=1 (xi − x̄ ) − i=1 (xi − x̄)(Yi − Y )
2
Yi i=1 xi
?
i=1
n = β̂0
n i=1 (xi − x̄ ) 2
Example 12.6 Linear Relationship between GPA and SAT Scores The
admissions committee of a comprehensive state university selected at random the records
of 200 second-semester freshmen. The results, first-semester college GPA and SAT scores,
are stored in the data frame Grades. The admissions committee wants to study the linear
relationship between first-semester college grade point average (gpa) and scholastic aptitude
test (sat) scores. Assume that the requirements for model (12.4) are satisfied.
(a) Create a scatterplot of the data to investigate the relationship between gpa and sat
scores.
(b) Obtain the least squares estimates for β0 and β1 , and state the estimated regression
function using
(c) What is the point estimate of the change in the mean gpa when the sat score increases
by 50 points?
(a) The scatterplot in Figure 12.2 on the next page suggests a linear relationship exists
between gpa and sat.
> attach(Grades)
> plot(sat, gpa)
(b)
3.5
3.0
2.5
gpa
2.0
1.5
1.0
800 1000 1200 1400
sat
(c) The point estimate of the change in the mean gpa when the SAT score increases by 50
points is β̂1 · 50 = 0.1547135:
> b1*50
[1] 0.1547135
> detach(Grades)
576 Probability and Statistics with R
In Example 12.5 on page 574, the variance of β̂ was shown to equal σ 2 (X X)−1 . Next, β̂
is shown to be an unbiased estimator of β. Specifically,
If β̂ = (X X)−1 X Y
Then E β̂ = E (X X)−1 X Y
= E (X X)−1 X (Xβ + ε)
= E (X X)−1 X Xβ + (X X)−1 X ε
= E Iβ + (X X)−1 X ε
= β since I and (X X)−1 X are constants and E(ε) = 0.
under the normal error regression model. However, unbiasedness does not guarantee unique-
ness. Fortunately, the Gauss-Markov theorem guarantees that among the class of linear
unbiased estimators for β, β̂ is the best in the sense that the variances of β̂0 , β̂1 , . . . , β̂p are
minimized. Consequently, β̂ is called a best linear unbiased estimator, or a BLUE. Note
that the error variance σ 2 is unknown, but its unbiased estimate is given by
n 2
SSE ε̂
σ̂ 2 = s2 = MSE = = i=1 i (12.31)
n−p n−p
If the matrix V is defined to be (X X)−1 , then σβ̂2 = σ 2 ·vk+1,k+1 , where vk+1,k+1 is the
k
(k + 1)st diagonal entry (k = 0, 1, . . . , p − 1) of V. It is preferable to calculate V with the
command summary([Link])$[Link], where [Link] is a linear model object,
rather than
& with
' the matrix computations t(X)%*%X, where X is the design matrix. Since
β̂ ∼ N β, σβ̂ , where σβ̂2 = σ 2 (X X)−1 , an estimate of σβ̂2 is
2
⎡ ⎤
s2β̂ sβ̂0 ,β̂1 · · · sβ̂0 ,β̂p−1
⎢ ⎥
0
% (
CI 1−α (βk ) = β̂k − t1−α/2;n−p · sβ̂k , β̂k + t1−α/2;n−p · sβ̂k . (12.34)
Note that the degrees of freedom for the t-distribution are n − p because σ 2 is estimated
with MSE = SSE
n−p .
Example 12.7 Consider Example 12.6 on page 574, where the admissions committee of a
comprehensive state university wants to study the linear relationship between first-semester
college grade point averages (gpa) and scholastic aptitude test (sat) scores. These are stored
in the data frame Grades. Assume that the requirements for model (12.4) are satisfied.
> attach(Grades)
> Y <- gpa
> x <- sat
> [Link] <- lm(Y~x)
> X <- cbind(rep(1,200), x)
> XTX <- t(X)%*%X
> solve(XTX)
x
0.3101379642 -2.689270e-04
x -0.0002689270 2.370131e-07
! "
4.948408 × 10−2 −4.290866 × 10−5
s2β̂ =
−4.290866 × 10−5 3.781665 × 10−8
> vcov([Link])
(Intercept) x
(Intercept) 4.948408e-02 -4.290866e-05
x -4.290866e-05 3.781665e-08
(b)
Step 2: Test Statistic — β̂1 = 0.0030943 is the test statistic. Assuming the assumptions
of Model (12.4) are satistfied,
The standardized test statistic under the assumption that H0 is true and its distri-
bution are
β̂1 − β1
∼ t200−2
sβ̂1
I. From the rejection region, reject H0 because |15.912| is greater than 1.6526.
II. From the ℘-value, reject H0 because the ℘-value = 0 is less than 0.10.
To see the test statistics and their ℘-values for the [Link], enter
> summary([Link])$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.192063812 0.2224501802 -5.35879 2.316666e-07
x 0.003094270 0.0001944650 15.91171 2.922995e-37
580 Probability and Statistics with R
and
% (
CI 0.90 (β1 ) = β̂1 − t.95; n−p · sβ̂1 , β̂1 + t.95; n−p · sβ̂1
CI 0.90 (β1 ) = [0.003 − 1.65(0.00019), 0.003 + 1.65(0.00019)]
CI 0.90 (β1 ) = [0.0028, 0.0034] .
Y −Y = Y: − Y + Y − Y: (12.35)
F i GH I F i GH I F i GH Ii
Total Deviation Deviation of Fitted Deviation around the
Regression Value Fitted Regression Line
around the Mean
Note that the total deviation is used to measure the variation of the Yi s without taking the
predictor variable(s) into account. Recall that since ε̂i = Yi − Y:i ,
n n
2 (Y:i − Y )(Yi − Y:i ) = 2 (Y:i − Y )ε̂i
i=1 i=1
n n
=2 Y:i ε̂i − 2Y ε̂i (12.36)
i=1 i=1
= 2×0 − 2×0 = 0,
F GH I F GH I
by Property (4) by Property (1)
The expression in (12.37) is commonly expressed as SST = SSR + SSE , where SST denotes
total sum of squares, SSR stands for regression sum of squares, and SSE represents error
(residual) sum of squares.
zero by property 2. Consequently, SSR in a simple linear regression model has one degree
of freedom.
When a sum of squares is divided by its associated degrees of freedom, the result is
called a mean square and is denoted with MS. Specifically,
SSR SSE
= MSR and = MSE
1 n−2
582 Probability and Statistics with R
Mean squares, unlike sums of squares, are not additive. That is,
σ2 ∼ χn−2 . Consequently,
For the normal error regression model in (12.4), SSE 2
! " ! "
SSE SSE
E = n − 2 =⇒ E = σ 2 =⇒ E[MSE ] = σ 2
σ2 n−2
In other words, the MSE is an unbiased estimator of σ2 .
To find the expected value of MSR, recall from property 5 that Y = β̂0 + β̂1 x̄ and
that the SSR for the simple linear model has one degree of freedom. This implies that
SSR = SSR/1 = MSR. Also, note that the definition of the variance of β̂1 is σβ̂2 =
2 2 1
n
2
SSR = Y:i − Y
i=1
n
2
SSR = (β̂0 + β̂1 xi ) − (β̂0 + β̂1 x̄)
i=1
n
SSR = β̂12 (xi − x̄)2
i=1
n
Then, E[SSR] = E β̂12 i=1 (xi − x̄) , since the x values are not random:
2
3 & '2 4 n
E[SSR] = σβ̂2 + E β̂1 · (xi − x̄)2
1
i=1
n
2
σ
= + β12 · (xi − x̄)2
n
(xi − x̄)2 i=1
F i=1 GH I
by Example 12.5 and (12.26)
n
E[SSR] = σ 2 + β12 · (xi − x̄)2 = E[SSR/1] =
i=1
n
E[MSR] = σ 2 + β12 · (xi − x̄)2
i=1
Note that the mean of the sampling distribution of MSE is σ 2 whether a linear relation-
ship exists between Y and x or not. The mean of the sampling distribution of MSR is also
σ 2 when β1 = 0. Consequently, MSR and MSE will be similar in magnitude when β1 = 0.
Likewise, when β1 = 0, the center of the sampling distribution of MSRwill be larger than
n
the center of the sampling distribution of MSE by approximately β12 · i=1 (xi − x̄)2 .
In particular, the test statistic for testing H0 : β1 = 0 versus H1 : β1 = 0 for model
(12.4) is
MSR
Fobs = . (12.38)
MSE
When the null hypothesis is true, H0 : β1 = 0, then
MSR
∼ F1,n−2 .
MSE
Regression 583
Although it is beyond the scope of this text, it is noted that the quantities SSR SSE
σ2 and σ2
are independent χ random variables with 1 and n − 2 degrees of freedom, respectively. It
2
Finally, values of Fobs close to 1 tend to support the null hypothesis, while large values of
Fobs tend to support the alternative hypothesis. Specifically, the null hypothesis is rejected
if Fobs > f1−α;1,n−2 . S generates an ANOVA table on linear model objects with the function
anova([Link]).
Example 12.8 Construct an ANOVA table using the data in Grades. Then, test if a linear
relationship exists between first-semester college grade point average (gpa) and scholastic
aptitude score (sat) using the information in the ANOVA table at the α = 0.05 level.
> attach(Grades)
> [Link] <- lm(gpa~sat)
> anova([Link])
> detach(Grades)
Step 2: Test Statistic — Fobs since MSR/MSE under the assumption that β1 = 0 has an
F1,198 distribution.
Step 3: Rejection Region Calculations — Because Fobs ∼ F1,198 and this is a one-tailed
test, the rejection region is Fobs > f0.95;1,198 = 3.888. The value of the standardized
test statistic is Fobs = 40.40
0.16 = 253.18.
I. From the rejection region, reject H0 because 253.18 is greater than 3.888.
II. From the ℘-value, reject H0 because the ℘-value = 0 is less than 0.05.
An important matrix in the theory of linear models is the H or “hat” matrix, defined
as
H = X(X X)−1 X (12.39)
The H matrix is a symmetric, idempotent (H2 = H), n × n matrix that transforms the
Yi s into Y:i s. Specifically,
Ŷ = Xβ̂
Ŷ = X(X X)−1 X Y
Ŷ = HY
The values for the sums of squares found in Table 12.3 can also be expressed in terms
of the hat matrix as well as identity and J matrices. Recall that β̂ X Y is a 1 × 1 vector
and is thus equal to its transpose:
Regression 585
SSE = Y Y − β̂ X Y SSR = β̂ X Y − n1 Y JY
= Y Y − Y Xβ̂ = Y Xβ̂ − n1 Y JY
= Y Y − Y X(X X)−1 X Y = Y X(X X)−1 X Y − n1 Y JY
= Y Y − Y HY = Y HY − n1 Y JY
= Y (I − H)Y (12.40) = Y (H − n1 J)Y (12.42)
Knowing that the sums of squares are quadratic forms allows the statistician to prove
various important results. Figure 12.3 provides a graphical representation of decomposition
of the total deviation (as in (12.35)) found in ANOVA.
5 , ⎫
:i
Yi −Y ⎪
⎪
⎧ ⎪
⎪
⎪
⎬
⎪
⎪
4 ⎨ Yi −Y
⎪
⎪
:i −Y ⎪ ⎪
⎪
Y
⎪
⎩ ⎪
⎭
Y =3 3
Y 3 ⎫
Y:i − Y ⎪
⎪
⎪
⎪
⎧ ⎪
⎬
2 ⎪
⎨ Yi − Y
⎪
⎪
Yi − Y:i ⎪
⎪
⎪
⎩ ⎪
⎭
1
0 1 2 3 4 5 6
R2 = 1 R2 = 0.8425 R2 = 0.00405
10 10 10
8 8 8
6 6 6
Y Y Y
4 4 4
2 2 2
0 0 0
0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10
x x x
The sum of squares due to error (SSE ) can be interpreted as the amount of variability
n
: 2 is smaller than the
in Y that is unexplained by a linear model. Since SSE i=1 (Yi − Yi )
sum of squared deviations of any other line, SSE ≤ SST . Note that only in the case of a
SSE
horizontal line would SSE = SST . Consequently, the ratio SST represents the proportion
of variability that cannot be explained by the linear regression model. In an analogous
fashion, R2 , the coefficient of determination, represents the proportion of variability in
the Yi s that can be explained by the simple linear regression model where
SSE SSR
R2 = 1 − = (12.43)
SST SST
computed by dividing each sum of squares by its associated degrees of freedom. That is,
SSE
n−p MSE
Ra2 = 1 − SST
=1− (12.44)
n−1
MST
Although R2 and Ra2 provide a certain measure of “goodness-of-fit” for the fitted model,
they should be used with caution and never as the sole criterion for determining which
among several models is best.
which is equivalent to
When the regression model contains r x-variables, there are r! possible decompositions of
the x-variables.
Example 12.9 Consider the case where r = 3. What are the six decompositions of
SSR(x1 , x2 , x3 )?
Solution:
Example 12.10 The data frame HSwrestler contains information on nine variables for
a group of 78 high school wrestlers that was collected by the human performance lab at
Appalachian State University. The variables are AGE (in years), HT (height in inches), WT
(weight in pounds), ABS (abdominal skinfold measure), TRICEPS (tricep skinfold measure),
SUBSCAP (subscapular skinfold measure), HWFAT (hydrostatic determination of fat), TANFAT
(Tanita determination of fat), and SKFAT (skinfold dtermination of fat). Use S to obtain
the ANOVA results when hydrostatic fat (Y ) is regressed on ABS (x1 ), TRICEPS (x2 ), and
SUBSCAP (x3 ) to verify empirically the results from (12.46) and (12.47).
Solution: The order variables specified in S impact the ANOVA table since the sums of
squares reported are conditional sums of squares. First, the SSR(x1 , x2 , x3 ) is computed
using the formula from Table 12.3 on page 584:
> attach(HSwrestler)
> Y <- HWFAT
> x1 <- ABS
588 Probability and Statistics with R
Computing SSR, SSE , and SST with (12.42), (12.40), and (12.41), respectively, yields
Note that the order of the xi s does not impact the computation of SSR:
H0 : βk = 0 versus H1 : βk = 0
It was shown earlier in (12.33) that tobs = β̂k /sβ̂k could be used as an appropriate test
statistic. It is also possible to test βk = 0 using a general linear test statistic that involves
extra sum of squares. Consider a regression model with three predictor variables (which
represent the full model). To test the hypothesis
H0 : β2 = 0 versus H1 : β2 = 0,
a reduced model where β2 x2 has been eliminated from the full model is computed. The
general linear test statistic is
SSR(F )−SSR(R)
dfF −dfR
Fobs = SSE
(12.48)
df
where F stands for the full model and R stands for the reduced model. SSE is the sum
of squares error for the full model and df is the degrees of freedom for error for the full
model. This Fobs follows an F distribution with (dfF − dfR , df ) degrees of freedom, under
the assumptions that H0 is true and the normal error linear model assumptions are satisfied.
590 Probability and Statistics with R
Example 12.11 Use the data frame HSwrestler to show the equivalence between the
tobs and Fobs values when testing the hypothesis H0 : β2 = 0 versus H1 : β2 = 0 when
regressing HWFAT (Y ) on ABS (x1 ), TRICEPS (x2 ), and SUBSCAP (x3 ).
Solution: The important concept to remember is that both tests assume x1 and x3 are
in the model. Consequently, x2 must be entered into the model last.
> attach(HSwrestler)
> Y <- HWFAT
> x1 <- ABS
> x2 <- TRICEPS
> x3 <- SUBSCAP
> mod132 <- lm(Y~x1+x3+x2)
> anova(mod132)
Analysis of Variance Table
Response: Y (12.49)
Df Sum Sq Mean Sq F value Pr(>F)
x1 1 5072.8 5072.8 535.858 < 2.2e-16 ***
x3 1 132.6 132.6 14.005 0.0003577 ***
x2 1 111.8 111.8 11.814 0.0009682 ***
Residuals 74 700.5 9.5
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Call:
lm(formula = Y ~ x1 + x3 + x2)
Residuals:
Min 1Q Median 3Q Max
-6.4316 -2.4258 -0.4800 2.2797 9.5509
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.06997 0.65592 3.156 0.002315 **
x1 0.31894 0.07447 4.283 5.47e-05 ***
x3 0.06632 0.13622 0.487 0.627819
x2 0.46069 0.13404 3.437 0.000968 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.077 on 74 degrees of freedom
Multiple R-Squared: 0.8836, Adjusted R-squared: 0.8789
F-statistic: 187.2 on 3 and 74 DF, p-value: < 2.2e-16
The full model is Yi = β0 + β1 xi1 + β2 xi2 + β3 xi3 + εi while the reduced model is Yi =
β0 + β3 xi3 + εi . Consequently, the general linear test statistic will be
> attach(HSwrestler)
> Y <- HWFAT
> x1 <- ABS
592 Probability and Statistics with R
Table 12.6: ANOVA table for Example 12.12 on the preceding page
anova Df Sum Sq Mean Sq F value Pr(>F)
x3 1 4939.0 4939.0 521.720 < 2.2e − 16
x1 1 266.4 266.4 28.143 1.129e − 06
x2 1 111.8 111.8 11.814 0.0009682
Residuals 74 700.5 9.5
SSR(F ) − SSR(R)
dfF − dfR
Fobs =
SSE
df
(4939 + 266.4 + 111.8) − (4939)
= 3−1 = 19.98
700.5
74
Since ℘-value = P(F2,74 ≥ 19.98) = 1.15 × 10−7 < 0.01, reject H0 and declare the results
statistically significant. The evidence suggests that ABS and TRICEPS should not be dropped
from a model that already contains SUBSCAP.
Model 1: Y ~ x3
Model 2: Y ~ x3 + x1 + x2
[Link] RSS Df Sum of Sq F Pr(>F)
1 76 1078.80
2 74 700.54 2 378.26 19.978 1.154e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> detach(HSwrestler)
Regression 593
Under the null hypothesis, γ = 0 and (12.51) is a central t-distribution with n − p degrees
of freedom. In the general linear hypothesis, only a two-sided alternative is given; however,
when q = 1, the one-sided alternative, H1 : Kβ > m or H1 : Kβ < m, may be specified
and tested using (12.51).
When the rank of K is greater than one (q > 1), the quantity
Example 12.13 General Linear Model Use a general linear hypothesis with
α = 0.05 to
(a) Test whether β2 xi2 and β3 xi3 can be dropped from the model Yi = β0 + β1 xi1 + β2 xi2 +
β3 xi3 + εi , where x1 = ABS, x2 = TRICEPS, and x3 = SUBSCAP, using information from
the data frame HSwrestler.
(b) Test the two linear relationships
2β1 + β2 = β3
−5β1 + β3 = 0.20
(a)
Step 1: Hypotheses —
H0 : Kβ = m versus H1 : Kβ = m
! " ! "
0 0 1 0 0
where K = and m = .
0 0 0 1 0
Step 3: Rejection Region Calculations — Because Fobs ∼ F2, 74 and this is a one-tailed
test, the rejection region is Fobs > f0.95; 2, 74 = 3.12. The value of the standardized
test statistic is Fobs = 19.98 :
! "! "
1819.5992 251.6221 0.319
[0.319 0.461]
251.6221 561.7319 0.461
Fobs = = 19.98
2(9.47)
> attach(HSwrestler)
> Y <- HWFAT
> x1 <- ABS
> x2 <- TRICEPS
Regression 595
> library(multcomp)
Loading required package: mvtnorm
> summary(glht(mod312, linfct=K, rhs=c(0,0)), test=Ftest())
Linear Hypotheses:
Estimate
1 == 0 0.3189
2 == 0 0.4607
Global Test:
F DF1 DF2 Pr(>F)
1 19.98 2 74 1.154e-07
(b)
Step 1: Hypotheses —
H0 : Kβ = m versus H1 : Kβ = m
! " ! "
0 2 1 −1 0
where K = and m = .
0 −5 0 1 .2
Step 3: Rejection Region Calculations — Because Fobs ∼ F2, 74 and this is a one-tailed
test, the rejection region is Fobs > f0.95; 2, 74 = 3.12. The value of the standardized
test statistic is Fobs = 0.062 :
! "! "
676.0834 297.8446 −0.0091
[−0.0091 − 0.0709]
297.8446 146.8894 −0.0709
Fobs = = 0.0623
2(9.47)
596 Probability and Statistics with R
I. From the rejection region, fail to reject H0 because 0.062 is less than 3.12.
II. From the ℘-value, fail to reject H0 because the ℘-value = 0.94 is greater than
0.05.
Linear Hypotheses:
Estimate
1 == 0 -0.00912
2 == 0.2 0.12911
Global Test:
F DF1 DF2 Pr(>F)
1 0.0623 2 74 0.9396
(c)
Step 1: Hypotheses —
H0 : Kβ = m versus H1 : Kβ = m
where K = 0 0 1 −1 and m = 0 .
Step 3: Rejection Region Calculations — Because Fobs ∼ F1, 74 and this is a one-tailed
test, the rejection region is Fobs > f0.95; 1, 74 = 3.97. The value of the standardized
test statistic is Fobs = 0.062 :
[−0.1418][332.3932][−0.1418]
Fobs = = 0.7056.
2(9.47)
I. From the rejection region, fail to reject H0 because 0.7056 is less than 3.97.
II. From the ℘-value, fail to reject H0 because the ℘-value = 0.40 is greater than
0.05.
Linear Hypotheses:
Estimate
== 0 -0.1418
Global Test:
F DF1 DF2 Pr(>F)
1 0.7056 1 74 0.4036
> detach(HSwrestler)
that produces the smallest ℘-value by regressing the response variable on the xi s one at a
time using lm() and summary() or by using the function add1().
Example 12.14 Model Selection with HSwrestler Create a model for predict-
ing wrestlers’ hydrostatic fat (HWFAT) for the data frame HSwrestler.
(a) Use backward elimination with the predictors AGE, HT, WT, ABS, TRICEPS, and SUBSCAP
and an αcrit of 0.20.
(c) Use the function regsubsets in the R package leaps to select a model using Ra2 as the
criterion.
(d) Use the function regsubsets in the R package leaps to select a model using Mallows’s
Cp as the criterion.
(a) Backward elimination starts with all the variables in the model and eliminates variables
with the largest (least significant) ℘-values:
> attach(HSwrestler)
> # Backward elimination showing all steps
> [Link] <- lm(HWFAT ~ AGE + HT + WT + ABS + TRICEPS + SUBSCAP)
> summary([Link])$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) 13.29369860 9.63026704 1.3804081 1.717917e-01
AGE -0.32893403 0.32157778 -1.0228755 3.098393e-01
HT -0.06730905 0.16050751 -0.4193514 6.762255e-01
WT -0.01365183 0.02590783 -0.5269385 5.998789e-01
ABS 0.37141976 0.08836595 4.2032001 7.548985e-05
TRICEPS 0.38742647 0.13761017 2.8153912 6.301113e-03
SUBSCAP 0.11405213 0.14192779 0.8035927 4.243145e-01
Note that HT has the largest ℘-value of 6.762255e-01, so it is eliminated from the model:
Note that SUBSCAP has the largest ℘-value of 4.306601e-01, so it is eliminated from the
model:
Note that WT has the largest ℘-value of 3.869075e-01, so it is eliminated from the model:
> reg.m3 <- lm(HWFAT ~ AGE + ABS + TRICEPS)
> summary(reg.m3)
Call:
lm(formula = HWFAT ~ AGE + ABS + TRICEPS)
Residuals:
Min 1Q Median 3Q Max
-5.8374 -2.0468 -0.4215 2.3076 7.9850
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 10.61606 4.23272 2.508 0.0143 *
AGE -0.53309 0.26067 -2.045 0.0444 *
ABS 0.35643 0.06354 5.610 3.32e-07 ***
TRICEPS 0.46561 0.09898 4.704 1.16e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.998 on 74 degrees of freedom
Multiple R-Squared: 0.8895, Adjusted R-squared: 0.885
F-statistic: 198.5 on 3 and 74 DF, p-value: < 2.2e-16
The remaining ℘-values for AGE, ABS, and TRICEPS are all less than 0.20, so the model is
composed of these three variables based on backward elimination.
Model:
HWFAT ~ AGE + HT + WT + ABS + TRICEPS + SUBSCAP
Df Sum of Sq RSS AIC F value Pr(F)
<none> 651.05 179.51
AGE 1 9.59 660.64 178.65 1.0463 0.309839
HT 1 1.61 652.66 177.70 0.1759 0.676225
WT 1 2.55 653.60 177.81 0.2777 0.599879
ABS 1 162.00 813.05 194.84 17.6669 7.549e-05 ***
TRICEPS 1 72.68 723.73 185.76 7.9264 0.006301 **
SUBSCAP 1 5.92 656.97 178.21 0.6458 0.424315
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Note that HT has the largest ℘-value of 0.676225, so it is eliminated from the model:
Model:
Regression 601
Note that SUBSCAP has the largest ℘-value of 0.430660, so it is eliminated from the model:
Model:
HWFAT ~ AGE + WT + ABS + TRICEPS
Df Sum of Sq RSS AIC F value Pr(F)
<none> 658.36 176.38
AGE 1 13.62 671.97 175.97 1.5097 0.2231
WT 1 6.83 665.19 175.18 0.7577 0.3869
ABS 1 220.99 879.35 196.95 24.5043 4.621e-06 ***
TRICEPS 1 201.77 860.12 195.23 22.3725 1.068e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Note that WT has the largest ℘-value of 0.3869, so it is eliminated from the model:
Model:
HWFAT ~ AGE + ABS + TRICEPS
Df Sum of Sq RSS AIC F value Pr(F)
<none> 665.19 175.18
AGE 1 37.59 702.78 177.47 4.1823 0.04441 *
ABS 1 282.90 948.08 200.82 31.4712 3.323e-07 ***
TRICEPS 1 198.89 864.08 193.59 22.1259 1.159e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The resulting model uses AGE, ABS, and TRICEPS to predict HWFAT.
(b) Forward selection assumes a model with an intercept only and adds the most significant
(smallest ℘-values) variables one at a time. The function add1() in R is used as the ℘-values
at each step are shown:
Model:
HWFAT ~ 1
Df Sum of Sq RSS AIC F value Pr(F)
<none> 6017.8 341.0
AGE 1 175.0 5842.8 340.7 2.2765 0.1355
HT 1 117.8 5900.0 341.4 1.5175 0.2218
WT 1 3237.6 2780.2 282.7 88.5045 2.219e-14 ***
ABS 1 5072.8 945.0 198.6 407.9929 < 2.2e-16 ***
TRICEPS 1 5056.3 961.5 199.9 399.6462 < 2.2e-16 ***
SUBSCAP 1 4939.0 1078.8 208.9 347.9456 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The variable ABS has the most significant (smallest) ℘-value = 2.2e − 16 with the largest F
value= 407.9929, so it is added to the model:
Model:
HWFAT ~ ABS
Df Sum of Sq RSS AIC F value Pr(F)
<none> 944.96 198.57
AGE 1 80.88 864.08 193.59 7.0199 0.0098255 **
HT 1 61.60 883.36 195.31 5.2298 0.0250250 *
WT 1 43.73 901.22 196.87 3.6396 0.0602498 .
TRICEPS 1 242.17 702.78 177.47 25.8443 2.639e-06 ***
SUBSCAP 1 132.58 812.38 188.77 12.2400 0.0007904 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The variable TRICEPS has the most significant (smallest) ℘-value = 2.639e − 06 with the
largest F value= 25.8443, so it is added to the model:
Model:
HWFAT ~ ABS + TRICEPS
Df Sum of Sq RSS AIC F value Pr(F)
<none> 702.78 177.47
AGE 1 37.59 665.19 175.18 4.1823 0.04441 *
HT 1 25.25 677.54 176.62 2.7574 0.10104
WT 1 30.81 671.97 175.97 3.3932 0.06947 .
SUBSCAP 1 2.24 700.54 179.22 0.2370 0.62782
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The variable AGE has the most significant (smallest) ℘-value = 0.04441 with the largest F
value= 4.1823, so it is added to the model:
Regression 603
Model:
HWFAT ~ ABS + TRICEPS + AGE
Df Sum of Sq RSS AIC F value Pr(F)
<none> 665.19 175.18
HT 1 7.03 658.16 176.35 0.7796 0.3802
WT 1 6.83 658.36 176.38 0.7577 0.3869
SUBSCAP 1 1.97 663.22 176.95 0.2171 0.6427
None of the ℘-values now meet the αcrit level of 0.20, so the model is complete with
ABS, TRICEPS, and AGE being used to predict HWFAT. If a summary is done for the models
where ABS, TRICEPS, and AGE are already in the model and HT, WT, or SUBSCAP were added
individually, the ℘-values would match the last column of the last add1() output:
> summary(lm(HWFAT~ABS+TRICEPS+AGE+HT))$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) 15.6108355 7.06886737 2.2083928 3.035723e-02
ABS 0.3701823 0.06550886 5.6508735 2.902965e-07
TRICEPS 0.4554293 0.09980055 4.5633949 1.990682e-05
AGE -0.4236659 0.28898736 -1.4660361 1.469329e-01
HT -0.1020099 0.11553071 -0.8829675 3.801523e-01
> summary(lm(HWFAT~ABS+TRICEPS+AGE+WT))$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9.94025577 4.31017288 2.3062313 2.393916e-02
ABS 0.39968360 0.08074124 4.9501789 4.621329e-06
TRICEPS 0.46942072 0.09924414 4.7299591 1.068468e-05
AGE -0.38382444 0.31238134 -1.2287048 2.231289e-01
WT -0.01585418 0.01821376 -0.8704507 3.869075e-01
> summary(lm(HWFAT~ABS+TRICEPS+AGE+SUBSCAP))$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) 10.59636278 4.25550451 2.490037 1.504963e-02
ABS 0.33934952 0.07364815 4.607713 1.688668e-05
TRICEPS 0.42485168 0.13249227 3.206615 1.994247e-03
AGE -0.53122920 0.26209533 -2.026855 4.633009e-02
SUBSCAP 0.06218487 0.13346571 0.465924 6.426572e-01
The ℘-value for SUBSCAP is 6.426572e-01= 0.6427 from the add1() output.
Note that in both the forward and backward selection procedures, the same model results:
(HWFAT ~ ABS + TRICEPS + AGE). This is not always the case.
(c) Ra2 is used with R. The R package leaps is needed for the function regsubsets(). The
arguments have predictors as a matrix first, then the response as a vector. The first six
variables of HSwrestler are the predictors, while the response, HWFAT, is in column 7.
604 Probability and Statistics with R
> library(leaps)
> a <- regsubsets([Link](HSwrestler[,-c(7,8,9)]), HSwrestler[,7])
> summary(a)
Subset selection object
6 Variables (and intercept)
Forced in Forced out
AGE FALSE FALSE
HT FALSE FALSE
WT FALSE FALSE
ABS FALSE FALSE
TRICEPS FALSE FALSE
SUBSCAP FALSE FALSE
1 subsets of each size up to 6
Selection Algorithm: exhaustive
AGE HT WT ABS TRICEPS SUBSCAP
1 ( 1 ) " " " " " " "*" " " " "
2 ( 1 ) " " " " " " "*" "*" " "
3 ( 1 ) "*" " " " " "*" "*" " "
4 ( 1 ) "*" "*" " " "*" "*" " "
5 ( 1 ) "*" " " "*" "*" "*" "*"
6 ( 1 ) "*" "*" "*" "*" "*" "*"
> summary(a)$adjr2
[1] 0.8409068 0.8801014 0.8849817 0.8846381 0.8840129 0.8826699
The largest Ra2 value is 0.8849817, which corresponds to the model with three predictors.
The row beside the 3 shows "*" symbols for AGE, ABS, and TRICEPS, so these are the
appropriate predictor variables.
(d) When using Mallows’s Cp , the idea is to select the smallest Cp value less than or equal to
p. In this case, the R package leaps and the output from regsubsets() gives the optimal
value C4 = 2.541953, so the three-predictor (plus an intercept) model using AGE, ABS, and
TRICEPS is again selected:
> summary(a)$cp
[1] 29.051861 4.641808 2.541953 3.775400 5.175856 7.000000
> par(pty="s")
> plot(2:7, summary(a)$cp, ylim=c(2,7), xlab="p", ylab="Cp")
> abline(a=0, b=1)
(e) The function stepAIC() in the MASS package will compute models based on both AIC
and BIC statistics. The argument k of this function will be set equal to 2 for the AIC
statistic and ln(n) for the BIC statistic. The user needs to specify the scope of the model
with the argument scope=. In this case, the scope of the model includes any of the six
predictors AGE, HT, WT, ABS, TRICEPS, and SUBSCAP. For further details, see the stepAIC()
help file. Initial and final output is shown from using stepAIC(). The starting AIC value
is 179.51. The stepAIC() function adds or removes variables until it finds the smallest AIC
value. The - before a variable indicates that the variable will be removed to produce the
given AIC, while a + indicates the variable will be added to produce the given AIC.
> library(MASS) # For function stepAIC()
> [Link] <- lm(HWFAT ~ AGE + HT + WT + ABS + TRICEPS + SUBSCAP)
> [Link] <- stepAIC([Link], direction="both",
+ scope=(~.+SUBSCAP+TRICEPS+ABS+WT+HT+AGE), k=2)
Regression 605
Call:
lm(formula = HWFAT ~ AGE + ABS + TRICEPS)
Coefficients:
(Intercept) AGE ABS TRICEPS
10.6161 -0.5331 0.3564 0.4656
4
3
2
2 3 4 5 6 7
(f) When BIC is the criterion, the model selected is HWFAT ~ ABS + TRICEPS. Only initial
and final output are shown.
> [Link] <- stepAIC([Link], direction="both",
+ scope=(~.+SUBSCAP+TRICEPS+ABS+WT+HT+AGE), k=log(length(HWFAT)))
Start: AIC= 196
HWFAT ~ AGE + HT + WT + ABS + TRICEPS + SUBSCAP
Step: AIC=184.54
HWFAT ~ ABS + TRICEPS
Call:
lm(formula = HWFAT ~ ABS + TRICEPS)
Coefficients:
(Intercept) ABS TRICEPS
2.0590 0.3371 0.5043
> detach(HSwrestler)
[Link] Summary
Variable selection is simply a means to select variables for inclusion or exclusion in a
model that can be used for explanatory or predictive purposes. That is, the goal is not
variable selection per se, rather, the goal is to create a model that adequately explains or
predicts from the data. Stepwise selection procedures do not always guarantee a model will
be selected that meets the user’s need to explain or predict from the data. Criterion-based
methods typically involve a wider search than do stepwise procedures, and many argue that
they return models that are better than those from stepwise procedures. Regardless of
the methods one uses to select a model, additional factors such as the cost to measure the
variables and model diagnostics should be considered in developing a model.
Regression 607
12.11.2 Diagnostics
While fitting a model using the principle of least squares regression requires no dis-
tributional assumptions, using the model for inferential purposes does depend on specific
assumptions. If (12.7) assumes ε ∼ N (0, σ 2 I), that is, the errors in the model are assumed
to be independent and to follow a normal distribution with a mean of zero and a constant
variance, then (12.7) is called the normal error model. Regression diagnostics play a critical
role in the verification of these assumptions. Regression diagnostics are also used to learn
about unusual observations. The diagnostics will often dictate changes in the model selected
initially. These changes emphasize the fact that model building is an iterative process.
Proof:
:
ε̂ = Y − Y
= Y − Xβ̂
= Y − X(X X)−1 X Y
= Y − HY
= (I − H)Y
The diagonal entry of H is denoted as hii , which is referred to as the leverage. Note that
the trace of H is p, the number of parameters (β0 , β1 , . . . , βp−1 ) in the linear model. (See
Problem 10 on page 650.)
Example 12.15 Find the H matrix and display the first five hii values for the model
selected with the AIC statistic from part (e) of Example 12.14 on page 598. Verify that
the sum of the hii values equals p. Recall that the variables selected were AGE, ABS, and
TRICEPS.
608 Probability and Statistics with R
> attach(HSwrestler)
> mod.3 <- lm(HWFAT~AGE+ABS+TRICEPS)
> # Getting the values manually...
> X <- [Link](mod.3)
> n <- nrow(X)
> p <- ncol(X)
> H <- X%*%solve(t(X)%*%X)%*%t(X)
> hii <- diag(H)
> hii[1:5]
1 2 3 4 5
0.05555037 0.02273942 0.03266124 0.02786396 0.20830418
> # Extracts hatvalues in R and S-PLUS
> influence(mod.3)$hat[1:5]
1 2 3 4 5
0.05555037 0.02273942 0.03266124 0.02786396 0.20830418
> # Verifying that sum(h_ii)=p
> sum(hii)
[1] 4
> detach(HSwrestler)
performed with the function bptest() from the R package lmtest; however, the test has
no power asymptotically (Zaman, 2000).
Small values of DW lead to the conclusion that ρ = 0 because adjacent error terms ε̂i −
ε̂i−1 tend to be similar when the data are correlated. The package car has a function
[Link]() that can be used to test for autocorrelation. Only the far right residual
plots (mod3 and mod6) of Figure 12.6 on the following page have small DW values leading
to ℘-values of 0. The ℘-values of the Durbin-Watson test for models 1, 2, 4, and 5 are 0.626,
0.24, 0.716, and 0.324, respectively.
The commands to obtain the Durbin-Watson and Shapiro-Wilk test results for the
residuals in mod1 of Figure 12.6 on the next page follow. Test results for models 2–6
stored in objects mod2 through mod6 can be obtained similarly.
> [Link](mod1)
lag Autocorrelation D-W Statistic p-value
1 0.01009468 1.923398 0.626
Alternative hypothesis: rho != 0
> [Link](resid(mod1))
10
3
2
2
1
1
ε̂i ε̂i ε̂i
0
−1
−5
−1
−2
−10
−3
−2
−0.0180 −0.0170 −0.0160 −0.0150 −0.2 −0.1 0.0 0.1 0.2 −0.05 0.05 0.15 0.25
10
3
2
2
1
1
0
−1
−5
−1
−2
−10
−3
−2
−0.20 −0.10 0.00 −0.5 −0.3 −0.1 0.1 −0.05 0.00 0.05 0.10
FIGURE 12.6: Residual plots for six different models with different residual patterns
3 3
2
1
Residuals
1
0
0
−1
−1
−2
−2
95 60
3
Standardized residuals
95
Standardized residuals
3
98
2
1.0
1
0
0.5
−1
−2
Cook’s distance
0.0
impact on the fitted regression model. One of the ways used to measure outlying Y values
is to evaluate standardized residuals. This is done because residuals may have substantially
different variances. Consequently, it makes sense to consider ε̂i relative to its estimated
standard deviation. When the residuals are rescaled to have unit variance, the resulting
residuals (ri ) are known as internally studentized residuals or standardized residuals,
where
ε̂i ε̂
ri = 2 = √ i . (12.59)
+ (ε̂i ) σ̂ · 1 − hii
Var
0
−1
−2
−3
−3 −2 −1 0 1 2 3
Theoretical Quantiles
FIGURE 12.8: Quantile-quantile plot for mod1 in Figure 12.6 on the preceding page
Another refinement to make the residuals more effective in detecting outlying observa-
tions is to use deleted residuals. Specifically, when a regression model is computed where
the ith case is excluded, the ith prediction is denoted Y:i(i) , and the deleted residual (ε̂(i) ) is
then defined as
Fortunately, an algebraic equivalent expression for ε̂(i) exists that does not require the
computation of Y:i(i) for each omitted case. Specifically, it can be shown that ε̂(i) = Yi −
612 Probability and Statistics with R
Y:i(i) = ε̂i
1−hii . The estimated variance of the ε̂(i) is
2
σ̂(i) MSE (i)
+ [ε̂(i) ] =
Var = (12.61)
1 − hii 1 − hii
Ordinarily, one prefers to study the studentized deleted residuals (ri∗ ) rather than
the ordinary deleted residuals. The ith studentized deleted residual is defined as
ε̂i
ε̂(i) ε̂
ri∗ = 2 = .1−hii = √i (12.62)
+ (ε̂(i) )
Var
2
σ̂(i) σ̂(i) · 1 − hii
1−hii
Again, there is an algebraic equivalent to (12.62) that avoids doing n regressions. The
algebraic equivalent definition of ri∗ is
& n − p − 1 ' 12
ri∗ = ri · ∼ tn−p−1 (12.63)
n − p − ri2
When the model is correct, each studentized deleted residual follows a t-distribution
with n − p − 1 degrees of freedom. Even though it is very likely only a few “large” ri∗ s
will be of interest, by identifying them as large, all cases have implicitly been tested. To
control the overall significance level, a Bonferroni approach is often used where ri∗ values are
declared significant if their absolute value exceeds t1−α/2n;n−p−1 . However, this approach
does tend to be conservative, especially for large n. The R function rstudent() computes
the studentized deleted residuals according to (12.63). The function studres() in the
MASS package (which can be used with R or S-PLUS) also computes the studentized deleted
residuals according to (12.63).
Example 12.16 Compute and plot the residuals, standardized residuals, and studentized
residuals for the model HWFAT ~ ABS + TRICEPS versus the fitted values using the data
frame HSwrestler. What HWFAT values do the residuals indicate are unusual? Can any of
the studentized residuals be considered an outlier according to the Bonferroni approach if
the significance level is 0.20?
> attach(HSwrestler)
> library(MASS)
> mod.2 <- lm(HWFAT~ABS+TRICEPS)
> par(mfrow=c(2,2))
> plot(fitted(mod.2), resid(mod.2), ylim=c(-10,10), main="")
> title(main="Residuals vs Fitted")
> abline(h=0, lty=2)
> plot(fitted(mod.2), stdres(mod.2), ylim=c(-3.5,3.5), main="")
> title(main="Standardized Residuals vs Fitted")
> abline(h=0, lty=2)
> plot(fitted(mod.2), studres(mod.2), ylim=c(-3.5,3.5), main="")
> title(main="Studentized Residuals vs Fitted")
> abline(h=0, lty=2)
> plot(mod.2, which=1, main="Default Graph 1")
> par(mfrow=c(1,1))
Regression 613
3
2
5
stdres(mod.2)
resid(mod.2)
1
0
−1
−5
−3
−10
10 15 20 25 30 35 40 10 15 20 25 30 35 40
fitted(mod.2) fitted(mod.2)
35
3
22
2
studres(mod.2)
5
Residuals
1
0
−1
−5
−3
42
10 15 20 25 30 35 40 10 15 20 25 30 35 40
FIGURE 12.9: Residuals versus fitted values for the model HWFAT ∼ ABS + TRICEPS
Cases 42, 22, and 35 have the largest absolute values of their plain residuals, standard-
ized residuals, and studentized residuals. Case 35 could be considered an outlier using a
significance level of α = 0.20 since the critical value is 3.121816.
model. When the estimated parameters are substantially different with and without the
ith case, the ith case is said to be influential. That is, not all high leverage observations
are influential. Clearly, which cases are influential (if any) may change when the model is
changed.
Influential Observations Some influence measures examined next, all of which measure
the effect of deleting the ith observation, include: Cook’s distance, Di , which measures
the effect on the β̂ or, equivalently, on the predicted values (see (12.64)); DFFITSi , which
measures the effect on the predicted Ŷi s; and DFBETASk(i) , which measures the effect on
the βˆj s. Fortunately, all of the influence measures considered can be computed from the
results of a single regression using all of the data.
Cook’s Distance Cook’s distance evaluates the influence of the ith case on all of the n
fitted values. It is a combined measure of the standardized residual (ri ) and the leverage
value (hii ) that produces a number used to assess the impact of removing the ith observation
on the all regression coefficients (β). Cook’s Di is defined as
Y : Y
: (i) − Y : (i) − Y
: β̂(i) − β̂ X X β̂(i) − β̂
= (12.64)
pσ̂ 2 pσ̂ 2
An algebraically equivalent expression for Di is
! "
ε̂2 hii ri2 hii
Di = i 2 = . (12.65)
pσ̂ (1 − hii )2 p 1 − hii
Di values are generally flagged for further scrutiny when they exceed f0.50;p,n−p ; however,
the exact distribution of Di is unknown, and the use of f0.50;p,n−p is only a suggestion.
Oftentimes, a simple graph of the Di s will indicate values that require further scrutiny. One
can always program a function according to (12.65) to compute the Di s; however, a better
approach is to use built-in functions on linear model objects. In R, [Link]()
will compute the Di s. The package car also has the function cookd() which will work
in both R and S-PLUS. The function [Link]() computes basic quantities used in
many diagnostics, including hii values and coefficients used to compute DFBETAS. In R,
[Link]() returns β̂k(i) − β̂k , while in S-PLUS, β̂k(i) is returned. The user should
consult the documentation for further details.
If the matrix V is defined to be (X X)−1 , then σβ̂2 = σ 2 · vk+1,k+1 , where vk+1,k+1 is the
k
(k + 1)st diagonal entry (k = 0, 1, . . . , p − 1) of V. To compute DFBETAS with R, use
dfbetas(linear model). The function dfbetas(linear model) in the package car will
work for both R and S-PLUS.
Example 12.17 Kinder The data frame Kinder contains the height in inches and
weight in pounds of 20 children from a kindergarten class. Use all 20 observations and
construct a regression model where the results are stored in the object mod by regressing
height on weight.
(a) Create a scatterplot of height versus weight to verify a possible linear relationship
between the two variables.
(b) Compute and display the hat values for mod in a graph. Use the graph to identify the
two largest hat values. Superimpose a horizontal line at 2p/n. Remove the values that
exceed 2p/n and regress height on weight, storing the results in an object named modk.
(c) Remove case 19 from the original data frame Kinder and regress height on weight,
storing the results in modk19. Is the child with the largest hat value an influential
observation if one considers the 19 observations without case 19 from the original data
frame? Compute and consider Cook’s Di , DFFITSi , and DFBETASk(i) , in reaching
a conclusion. Specifically, produce a graph showing hii , the differences in β̂1(i) −
β̂1 , DF BET ASk(i) , studentized residuals, DF F IT Si , and Cook’s Di along with the
corresponding values that flag observations for further scrutiny assuming α = 0.10.
(d) Remove case 20 from the data frame Kinder and regress height on weight, storing the
results in modk20. Is the child with the largest hat value an influential observation if
one considers the 19 observations without case 20 from the original data frame? Com-
pute and consider Cook’s Di , DFFITSi , and DFBETASk(i) in reaching a conclusion.
Specifically, produce a graph showing hii , the differences in β̂1(i) − β̂1 , DFBETASk(i) ,
616 Probability and Statistics with R
studentized residuals, DFFITSi , and Cook’s Di along with the corresponding values
that flag observations for further scrutiny assuming α = 0.10.
(e) Create a scatterplot showing all 20 children. Use a solid circle to identify case 19 and a
solid triangle to identify case 20. Superimpose the lines for models mod (type=1), modk
(type=2), mod19 (type=3), and mod20 (type=4).
Solution: The code given is for R.
44
42
40
30 35 40 45 50 55 60
wt
FIGURE 12.10: Scatterplot of height (ht) versus weight (wt) for the data set Kinder
(b) Note that the largest hii values are for observations 19 and 20. Child 19, although taller
and heavier than the other children, seems to follow the linear trend of increased height
with increased weight. Child 20 appears to be right around the 50% percentile in height
but has the largest weight (obese child).
> mod <- lm(ht~wt)
> hii <- [Link](mod)$hat
> hii
1 2 3 4 5 6
0.06738101 0.08955925 0.12575694 0.08161811 0.05184540 0.06981955
7 8 9 10 11 12
0.05268211 0.06474060 0.06038889 0.06100160 0.05773825 0.05030394
13 14 15 16 17 18
0.05499910 0.05573322 0.05093256 0.05688131 0.10054318 0.08821112
19 20
0.37485962 0.38500423
The following code creates the H matrix and extracts the diagonal values (leverage values).
It is better to use internal functions rather than matrix multiplications.
Regression 617
0.15
0.05
5 10 15 20
Index
FIGURE 12.11: Graph of leverage values versus order for regressing height on weight for
the data set Kinder
(c) The 19th observation now corresponds to the “obese” child. From the diagnos-
tics below, the obese child is flagged in each graph for further scrutiny. Note that
[Link](linear model)$coefficients returns β̂k(i) − β̂k in R, while the same
command in S-PLUS returns β̂k(i) . The “obese” child is an observation with high leverage
that is also influential.
> library(MASS) # Need for function studres()
> library(car) # Need for function cookd
> modk19 <- lm(ht[-19]~wt[-19])
> n <- 19
> p <- 2
> par(mfrow=c(2,3))
> hiik19 <- [Link](modk19)$hat # extracting hii values
> plot(hiik19, ylab="Leverage")
> cv <- 2*p/n
> abline(h=cv, lty=2)
> plot([Link](modk19)$coefficients[,2],
+ ylab="Difference in Coefficients")
618 Probability and Statistics with R
0
0.5
−0.05
−2
DFBETAS
Leverage
0.3
−4
−0.15
−6
0.1
5 10 15 5 10 15 5 10 15
8
Cook´s Distance
0
−2
6
DFFITS
−2
−4
4
−4
2
−6
−6
5 10 15 5 10 15 5 10 15
FIGURE 12.12: Diagnostic graphs for modk19 requested in part (c) of Example 12.17
(d) The 19th observation now corresponds to the “tall but normal weight” child. From the
diagnostics below, this child is flagged in the leverage, DFBETAS, and DFFITS graphs
for further scrutiny. Interestingly, it is not flagged with Cook’s Di . The “tall but normal
weight” child only marginally alters the regression line. Consequently, the 19th observation
has high leverage but is not that influential. See the graph for part (e) for a visual
explanation.
> modk20 <- lm(ht[-20]~wt[-20])
> n <- 19
> p <- 2
> par(mfrow=c(2,3))
> hiik20 <- [Link](modk20)$hat
Regression 619
1.0
Difference in Coefficients
0.5
0.5
DFBETAS
Leverage
0.01
0.3
0.0
−0.01
0.1
−0.5
5 10 15 5 10 15 5 10 15
Cook´s Distance
1
0.4
0.5
DFFITS
0
0.2
0.0
−1
−0.5
0.0
−2
5 10 15 5 10 15 5 10 15
FIGURE 12.13: Diagnostic graphs for modk20 requested in part (d) of Example 12.17
(e) In Figure 12.14 on the next page, when all 20 cases are included in the regression, cases
19 (solid circle) and 20 (solid triangle) both have large leverage values; however, if case 20
is omitted, case 19 still has a large leverage value, yet it is not very influential. Consider
the differences between the lines modk20 (dot-dash, case 20 omitted) and modk (dash, where
cases 19 and 20 are omitted). There is very little difference between the lines modk20 and
modk. On the other hand, if case 19 (solid circle) is omitted, the resulting regression modk19
(dotted) is substantially different from modk. In other words, case 20 has high leverage and
is influential when case 19 is omitted.
620 Probability and Statistics with R
45
40
30 35 40 45 50 55 60
Weight in Pounds
FIGURE 12.14: Scatterplot of height versus weight for data from Kinder with four
superimposed regression lines: The solid line is for model mod (all observations); the dot-dash
line is for model modk20 (case 20 omitted); the dashed line is for model modk (case 19 and
20 omitted); and the dotted line is for model modk19 (case 19 omitted).
12.11.3 Transformations
When residual analysis reveals serious problems, or when the relationships between the
response and predictors are clearly non-linear, regression may still yield a reasonable model
with either a transformation of the response variable, the predictors, or both response and
predictors. When a scatterplot between the response and a predictor shows a non-linear
relationship where the residuals are reasonably normal in distribution, appropriate trans-
formations on the predictor may linearize the relationship between the variables without
drastically altering the distribution of the residuals. After the transformation of the predic-
tor(s), the residuals produced with the transformed variable(s) in the new model will need
to be reanalyzed to assure normality assumptions are still satisfied.
Transform x1 : The top left graph in Figure 12.15 shows a non-linear relationship between
Y and x1 . The second graph shows the residuals from regressing Y on x1 ,
both the first and second graphs suggest a simple transformation on x1 . The
pattern suggests a square root transformation. The resulting scatterplot and
√
residual analysis for regressing Y on x0.5
1 = x1 are illustrated in the bottom
row of graphs. The curvilinear relationship evident in both the scatterplot
and the residual plot using the untransformed x1 disappear once a square
root transformation is applied to x1 .
> attach(SimDataXT)
> par(mfrow=c(2,3))
> plot(x1, Y)
> lines(x1, x1^.5) # function Y = x1^.5
> plot(lm(Y~x1), which=c(1,2)) # Residual and Q-Q normal plots
> plot(x1^.5, Y)
> mod1 <- lm(Y~I(x1^.5)) # Works in R for S-PLUS see *
> abline(mod1)
> plot(mod1, which=c(1,2)) # Q-Q plot in S-PLUS is 4 not 2
> par(mfrow=c(1,1))
* There are negative values in x1 , and taking their square root produces
NA values. R, by default, removes missing observations in its lm() function
with [Link]=[Link]; however, S-PLUS does not. To remove the NA
observations while using the function lm() in S-PLUS, use the argument
[Link]=[Link] inside the lm() function.
Standardized residuals
4
157 157
3
93 93
2
2
Residuals
8
1
Y
0
4
−2
2
−2
200
0
−4
200
0 20 40 60 80 120 2 4 6 8 10 12 14 −3 −2 −1 0 1 2 3
Standardized residuals
157 157
3
93 93
2
Residuals
8
1
Y
0
4
−1
2
−2
−3
63
0
63
0 2 4 6 8 10 12 0 2 4 6 8 10 12 −3 −2 −1 0 1 2 3
FIGURE 12.15: Scatterplot, residuals versus fitted values, and quantile-quantile plot of
standardized residuals for Y versus x1 and Y versus x0.5
1 models
622 Probability and Statistics with R
Transform x2 : The concave up relationship depicted in the first two graphs of Figure 12.16
suggests a quadratic transformation on x2 . The resulting scatterplot and
residual graphs for the transformed predictor are depicted in the bottom
row of graphs.
> par(mfrow=c(2,3))
> plot(x2, Y)
> lines(x2, x2^2) # function Y = x2^2
> plot(lm(Y~x2),which=c(1,2)) # Residuals and Q-Q normal plots
> plot(x2^2, Y)
> mod2 <- lm(Y~I(x2^2))
> abline(mod2)
> plot(mod2, which=c(1,2))
> par(mfrow=c(1,1))
Standardized residuals
21 2
3
157 1
157
2
2
Residuals
8
1
Y
0
4
−2
2
−2
0
Standardized residuals
157 157
3
93 93
64 64
2
Residuals
8
1
Y
0
4
−1
2
−2
0
−3
0 2 4 6 8 10 12 0 2 4 6 8 10 12 −3 −2 −1 0 1 2 3
FIGURE 12.16: Scatterplot, residuals versus fitted values, and quantile-quantile plot of
standardized residuals for Y versus x2 and Y versus x22 models
Transform x3 : The first two graphs of Figure 12.17 on the facing page suggest a reciprocal
transformation on x3 . As before, the graphs in the second row of Figure
12.17 are for the transformed predictor (x3 ).
> par(mfrow=c(2,3))
> plot(x3, Y)
> lines(x3, x3^(-1)) # function Y = 1/x3
> plot(lm(Y~x3),which=c(1,2)) # Residuals and Q-Q normal plots
> plot(x3^(-1), Y)
> mod3 <- lm(Y~I(x3^(-1)))
> abline(mod3)
> plot(mod3, which=c(1,2))
> par(mfrow=c(1,1))
> detach(SimDataXT)
Regression 623
Residuals vs Fitted Normal Q−Q
2 4 6 8
12
Standardized residuals
3
2 198 2
196
198
196
2
Residuals
8
1
Y
0
4
−2
2
−1
0
−6
0 1 2 3 4 5 −6 −4 −2 0 2 4 6 −3 −2 −1 0 1 2 3
1 2 3 4
12
Standardized residuals
157 157
3
93 93
64 64
2
Residuals
8
1
Y
0
4
−1
2
−2
0
−3
0 2 4 6 8 10 12 0 2 4 6 8 10 12 −3 −2 −1 0 1 2 3
FIGURE 12.17: Scatterplot, residuals versus fitted values, and quantile-quantile plot of
standardized residuals for Y versus x3 and Y versus x−1
3 models
[Link] Collinearity
Collinearity in regression occurs when some of the predictors are a linear combination of
other predictors. When X X is singular, there is said to be exact collinearity and there
is no unique estimate of β. When X X is near singular, the problem is often called
multicollinearity. Multicollinearity causes problems with the estimation of β and its
subsequent interpretation. Severe multicollinearity can cause the sign of the coefficients
to be opposite what is expected and typically inflates the standard errors of the estimates
to the point where variables appear no longer to be significant. Two techniques to detect
collinearity include computation of the condition number and computation of the variance
inflation factor.
The condition number κ is defined as the square root of the largest eigenvalue of
X X divided by the smallest eigenvalue of X X. κ values between 30 and 100 indicate that
there are moderate to strong dependencies among the predictors. κ values greater than
100 indicate serious multicollinearity problems. The S function kappa() can be used to
estimate the condition number of a matrix.
A related method of detecting multicollinearity is to regress xj on all of the other
predictors. When the coefficient of determination (Rj2 ) from regressing xj on all of the
other predictors is near one, there is multicollinearity among the predictors. The variance
inflation factor is defined as
1
VIFj = . (12.69)
1 − Rj2
When there are dependencies among the predictors, Rj2 will be near one and VIFj will be
large. VIFj values greater than 10√ suggest serious collinearity. The VIFj for a predictor xj
can be interpreted as the factor VIFj by which the standard error of β̂j is increased
due to the presence of multicollinearity.
Example 12.19 Multicollinearity In Example 12.18 on page 620, using the data
frame SimDataXT, Y was regressed on the transformed variables x1 , x2 , and x3 one at a
time.
−1
(a) Regress Y on x0.5 2
1 , x2 , and x3 and store the results in the object modC. Are there any
linear dependencies among the predictors?
624 Probability and Statistics with R
singular model.
> attach(SimDataXT)
> modC <- lm(Y~I(x1^.5)+I(x2^2)+I(x3^(-1)))
> summary(modC)
Call:
lm(formula = Y ~ I(x1^0.5) + I(x2^2) + I(x3^(-1)))
Residuals:
Min 1Q Median 3Q Max
-2.56394 -0.77548 -0.01170 0.75323 3.43862
Note that the output shown is from R. To remove the NA observations (produced from taking
the square root of a negative value) while using the function lm() in S-PLUS, use the option
[Link]=[Link] inside the function lm().
Verify the results with the function kappa(). The argument exact=TRUE used with the
function kappa() only works with R.
> 1/(1-summary(lm(I(x1^.5)~I(x2^2)))$[Link])
[1] 382.7241
Compute the variance inflation factors with the function vif() from the car package:
Verify that the standard error forβ̂1 from a model where Y is regressed solely on x0.5
1 to
modB increases by approximately VIF1 :
by the method of maximum likelihood. The function boxcox() of the MASS package produces
a plot of the log-likelihood against the transformation parameter λ for a particular model.
By default, the range of λ is from −2 to 2. However, once the value of λ that maximizes the
log-likelihood is known, the range of the plot in boxcox() can be tightened to highlight the
area where the function is maximized with the argument lambda=. For more details, see
the boxcox() help file. The boxcox() function is generally used just to get an idea for an
appropriate transformation. The value of λ that maximizes the log-likelihood function may
turn out to be 0.53; but if there is a possible explanation for taking the square root of the
response, the transformation applied should be λ = 0.5 and not the value that maximizes
the log-likelihood function.
Example 12.20 Box-Cox Transformation Use the data frame SimDataST and
the boxcox() function to find the transformation on Y that maximizes the log-likelihood
of the model created by regressing Y1 on x1 . Once the value of λ that maximizes the
log-likelihood is known, reduce the range of the plot produced with boxcox() to focus on
the area around the value of λ that maximizes the log-likelihood.
Solution: Using the default range −2 < λ < 2, the boxcox() function shows that the
transformation λ = 0, that is, ln Y , comes close to maximizing the log-likelihood and is
included in the 95% confidence band for λ, as seen in Figure 12.18 on the next page.
Consequently, the range of λ is reduced and plotted over the region −0.3 to 0.3 using the
argument lambda=seq(-.3,.3,.01):
> attach(SimDataST)
> library(MASS)
> par(mfrow=c(1,2)) # 1 row by 2 columns
> modx1 <- lm(Y1~x1)
> boxcox(modx1)
> boxcox(modx1, lambda=seq(-.3,.3,.01))
> par(mfrow=c(1,1))
> detach(SimDataST)
Regression 627
−750
95%
−850
log−Likelihood
log−Likelihood
−950
−1050
λ λ
FIGURE 12.18: Box-Cox graph of λ for Example 12.20 on the facing page
Solution: Based on the first two graphs of the first row of Figure 12.19 on the next page,
and the subsequent plot of λ, the transformation λ = 0, that is, ln(Y ), is justified. Once the
response is transformed, the problems of non-normality and unequal variance of the errors
apparently disappear.
> attach(SimDataST)
> library(MASS)
> par(mfrow=c(2,3))
> plot(x1, Y1)
> modx1 <- lm(Y1~x1)
> plot(modx1, which=1)
> boxcox(modx1, lambda=seq(-.3,.3,.01))
> plot(x1, log(Y1))
> plot(lm(log(Y1)~x1), which=c(1,2)) #Q-Q plot in S-PLUS is 4 not 2
> par(mfrow=c(1,1))
> detach(SimDataST)
Residuals vs Fitted
40
158
192 95%
−746
15
191
log−Likelihood
30
Residuals
−750
Y1
20
0 5
10
−754
−10
0
1.5 2.0 2.5 3.0 0 5 10 15 20 −0.3 −0.1 0.0 0.1 0.2 0.3
x1 Fitted values λ
1.0
3.5
3
Standardized residuals
123 158 158123
2
0.5
Residuals
2.5
log(Y1)
1
0.0
0
1.5
−2
0.5
−1.0
4
4
FIGURE 12.19: Scatterplot and residual versus fitted plot of Y1 versus x1 ; Box-Cox plot of
λ; scatterplot, residual versus fitted plot, and quantile-quantile plot of ln(Y1 ) versus x1
transformation for the response variable make sense? Use and plot the results from the
boxcox() function applied to modx2. In the second row of graphs, create a scatterplot of
Y2−1 versus x2 , a plot of the residuals versus the fits for the model I(Y2^(-1))~x2, and
a quantile-quantile normal plot of the residuals from the model I(Y2^(-1))~x2. Based
on the second row of graphs, do the assumptions for the normal error model seem to be
satisfied for the model I(Y2^(-1))~x2?
Solution: Based on the first two graphs of the first row of Figure 12.20 on the facing page,
and the subsequent plot of λ, the transformation λ = −1, that is, Y2−1 , is justified. Once
the response is transformed, the non-normality problem as well as the unequal variance of
the errors problem appear to vanish.
> attach(SimDataST)
> par(mfrow=c(2,3))
> plot(x2, Y2)
> modx2 <- lm(Y2~x2)
> plot(modx2, which=1)
> boxcox(modx2, lambda=seq(-1.4,-.6,.01))
> plot(x2, Y2^(-1))
> plot(lm(I(Y2^(-1))~x2), which=c(1,2)) #Q-Q plot in S-PLUS is 4 not 2
> par(mfrow=c(1,1))
As mentioned earlier, at times it will be necessary to transform the response and the
predictor. Consider the top left graph in Figure 12.21 on page 630 along with the bottom
left graph produced with boxcox() suggesting a ln Y transformation. The middle column
of graphs depicts a scatterplot of loge Y versus x3 with a superimposed line from the least
Regression 629
Residuals vs Fitted
1.2
−13
43 15
log−Likelihood
95%
Residuals
0.8
Y2
−15
−0.2
0.4
−17
1.5 2.0 2.5 3.0 0.3 0.4 0.5 0.6 0.7 0.8 −1.4 −1.2 −1.0 −0.8 −0.6
x2 Fitted values λ
Standardized residuals
1.0
157 157
3
93 93
64 64
3.0
2
0.5
Residuals
Y2^(−1)
1
2.0
0
−0.5
1.0
−2
1.5 2.0 2.5 3.0 1.5 2.0 2.5 3.0 −3 −2 −1 0 1 2 3
FIGURE 12.20: Scatterplot and residual versus fitted plot of Y2 versus x2 ; Box-Cox plot of
λ; scatterplot, residual versus fitted plot, and quantile-quantile plot of Y2−1 versus x2
squares fit of the model log(Y1)~x3 as well as the residuals versus fits plot for the same
model. Close scrutiny reveals a slight curvilinear pattern in both of the graphs in the middle
column. This suggests some type of transformation for x3 . The slight curvature is eliminated
in both the scatterplot and the residual plot by applying a square root transformation to
x3 . The R code used to create Figure 12.21 is
> library(MASS)
> par(mfrow=c(2,3))
> plot(x3, Y1)
> plot(x3, log(Y1))
> mod <- lm(log(Y1)~x3)
> abline(mod)
> plot(x3^.5, log(Y1))
> mod2 <- lm(log(Y1)~I(x3^.5))
> abline(mod2)
> boxcox(lm(Y1~x3), lambda=seq(-.3,.3,.01))
> plot(mod, which=1)
> plot(mod2, which=1)
> par(mfrow=c(1,1))
> detach(SimDataST)
630 Probability and Statistics with R
40
3.5
3.5
30
2.5
2.5
log(Y1)
log(Y1)
Y1
20
1.5
1.5
10
0.5
0.5
0
x3 x3 x3^0.5
1.0
−765 −760 −755 −750
0.5
Residuals
Residuals
0.0
−1.0
−1.0
4 4
−0.3 −0.1 0.0 0.1 0.2 0.3 1.5 2.0 2.5 3.0 1.5 2.0 2.5 3.0
FIGURE 12.21: Process of model building with transformations: (I) original scatterplot,
(II) boxcox() transformation suggestion, (III) scatterplot with Y transformed, (IV) residual
plot shows curvature, (V) x-variable transformed, (VI) residuals appear normalized
which is in the form of a simple linear model (Y = β0 + β1 x + ε). Note that the errors in a
simple linear model are additive.
The parameter β in (12.71) can be interpreted as the percent change in Q over the
percent change in P , which is the definition of price elasticity. In other words, |β| =
price elasticity. When dealing with a simple linear model of the form
y = ln(x)
Δ ln(x) ≈ Δy ≈ 10% −→
Δ ln(x) ≈ Δy ≈ 10% −→
1 3 3.3 6 6.6
Δx 0.3 Δx 0.6
x = 3 = 0.1 x = 6 = 0.1
β1 can be interpreted as
%Δy
β1 = . (12.73)
%Δx
In Example 2.24 on page 61, the data frame Animals from the MASS package was used to
find the least squares line for regressing log(brain) on log(body) once the three dinosaurs
were removed from the data. (Note that log(x) in S is the natural logarithm function,
ln(x).) The resulting least squares estimates of β0 and β1 after the dinosaurs are removed
are
> library(MASS)
> attach(Animals)
> SA <- Animals[order(body),]
> NoDINO <- SA[-c(28:26),]
> detach(Animals)
> attach(NoDINO)
> Y <- log(brain)
> x <- log(body)
> [Link] <- lm(Y~x)
> ([Link])$coef
(Intercept) x
2.1504121 0.7522607
> detach(NoDINO)
If the body weight of an animal increases by 1%, the approximate increase in brain weight
is (0.01 × 0.75 = 0.0075 = 0.75%) since β̂1 = 0.75. The predicted brain weight of the Jaguar
whose weight is listed as 100 kg. with the fitted model is 2.15 + 0.75 × ln(100) = 5.61.
However, this must be back transformed to get the units of original brain measurement
(grams). The brain weight predicted by the model is exp(5.61) = 274.431 g. If said
632 Probability and Statistics with R
Jaguar were to increase its weight by 10%, the expected increase in brain weight would
be approximately 7.5% for a new weight of 1.075 × 274.431 = 295.014 g. The actual brain
weight change predicted by the model for a body weight of 110 kg is 294.83 g and the change
in brain weight as predicted from the model is 7.43% (see Table 12.8). Note that for this
model, β̂1 = 0.0752 ≈ ΔY = 0.0743. In fact, when both the response and the predictors
have been transformed with a natural logarithm, one can use the percentage interpretation
of β1 as in (12.73) and be very close to the actual change given by the model for small
changes in the x-variables. The parameters of growth models of the form P (t) = ceβt are
x ln(x) ln(Y ) Y
100.0 4.605 5.615 274.43
110.0 4.700 5.686 294.83
Δ 0.1 0.0743
often estimated with ordinary least squares regression after taking the natural logarithms
of both sides since ln P (t) = ln(c) + βt is the form of a simple linear model. When the slope,
β, is estimated for such a model, it provides an estimate of the approximate growth rate in
units of t. More generally, for models of the form ln Y = β0 + β1 x, for each unit of increase
in x, Y increases roughly by β1 × 100%.
> [Link](4)
2 3 4
1 0 0 0
2 1 0 0
3 0 1 0
4 0 0 1
Regression 633
The rows of this matrix (4 × 3) are the levels of the qualitative predictor and the columns
are the dummy variables. R assigns levels to a qualitative variable in alphabetical order by
default.
Example 12.23 Ease Levels Dummy Variables Consider the variable Ease
from the EPIDURAL data frame. Define appropriate dummy variables to specify the three
levels of this variable.
Solution: The three levels of Ease (Difficult, Easy, and Impossible) require two
dummy variables to be able to identify all three levels of Ease:
> attach(EPIDURAL)
> contrasts(Ease)
Easy Impossible
Difficult 0 0
Easy 1 0
Impossible 0 1
Note that the first level in alphabetical order is Difficult. To change the first level of
Ease to Easy, enter
> levels(Ease) <- c("Easy", "Difficult", "Impossible")
> levels(Ease)
[1] "Easy" "Difficult" "Impossible"
> contrasts(Ease)
Difficult Impossible
Easy 0 0
Difficult 1 0
Impossible 0 1
> detach(EPIDURAL)
The simplest situation where dummy variables might be used in a regression model
is when the qualitative predictor has only two levels. The regression model for a single
quantitative predictor (x1 ) and a dummy variable (D1 ) is written
Y = β0 + β1 x1 + β2 D1 + β3 x1 D1 + ε (12.74)
where
0 for the first level
D1 =
1 for the second level
The model in (12.74) when D1 has two levels will yield one of four possible scenarios, as
shown in Figure 12.23 on the next page. This type of model requires the user to answer
three basic questions:
(1) Are the lines the same?
(2) Are the slopes the same?
(3) Are the intercepts the same?
To address basic question (1), the null hypothesis H0 : β2 = β3 = 0 must be tested.
One way to perform the test is to use the general linear test statistic based on the full
model found in (12.74) and the reduced model Y = β0 + β1 x1 + ε. If the null hypothesis
is not rejected, the interpretation is that there is one line present (the intercept and the
634 Probability and Statistics with R
I II
Y Y
x x
III IV
Y Y
x x
FIGURE 12.23: Four possible results for a single dummy variable with two levels. Graph
I has the intercept and the slope the same for both levels of the dummy variable. Graph
II has the two lines with the same slope, but different intercepts. Graph III shows the two
fitted lines with the same intercept but different slopes. Graph IV shows the two lines with
different intercepts and different slopes.
slope are the same for both levels of the dummy variable). This is the case for graph I of
Figure 12.23. If the null hypothesis is rejected, either the slopes, the intercepts, or possibly
both the slope and the intercept are different for the different levels of the dummy variable,
as seen in graphs II, III, and IV of Figure 12.23, respectively.
To answer basic question (2), the null hypothesis H0 : β3 = 0 must be tested. If the
null hypothesis is not rejected, the two lines have the same slope, but different intercepts,
as show in graph II of Figure 12.23. The two parallel lines that result when β3 = 0 are
Y = β0 + β1 x1 + ε for (D1 = 0) and Y = (β0 + β2 ) + β1 x1 + ε for (D1 = 1)
When H0 : β3 = 0 is rejected, one concludes that the two fitted lines are not parallel as in
graphs III and IV of Figure 12.23.
To answer basic question (3), the null hypothesis H0 : β2 = 0 for model (12.74) must
be tested. The reduced model for this test is Y = β0 + β1 x1 + β3 x1 D1 + ε. If the null
hypothesis is not rejected, the two fitted lines have the same intercept but different slopes:
Y = β0 + β1 x1 + ε for (D1 = 0) and Y = β0 + (β1 + β3 )x1 + ε for (D1 = 1)
Graph III of Figure 12.23 represents this situation. If the null hypothesis is rejected, one
concludes that the two lines have different intercepts, as in graphs II and IV of Figure 12.23.
Regression 635
Example 12.24 Elevators Suppose a realtor wants to model the appraised price of
an apartment as a function of the predictors living area (in m2 ) and the presence or absence
of elevators. Consider the data frame vit2005, which contains data about apartments
in Vitoria, Spain, including totalprice, area, and elevator, which are the appraised
apartment value in Euros, living space in square meters, and the absence or presence of at
least one elevator in the building, respectively.
(a) The realtor first wants to know if there is any relationship between appraised price (Y )
and living area (x1 ).
(b) Next, the realtor wants to know how adding a dummy variable for whether or not an
elevator is present changes the relationship:
(i) Are the lines the same?
(ii) Are the slopes the same?
(iii) Are the intercepts the same?
Y = β0 + β1 x1 + ε (12.75)
is fit yielding
Y:i = 40822.4 + 2704.8xi1
and a scatterplot of totalprice versus area with the fitted regression line superimposed
over the scatterplot is show in Figure 12.24 on the following page.
> attach(vit2005)
> Elevator <- [Link](elevator)
> contrasts(Elevator)
1
0 0
1 1
> modSimpl <- lm(totalprice~area)
> summary(modSimpl)
Call:
lm(formula = totalprice ~ area)
Residuals:
Min 1Q Median 3Q Max
-156126 -21564 -2155 19493 120674
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 40822.4 12170.1 3.354 0.00094 ***
area 2704.8 133.6 20.243 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 40810 on 216 degrees of freedom
Multiple R-Squared: 0.6548, Adjusted R-squared: 0.6532
F-statistic: 409.8 on 1 and 216 DF, p-value: < 2.2e-16
636 Probability and Statistics with R
6e+05
No Elevator
Yes Elevator
5e+05
4e+05
Appraised Price in Euros
3e+05
2e+05
1e+05
0e+00
FIGURE 12.24: Scatterplot of totalprice versus area with the fitted regression line
superimposed
Based on Figure 12.24, there appears to be a linear relationship between appraised price and
living area. Further, this relationship is statistically significant, as the ℘-value for testing
H0 : β1 = 0 versus H1 : β1 = 0 is less than 2.2 × 10−16 .
(b) The regression model including the dummy variable for Elevator is written
Y = β0 + β1 x1 + β2 D1 + β3 x1 D1 + ε (12.76)
where
0 when a building has no elevators
D1 =
1 when a building has at least one elevator
(i) To determine if the lines are the same (which means that the linear relationship between
appraised price and living area is the same for apartments with and without elevators), the
hypotheses are
In this problem, one may conclude that at least one of β2 and β3 is not zero since the
℘-value = 9.478 × 10−9 . In other words, the lines have either different intercepts, different
slopes, or different intercepts and slopes.
(ii) To see if the lines have the same slopes (which means that the presence of an elevator
adds constant value over all possible living areas), the hypotheses are
H0 : β3 = 0 versus H1 : β3 = 0.
> anova(modTotal)
Analysis of Variance Table
Response: totalprice
Df Sum Sq Mean Sq F value Pr(>F)
area 1 6.8239e+11 6.8239e+11 482.4846 < 2.2e-16 ***
Elevator 1 4.5308e+10 4.5308e+10 32.0352 4.83e-08 ***
area:Elevator 1 1.1732e+10 1.1732e+10 8.2949 0.00438 **
Residuals 214 3.0267e+11 1.4143e+09
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Based on the ℘-value = 0.00438, it may be concluded that β3 = 0, which implies that the
lines are not parallel.
(iii) To test for equal intercepts (which means that appraised price with and without
elevators starts at the same value), the hypotheses to be evaluated are
H0 : β2 = 0 versus H1 : β2 = 0.
Since the ℘-value for testing the null hypothesis is 0.1133, one fails to reject H0 and should
conclude that the two lines have the same intercept but different slopes.
> summary(modInter)$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) 71352.0844 12309.1794 5.796656 2.389680e-08
area 1897.9368 180.5908 10.509596 4.082287e-21
area:Elevator1 553.9856 90.4240 6.126534 4.227047e-09
> detach(vit2005)
The fitted model is Y:i = 7135 + 1898xi1 + 554xi1 Di1 , and the fitted regression lines for the
two values of D1 are shown in Figure 12.25 on the following page. The fitted model using
the same intercept with different slopes has an Ra2 of 0.7034, a modest improvement over
the model without the variable Elevator, which had an Ra2 value of 0.6532.
638 Probability and Statistics with R
6e+05
No Elevator
Yes Elevator
5e+05
4e+05
Appraised Price in Euros
3e+05
2e+05
1e+05
0e+00
As the numbers of levels in the qualitative variables increases, the number of dummy
variables required to represent all of the possible combinations of variables (both dummy and
numerical) increases rapidly, and the comparison of regression equations becomes virtually
intractable. Further exploration on this topic could be carried out with a book dedicated
to regression.
Recall (12.32) states that s2β̂ = σ̂ 2 (X X)−1 = MSE (X X)−1 , while (12.77) gives σY2: =
h
σ 2 Xh (X X)−1 Xh , from which it follows that
Consequently, for a vector of given values (Xh ), a (1 − α) · 100% confidence interval for the
mean response E(Yh ) is
% (
CI 1−α [E(Yh )] = Y:h − t1−α/2;n−p · sY:h , Y:h + t1−α/2;n−p · sY:h (12.79)
Regression 639
The S function predict() applied to a linear model object will compute Y:h and sY:h for a
given Xh . S output has Y:h labeled fit and sY:h labeled [Link]. The function predict()
can be used for a wide range of applications where the statistician would like to predict
values of new data. One of predict()’s arguments is newdata=, where what follows the =
should be a data frame whose columns have identical names to those of the variables that
were used in constructing the original model.
with MSE as well as the variance of the sampling distribution of Y:h with s2Y: .
h
For the normal error model,
& '
Y:h(new) ∼ N Yh = Xh β, σ 2 1 + Xh (X X)−1 Xh . (12.80)
It follows that s2Y: = MSE 1 + Xh (X X)−1 Xh and the (1 − α) · 100% prediction
h(new)
interval for new observation Yh(new) is written as
% (
PI 1−α Yh(new) = Y:h − t1−α/2; n−p · sY:h(new) , Y:h + t1−α/2; n−p · sY:h(new) . (12.81)
To compute prediction intervals, the S function predict() may be applied to a linear model
using the argument interval="pred" in R and [Link]=T in S-PLUS.
Example 12.25 Use the Grades data set and model gpa as a function of sat assuming
that the requirements for model (12.4) are satisfied.
(a) Compute the expected GPA (gpa) for an SAT score (sat) of 1300.
(b) Construct a 90% confidence interval for the mean GPA for students scoring 1300 on the
SAT.
(c) Find the prediction limits on GPA for a future student who scores 1300 on the SAT.
(a) The expected GPA for an SAT score of 1300 is Y:h = Xh · β̂ = 2.830488, where Xh =
(1, 1300) and β̂ = [−1.192, 0.003]
640 Probability and Statistics with R
> attach(Grades)
> Y <- gpa
> x <- sat
> [Link] <- lm(Y~x)
> betahat <- [Link]$coef
> betahat
(Intercept) x
-1.192063812 0.003094270
> Xh <- matrix(c(1,1300), nrow=1)
> Yhath <- Xh%*%betahat
> Yhath
[,1]
[1,] 2.830488
Or, in R only,
(c) The prediction limits on GPA for a future student who scores 1300 on the SAT are
PI 0.90 = [2.166595, 3.494380] using (12.81).
Or, in R only,
Only in S-PLUS:
Now that a determination has been made of a correct (1 − α) · 100% confidence interval
for a single βk , confidence intervals for multiple βk s are desired such that the significance
level of all the intervals together will be only a specified α. For example, if α = 5% and two
independent confidence intervals were created for a β0 and a β1 , the probability that both
would contain their parameters would be only (0.95)2 = 0.9025, giving a family α = 0.0975.
The goal is to create intervals such that the family α is a given value. This goal is more
difficult than the example because the same data are used to construct all the confidence
intervals, so they are not independent and the α calculation is not straightforward. A
family confidence coefficient is the proportion of confidence intervals that contain all
the βk parameters specified for the entire family of g ≤ p parameters for a given sample.
One approach to calculating these simultaneous confidence intervals is named the Bon-
ferroni method. In this method, the joint interval estimates for βk , k = 0, . . . , g parameters
are % (
CI 1−α (βk ) = β̂k − t1− 2g
α
; n−p · sβ̂k , β̂k + t1− 2g
α
; n−p · sβ̂k (12.82)
fall inside a (1 − α) · 100% ellipsoidal confidence region for β where MSE is that of the
full model. Note that q is the rank of K for the hypothesis H0 : Kβ = m discussed
in Section 12.10. When the simultaneous confidence limits are for β = (β1 , β2 , . . . , βk ),
excluding β0 , q will be equal to p − 1, the number of predictors in the full model. This is
a rather computationally intensive method. The simultaneous Scheffé confidence limits for
the individual βk s based on (12.83) are
% (
CI 1−α (βk ) = β̂k − q · f1−α; q, n−p · sβ̂k , β̂k + q · f1−α; q, n−p · sβ̂k . (12.84)
For simple linear regression, the function [Link]() in the car package
will construct and display a simultaneous confidence region for β0 and β1 (q = 2). For
models with p > 2, [Link]() will draw a simultaneous confidence region for
any two βk s, k = 1, . . . , p − 1, specified by the user. Note that in the p > 2 case, q will
equal p − 1. The R function confint() will compute individual confidence intervals for one
or more parameters in a fitted model. The Bonferroni intervals from (12.82) will be wider
than those from (12.84) whenever t1− 2g α
; n−p > q · f 1−α; q, n−p .
To construct several confidence intervals for the mean response, E(Yh ), corresponding to
different Xh vectors such that the family confidence coefficient is 1 − α, use (12.85), where
Regression 643
√
sY:h = Xh s2β̂ Xh :
% (
CI 1−α E(Yh ) = Y:h − p · f1−α; p,n−p · sY:h , Y:h + p · f1−α; p,n−p · sY:h (12.85)
or
% (
CI 1−α E(Yh ) = Y:h − t1− 2g
α
; n−p · sY
:
:h , Yh + t1− 2g
α
; n−p · sY
:h (12.86)
A confidence band is a region of confidence around the entire regression line con-
structed by plotting the upper and lower values of (12.85) over the range of Xh and
subsequently connecting all of the upper values with a curve and all of the lower values
with a curve. See Figure 12.26 on the next page for an example.
or
% (
Y:h − t1−α/2g;n−p · sY:h(new) , Y:h + t1−α/2g; n−p · sY:h(new) (12.88)
Example 12.26 Use the data HSwrestler and the linear model in (12.6) with HWFAT as
the response and AGE, ABS, and TRICEPS as the predictors, assuming the errors from this
model are normally distributed with mean zero and constant variance σ 2 .
644 Probability and Statistics with R
4
3
gpa
90% Pointwise CI
90% Confidence Band
1
90% Pointwise PI
sat
(a) Obtain joint interval estimates for β1 , β2 , and β3 using a 90% family confidence
coefficient with both the Bonferroni and Scheffé approaches.
(b) Use the function [Link]() from the package car to construct a 90% si-
multaneous confidence region for β2 and β3 . Use the function abline() to verify visually
that the limits of the simultaneous confidence region drawn by [Link]()
agree with the values found in part (a).
(c) Find 90% joint interval estimates for the mean HWFAT of wrestlers with values of Xhi
given in Table 12.9.
(d) Find 90% joint prediction intervals for three new wrestlers with values of Xhi given in
Table 12.9.
(a) The estimates of the β̂k s, sβ̂ s, the Bonferroni critical value t1−α/2∗g , and the Scheffé
Regression 645
critical value pf1−α; q, n−p are computed with S and then used in (12.82) and (12.84) to
compute three simultaneous confidence intervals, respectively.
> attach(HSwrestler)
> alpha <- 0.10
> [Link] <- lm(HWFAT~AGE+ABS+TRICEPS)
> summary([Link])$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) 10.6160623 4.23272425 2.508092 1.433001e-02
AGE -0.5330948 0.26067474 -2.045057 4.440545e-02
ABS 0.3564311 0.06353588 5.609918 3.323075e-07
TRICEPS 0.4656071 0.09898493 4.703819 1.158514e-05
> b <- summary([Link])$coef[2:4,1]
> s.b <- summary([Link])$coef[2:4,2]
> g <- 3
> B <- qt((1-alpha/(2*g)),78-4)
> B
[1] 2.168523
> BonSimCI.b <- matrix(c(b-B*s.b, b+B*s.b), ncol=2)
> conf <- c("5%","95%")
> bnam <- c("AGE","ABS","TRICEPS")
> dimnames(BonSimCI.b) <- list(bnam, conf)
> BonSimCI.b
5% 95%
AGE -1.0983739 0.0321843
ABS 0.2186521 0.4942101
TRICEPS 0.2509561 0.6802582
> Q <- 3
> S <- sqrt(Q*qf(.9, Q,78-4))
> S
[1] 2.545185
> SchSimCI.b <- matrix(c(b-S*s.b, b+S*s.b), ncol=2)
> dimnames(SchSimCI.b) <- list(bnam, conf)
> SchSimCI.b
5% 95%
AGE -1.1965602 0.1303706
ABS 0.1947205 0.5181416
TRICEPS 0.2136722 0.7175421
646 Probability and Statistics with R
(b) The following code is used to create the left graph in Figure 12.27, which depicts a joint
confidence region for β2 and β3 enclosed by the Bonferroni confidence limits:
In a similar fashion, the right graph of Figure 12.27 depicts a joint confidence region for β2
and β3 enclosed by the Scheffé confidence limits. The code to reproduce the right graph of
Figure 12.27 is
0.6
TRICEPS coefficient
TRICEPS coefficient
0.5
0.5
0.4
0.4
0.3
0.3
0.2
0.25 0.30 0.35 0.40 0.45 0.50 0.20 0.25 0.30 0.35 0.40 0.45 0.50
FIGURE 12.27: Joint confidence region for β2 and β3 enclosed by the Bonferroni (left
graph) and Scheffé (right graph) confidence limits
(c) The 90% simultaneous confidence intervals for the mean HWFAT of wrestlers with val-
ues of Xhi given in Table 12.9 on page 644 using (12.86) since t1− 2g
α
; n−p = 2.16 <
g · f1−α; g, n−p = 2.55 are
> g <- 3
> alpha <- 0.10
> SC <- sqrt(g*qf(1-alpha,3,74))
Regression 647
(d) The 90% joint prediction intervals for three new wrestlers with
values of Xhi given in
Table 12.9 on page 644 using (12.81) since t1−α/2g ; n−p = 2.17 < gf1−α; g, n−p = 2.55 are
> g <- 3
> alpha <- 0.10
> SC <- sqrt(g*qf(1-alpha,3,74))
> TC <- qt(1-alpha/(2*g),74)
> c(SC, TC)
[1] 2.545185 2.168523
> # Use TC with equation 12.69
> MSE <- anova([Link])[4,3]
> MSE
[1] 8.989042
> s2yhathnew <- MSE + Syhath^2
> Syhathnew <- sqrt(s2yhathnew)
> ll <- Yhath - TC*Syhathnew
> ul <- Yhath + TC*Syhathnew
> SPI <- cbind(Yhath, Syhathnew, ll, ul)
> SPI
Yhath Syhathnew ll ul
1 9.841321 3.023289 3.2852500 16.39739
2 10.595871 3.044560 3.9936729 17.19807
3 7.596662 3.082063 0.9131382 14.28019
> detach(HSwrestler)
648 Probability and Statistics with R
12.17 Problems
1. The manager of a URL commercial address is interested in predicting the number of
megabytes downloaded, megasd, by clients according to the number of minutes they
are connected, mconnected. The manager randomly selects (megabyte, minute) pairs,
records the data, and stores the pairs (megasd,mconnected) in the file URLaddress.
(a) Create a scatterplot of the data. Is the relationship between megasd and mconnected
linear?
(b) Fit a regression line to the data, and superimpose the resulting line in the plot
created in part (a).
(c) Compute the covariance matrix of the β̂s.
(d) What is the standard error of β̂1 ?
(e) What is the covariance between β̂0 and β̂1 ?
(f) Construct a 95% confidence interval for the slope of the regression line.
(g) Compute R2 , Ra2 , and the residual variance for the fitted regression.
(h) Is the normality assumption satisfied?
(i) Are there any outlying observations?
(j) Are there any influential observations? Compute and graph Cook’s distances, DF-
FITS, and DFBETAS to answer this question.
(k) Estimate the mean value of megabytes downloaded by clients spending 10, 30, and
50 minutes on line. Construct the corresponding 95% confidence intervals.
(l) Predict the megabytes downloaded by a client spending 30 minutes on line. Con-
struct the corresponding 95% prediction interval.
2. A metallurgic company is investigating lost revenue due to worker illness. It is interested
in creating a table of lost revenue to be used for future budgets and company forecasting
plans. The data are stored in the data frame LostR.
(a) Create a scatterplot of lost revenue versus number of ill workers. Is the relationship
linear?
(b) Fit a regression line to the data, and superimpose the resulting line in the plot
created in part (a).
(c) Compute the covariance matrix of the β̂s.
(d) Create a 95% confidence interval for β1 .
(e) Compute the coefficient of determination and the adjusted coefficient of determina-
tion. Provide contextual interpretations of both values.
(f) What assumptions need to be satisfied in order to use the model from part (b) for
inferential purposes?
(g) Create a table of expected lost revenues when 5, 15, and 20 workers are absent due
to illness.
(h) Compute a 95% prediction interval of lost revenues when 13 workers are absent due
to illness.
Regression 649
12 12 12 12 12
xi = 581 x2i = 28507 xi Yi = 2630 Yi = 53 Yi2 = 267
i=1 i=1 i=1 i=1 i=1
4. The speed of a tennis ball after being struck with a tennis racket depends on the length
of the racket and the string tension. A multiple regression model is fit where Y is the
speed of the struck tennis ball, x1 is the length of the racket, and x2 is the string tension,
for 16 different tennis rackets. The following table displays the analysis of variance for
the fitted regression model:
H0 : β 0 = 0 H0 : β 1 = 3 H0 : β2 = −1
H1 : β0 = 0, H1 : β1 > 3, H1 : β2 < −1.
1 (xi − x̄)2
hii = xi (X X)−1 xi = + ,
i (xi − x̄)
n 2
.
|Y:i − Y:i (i)| hii
DFFITSi = √ = ri∗
σ(i) hii 1 − hii
8. Show that the SSE in a linear model expressed in summation notation is equivalent to
the SSE expressed in matrix notation:
n
SSE = (Yi − Y:i )2 = Y Y − β̂X Y
i=1
9. Show that the SSR in a linear model expressed in summation notation is equivalent to
the SSR expressed in matrix notation:
n
1
SSR = (Y:i − Ȳ )2 = β̂ X Y − Y JY
i=1
n
10. Show that the trace of the hat matrix H is equal to p, the number of parameters (βs),
in a multiple linear regression model.
11. Suppose a realtor wants to model the appraised price of an apartment in Vitoria as a
function of the predictors living area and the status of the apartment’s conservation.
Consider the data frame vit2005, which contains data about apartments in Vitoria,
Spain, including total price, area, and conservation. The variable conservation has four
levels: 1A, 2A, 2B, and 3A.
(a) Define a new conservation variable called conservation1 with three levels, A, B, and
C, where A = 1A, B = 2A, and C = 2B and 3A together. Define the corresponding
dummy variables considering A (the first category) as the reference category.
(b) Write and fit separate linear regression models (different intercepts and different
slopes) for each conservation1 category.
(c) Construct a single scatterplot of the data where the fitted models are superimposed
over the scatterplot.
12. To estimate the amount of carbon dioxide retained in a tree, its biomass needs to be
known and multiplied by an expansion factor (there are several alternatives in the liter-
ature). To calculate the biomass, specific regression equations by species are frequently
used. These regression equations, called allometric equations, estimate the biomass of
the tree by means of some known characteristics, typically diameter and/or height of the
stem and branches. The biomass file contains data of 42 beeches (Fagus Sylvatica) from
a forest of Navarra (Spain) in 2006, where
(a) Create a scatterplot of PSA versus Dn. Is the relationship linear? Superimpose a
regression line over the plot just created.
(b) Create a scatterplot of log(PSA) versus log(Dn). Is the relationship linear? Superim-
pose a regression line over the plot just created.
(c) Fit the regression model log(PSA) = β0 + β1 log(Dn), and compute R2 , Ra2 , and the
variance of the residuals.
(d) Introduce H as an explanatory variable and fit the model log(PSA) = β0 +β1 log(Dn)+
β2 H. What is the effect of introducing H in the model?
(e) Complete the Analysis questions for the model in (d).
Analysis questions:
(1) Estimate the model’s parameters and their standard errors. Provide an inter-
pretation for the model’s parameters.
(2) Compute the variance-covariance matrix of the β̂s.
(3) Provide 95% confidence intervals for β̂1 and β̂2 .
(4) Compute the R2 , Ra2 , and the residual variance.
(5) Construct a graph with the default diagnostics plots of R.
(6) Can homogeneity of variance be assumed?
(7) Do the residuals appear to follow a normal distribution?
(8) Are there any outliers in the data?
(9) Are there any influential observations in the data?
13. To estimate the total surface occupied by fruit trees in three small areas (R63, R67,
and R68) of Navarra in 2001, a sample of 47 square segments has been taken. The
experimental units are square segments or quadrats of 4 hectares, obtained by random
sampling after overlaying a square grid on the study domain. The focus of this case
study is to illustrate two different techniques used to obtain estimates: direct estimation
and small area estimation. The direct technique estimates the total surface area by
multiplying the mean of the occupied surface in the sampled segments by the total
number of segments in every small area. The small area technique consists of estimating
a regression model where the dependent variable is the observed surface area occupied
by fruit trees in every segment and the explanatory variables are the classified cultivars
by satellite in the same segment and the small areas where they belong to. The final
surface area totals are obtained by multiplying the total classified surface area of every
small area by the β’s parameter estimates obtained from the regression model (observed
surface area ∼ classified surface area + small areas).
The surface variables in the data frame satfruit are given in m2 :
(a) Characterize the shape, center, and spread for the variable FR.
(b) What is the maximum number of m2 of classified fruits by segment?
(c) How many observations are there by small area?
(d) Use pairs() to explore the linear relationships between OBS and the remainder of
the numerical variables. Comment on the results.
Regression 653
(e) Create histograms of the observed fruits surface area (OBS) by small areas (SArea).
(f) Use boxplots and barplots with standard errors to compare the observed surface
area (OBS) and the classified surface area (FR) by small areas (SArea).
(g) Compute the correlation between OBS and all other numerical variables. List the
three variables in order along with their correlation coefficients that have the highest
correlation with OBS.
Model (A) Fit the linear regression model, called Model (A), of OBS versus the rest
of the numerical variables in the same order as they are recorded in the
file.
i. Do an ANOVA and decide which variables are statistically significant. Use
α = 0.05.
ii. Compute the coefficient of determination R2 , Ra2 , the AIC, and the BIC statis-
tic. What is the proportion of total variability explained by Model (A)?
Model (B) Find the best regression model using leaps() with the Ra2 method,
from the package leaps. Call this Model (B).
Model (C) Find the best regression model using step() to determine the best
subset regression. Call this Model (C).
i. Check that the coefficient of determination R2 , the adjusted Ra2 , the AIC, and
the BIC of Models (A), (B), and (C) are the following:
Model R2 Ra2 AIC BIC
Model (A) 0.78 0.69 880 909
Model (B) 0.78 0.72 871 891
Model (C) 0.75 0.72 867 878
What is the best model using both AIC and BIC statistics? Why?
ii. Check that leaps() chooses the variables SF, PS, ES, AF, CO, AR, AL, OL, and
FR and step() chooses PS, AL, OL, and FR.
iii. Graph the default diagnostic regression plots of Model (C). Plot the standard-
ized residuals, the student residuals, the Cook distances, the diagonal elements
of the hat matrix, the DFFITS, and DFBETAS of Model (C).
iv. Are there any leverage points?
v. Are there any outliers?
vi. Test the normality hypothesis with [Link]. Check graphically the
absence of heteroscedasticity in Model (C).
Model (D) Introduce SArea in Model (A). Choose the best model using step and
call it Model (D).
i. Do an ANOVA for Model (D). What variables are statistically significant?
Calculate 95% confidence intervals for the βs of the explanatory variables.
ii. Check that the coefficient of determinations R2 , Ra2 , the AIC, and the BIC
statistic of Model (D) are the following:
Model R2 Ra2 AIC BIC
Model (D) 0.81 0.79 855 868
iii. Use the function drop1() to test the statistically significant presence of PS and
AL.
iv. Use the [Link]() function from the package car to test that PS
and AL are jointly equal to zero.
654 Probability and Statistics with R
Model (E) Drop out the variables PS and AL of Model (D). The new model is called
Model (E).
i. Do the default diagnostic regression plots of Model (E).
ii. Plot the standardized residuals, the student residuals, the Cook distances, the
diagonal elements of the hat matrix, the DFFITS, and DFBETAS of Model
(E).
iii. Are there any leverage points? Justify the answer given.
iv. Are there any outliers? Justify the answer given.
v. Check normality and homoscedasticity for Model (E) using graphics and hy-
potheses tests.
Model (F) Drop out the 46 record of Model (E). Fit the new model and call it
Model (F).
i. Do the default diagnostic regression plots of Model (F).
ii. Plot the standardized residuals, the student residuals, the Cook distances, the
diagonal elements of the hat matrix, the DFFITS, and DFBETAS of Model
(F).
iii. Are there any leverage points?
iv. Are there any outliers?
v. Check the adequacy of the normality and homoscedasticity assumptions of
Model (F).
vi. Compute 95% confidence intervals for the parameters of the explanatory vari-
ables in Model (F) and comment on the results.
(h) How many hectares of observed fruits are expected to be incremented if the classified
hectares of fruit trees by the satellite are increased by 10000 m2 (1 ha)?
(i) Suppose the total classified fruits by the satellite in area R63 is 97044.28 m2 , in
area R67 is 4878603.43 m2 , and in area R68 is 2883488.24 m2 . Calculate the total
prediction of fruit trees by small areas.
(j) Plot in the same graphical page FR versus OBS separately by the three areas.
Superimpose the corresponding regression lines.
(k) Plot the individual predictions versus the observed data. Add a diagonal line to the
plot.
(l) Do a barplot to graph simultaneously the predicted totals and the direct estimates
by areas knowing that the total number of classified segments in areas R63, R67,
and R68, are 119, 703, and 564, respectively..
14. The goal of this case study is to walk the user trough the creation of a parsimonious
multiple linear regression model that can be used to predict the total price (totalprice)
of apartments by their hedonic (structural) characteristics. The data frame vit2005
contains several variables, and further description of the data can be found in the help
file.
(a) Plot totalprice versus the numerical explanatory variables area, age, floor,
rooms, toilets, garage, elevator, and tras to see if these variables have a linear
relationship with totalprice.
Regression 655
Model (A) Create a linear regression model, called Model (A), between totalprice
as the response variable and the rest of the variables as explanatory
variables in the same order as they appear in the file.
(i) Do an analysis of variance of Model (A). Decide which variables are statistically
significant using α = 0.05.
(ii) Verify that if age and floor are specified last in the model, then age is not
statistically significant.
Model (B) Load the leaps package. Using leaps() with the Ra2 method, determine
the best regression subset, and call this subset Model (B).
(i) The function leaps() excludes conservation but not the age variable. This
happens because both variables are correlated. Create a boxplot of age for each
level of conservation.
(ii) Does the boxplot help to explain the correlation between age and conservation?
Model (C) Use the step() command and determine the best regression subset. Call
this Model (C).
(i) Comment on the results of Model (C)
(ii) Compare the results with those obtained from Model (B). In other words, do
the procedures step() and leaps() select the same variables?
Model (D) Define a new model using the name Model (D) with the intersection of
the variables from models (B) and (C).
(i) Find R2 , Ra2 , AIC, and BIC for Model (D).
(ii) Compare R2 , Ra2 , AIC, and BIC for Model (D) with those values as obtained
from Models (A), (B), and (C).
(b) Graph the default diagnostic regression plots of Models (A), (B), (C), and (D), and
test for the models’ assumptions.
(c) Load the MASS package. Choose a transformation of the Box-Cox family to reduce
the heteroskedasticity in Model (D).
(d) Compute the correlations between log(totalprice) and the rest of the quantitative
variables in vit2005. Compare the results with those obtained with totalprice.
(e) Plot log(totalprice) versus the numerical explanatory variables area, age, floor,
rooms, toilets, garage, elevator, and tras one at a time. Is the relationship
between log(totalprice) and the chosen explanatory variables linear?
Model (E) Find the linear regression model between log(totalprice) and the rest
of the variables in data frame vit2005.
(i) Do the analysis of variance for Model (E).
(ii) What variables are statistically significant?
(iii) Is it possible to select an appropriate model using the analysis of variance?
Model (F) Let Model (F) be the model that results when the Ra2 criterion and the
function leaps() are used to select a model.
Model (G) Let Model (G) be the model that results from using the function step()
to select a model.
(i) Find R2 , R2 , Ra2 , AIC, and BIC for Models (E), (F), and (G), and interpret
the results.
(ii) Compute the model’s parameters of Model (G) and their standard errors.
656 Probability and Statistics with R
(iii) Find the variance inflation factors for Model (G). Is multicollinearity a prob-
lem?
(iv) Graph the diagnostic regression plots of Model (G). Check if the model as-
sumptions are valid.
(v) Construct and interpret 95% confidence intervals for the parameters in Model
(G).
(vi) Check graphically the linearity of the explanatory variables area and age in
Model (G). Create a graph of the standardized residuals versus the explanatory
variables.
(vii) Plot the standardized residuals, the studentized residuals, the Cook distances,
the diagonal elements of the hat matrix, the DFFITS, and the DFBETAS
(variables area and age ) of Model (G).
(viii) Are there any leverage points?
(ix) Are there any outliers?
(f) Drop separately and jointly both observations 3 and 93 and refit Model (G).
Model (H) Drop observation 3 to obtain Model (H).
Model (I) Drop observation 93 to obtain Model (I).
Model (J) Drop both observations 3 and 93 to obtain Model (J).
(i) Find R2 , Ra2 , AIC, and BIC for Models (H), (I), and (J). Comment on the
results.
(ii) Graph the model regression diagnostics of Models (H), (I), and (J). Check if
the models’ assumptions are satisfied.
(g) Use drop1() to check whether or not every explanatory variable in Model (J) is
statistically significant.
Model (K) Define a new Model (K) without the streetcategory variable.
(i) Find R2 , Ra2 , AIC, and BIC of Model (K). Compare them with those obtained
for Model (J).
(ii) Graph the default diagnostic regression plots of Model (K), and test the model’s
assumptions.
(iii) Calculate the influence measures for Model (K).
(iv) Are there any leverage points? Justify.
(v) Are there any outliers? Justify.
(vi) Find the parameter estimates, and compute 95% confidence intervals for the
parameters of Model (K).
(vii) Find the relative contribution of the explanatory variables to explaining the
variability of the prices in Model (K).
(viii) What is the variable that explains the most variability?
(ix) What variables jointly explain 80% of the total variability of log(totalprice)?
(x) Find the predictions of Model (K) (a) with bias correction and (b) without bias
correction. The bias correction is obtained by means of the lognormal distribu-
tion: If Y:pred is the prediction of Model (K), the corrected (backtransformed)
prediction YEpred of Model (K) is given by
2
linf = exp(Y:pred + σ̂ 2 /2 − z1−α/2 + Y:pred ) + Var(σ̂
Var( + 2 )/4)
2
lsup = exp(Y:pred + σ̂ 2 /2 + z1−α/2 + Y:pred ) + Var(σ̂
Var( + 2 )/4)
+ 2) =
and Var(σ̂ 2σ̂4
dfresidual
(xi) For Model (K), plot the predicted values (with and without bias correction)
versus observed values. Comment on the results.
(xii) Show that in Model (K) an increment of 10 m2 in the area of a flat implies an
increment of 4% in the predicted total price. To verify this, find the predicted
price of three apartments with areas 80, 90, and 100 m2 , respectively, and
keep the rest of the explanatory variables fixed. For example, assign them the
following values: category=2A, age=10, toilets=1, garage=1, elevator=1,
out=50, rooms=3, zone=41, and tras=1. Compute the corresponding 95%
prediction intervals.
(xiii) What is the percentage change in the total price of an apartment when the
number of garages changes from one to two?
(xiv) What is the percentage change in the total price of an apartment when the
heating type changes from “1A” to “3B”?
Appendix A
S Commands
659
660 Probability and Statistics with R
Table A.1: Useful Commands When Working with Numeric Vectors (continued)
Function Description
sqrt(x) Computes the square root for all values in x
stdev(x) Computes the sample standard deviation of x in S-PLUS
sum(x) The sum of all the values in x
summary(x) Returns the minimum, the first quartile, the median, the
mean, the third quartile, and the maximum of x
tan(x) Returns the tangent for all values in x
var(x) Computes the sample variance of x
which(x==n) Give the index of number n in vector x
Table A.4: Important Probability Distributions That Work with rdist , pdist , ddist ,
and qdist
Distribution S name Parameters
beta beta shape1, shape2
binomial binom n, p
chi-square chisq df = ν
exponential exp λ
F f ν1 , ν2
Gamma gamma shape, rate
geometric geom p
hypergeometric hyper m, n, k, where
m = number of black balls in urn
n = number of white balls in urn
k = number of balls drawn from the urn
negative binomial nbinom n, p
normal norm μ, σ
Poisson pois λ
Student’s t t df = ν
uniform unif a, b
Weibull weibull shape, scale
Wilcoxon rank sum wilcox n,m (number of observations on the first
and second sample, respectively)
Wilcoxon signed rank signrank n
Table A.7: Useful Functions in S for Linear Regression and Analysis of Variance
Function Description
aov(formula,data) Fits an analysis of variance model according to
the specified formula using the specified data.
coefficients(lm object) Returns the coefficients from a fitted linear
regression model. A shorter command with
identical results is coef(lm object).
[Link](proj([Link])) Returns columns containing estimates for
both factors and residuals of aov-type objects.
formula(lm object) Returns the formula used to fit the linear
model.
lm(formula,data) Fits a linear model to the data according to
the user-specified formula.
[Link](lm object) A command used on a ls- or lm-type of object
that returns a list containing several quantities
for assessing the fit of a least squares regres-
sion model including the standard deviation
of the residuals, studentized residuals, and the
standard errors of the parameter estimates.
lsfit(explanatory variables, Fits a model using least squares multivariate
response variable(s)) regression. A list of the estimated coefficients
and residuals as well as the QR decomposition
of the matrix of explanatory variables is
returned. Although the fitted model from
lsfit() is identical to lm(), the manner in
which the model is specified and the output
for the two functions are different.
[Link](lm object) Creates a design matrix.
multicomp(aov object) (Not directly available in R; however,
TukeyHSD() provides similar results to
multicomp() when the default argument
for method, "[Link]," is changed
to method="tukey".) S-PLUS function
multicomp() computes simultaneous or
non-simultaneous confidence intervals or
bounds for the specified estimatable linear
combinations of the parameters in a fixed
effects model (stored in aov).
[Link](x) Computes the Shapiro-Wilk W -statistic, a
well-known goodness-of-fit test for the normal
distribution.
S Commands 665
Table A.8: Useful Contrast Functions in S for Linear Regression and Analysis of Variance
Function Description
[Link]() Returns a matrix of orthogonal contrasts for different
combinations of the factor levels. Contrast i is the
difference between level i + 1 and the average of levels 1
through i.
[Link]() Returns a matrix of orthogonal contrasts for different
combinations of the factor levels. Creates orthogonal
polynomials of degree 1, 2, etc., either on equally spaced
points if n was a single number, or on the points specified
by x. Columns are scaled to have norm 1.
[Link]() Returns a matrix of non-orthogonal contrasts.
[Link]() Returns the coding that is not technically a set of contrasts
at all.
Table A.9: Useful Model Building Functions in S for Linear Regression and Analysis of
Variance
Function Description
add1(lm object, ∼.+ Returns information on models that have one
[Link]) more term than the given object. Tilde, ∼, is
the symbol used by S to separate the response
variable from the explanatory variables.
drop1(lm object) Returns total sum of squares, residual sum of
squares, and AIC each time a variable is dropped
from a regression model. S-PLUS also reports
the CP each time a variable is dropped from the
model.
2
leaps(explanatory variables, Returns CP , Radj , and R2 so user can select
response variable) the best regressions using a subset of the given
explanatory variables.
step(lm object, scope, ... ) Performs stepwise model selection. The starting
model is specified in the first argument (lm
object) and the range of models is specified in
the scope argument.
update(lm object, ∼.± Allows a linear model object to be updated by
[Link]) including, eliminating, or modifying the variables.
666 Probability and Statistics with R
Table A.10: Useful Diagnostic Functions in S for Linear Regression and Analysis of Variance
Function Description
fitted(lm object) Returns the fitted values from the fitted linear model.
plot(lm object) S-PLUS creates six graphs (four in R) showing
various residuals graphs and a graph of Cook’s
distance values for the fitted linear model.
predict(lm object) Returns a vector or an array of predictions using the
model specified in the lm object.
residuals(lm object) Returns the residuals for the fitted linear model. A
shorter command with identical results is resid(lm
object).
summary(lm object) Returns a complete statistical summary for the fitted
linear model.
Definition B.1: Assume that the scalar W can be expressed as a function of the n
variables Y1 , Y2 , . . . , Yn . That is,
⎡ δW ⎤
δY1
δW ⎢ ⎥
W = f (Y1 , Y2 , . . . , Yn ) = f (Y) and = ⎣ ... ⎦ .
δY δW
δYn
⎡ ⎤
Y1
⎢ .. ⎥
Definition B.2: Let A be an n × n matrix and Y = ⎣ . ⎦ be an n × 1 column vector
Yn
of real variables. Then q = Y AY is called a quadratic form in Y, and A is called the
matrix of the quadratic form.
2. Let W = Y Y . Then, δW
δY = 2Y.
Then, W = Y AY = 5Y12 + 3Y22 + 4Y32 + 4Y1 Y2 + 2Y1 Y3 − 12Y2 Y3 , and the partial
derivatives of W are
δW
= 10Y1 + 4Y2 + 2Y3
δY1
δW
= 6Y2 + 4Y1 − 12Y3
δY2
δW
= 8Y3 + 2Y1 − 12Y2
δY3
671
672 Probability and Statistics with R
A random vector or a random matrix contains elements that are themselves random
variables rather than real variables or scalar values.
⎡ ⎤
Y1
⎢ Y2 ⎥
⎢ ⎥
Definition B.3: Given a p × 1 random vector Y = ⎢ . ⎥, the expected value of Y,
⎣ .. ⎦
Yp
⎡ ⎤
E(Y1 )
⎢ E(Y2 ) ⎥
⎢ ⎥
denoted by E(Y), is defined as E(Y) = ⎢ . ⎥.
⎣ .. ⎦
E(Yp )
Basically, the expected value of a random vector is the vector of the expected values of
the elements in the random vector. This concept extends to the expected value of a random
matrix as well. That is, given a random n× p matrix Y, E(Y) = [E(Yij )] for all i = 1, . . . , n
and j = 1, . . . , p pairs.
Recall that the variance of a random variable Y defined in (3.7) on page 92 measures
the variability of Y about its mean μ. Specifically,
% 2 (
σY2 = Var(Y ) = E Y − E(Y ) = E (Y − μ)2
The notion of variability is slightly more challenging to extend to vectors and matrices.
The difficulty arises because of the covariance between random variables. Recall that the
covariance between random variables X and Y was defined as
Cov [X, Y ] = E (X − μX )(Y − μY )
are the variances of Y1 to Yp , while the off diagonal entries are the covariances between Yi
and Yj , where i = j.
2
The calculations of the expanded form of σY are
σY2
= E (Y − μY )(Y − μY )
⎧⎡ ⎤ ⎫
⎪
⎪ Y1 − μ1 ⎪
⎪
⎪
⎪ ⎪
⎨⎢⎢ Y2 − μ2 ⎥
⎥ ⎪
⎬
=E ⎢ ⎢ .
⎥ [Y1 − μ1 , Y2 − μ2 , . . . , Yp − μp ]
⎥
⎪
⎪ ⎣ .. ⎦ ⎪
⎪
⎪
⎪ ⎪
⎪
⎩ ⎭
Yp − μp
⎡ ⎤
E (Y1 − μ1 )2 E (Y1 − μ1 )(Y2 − μ2 ) · · · E (Y1 − μ1 )(Yp − μp )
⎢ ⎥
⎢ E (Y2 − μ2 )(Y1 − μ1 ) E (Y2 − μ2 )2 · · · E (Y2 − μ2 )(Yp − μp ) ⎥
=⎢⎢ ⎥
.. .. .. ⎥
⎣ . . . ⎦
E (Yp − μp )(Y1 − μ1 ) E (Yp − μp )(Y2 − μ2 ) · · · E (Yp − μp )2
⎡ ⎤
σY2 1 σY1 Y2 · · · σY1 Yp
⎢ ⎥
⎢ σY2 Y1 σY2 2 · · · σY2 Yp ⎥
⎢
=⎢ . .. ⎥
. .. ⎥
⎣ . . . ⎦
σYp Y1 σYp Y2 · · · σY2 p
The following rules will help simplify complex expressions so that their variances can be
determined more easily. It is frequently the case that a random vector, Z, is obtained by
premultiplying the random vector Y by a constant matrix A. That is, Z = AY .
1. E[A] = A
2
where σY is the variance-covariance matrix of Y.
References
675
676 Probability and Statistics with R
Chatterjee, S. and B. Price. 1991. Regression Diagnostics. New York: John Wiley & Sons.
Christensen, R. 1996. Analysis of Variance, Design and Regression. New York: Chapman
and Hall.
Chu, S. 2003. Using Soccer Goals to Motivate the Poisson Process. INFORMS Transaction
on Education 3, no. 2:62–68.
Cleveland, W. S. 1993. Visualizing Data. Summit, New Jersey: Hobart Press.
———. 1994. The Elements of Graphing Data. Summit, New Jersey: Hobart Press.
Cochran, W. G. 1977. Sampling Techniques. Wiley Series in Probability and Mathematical
Statistics. New York: John Wiley & Sons, third ed.
Conover, W. J. 1999. Practical Nonparametric Statistics. Monographs on Statistics and
Applied Probability. New York: John Wiley & Sons, third ed.
Cook, R. D. and S. Weisberg. 1982. Residuals and Influence in Regression. Monographs on
Statistics and Applied Probability. London: Chapman & Hall.
Cuesta, M. J., M. D. Ugarte, T. Goicoa, S. Eraso, and V. Peralta. 2007. A Taxometric
Analysis of Schizophrenia Symptoms. Psychiatry Research 150:245–253.
Dahl, D. B. 2006. xtable: Export tables to LATEX or HTML. R package version 1.4-2.
Dalgaard, P. 2002. Introductory Statistics with R. Statistics and Computing. New York:
Springer-Verlag.
Dallal, G. E. and L. Wilkinson. 1986. An analytic approximation to the distribution of
Lilliefors’ test for normality. The American Statistician 40:294–296.
Davis, J. 1986. Statistics and Data Analysis in Geology. New York: John Wiley & Sons.
Davison, A. C. and D. V. Hinkley. 1997. Bootstrap Methods and Their Application, vol. 1 of
Cambridge Series in Statistical and Probabilistic Mathematics. Cambridge: Cambridge
University Press. With 1 IBM-PC floppy disk (3.5 inch; HD).
Devore, J. L. 2000. Probability and Statistics for Engineering and the Sciences. Monographs
on Statistics and Applied Probability. Australia: Duxbury Thomson Learning, fifth ed.
Draper, N. R. and H. Smith. 1998. Applied Regression Analysis. Wiley Series in Probability
and Statistics: Texts and References Section. New York: John Wiley & Sons, third ed.
With 1 IBM-PC floppy disk (3.5 inch; DD).
Efron, B. 1979. Bootstrap methods: another look at the jackknife. Annals of Statistics
7:1–26.
Efron, B. and R. J. Tibshirani. 1993. An Introduction to the Bootstrap, vol. 57 of
Monographs on Statistics and Applied Probability. New York: Chapman and Hall.
Faraway, J. J. 2005. Linear models with R. Chapman & Hall/CRC Texts in Statistical
Science Series. Boca Raton, Florida: Chapman & Hall/CRC.
———. 2006. faraway: Functions and data sets for books by Julian Faraway.
[Link] R package version 1.0.0.
Fdez. Militino, A., S. Gómez, and G. Aldaz. 1994. Problemas Resueltos y Aplicaciones de
Estadı́stica. Pamplona: UNED.
Fisher, R. A. 1936. The Use of Multiple Measurements in Taxonomic Problems. Annals of
References 677
Hocking, R. R. 1996. Methods and Applications of Linear Models. New York: John Wiley
& Sons.
Hollander, M. and D. A. Wolfe. 1999. Nonparametric Statistical Methods. Wiley Series in
Probability and Statistics: Texts and References Section. New York: John Wiley & Sons,
second ed.
Hothorn, T. and K. Hornik. 2006. exactRankTests: Exact Distributions for Rank and
Permutation Tests. R package version 0.8-15.
Hothorn, T., K. Hornik, M. A. van de Wiel, and A. Zeileis. 2006. A Lego system for
conditional inference. The American Statistician 60, no. 3:257–263.
Hothorn, T., F. Bretz, and P. Westfall. 2007. multcomp: Simultaneous Inference for General
Linear Hypotheses. With contributions by Richard M. Heiberger. R package version
0.992-4.
Insightful Corporation. 2005a. S-PLUS 7 Application Developer’s Guide. Seattle, WA.
[Link]
———. 2005b. S-PLUS 7 for Windows User’s Guide. Seattle, WA.
[Link]
———. 2005c. S-PLUS 7 Guide to Statistics, Volume 1. Seattle, WA.
[Link]
———. 2005d. S-PLUS 7 Guide to Statistics, Volume 2. Seattle, WA.
[Link]
———. 2005e. S-PLUS 7 Programmer’s Guide. Seattle, WA.
[Link]
———. 2007. S-PLUS 8 Guide to Packages. Seattle, WA. [Link]
Johnson, N. L., S. Kotz, and N. Balakrishnan. 1995. Continuous Univariate Distributions.
Vol. 2. Wiley Series in Probability and Mathematical Statistics: Applied Probability and
Statistics. New York: John Wiley & Sons, second ed.
Kalbfleisch, J. G. 1985a. Probability and Statistical Inference. Vol. 1. Springer Texts in
Statistics. New York: Springer-Verlag, second ed.
———. 1985b. Probability and Statistical Inference. Vol. 2. Springer Texts in Statistics.
New York: Springer-Verlag, second ed.
Kitchens, L. J. 2003. Basic Statistics and Data Analysis. Pacific Grove, California:
Brooks/Cole, a division of Thomson Learning.
Kleinbaum, D. and L. Kupper. 1998. Applied Regression Analysis and Other Multivariable
Methods. London: Duxbury Press, third ed.
Kopka, H. and P. W. Daly. 1995. A Guide to LATEX2e. New York: Addison-Wesley, second
ed.
Krause, A. and M. Olson. 1997. The Basics of S and S-PLUS. New York: Springer-Verlag.
Kutner, M. H., C. J. Nachtsheim, J. Neter, and W. Li. 2004. Applied Linear Statistical
Models. Boston: McGraw-Hill/Irwin, fifth ed.
Lapin, L. L. 1990. Probability and Statistics for Modern Engineering. Boston: PWS-KENT
Publishing Company, second ed.
References 679
Lawless, J. 1982. Statistical Models and Methods for Lifetime Data. New York: John Wiley
& Sons.
Levy, P. S. and S. Lemeshow. 1999. Sampling of Populations. Boston: John Wiley & Sons,
third ed.
Lilliefors, H. W. 1967. On the Kolmokorov-Smirnov Tests for Normality with Mean and
Variance Unknown. Journal of the American Statistical Association 62:399–402.
López, J. 1994. Problemas de Inferencia Estadı́stica, (Muestreo y Control de Calidad).
Albacete: Tébar Flores, third ed.
Lumley, T. 2004. Leaps: Regression Subset Selection. R package version 2.7 using Fortran
code by Alan Miller.
Lunneborg, C. E. 2000. Data Analysis by Resampling: Concepts and Applications. Pacific
Grove, California: Duxbury, first ed.
Maindonald, J. and J. Braun. 2003. Data Analysis and Graphics Using R—An Example-
Based Approach, vol. 10 of Cambridge Series in Statistical and Probabilistic Mathematics.
Cambridge: Cambridge University Press.
Mazess, R. B., W. W. Peppler, and M. Gibbons. 1984. Total Body Composition by Dual-
Photon (153 Gd) Absorptiometry. American Journal of Clinical Nutrition 40, no. 4:834–
839.
Militino, A. F., M. D. Ugarte, T. Goicoa, and M. González-Audı́cana. 2006. Using Small
Area Models to Estimate the Total Area Occupied by Olive Trees. Journal of Agricultural,
Biological and Environmental Statistics 11:450–461.
Montgomery, D. C. 1991. Design and Analysis of Experiments. New York: John Wiley &
Sons, third ed.
Muro, J., I. Irigoyen, A. Militino, and C. Lamsfus. 2001. Defoliation Effects on Sunflower
Yield Reduction. Agronomy Journal 93:634–637.
Murrell, P. 2006. R Graphics. Computer Science and Data Analysis Series. Boca Raton,
Florida: Chapman & Hall/CRC.
Neter, J., M. H. Kutner, C. J. Nachtsheim, and W. Wasserman. 1996. Applied Linear
Statistical Models. Boston: McGraw-Hill, fourth ed.
Oehlert, G. W. 2000. A First Course in Design and Analysis of Experiments. New York:
W. H. Freeman and Company.
Ott, L. and W. Mendenhall. 1985. Understanding Stastistics. Boston: Duxbury Press.
Peña, D. 2001. Fundamentos de Estadı́stica. Madrid: Alianza Editorial.
———. 2002. Regresión y Diseño de Experimentos. Madrid: Alianza Editorial.
Petruccelli, J. D., B. Nandram, and M. Chen. 1999. Applied Statistics for Engineers and
Scientists. Upper Saddle River, New Jersey: Prentice Hall.
Pinheiro, J. C. and D. M. Bates. 2000. Mixed-Effects Models in S and S-PLUS. New York:
Springer-Verlag.
Pratt, J. W. and J. D. Gibbons. 1981. Concepts of Nonparametric Theory. New York:
Springer-Verlag.
680 Probability and Statistics with R