Download Using R, Chapter 7: Confidence Intervals and more Study notes Statistics in PDF only on Docsity!
Using R, Chapter 7: Confidence Intervals
Here we build confidence intervals. This is broken into four different types.
Building confidence intervals for a population
- mean with σ known using summary statistics qnorm 2
- mean with σ unknown using summary statistics qt 3
- mean with σ unknown using raw data t.test 4
- proportion using summary statistics qnorm 5
Confidence intervals about a mean with σ known using summary stats:
- Example in the Text: A simple random sample of 35 men yields a mean pulse rate of 72.5 beats per minute (bpm). Assume the standard deviation for the population is 10.2 bpm. Find the 95% confidence interval estimate for the mean pulse rate of all men.
preliminary info: x¯ = 72.5 σ = 10.2 n = 35
- The critical value of z: zα/ 2 = 1. 96
- Margin of Error: E = zα/ 2 ·
σ √ n
- Confidence interval: The lower limit is ¯x − E = 72. 5 − 3 .38 = 69.12. The upper limit is ¯x + E = 72.5 + 3.38 = 75.88. The confidence interval is 69. 1 < μ < 75. 9
- Conclusion: I am 95% confident that the mean pulse rate for all men is between 69.1 and 75. beats per minute.
- Example with R: Everything in red is typed by the user. Everything in blue is output to the console.
The differences from the calculations done by hand are due to round-off error in the hand calculations.
Confidence intervals about a mean with σ unknown using raw data:
- Suppose heart-rate data is in an Excel file saved as a .CSV file named Excel-Heart-Rate-Data.csv that looks like
This is imported into R using the read.table command.
data <- read.table("Excel-Heart-Rate-Data.csv",header=TRUE,sep=",")
- Use the function t.test with a confidence level.
t.test(data,con.level=.95)
There is a lot of stuff put out by the t.test function. The desired confidence interval is highlighted below. Everything in red is typed by the user. Everything in blue is output to the console.
Confidence intervals for proportions using summary stats:
- Example from the text: A local company conducts a survey where people watch an advertisement which contains a phone number. Afterwards they are asked to pick the phone number from a short list of numbers. In a simple random sample of 1512 respondents 474 picked the right answer and 1038 did not. Find the 90% confidence interval for the proportion of all people who could correctly choose the given phone number.
preliminary info: pˆ =
≈ 0. 313 qˆ = 1 - ˆp = 0.687 n = 1512
- The critical value of z: zα/ 2 = 1. 645
- Margin of Error: E = zα/ 2 ·
√ p ˆqˆ n
√ (.313)(.687) 1512
- Confidence interval: The lower limit is ˆp − E = 0. 313 − 0 .0196 = 0.2934. The upper limit is ˆp + E = 0.313 + 0.0196 = 0.3326. The confidence interval is 0. 293 < p < 0. 333
- Conclusion: I am 90% confident that the proportion of all people who could correctly pick the given phone number is between 0.293 and 0.333. ... or less formally ... Between 29 and 33 percent of us can pick the right phone number.
- Example with R: Everything in red is typed by the user. Everything in blue is output to the console.
The differences from the calculations done by hand are due to round-off error in the hand calculations.