The R command for testing and to find confidence intervals for a population mean \(\mu\) is one.sample.t. The assumptions for this method are: either the data comes from a normal distribution or the sample size is large enough.
Some Monarch butterflies fly early in the day, others somewhat later. After the flight they have to rest for a short period. It has been theorized that the resting period (RIP) of butterflies flying early in the morning is shorter because this is a thermoregulatory mechanism, and it is cooler in the mornings. The mean RIP of all Monarch butterlfies is 133 sec. Test the theory at the 10% level.
Research by Anson Lui, Resting period of early and late flying Monarch butterflies Danaeus plexippus, 1997
First we check the assumptions:
attach(butterflies)
sort(RIP.sec.)
## [1] 52 64 66 75 77 85 86 92 93 98 102 102 103 112 115 117 120
## [18] 121 124 124 124 125 132 132 134 140 142 145 148 148 152 155 156 156
## [35] 167 167 170 177 181 187
nplot(RIP.sec.)
The graph shows that the data comes from a normal distribution.
one.sample.t(RIP.sec., conf.level = 90)
## A 90% confidence interval for the population mean is (115, 133.3)
Note the one.sample.t command does the normal plot automatically.
one.sample.t(RIP.sec., mu.null = 133, alternative = "less")
## p value of test H0: mu=133 vs. Ha: mu < 133: 0.0558
We have the routine t.ps for power and sample size calculations:
t.ps(n = 40, diff = 125-133, sigma = sd(RIP.sec.),
alternative="less")
## Power of Test = 41.6%
Note for this method the actual values of 125 and 133 are irrelevant, only the difference 125-133 matters!
Note the argument name for the standard deviation is sigma because ideally we would want this to be the population standard deviation. In practice we often only have the sample standard deviation, though. Usually that is ok.
Note The routine doesn’t just find the power for the given diff but does a whole curve, for lots of differences!
The same command will find the sample size if we give it the required power, and leave the sample size n out:
t.ps(power = 90, diff = 125-133, sigma = sd(RIP.sec.),
alternative="less")
## Sample size required is 160
A sociologist wants to study the educational level in some population. She randomly selects 120 people and asks them for their education, specifically the number of years (for example, someone who finishes high school is a 12 and someone with a bachelors degree a 16). She finds a mean of 12.3 with a standard deviation of 1.7. She wants to find a \(90\%\) confidence interval for the population mean.
Here we don’t have the data itself, but the information given is all we need:
one.sample.t(y=12.3, shat=1.7, n=120, conf.level=90)
## A 90% confidence interval for the population mean is (12, 12.6)
Note One problem with this situation is that without the data we can not do a normal plot and verify that the data came from a normal distribution.
If she wanted the interval to have an error of 0.2, what sample size would she need?
t.ps(E=0.2, sigma=1.7, conf.level=90)
## [1] "Sample size required is 196"
Note: proportion = probability = percentage/100
The R command for testing and to find confidence intervals for a population proportion \(\pi\) is one.sample.prop. This method has no assumptions!
The South African mathematician John Kerrich was in a German POW camp during World War II. During his time there he flipped a coin 10000 times, getting 5067 heads and 4933 tails.
one.sample.prop(x=5067, n=10000, pi.null=0.5)
## p value of test H0: pi=0.5 vs. Ha: pi <> 0.5: 0.1835
Note problems for proportions do not include a standard deviation \(\sigma\). (actually, they do, but it is “included” in the \(\pi\))
for power and sample size calculations for percentages/proportions/probabilities we have the prop.ps command:
prop.ps(n=10000, phat=0.505, pi.null=0.5)
## [1] "Power of Test = 16.6%"
Note that in the case of a proportion we need both phat and pi.null, not just the difference.
prop.ps(n=100, phat=0.5, pi.null=0.6)
## [1] "Power of Test = 53.7%"
prop.ps(n=100, phat=0.6, pi.null=0.5)
## [1] "Power of Test = 46.0%"
prop.ps(phat=0.505, pi.null=0.5, power=90)
## [1] "Sample size required is 105281"
The Professors of some course are worried about the failure rates (F’s and W’s) in some course. They are planning to randomly select students from the last few years and then find a 99% confidence interval for the failure rate. What sample size will they need if they want the interval to have an error of \(7.5\%\)?
prop.ps(E=0.075, conf.level=99)
## [1] "Sample size required is 295"
Note here we didn’t mention phat. In that case prop.ps will use phat=0.5:
prop.ps(E=0.075, phat=0.5, conf.level=99)
## [1] "Sample size required is 295"
Say they know that the failure rate is around \(40\%\), what would the sample size be then?
prop.ps(phat=0.4, E=0.075, conf.level=99)
## [1] "Sample size required is 284"
Note with phat=0.5 we get 295, with phat=0.4 it is n=284. In fact phat=0.5 will always yield a sample size larger than for any other phat. A larger sample size is always safer (yield a smaller confidence interval).
When you read the results os an election poll it often says that the poll has a margin of error of \(\pm 3\%\). This means that the error of the confidence interval is 0.03.
In the next election for governor of Puerto Rico, what sample size will be needed in a poll to have an error of \(\pm 3\%\)?
prop.ps(E=0.03)
## [1] "Sample size required is 1068"
In the next election for president of the US, what sample size will be needed in a poll to have an error of \(\pm 3\%\)?
prop.ps(E=0.03)
## [1] "Sample size required is 1068"
Whu is this quite remarkable?
This test compares proportions from a sample with proportions from a population. The test has the assumption that all expected counts be at least five.
In order to test whether a certain die is fair it was rolled 100 times. The results were
x | p |
---|---|
12 | 1/6 |
17 | 1/6 |
20 | 1/6 |
15 | 1/6 |
10 | 1/6 |
26 | 1/6 |
Let’s enter the data in R and run the test:
x <- c(12, 17, 20, 15, 10, 26)
p <- c(1, 1, 1, 1, 1, 1)/6
chi.gof.test(x, p)
## p value of test p=0.0741
\(p = 0.0741 > 0.05 = \alpha\), so we fail to reject the null, the die does not appear to loaded.
Experiments in Plant Hybridization (1865) by Gregor Mendel is one of the most famous papers in all of Science. His theory of genetics predicted that the number of Smooth yellow, Wrinkled yellow, Smooth green and Wrinkled green peas would be in the proportions 9/16, 3/16, 3/16 and 1/16. In one of his experiments he oberved 315, 101, 108 and 32. Does this agree with his theory?
x <- c(315, 101, 108 , 32)
p <- c(9, 3, 3, 1)/16
chi.gof.test(x, p)
## p value of test p=0.9254
\(p = 0.9254> 0.05\), the data from the experiment is in agreement with Mendel’s theory.
a biologist has a theory regarding the number of eggs laid by a certain species of birds. He does a survey of these birds and finds:
Number of Eggs | Observed | Theory (in%) |
---|---|---|
0 | 15 | 25 |
1 | 24 | 30 |
2 | 8 | 20 |
3 | 11 | 20 |
More than 3 | 4 | 5 |
Test at the 5% level whether his data agrees with his theory
x <- c(15, 24, 8, 11, 4)
p <- c(25, 30, 20, 20, 5)/100
chi.gof.test(x, p)
## Warning : some expected counts<5
## p value of test p=0.4681
Notice the warning regarding the expected counts < 5. This is because the last category doesn’t have enough data. The solution is to combine some categories:
Number of Eggs | Observed | Theory (in%) |
---|---|---|
0 | 15 | 25 |
1 | 24 | 30 |
2 | 8 | 20 |
More than 2 | 15 | 25 |
Now rerun that test
x <- c(15, 24, 8, 11+4)
p <- c(25, 30, 20, 20+5)/100
chi.gof.test(x, p)
## p value of test p=0.3674
you flip a coin 100 times and get 60 heads. Test at the 5% level whether this is a fair coin.
Solution A:
each flip is either “heads”" or “tails”, so it is a Bernoulli trial. Therefore we can use
one.sample.prop(60, 100, pi.null = 0.5)
## p value of test H0: pi=0.5 vs. Ha: pi <> 0.5: 0.0574
\(p > \alpha\), so we fail to reject the null hypothesis.
Solution B:
We can use the chisquare goodness-of-fit test:
x <- c(40, 60)
p <- c(0.5, 0.5)
chi.gof.test(x, p)
## p value of test p=0.0455
\(p < \alpha\), so we reject the null hypothesis.
What is going on? To begin with, both methods are perfectly ok. They are not the same methods, so we should not expect them to give the (exactly) same answer. Also, in either case the p values are close to 0.05, in which case one should not really either reject or fail to reject the null hypothesis but if possible collect more data.