Reading in Data

library(tidyverse)
library(tmap)

ch_pums <- read_csv("https://drive.google.com/uc?export=download&id=1ifsUSH8veyhn8x8gUMo0MGu4VxYWpI9p")

Calculating Point Estimate and Confidence Interval for Means

The formula for a confidence interval around a sample mean is:

\[ \bar{x} \pm t^* \frac{s}{\sqrt{n}} \]

The code below calculates the 95% confidence interval for average travel time to work.

# calculate mean
sample_mean <- mean(ch_pums$travel_time_work)

# calculate sample variance
sample_variance <- sd(ch_pums$travel_time_work)^2

# sample size
sample_size <- nrow(ch_pums)

# critical t-value for a 95% confidence interval
t_val <- qt(0.975, df = sample_size - 1)

# lower and upper bounds
confidence_l <- sample_mean - t_val * sqrt(sample_variance / sample_size)
confidence_h <- sample_mean + t_val * sqrt(sample_variance / sample_size)

sample_mean
## [1] 11.84543
confidence_l
## [1] 11.42019
confidence_h
## [1] 12.27068
t.test(ch_pums$travel_time_work, conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  ch_pums$travel_time_work
## t = 54.606, df = 6501, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  11.42019 12.27068
## sample estimates:
## mean of x 
##  11.84543

Q1. We have a large sample size (6502). What would happen to the confidence interval if we had a smaller sample?

With a smaller sample, the confidence interval would get wider because there is more uncertainty in the estimate. A larger sample gives a more precise estimate and a narrower confidence interval.

Q2. Rerun the script above using a different confidence level. How do the results change?

I changed the confidence level to 90%. The interval became narrower because we are accepting less confidence that it contains the true population mean. For travel time to work, the 90% confidence interval is about 11.49 to 12.20 minutes, compared with 11.42 to 12.27 minutes for the 95% confidence interval.

t.test(ch_pums$travel_time_work, conf.level = 0.90)
## 
##  One Sample t-test
## 
## data:  ch_pums$travel_time_work
## t = 54.606, df = 6501, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 90 percent confidence interval:
##  11.48857 12.20229
## sample estimates:
## mean of x 
##  11.84543

Calculating Point Estimate and Confidence Interval for Proportions

The formula for a confidence interval around a proportion is:

\[ \hat{p} \pm z^* \sqrt{\frac{\hat{p}(1-\hat{p})}{n}} \]

# people with public health insurance
# binary variable: 1 = yes, 0 = no
total_pop_pub_health <- sum(ch_pums$public_health_ins)
total_sample_pop <- nrow(ch_pums)
prop_pub_health <- total_pop_pub_health / total_sample_pop

# z critical value for a 95% two-tailed confidence interval
z_val <- qnorm(0.975)

# lower and upper bounds
confidence_lower <- prop_pub_health - z_val * sqrt(
  prop_pub_health * (1 - prop_pub_health) / total_sample_pop
)

confidence_higher <- prop_pub_health + z_val * sqrt(
  prop_pub_health * (1 - prop_pub_health) / total_sample_pop
)

prop_pub_health
## [1] 0.1724085
confidence_lower
## [1] 0.163227
confidence_higher
## [1] 0.18159
prop.test(total_pop_pub_health, total_sample_pop, conf.level = 0.95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  total_pop_pub_health out of total_sample_pop, null probability 0.5
## X-squared = 2789.8, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.1633459 0.1818612
## sample estimates:
##         p 
## 0.1724085

Mini Challenge

Using the code above, calculate the point estimate and the 90% and 95% confidence intervals for the two other variables in the PUMS dataset: hrs_wrked_per_week and income.

Hours worked per week

# point estimate
mean_hours <- mean(ch_pums$hrs_wrked_per_week, na.rm = TRUE)
mean_hours
## [1] 27.64611
# 90% confidence interval
t.test(ch_pums$hrs_wrked_per_week, conf.level = 0.90)
## 
##  One Sample t-test
## 
## data:  ch_pums$hrs_wrked_per_week
## t = 113.19, df = 6501, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 90 percent confidence interval:
##  27.24431 28.04791
## sample estimates:
## mean of x 
##  27.64611
# 95% confidence interval
t.test(ch_pums$hrs_wrked_per_week, conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  ch_pums$hrs_wrked_per_week
## t = 113.19, df = 6501, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  27.16731 28.12491
## sample estimates:
## mean of x 
##  27.64611

The point estimate is the sample mean shown above. The 90% confidence interval is narrower than the 95% confidence interval because a higher confidence level requires a wider range.

Income

# point estimate
mean_income <- mean(ch_pums$income, na.rm = TRUE)
mean_income
## [1] 69586.45
# 90% confidence interval
t.test(ch_pums$income, conf.level = 0.90)
## 
##  One Sample t-test
## 
## data:  ch_pums$income
## t = 57.559, df = 6501, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 90 percent confidence interval:
##  67597.61 71575.30
## sample estimates:
## mean of x 
##  69586.45
# 95% confidence interval
t.test(ch_pums$income, conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  ch_pums$income
## t = 57.559, df = 6501, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  67216.49 71956.41
## sample estimates:
## mean of x 
##  69586.45

Again, the point estimate is the sample mean. The 95% confidence interval is wider than the 90% interval because it is designed to give us more confidence that the true population mean falls inside the interval.