The application of a one-way ANOVA test to examine the dependency between shop distance from the bakery and irregularities in the level of returns

June 2022 | Przegląd Piekarski i Cukierniczy (Baking and Confectionery Review)

The application of a one-way ANOVA test to examine the dependency between the distance of shops from the bakery, in the context of irregularities in the level of returns

The basic tool for studying phenomena is the comparative method. It consists in comparing many data sets with respect to some hypothesis. For example, someone suspects that the highest level of bread sales occurs on Wednesdays. Of course they can sum up the whole year’s sales of bread on Wednesdays and compare it with the whole year’s sales on Thursdays, Fridays, etc.

Such a comparison will show the overall volume of sales. This comparison will not, however, answer the question of whether such large sales on Wednesday are not caused by just a few Wednesdays in the year. Or perhaps lower sales on Thursdays are caused by a dozen or so specific cases (among others, Fat Thursday, Maundy Thursday and the Thursday of the May bank-holiday weekend). Hasty conclusions may expose us to ineffectiveness or unnecessary promotional expenditure. After all, we live in the information age. Single summaries give only general and impractical knowledge. In order to find out whether Wednesdays are statistically significantly better in the level of sales, we should compare a set of values, not single numbers resulting from summaries.

W-MOSZCZYNSKI ppic 6-22

Table 1 presents a summary of the bakery’s sales over 18 weeks. In the first two columns we have the sales quantity for Monday and Tuesday. The next column, marked in red, is Wednesday’s sales. The rows denote weeks of sales. At first glance the quantity sold on Wednesday does not differ from the other days of the week. But is this difference really not statistically significant, given that the annual summary showed that the sum of sales on Wednesday is greater than on the other days of the week?

The example of the Moczary bakery

The Moczary bakery sells its bread to 86 nearby village shops. The shops supplied are located within a radius of 50 km of the bakery. The bakery analyses discrepancies in the quantity of returns, understood as the difference between the theoretical value resulting from the size of the delivery and the quantity of sales, and the actual quantity of bread returned. Weekly, the value of the discrepancy reaches as much as 9%.

The bakery owner noticed that in the more distant shops, the quantity of discrepancies in the amount of returns is higher. They therefore put forward the hypothesis that the distance of the shop from the bakery is a statistically insignificant factor influencing the level of irregularity in the returns. This is the so-called null hypothesis H₀. In this situation the alternative hypothesis H₁ is the opposite position, saying that distance has a significant influence on the level of irregularity.

H₀: distance has no significance for the level of irregularity
H₁: distance has significance for the level of irregularity

Table 2. A summary of the bread sales of the Moczary bakery

Village name Returns Returns/day Discrepancy level of returns Distance (km)
Anielin 37 5.29 0.0151 21.8
Arkowo 51 7.29 0.011 6.6
Alerpin 13 1.86 0.0591 16.2
Baskowice 46 6.57 0.0152 35.1
Halęcin 69 9.86 0.0122 32
Złodzieje Kolonia 25 3.57 0.0196 17.1
Szkarady 13 1.86 0.0484 19.9
Szkarady Nadzieja 62 8.86 0.0147 17.4
Tupadły 47 6.71 0.0134 8.2
Kapitany Stare 49 7 0.01 53.9

The owner divided the 86 shops into distance ranges using a quantile formula. The formula below is written in the Python programming language.

df[’Group_of_distance’]=pd.qcut(df[’Distance km’], 4,labels=[„up to 12 km”,”12-20 km”,”20-28 km”,”28-54 km”])

Table 3. The discrepancy level of returns and the distance group from the bakery

Village name Discrepancy level of returns Distance group
Anielin 0.0151 20-28 km
Arkowo 0.0110 up to 12 km
Alerpin 0.0591 12-20 km
Baskowice 0.0152 28-54 km
Halęcin 0.0122 28-54 km
Złodzieje Kolonia 0.0196 12-20 km
Szkarady 0.0484 12-20 km
Szkarady Nadzieja 0.0147 12-20 km
Tupadły 0.0134 up to 12 km
Kapitany Stare 0.0100 28-54 km

From the results obtained, a pivot table was created.

pd.pivot_table(df, index=[’Group_of_distance’], values=”Distance km”, aggfunc=[’min’,’max’,’count’])

