# Explaining Statistical Goodness of fit Tests with Beer

**Jasmine Dumas' R Blog**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Statistics + Beer = Comprehension –

A goodness of fit test are a collection of statistical techniques used to summarize the differences between observed values and expected values. These tests can be used be used for hypothesis testing, testing residual normality, distribution comparisons, and outcome frequency distribution comparisons.

It can be difficult to know which statistical test to select given a problem domain and *often* the results won’t point you to any mistakes if the wrong assumptions are made. In this post, I have detailed three common goodness of fit tests, the `R`

code to perform the tests, interpretation of the results while using beer (analytics) in the examples.

## Chi-squared test or χ2 test

*Chi-squared tests compare observed frequencies to expected frequencies given independent normally distributed data.*

For example, is the distribution of beer style and production volume by year occur by random chance or is there a difference between beer style and how much is produced as a condition of year (either higher, lower, or the same form year to year)?

The beer style and production volume attributes are independent if the probability distribution of one attribute is not affected by the presence of the other. So, let’s test the (null) hypothesis that there is no difference between beer style from each of production volume figures by year at the 0.05 (5%) significance level. At this level we are accepting the 5% risk of saying that there is a difference between beer styles and production volume count when there is no actual difference.

```
<span class="n">set.seed</span><span class="p">(</span><span class="m">20170103</span><span class="p">)</span><span class="w">
</span><span class="n">production</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">matrix</span><span class="p">(</span><span class="w">
</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">floor</span><span class="p">(</span><span class="n">runif</span><span class="p">(</span><span class="m">12</span><span class="p">,</span><span class="w"> </span><span class="n">min</span><span class="o">=</span><span class="m">10000</span><span class="p">,</span><span class="w"> </span><span class="n">max</span><span class="o">=</span><span class="m">20000</span><span class="p">)),</span><span class="w">
</span><span class="n">nrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w">
</span><span class="n">ncol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w">
</span><span class="n">dimnames</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"red_ale"</span><span class="p">,</span><span class="w"> </span><span class="s2">"stout"</span><span class="p">,</span><span class="w"> </span><span class="s2">"lager"</span><span class="p">,</span><span class="w"> </span><span class="s2">"ipa"</span><span class="p">),</span><span class="w">
</span><span class="nf">c</span><span class="p">(</span><span class="m">2016</span><span class="p">,</span><span class="w"> </span><span class="m">2015</span><span class="p">,</span><span class="w"> </span><span class="m">2014</span><span class="p">))</span><span class="w">
</span><span class="p">)</span><span class="w">
</span><span class="n">production</span><span class="w">
</span>
```

```
<span class="c1"># 2016 2015 2014
# red_ale 18421 17138 14157
# stout 18753 14833 13701
# lager 11035 16021 17017
# ipa 19180 14607 18113
</span><span class="w">
</span>
```

Results:

```
<span class="n">chisq.test</span><span class="p">(</span><span class="n">production</span><span class="p">)</span><span class="w">
</span><span class="c1"># Pearson's Chi-squared test
#
# data: production
# X-squared = 3267.2, df = 6, p-value < 2.2e-16
</span>
```

As the p-value is less than our significance level of 0.05 at 2.2e-16 (*there is a really low probability that this occurred just by chance*), we fail to reject the null hypothesis that there is no difference in beer style and production volume by year and can state that the alternative hypothesis that there is a difference between beer style and production volume fro year to year. As the values were randomly generated, this analysis can be furthered by exploring the potential popularity of certain styles increasing or decreasing the demand for production over the years.

## t-Test

*t-tests can be used to determine if two sets of data are significantly different from each other.*

For example, do brewery tour attendance differ on different weekend days – Do the amount of visitors on Saturday differ than on Sunday? let’s pose the question of interest by hypothesizing that there is no difference between attendance counts for Saturday or Sunday at the 0.05 significance level.

