Rweb in Stat 3011 Home Page   Stat 3011 Home Page   About the Rweb in Stat 3011 Web Pages

One Sample Tests and Confidence Intervals in Rweb (Stat 3011)

Contents

Large-Sample Procedures for Means

R has no simple procedure for doing "large sample" procedures for means. The reason is that when using a computer, you might as well use the "small sample" procedure. Just ignore the n > 30 rule and use the t distribution for all procedures. It will do almost the same thing for large n and be more conservative to boot. Hence, to do things the easy way, go to the small sample section.

Confidence Intervals

To do the "large sample" confidence interval as described in the textbook, you must implement the formulas on the computer. For example, consider Example 8.6 in Devore and Peck. The problem data are

sample mean 5.14
sample s. d. 1.30
sample size 52
and we want a 95% confidence interval for the population mean. Basically, we just use Rweb as a calculator. The plus-or-minus is
     1.96 * 1.30 / sqrt(52)
for which Rweb gives 0.353344. Hence the interval is 5.14 ± 0.353.

If we like, we can use qnorm to look up the z critical value. The probability we want to look up is .025 because there is 2.5% in each tail. Thus

     - qnorm(0.025) * 1.30 / sqrt(52)
gives a slightly more precise answer.

If you want the endpoints of the interval you can do

     5.14 + qnorm(0.025) * 1.30 / sqrt(52)
     5.14 - qnorm(0.025) * 1.30 / sqrt(52)
or the same thing in one statement with
     5.14 - c(-1,1) * qnorm(0.025) * 1.30 / sqrt(52)

Hypothesis Tests

Again, we just use Rweb as a calculator. This time we use Example 9.12 in Devore and Peck. The problem data are

sample mean 130
sample s. d. 8
sample size 101
The hypotheses to test are
Null Hypothesis : mu equals 127.3
Alternative Hypothesis : mu not equal to 127.3
The test statistic is
     (130 - 127.3) / (8 / sqrt(101))
which Rweb says is 3.391833. The one-tailed P-value is the tail area to the right of this
     z <- (130 - 127.3) / (8 / sqrt(101))
     1 - pnorm(z)
Of course, this particular example asks for a two-tailed test, and a two-tailed P-value is twice the one-tailed
     z <- (130 - 127.3) / (8 / sqrt(101))
     2 * (1 - pnorm(z))

If the test statistic had turned out negative, the one-tailed P-value for a lower-tailed test would be

     pnorm(z)
and the two-tailed P-value would be
     2 * pnorm(z)

Single expressions that to either job, upper-tailed or lower-tailed are

     pnorm(- abs(z))
for the one-tailed P-value, and
     2 * pnorm(- abs(z))
for the two-tailed P-value. Question: why do these work? You should figure this out.

Small-Sample Procedures for Means

Confidence Intervals

For this section, we will use the data set for Example 8.10 in Devore and Peck

     http://superior.stat.umn.edu/~charlie/3011/te0810.dat

Rweb has a function t.test that does t tests and confidence intervals. Type the URL above in the data URL window and submit

     t.test(x)
and Rweb returns, among other things, the 95% confidence interval (0.8876158, 0.9633842).

This example, actually asks for 99% confidence rather than 95% confidence. To get that you use an optional argument (all the optional arguments are described on the on-line help).

     t.test(x, conf.level=0.99)
gives the answer in the book.

Hypothesis Tests

For this section, we will use the data set for Problem 9.57 in Devore and Peck

     http://superior.stat.umn.edu/~charlie/3011/ex0957.dat

As the name indicates, the Rweb function t.test does t tests as well as confidence intervals Type the URL above in the data URL window and submit

     t.test(x, mu=25)
using the optional argument mu to specify the null hypothesis. By default Rweb does a two-tailed test, that is, the hypotheses are
Null Hypothesis : mu equals 25
Alternative Hypothesis : mu not equal to 25
Rweb gives P = 0.08525 for the P-value of the test.

This is not the answer in the back of the book for part (b). The book asks for an upper-tailed test, with hypotheses

Null Hypothesis : mu equals 25
Alternative Hypothesis : mu greater than 25
The Rweb code that does that is
     t.test(x, mu=25, alternative="greater")
this gives P = 0.04263 for the P-value, which agrees with the answer in the back of the book. You can find the optional argument for any eventuality by looking at the on-line help).

No fuss. You don't have to calculate anything. Rweb does it all. Unfortunately, when doing problems out of the book, the book usually gives only the sample mean and standard deviation, which the t.test function can't use. It wants the actual data. Thus Rweb is fairly useless for doing textbook toy problems, but very useful for doing real-world problems.

If you want to do a textbook problem with no data, you will have to do it like the examples in the large sample section replacing the pnorm and qnorm functions with the pt and qt functions, which calculate probabilities and quantiles for the t distribution, see the on-line help for details.

Large-Sample Procedures for Proportions

As for the t-test, there is an Rweb function prop.test that does large sample tests and confidence intervals for proportions.

Confidence Intervals

For a confidence interval example we will use Example 8.8 in Devore and Peck. The problem data are

x 466
n 1014
and we want a 95% confidence interval for the population proportion. The Rweb code is
     prop.test(466, 1014)
Rweb's confidence interval (0.4286132, 0.4908291) agrees with the book. As with all Rweb functions, see the on-line help for details of how to use it.

Hypothesis Tests

For a hypothesis test example we will use Example 9.15 in Devore and Peck. The problem data are

x 51
n 300
and the hypotheses to be tested are
Null Hypothesis : pi equals 0.25
Alternative Hypothesis : pi less than 0.25
The Rweb code is
     prop.test(51, 300, p=0.25, alternative="less")
and Rweb gives the one-tailed P-value P = 0.0008642, which does not agree with the answer in the book P = 0.0007. What happened?

Actually, the book is wrong. Rweb has gotten a better answer using (as it says in the printout) a "continuity correction". To understand how Rweb actually gets this answer and why it is better than the book's answer, you would have to look in another book, but we won't go into that. You could get the answer in the book from

     prop.test(51, 300, p=0.25, alternative="less", correct=FALSE)
(the "correct=FALSE" says it all).