Table 4. Groups of shops according to distance from the Moczary bakery

Distance group Distance km (min) Distance km (max) Distance km (count)
up to 12 km 0.0 12.0 22
12-20 km 12.6 19.9 21
20-28 km 20.2 28.4 21
28-54 km 29.2 53.9 22

The shops were divided into four groups; each contains 21–22 shops. The choice of the number of groups was carried out on the basis of the researcher’s subjective decision.

The ANOVA test

Analysis of variance, ANOVA, is a statistical method serving to examine the dependency on one or several factors acting simultaneously. This method makes it possible to explain whether the indicated factors may be the reason for the differences between the observed groups.

In our case we have four groups of shops according to distance. We put forward the hypothesis H₀, that distance has no statistically significant influence on the level of irregularity in the returns process.

We apply the functions of the Python language:

import statsmodels.api as sm
from statsmodels.formula.api import ols
model_H = ols(’Discrepancy_level_of_returns ~ C(Group_of_distance)’, data=df).fit()
anova_table = sm.stats.anova_lm(model_H, typ=2)
print(anova_table)

We obtain the ANOVA test value:

sum_sq df F PR(>F)
C(Group_of_distance) 0.005155 3.0 6.746977 0.0004
Residual 0.020883 82.0 NaN NaN

The value marked in red indicates the level of probability that the distance of the bakery from the shops has no significance. The probability value of the null hypothesis being satisfied, p < 0.05, is very small, which means that there exists a statistically significant influence of distance on the level of irregularity in returns. The null hypothesis H₀, put forward at the beginning of the study, was therefore rejected in favour of the alternative hypothesis H₁.

Tukey’s HSD test

ANOVA showed that there is a statistically significant difference in the level of irregularity in the groups. Unfortunately, ANOVA does not indicate which distance groups significantly differ from one another. In order to learn the significance of the differences between the groups, one has to carry out a multiple pairwise comparison analysis (comparing pairs of groups), using Tukey’s HSD test.

from statsmodels.stats.multicomp import pairwise_tukeyhsd
m_comp = pairwise_tukeyhsd(endog=df[’Discrepancy_level_of_returns’], groups=df[’Group_of_distance’], alpha=0.05)
print(m_comp)

Table 5. The significance table of Tukey’s HSD test

group1 group2 meandiff p-adj lower upper reject
12-20 km 20-28 km 0.0054 0.6703 -0.0075 0.0183 False
12-20 km 28-54 km -0.0046 0.7596 -0.0173 0.0082 False
12-20 km up to 12 km -0.0156 0.0103 -0.0284 -0.0028 True
20-28 km up to 12 km -0.021 0.001 -0.0338 -0.0082 True
28-54 km up to 12 km -0.011 0.1087 -0.0236 0.0016 False

Two marked pairs of distances indicate statistically significant differences. The test indicated:

12-20 km – up to 12 km
20-28 km – up to 12 km

At the same time, the test indicated that the means in the remaining pairs of distances do not differ statistically significantly, and it is not worth dealing with them. The bakery owner received the information that they should focus on shops located at a distance above 12 km and below 28 km. These conclusions can be seen better on the graph below.

m_comp.plot_simultaneous()
plt.vlines(x=20000,ymin=-0.5,ymax=3.5, color=”red”, alpha=0.8, linestyle=’–’)

Chart 1. Tukey’s HSD test for four distance groups

On Chart 1 it can be seen that the range of irregularity in the distance ranges of up to 12 km and 28-54 km is statistically similar. The area of similarity has been marked with a red frame. This means that for the pair of ranges 12 km – 28-54 km, the null hypothesis H₀ was retained. On the X axis of the chart the range of irregularity in the number of returns is marked. Attention should be paid to the distance ranges 20-28 km and 12-20 km, which differ significantly from the range up to 12 km.

Verification of the conformity of the ANOVA test of homogeneity of variance

In order to be certain of the results of the one-way ANOVA test, one has to verify the basic conditions for applying this test. These are as follows:

  1. the residual values have a normal distribution (Shapiro–Wilk test),
  2. the variances in the groups are homogeneous (Levene’s or Bartlett’s test),
  3. the observations are conducted independently of one another.

Levene’s test — checking the homogeneity of variance

