hypothesis test for one mean

Schwab

Libraries

library(tidyverse)
library(palmerpenguins)

Test Statistics

What is a test statistic?

It is the value from your data that you can compare to a sampling (or comparison) distribution primarily for finding a p-value.

Single proportion

The test statistic for a single proportion is a z-score for the standard normal N(0,1):

\[ T=\frac{\widehat{p} - p_{null}}{SE} \]

Where \(p_{null}\) is the proportion under the null hypothesis.

Diff of Proportions

The test statistic for a difference of proportions is also a z-score for the standard normal N(0,1):

\[ T = \frac{\widehat{p_1} - \widehat{p_2} - 0}{SE} \]

Where \(\widehat{p_1} - \widehat{p_2}\) is the point estimate and 0 is the difference under the null.

Why did we compute these?

We find the test statistics on the standard normal table of z-scores to be able to find a p-value.

We don’t really need to know how to do this because we have pnorm().

When reading stats papers the phrase test statistic is still often given along side the p-value.

A single mean example

Fictional:

We are concerned about over-fishing depleting the food for penguin’s diets.

Last year the average weight of Adelie penguins on Togersen island was 3900 grams.

We went to Togersen Island this year and weighed some penguins.

Our data is in penguins.

Penguin Sample

Togersen_penguins <- penguins |>
  filter(island == "Torgersen") 

mean(Togersen_penguins$body_mass_g, na.rm = TRUE)
[1] 3706.373

Is the difference from 3900 due to chance?

Hypothesis test for a single mean

Let’s set up the test for the penguins

\[ H_0: \mu = 3900 \\ H_a: \mu \ne 3900 \]

Conditions

Large Independent sample

  • No extreme outliers

  • Larger than 30

or

Small Independent sample

  • The data distribution is normal.

  • No clear outliers.

Check our conditions

#Sample size 
nrow(Togersen_penguins)
[1] 52
# Outliers? 
ggplot(Togersen_penguins)+
  geom_boxplot(aes(x=body_mass_g))

What is the sampling distribution?

We could use the sampling distribution… But its much better to use the Student t.

The Student’s t dist

The student t distribution is similar to the normal distribution in shape.

It has wider tails.

It is used with confidence intervals and hypothesis testing of a single mean.

Unlike the normal distribution it works well for small sample sizes if the population is normal distributed.

Very Short History

William Sealy Gosset

Worked at Guiness and was interested in the chemical properties of barley. source

The parameters of a student t.

The student t takes just one parameter


df = degree of freedom.


\(df= n-1\)

limit of t-dist is normal

As \(\text{lim}_{df \rightarrow \infty} t_{dist} \rightarrow N(\mu=0,sigma=1)\)

https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/File:Student_t_pdf.svg

t-dist is better than the normal for sampling distributions.

You can use either if:

  • If the sample size is large or underlying distribution is normal

  • Population standard deviation is known \(\sigma\)

  • But if population sd \(\sigma\) is unknown or n is small use student t.

  • In general the t distribution works, the normal less so.

The sampling distribution for sample mean:

\(\mu \sim \text{t}_{df}\)


The mean of the t-dist is 0.


The mean of your sample is \(\bar{x}\) and the standard error is \(SE = \frac{s}{\sqrt{n}}\)

What is df?

  • The number of values that can vary when finding a mean.
  • Consider that you find a mean of 3 numbers. x,y,and z.
  • If I tell you the mean is 10, you can guess any two numbers and the third is forced.
  • This mean would have 2 degrees of freedom.

test statistic for a mean

We’ve skipped discussing test statistics until now, because of pnorm().

Ideally

\[ T = \frac{\bar{x} - \mu}{\frac{\sigma}{\sqrt{n}}}\]

or more realistically:

\[ T = \frac{\bar{x} - \mu}{\frac{s_x}{\sqrt{n}}}\]

Let’s finish our hypothesis test

We are considering the weight of penguins.

Last year the weight was 3900, and this yeah its 3706.

We checked conditions.

The standard deviation is:

s_x = sd(Togersen_penguins$body_mass_g, na.rm = TRUE)
s_x
[1] 445.1079

So the test statistic is:

T = (3706.373 - 3900)/ (s_x/sqrt(51))
T
[1] -3.106602

Find pvalue

We need to calculate the probability of getting -3.11 for a test statistic.

Multiply by 2 for a two tailed test.