```
<span class="n">tours</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">data.frame</span><span class="p">(</span><span class="s2">"attendance_count"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">floor</span><span class="p">(</span><span class="n">runif</span><span class="p">(</span><span class="m">105</span><span class="p">,</span><span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="n">max</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">35</span><span class="p">)),</span><span class="w">
</span><span class="s2">"day"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">rep</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s2">"Saturday"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Sunday"</span><span class="p">)))</span><span class="w">
</span><span class="n">head</span><span class="p">(</span><span class="n">tours</span><span class="p">)</span><span class="w">
</span>
```

```
<span class="c1"># attendance_count day
# 1 21 Saturday
# 2 17 Sunday
# 3 16 Saturday
# 4 10 Sunday
# 5 7 Saturday
# 6 11 Sunday
</span>
```

Results:

```
<span class="n">t.test</span><span class="p">(</span><span class="n">attendance_count</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">day</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tours</span><span class="p">,</span><span class="w"> </span><span class="n">var.equal</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span><span class="c1"># Two Sample t-test
#
# data: attendance_count by day
# t = 0, df = 104, p-value = 1
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
# -3.43306 3.43306
# sample estimates:
# mean in group Saturday mean in group Sunday
# 19 19
</span>
```

As the p-value is larger than our 0.05 significance level, we can fail to reject the null hypothesis that there is no difference in the attendance count for each weekend day. Tis type of statistical test could be useful for breweries to determine if having tours on either day of the weekend resulted in a statistical difference in attendance.

## ANOVA (Analysis of Variance)

*ANOVA, or Analysis of Variance tests the significance of group differences between two or more groups. This tests that there is a difference between groups but not which ones are different.*

For example, do beer ratings differ between age groups, 21-25, 26-30, 31+? As a hypothesis we can state that beer ratings will be no different among age groups – Beer drinkers of all ages give the same ratings for some beer that they are surveyed on.

```
<span class="n">ratings</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">data.frame</span><span class="p">(</span><span class="s2">"age_group"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="nf">rep</span><span class="p">(</span><span class="s2">"21-25"</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">),</span><span class="w"> </span><span class="nf">rep</span><span class="p">(</span><span class="s2">"26-30"</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">),</span><span class="w"> </span><span class="nf">rep</span><span class="p">(</span><span class="s2">"31+"</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">)),</span><span class="w">
</span><span class="s2">"rating"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">floor</span><span class="p">(</span><span class="n">runif</span><span class="p">(</span><span class="m">27</span><span class="p">,</span><span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">max</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">)))</span><span class="w">
</span><span class="n">head</span><span class="p">(</span><span class="n">ratings</span><span class="p">)</span><span class="w">
</span>
```

```
<span class="c1"># age_group rating
# 1 21-25 1
# 2 21-25 1
# 3 21-25 4
# 4 21-25 4
# 5 21-25 1
# 6 21-25 2
</span><span class="w">
</span>
```

Results:

```
<span class="n">fit_anova</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">aov</span><span class="p">(</span><span class="n">rating</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">age_group</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ratings</span><span class="p">)</span><span class="w">
</span><span class="n">summary</span><span class="p">(</span><span class="n">fit_anova</span><span class="p">)</span><span class="w">
</span><span class="c1"># Df Sum Sq Mean Sq F value Pr(>F)
# age_group 2 0.22 0.1111 0.073 0.93
# Residuals 24 36.44 1.5185
</span>
```

As the p-value is higher than our significance level, we can fail to reject the hypothesis that beer ratings are no different between age groups. We can accept the alternative hypothesis that different age groups rate beer differently. In a real world analysis, you can determine beer ratings by gathering more data between age groups or ratings from a given platform, such as **untapped** or **Beer Advocate**.

I hope this post is helpful to those interested in selecting the right statistical test for the appropriate questions!

### Additional Resources

- When to Use a Particular Statistical Test (PDF)
- Fitting & Interpreting Linear Models in R
- Independent t-test for two samples
- WHAT A P-VALUE TELLS YOU ABOUT STATISTICAL DATA
- Example of Kolmogorov-Smirnov test
- Hypothesis Testing – Analysis of Variance (ANOVA)
- Five 2016 beer statistics and why they matter

**leave a comment**for the author, please follow the link and comment on their blog:

**Jasmine Dumas' R Blog**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.