In this test we check whether the distance groups have homogeneous variances. The normal distributions of the individual groups should be visually similar to one another. Then Levene’s test will probably show that the condition of homogeneity of variance is satisfied. We therefore put forward the null hypothesis H₀, saying that the variances of the groups of distance from the bakery have similar variance.

PKS = pd.pivot_table(df, index=’Village_name’, columns=’Group_of_distance’, values=’Discrepancy_level_of_returns’)
PO1=PKS[’up to 12 km’].dropna(how=’any’)
PO2=PKS[’12-20 km’].dropna(how=’any’)
PO3=PKS[’20-28 km’].dropna(how=’any’)
PO4=PKS[’28-54 km’].dropna(how=’any’)
PKS.plot.kde()

Chart 2. The probability density distributions of the four distance groups

As can be seen, the group up to 12 km differs considerably from the others. Chart 2 shows that the process of collecting returns is significantly best in shops located up to 12 km from the bakery. Undoubtedly the worst course of the process takes place in shops located at a distance of between 20 and 28 km from the bakery. This is also confirmed by the box plot below.

PKS.boxplot(column=[’up to 12 km’, ’12-20 km’, ’20-28 km’, ’28-54 km’], grid=False)

Chart 3. A box plot of the distance groups

It is time to carry out Levene’s test.

import scipy.stats as stats
w,p = stats.levene(PO1,PO2,PO3,PO4)
print(„Value: „,w)
print(„p-value: „,p)

Value: 4.909693810347966
p-value: 0.0034594148761172058

As can be seen, the p-value probability is smaller than 0.05, which means that the null hypothesis H₀, saying that the variances of the groups are homogeneous, should be rejected. Since the variances are not homogeneous, the result of the similarity of pairs in the ANOVA test can be called into question.

The Shapiro–Wilk test — checking the normality of the distribution of residuals

This test will answer the question of whether the differences between the residual and empirical values have a normal distribution. In this particular case the point is to confirm the unsuitability of the ANOVA test. This doubt appeared after Levene’s test.

w, p = stats.shapiro(model_H.resid)
print(„Value: „,w)
print(„p-value: „,np.round(p, decimals=2))

Value: 0.9254999756813049
p-value: 0.0

The Shapiro–Wilk test showed that the probability of normality of the distribution of residuals is not normal. Unfortunately the test showed that the chosen groups are non-statistical, and that in order to better examine the phenomenon, more distance groups than 4 should be defined.

from statsmodels.graphics.gofplots import qqplot
from matplotlib import pyplot
qqplot(model_H.resid, line=’s’)
pyplot.show()

Chart 4. Verification of the normal distribution of residuals

Chart 4 of the normality of the residuals indicated that there are several shops which statistically significantly deviate from the average. These shops are symbolised by the navy-blue dots located far from the red line symbolising an ideal normal distribution. Finding these shops should significantly limit the phenomenon of irregularity in handling returns.

The chart also showed that the normal distribution is rather retained, which means that no falsification is taking place in keeping the returns documentation. This chart works excellently for verifying whether the process of describing the phenomenon by the shops’ staff has a genuine character, or is perhaps conducted in a creative way. Since the Shapiro–Wilk and Levene tests did not come out too badly, it is worth conducting the whole process again using a larger number of distance groups.

Anyone who wants to manage their bakery effectively must have accurate information. An experienced statistician can quickly carry out a precise analysis, locate the places and the character of the irregularities. Of course one can also calculate the average weekly inventory difference for each shop oneself, sort the results, choose the worst shops and take corrective action. However, if someone wants to introduce machine learning forecasting models into their bakery, or wants to introduce modern artificial intelligence algorithms, they must carry out sophisticated research processes. A sample of such research has been presented in this publication.

Wojciech Moszczyński

Wojciech Moszczyński — graduate of the Department of Econometrics and Statistics of Nicolaus Copernicus University in Toruń; specialist in econometrics, finance, data science, and management accounting. He specializes in the optimization of production and logistics processes. He conducts research in the area of the development and application of artificial intelligence. For years he has been engaged in the popularization of machine learning and data science in business environments.

Bądź pierwszy, który skomentuje ten wpis!

Dodaj komentarz

Twój adres email nie zostanie opublikowany.


*