Hypothesis testing

In-class example

Load the data and libraries:

library(tidyverse)
library(juanr)
library(moderndive)
library(huxtable)

# data
head(wealth)
r1r3r4r4ar5r6r7r8r12r14r15r16r18uredq10new_18q14fs2fs8wf1
1101000011000Rural601000
1101100011000Rural650000
1101011001011Urban1231110
001000000000Rural621110
1101000000010Rural900100
1001000000000Rural60000

What percent of households in Honduras have a TV?

wealth |> 
  summarise(r1 = mean(r1, na.rm = TRUE))
r1
0.824

82.4% of households in our sample have a TV in their home. This is an estimate based on one sample. How uncertain should we feel about this estimate given our sample size and variability?

We can bootstrap 1,000 samples with replacement of the same size as ours:

set.seed(1990)
# how uncertain should we be?
boots = wealth |> 
  rep_sample_n(size = 1560, reps = 1000, 
               replace = TRUE) |> 
  summarise(r1_avg = mean(r1, na.rm = TRUE))

We can then look at the distribution of likely averages:

ggplot(boots, aes(x = r1_avg)) + 
  geom_density() + 
  labs(x = "Bootstrapped proportion of households with TV")

We can quantify uncertainty using the standard error:

## standard error?
boots |> 
  summarise(r1_avg_avg = mean(r1_avg, na.rm = TRUE),
            r1_se = sd(r1_avg, na.rm = TRUE))
r1_avg_avgr1_se
0.8240.00966

So our best guess is \(Estimate \pm 2 \times StandardError\) = \(.82 + 2 \times .01 = .84, .82 - 2 \times .01 = .80\)

Our best guess for percent of households in Honduras with a TV is between 80% and 84%.

We can also look at the 95% CI:

## confidence interval?
boots %>% 
  summarise(low = quantile(r1_avg, .025),
            mean = mean(r1_avg), 
            high = quantile(r1_avg, .975))
lowmeanhigh
0.8040.8240.842

When we do regression with lm, R returns two measures of uncertainty: (1) the standard error, and (2) whether the 95% confidence interval excludes zero (ie., statistical significance)

mod = lm(mpg ~ wt, data = mtcars)
huxreg(mod, statistics = "nobs")
(1)
(Intercept)37.285 ***
(1.878)   
wt-5.344 ***
(0.559)   
nobs32        
*** p < 0.001; ** p < 0.01; * p < 0.05.

The standard error is the number in parentheses below each coefficient. The standard error for wt is (.56).

The stars tell you whether a result is statistically significant or not, and what size of confidence interval excludes zero. The estimate for wt is statistically significant, and the 99.9% (100 - .001) confidence interval.