2 * pt(-3.11, df = 51-1)
[1] 0.003086856

conclusion

Reject the null hypothesis. We have strong evidence to believe that the average weight this year is different from 3900 grams.

Its possible we made a type 1 error and rejected the null even if it was true.

You can make confidence intervals with the student t.

Its the same as doing it with the normal distribution.

Except you use a t* score instead of z*score.

\[ \bar{x} \pm t^* SE\]

Lets make a 90% CI for our penguin data.

\(\bar{x} = 3706.373\)

\(SE = \frac{s}{n}=\frac{445.10}{51} = 8.73\)

\(t^*=\)

t_star = qt(0.95 , df=50)
t_star
[1] 1.675905
x_bar = 3706.373
SE = s_x/sqrt(51)

# Lower Bound
x_bar - t_star*SE
[1] 3601.918
# Upper Bound
x_bar + t_star*SE
[1] 3810.828

Conclusion

We are 90% confident that the true weight of penguins is between 3602 and 3811.

You try: Hypothesis testing on a single mean

Awesome Auto data set. 𝑛 = 5, \(\bar{x}\)= 14600, and \(s_x\)= 7765.31. Suppose you hear that the Awesome Auto dealership typically sells cars for 18000. You decide to test this claim.

  • Write the hypotheses in symbols.

  • Check conditions, then calculate the test statistic, 𝑇 , and the associated degrees of freedom.

  • Find and interpret the p-value in this context.

  • What is the conclusion of the hypothesis test when using 𝛼 = 0.05?

Solution

\[ H_0: \mu = 18000 \\ H_a: \mu \ne 18000 \\ \alpha = 0.05 \]

Unfortunately we cannot at present test the normal condition. The cars are independent.

Test Statistic

\(T = \frac{14600 - 18000}{\frac{7765.31}{\sqrt{5}}}\)

df = 4

p-value:

# give info below
x_bar = 14600
mu = 18000
s_x = 7765.31
n = 5

# test statistic
test_stat = (x_bar - mu)/(s_x/sqrt(n))

# Find pvalue
2*pt(q=test_stat, df=4)
[1] 0.3829896

Conclusion

With a large p value we find no evidence that the prices at Awesome Auto average 18000.

We could never verify that the underlying data was normal.

The t.test()

R, of course, can make this test easy if you are working from a vector of values (x) (instead of sample statistics, like above). You will do this on occasion in your homework.

You can also take the time to have R calculate the mean and standard deviation, but t.test() is fine.


t.test(x= , alternative= , mu=)

Awesome auto revisited

Suppose the awesome auto data was:

awesome_auto <- c(15000, 26000, 6000, 17000, 9000)
t.test(x = awesome_auto,
       alternative = "two",
       mu = 18000)

    One Sample t-test

data:  awesome_auto
t = -0.97905, df = 4, p-value = 0.383
alternative hypothesis: true mean is not equal to 18000
95 percent confidence interval:
  4958.097 24241.903
sample estimates:
mean of x 
    14600 

You try: Another one with t.test()

The 2017 Toyota Prius Prime has a MPG_e = 54. Consider the prius_mpg data set in the openintro package.


Test the hypothesis that the actual MPG is greater than 54.


Consider Conditions, but do the test either way and we’ll discuss conditions afterwards.


Hint: use prius_mpg$_____ to take just one column from the data.

My output

library(tidyverse)
library(openintro)

t.test(x=prius_mpg$average_mpg, alternative = "greater", mu = 54)

    One Sample t-test

data:  prius_mpg$average_mpg
t = 5.757, df = 18, p-value = 9.304e-06
alternative hypothesis: true mean is greater than 54
95 percent confidence interval:
 117.4944      Inf
sample estimates:
mean of x 
 144.8632 

Preview: Bootstrapping

library(infer)

x_bar <- mean(prius_mpg$average_mpg)

prius_randomization <-prius_mpg |>
  specify(response = average_mpg)|>
  hypothesise(null= "point", mu = 54) |>
  generate(reps = 5000)|>
  calculate(stat = "mean")

Graph and pvalue

prius_randomization |>  visualise()+
  shade_p_value(obs_stat = x_bar, direction = "greater")
prius_randomization |>
  get_pvalue(obs_stat = x_bar, direction = "greater")
# A tibble: 1 × 1
  p_value
    <dbl>
1       0