Theory-Based Test of Significance

Author

Jose Toledo Luna

Required Packages
library(openintro)

One Sample \(t\)-test

To perform a theory-based hypothesis test for the mean of a quantitative variable, following the components of a hypothesis test, but in the context of a one sample \(t\)-test

  1. State the null and alternative hypotheses about the mean parameter \(\mu\)

  2. From a sample of data, compute the sample mean \(\bar{x}\) and its standardized \(t\)-statistic

  3. Check the validity conditions to apply the Central Limit Theorem

  4. If the validity conditions hold, compute the \(p\)-value by comparing the observed \(t\)-statistic to a \(t\)-distribution with \(df = n-1\)


We can use the t.test() function to conduct this hypothesis test

The basic syntax for the t.test() function is as follows:

t.test(x, y = NULL, 
       alternative = c("two.sided", "less", "greater"),
       mu = 0, conf.level = 0.95, ...)
  • x: numeric vector of data values (our sample)
  • y: an optional numeric vector of data values (response)
  • alternative: type of hypothesis being performed,
    • "two.sided": \(H_a: \mu \neq \mu_0\)
    • "less": \(H_a: \mu < \mu_0\)
    • "greater": \(H_a: \mu > \mu_0\)
  • mu: population mean \(\mu\) (null value), can be difference of means if performing a two-sample test
  • conf.level: confidence level of the interval \(100(1-\alpha)\)%, where \(\alpha\) is the significance level

For additional arguments ... see ?t.test()


Consider the Tip Data from opentintro package. The tip data is a simulated data set of tips over a few weeks on a couple days per week. Each tip is associated with a single group, which may include several bills and tables

From this dataset we obtain a simple random sample of 30 tips, but first we only consider tips from bills that were at most $80

tips <- openintro::tips
#> Rows: 95
#> Columns: 5
#> $ week   <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
#> $ day    <fct> Tuesday, Tuesday, Tuesday, Tuesday, Tuesday, Tuesday, Tuesday, …
#> $ n_peop <dbl> 0.67, 1.33, 5.33, 2.67, 1.33, 2.00, 1.33, 2.67, 2.00, 1.33, 1.3…
#> $ bill   <dbl> 17.99, 26.05, 79.49, 32.69, 29.97, 23.91, 18.18, 41.23, 25.19, …
#> $ tip    <dbl> 2.0000, 4.0000, 14.3082, 5.5000, 6.0000, 7.5000, 3.0600, 8.5000…
tips_bill_at_most80 <- tips[tips$bill <= 80,]

Among the tips from bills that were at most $80, we obtained a simple random sample of size 30. We are interested in determining if the average tips throughout the weeks will be greater than $5 per group (for some reason, the business needs on average at least $5 tips per group to stay in business).

The null and alternative hypothesis are given by \[ \begin{align*} H_0: \mu = 5 \\[5pt] H_a: \mu > 5 \end{align*} \]

set.seed(1234)
rows_sample <- sample(1:nrow(tips_bill_at_most80), 30)
tips_sample <- tips_bill_at_most80[rows_sample,]

First we will carry out a one-sample \(t\)-test the long way. First, we compute the sample mean for the tips

x_bar <- mean(tips_sample$tip)
x_bar
#> [1] 6.533687

Next, compute the sample standard deviation of the tips

s <- sd(tips_sample$tip)

the \(t\)-statistic is then computed as

\[ T = \frac{\bar{x}-\mu_0}{\left(\frac{s}{\sqrt{n}} \right)} \]

n <- length(rows_sample)
mu_0 <- 5
t_stat <- (x_bar - mu_0) / (s / sqrt(n))
t_stat
#> [1] 1.74364

Lastly, obtain the \(p\)-value

p_value <- pt(t_stat, df = n - 1,lower.tail = FALSE)
p_value
#> [1] 0.0459079

At a significance level of \(\alpha=0.05\), we reject the null hypothesis, indicating there is enough evidence to propose that, on average, the tips per group exceed $5 for bills equal to or less than $80, as supported by the three-week data provided

With a single command, we can use t.test() function and obtain all of the following information, without having to compute every step ourselves

  • observed sample mean \(\bar{x}\)
  • \(t\)-statistic
  • degrees of freedom \((df)\)
  • the alternative hypothesis we are testing
  • \(p\)-value
  • A 95% confidence interval for \(\mu\)
t.test(tips_sample$tip, mu=5, 
       alternative = 'greater')
#> 
#>  One Sample t-test
#> 
#> data:  tips_sample$tip
#> t = 1.7436, df = 29, p-value = 0.04591
#> alternative hypothesis: true mean is greater than 5
#> 95 percent confidence interval:
#>  5.039153      Inf
#> sample estimates:
#> mean of x 
#>  6.533687