Statistical Hypothesis Testing

What is hypothesis testing?

Hypothesis testing is a statistical tool used to, well, hypothesize and predict characteristics about a data set, particularly when only a sample or subset of the data is available.


There are many significant values to pay attention to when doing a hypothesis test.

- Null Hypothesis: What we want to disprove
- Alternative Hypothesis: What we want to prove
- P-value: A probability that will determine if we reject the null hypothesis
- Significance: A threshold value that we compare the p-value to

Hypothesis Testing Methods

Z-Test

Z-Test is a hypothesis testing technique used to test if the means, or averages, of two groups are equal. These two groups are typically the sample and population groups. It can only be used when the standard deviation of the population data sets are known.

T-Test

Functions similarly to the Z-Test, but can be used when the standard deviation of the population is not known.

Chi-Square

Chi-square tests are typically used to test attributes of categorical data, or to check if a distribution matches an expected value.

Going forward, we will be making use of the T-test.

T-Test Information

As already mentioned, the t-test is used to test if the means of a population and a sample are the same. This is determined through the use of the following calculation:

\(t = \frac{\bar{x} - \mu}{s / \sqrt{n}}\)

\(where: \\\bar{x} = sample \ mean \\ \mu = population \ mean\)

\(s = sample \ standard \ deviation \\ n = sample \ size\)

Dataset: trees

To demonstrate a t-test, we will be taking a look at the trees data set provided in R. This data set looks at the height, girth, and volume of black cherry trees. It contains 31 observations.

A small peek at the mtcars data
Girth Height Volume
8.3 70 10.3
8.6 65 10.3
8.8 63 10.2
10.5 72 16.4

What will we look at?

We will be focusing on the mean of the tree’s “Height” field of the data set. Here’s a look at the entire data set’s Height field.

ggplot(trees, aes(x=trees$Height)) + 
  geom_histogram(color='darkblue', fill='lightblue', binwidth=2) +
  labs(title='Frequency of Tree Heights in Sample',
       x='Height', y='Frequency')

Ensuring Usability

To make sure we can use a t-test on this data, it either needs a lot of data points, or be somewhat normally distributed. We can check if the data fits a normal distribution with the following:

qqnorm(trees$Height); qqline(trees$Height)

While not perfect, we can see the samples fit the line decent enough to be used as a normal distribution.

Another Way

We can also eyeball the normal distribution by checking its density function. We’ll overlay it on a bar plot.

ggplot(trees, aes(x=trees$Height, y=..density..)) + 
  geom_histogram(color='darkblue', fill='lightblue', binwidth=2) +
  geom_density() + 
  labs(title='Frequency of Tree Heights in Sample', x='Height', y='Frequency')

It’s not perfect, but it will do.

Running the calculation

Now, we want to run the t-test to see if we can hypothesize anything about the population data. Let’s say, we believe the average height 70 feet.

Essentially, the code will do this for us:

\(t = \frac{76 - 65}{6.371813 / \sqrt{31}}\)

And uses the t value to generate the t-statistic, seen here with other data. The t-statistic will be used to determine other important attributes like the p-value.

results = t.test(trees$Height, mu=70, conf.level=0.95)
    One Sample t-test

data:  trees$Height
t = 5.2429, df = 30, p-value = 1.173e-05
alternative hypothesis: true mean is not equal to 70
95 percent confidence interval:
 73.6628 78.3372
sample estimates:
mean of x 
       76 

What does this mean?

The t-test is telling us that there is a high likelihood that the actual mean of, in this scenario, all felled black cherry trees, is between about 73.6 and 78.3 feet. The red dashed line the estimated mean of 76, which is identical to our sample mean!