Confidence intervals, statistical tests, and comparison in R

Note

These supplemental notes addends the following lectures: Confidence intervals, Statistical tests, and Comparison. It demonstrates how to do some of the in class examples in R, and how to do the same tests but with data instead of calculations. This will be useful for homework and projects.

confidence intervals

for the proportion

For confidence intervals for the proportion use prop.test():

The solution to example 1 is:

## The expected counts (x) is .14*35
prop.test(x = .14*350, n = 350, correct = FALSE)

    1-sample proportions test without continuity correction

data:  0.14 * 350 out of 350, null probability 0.5
X-squared = 181.44, df = 1, p-value < 2.2e-16
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
 0.1075436 0.1802730
sample estimates:
   p 
0.14 

The solution to example 2 is:

prop.test(x = 35, n = 225, correct = FALSE)

    1-sample proportions test without continuity correction

data:  35 out of 225, null probability 0.5
X-squared = 106.78, df = 1, p-value < 2.2e-16
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
 0.1140250 0.2086502
sample estimates:
        p 
0.1555556 

The solutions in the slides are not exactly those given by R. The slides are using a normal approximation to compute the confidence interval while R uses the Wilson interval. The interval from R is more accurate because it does two things:

1. It’s wider around \(0.5\) than away from \(0.5\). This is important because an estimate for the proportion can not be below \(0\) or above \(1\). It makes sense that it should be shorter at the edges and wider in the middle.

2. It lets the variance change along the interval. At p_low it uses \(p_{low}(1-p_{low})\) and at \(p_{high}\) it uses \(p_{high}(1-p_{high})\) where as the approximation we use has fixed variance.

for the mean

To find confidence intervals for the mean use t.test():

Example 1 and example 2 do not have a built-in function in R to automagically compute them. Instead, I show you how to compute them directly from the data:

## default confidence level is 95%
t.test(diamonds$price)

    One Sample t-test

data:  diamonds$price
t = 228.95, df = 53939, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 3899.132 3966.467
sample estimates:
mean of x 
   3932.8 
## a 72% confidence level
t.test(diamonds$price, conf.level = 0.72)

    One Sample t-test

data:  diamonds$price
t = 228.95, df = 53939, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
72 percent confidence interval:
 3914.243 3951.357
sample estimates:
mean of x 
   3932.8 

statistical tests

for proportions

The solution for example 3 follows:

prop.test(x = 17, n = 36, p = 9/24, alternative = "greater")

    1-sample proportions test with continuity correction

data:  17 out of 36, null probability 9/24
X-squared = 1.0667, df = 1, p-value = 0.1508
alternative hypothesis: true p is greater than 0.375
95 percent confidence interval:
 0.3294798 1.0000000
sample estimates:
        p 
0.4722222 

Again, this is not the same as our calculations because R is more accurate.

for means, directly from the data

The following code does this directly from the data:

## alternative: true mean is not equal to 2000
t.test(diamonds$price, mu = 2000, alternate = "two.sided")

    One Sample t-test

data:  diamonds$price
t = 112.52, df = 53939, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 2000
95 percent confidence interval:
 3899.132 3966.467
sample estimates:
mean of x 
   3932.8 
## alternative: true mean is greater than 2000
t.test(diamonds$price, mu = 2000, alternate = "greater")

    One Sample t-test

data:  diamonds$price
t = 112.52, df = 53939, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 2000
95 percent confidence interval:
 3899.132 3966.467
sample estimates:
mean of x 
   3932.8 
## a 72% confidence level
t.test(diamonds$price, conf.level = 0.72)

    One Sample t-test

data:  diamonds$price
t = 228.95, df = 53939, p-value < 2.2e-16
alternative hypothesis: true mean is not equal to 0
72 percent confidence interval:
 3914.243 3951.357
sample estimates:
mean of x 
   3932.8 

comparison

two sample \(z\) test for proportions

Example 2 can almost be done in R. This does not specify the difference, just that the \(H_A:p_s>p_i\): the population average estimated by \(\hat{p_s}\) input first (280/809) is greater than the population average estimated by \(\hat{p_i}\) the fraction input second (197/646))

## alternative: true mean is not equal to 2000
prop.test(x = c(280, 197), n = c(809, 646), alternative = "greater")

    2-sample test for equality of proportions with continuity correction

data:  c(280, 197) out of c(809, 646)
X-squared = 2.5769, df = 1, p-value = 0.05422
alternative hypothesis: greater
95 percent confidence interval:
 -0.0007927079  1.0000000000
sample estimates:
   prop 1    prop 2 
0.3461063 0.3049536 

two sample \(z\) test for proportions

example 3. Again, the calculation in R is more accurate.

## default is 95% confidence interval
prop.test(x = c(280, 197), n = c(809, 646))

    2-sample test for equality of proportions with continuity correction

data:  c(280, 197) out of c(809, 646)
X-squared = 2.5769, df = 1, p-value = 0.1084
alternative hypothesis: two.sided
95 percent confidence interval:
 -0.008561667  0.090867154
sample estimates:
   prop 1    prop 2 
0.3461063 0.3049536 

two sample \(t\) test for means

d.color <- diamonds %>% 
  filter(color == "D") %>% 
  select(carat)
  
j.color <- diamonds %>% 
  filter(color == "J") %>% 
  select(carat)

## 2 - sided test
## Alternative hypothesis: x != y
t.test(x = d.color, y = j.color, 
       alternative = "two.sided")

    Welch Two Sample t-test

data:  d.color and j.color
t = -41.811, df = 3683.7, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.5279915 -0.4806923
sample estimates:
mean of x mean of y 
0.6577948 1.1621368 
## Alternative hypothesis: x < y by 0.1
t.test(x = d.color, y = j.color, 
       alternative  = "less", mu = .1)

    Welch Two Sample t-test

data:  d.color and j.color
t = -50.101, df = 3683.7, p-value < 2.2e-16
alternative hypothesis: true difference in means is less than 0.1
95 percent confidence interval:
       -Inf -0.4844961
sample estimates:
mean of x mean of y 
0.6577948 1.1621368 

CI for the difference between two means

t.test(x = d.color, y = j.color, conf.level = 0.95)

    Welch Two Sample t-test

data:  d.color and j.color
t = -41.811, df = 3683.7, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.5279915 -0.4806923
sample estimates:
mean of x mean of y 
0.6577948 1.1621368 

example 5 is already done in R

Paired comparisons

# Weight of mice before treatment
before <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7)
# Weight of mice after treatment
after <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2)

# A tibble
mice <- tibble(
  group = rep(c("before", "after"), each = 10),
  weight = c(before, after)
  )

t.test(weight ~ group, data = mice, paired = TRUE)

    Paired t-test

data:  weight by group
t = 20.883, df = 9, p-value = 6.2e-09
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 173.4219 215.5581
sample estimates:
mean difference 
         194.49