Data Spread

  • In statistics, the mean is the average of a list of numbers.
  • There are many applications of the mean value, though one of its most useful applications is Standard Deviation.
  • I will be using the well known mtcars dataset included within base R; more specifically the Miles Per Gallon column.
>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Visual

  • The boxplot below shows the mtcars$mpg data.
  • min: 10.4 || quarter1: 15.35 || median: 19.2 || quarter3: 22.8 || max: 33.9
  • Unfortunately, plotly never works correctly for me, so here is the code for it.
fig = plot_ly(
    x = mtcars$mpg,
    type = "box",
    name = "MPG",
    alpha = 0.6,
    color = I("#8C1D40")
) %>% layout(
    margin = list(l = 50, r = 50, b = 50, t = 50),
    xaxis = list(title = "Miles Per Gallon (mpg)"),
    hoverlabel = list(
        font = list(color = "white")
    )
)

fig

Base R for Plotly Plot

Mean

  • Since standard deviation measures deviation relative from the mean, it stands to reason the mean should be found.
  • The mean is calculated by summing all values and dividing by the total amount of values there are. \[ \bar{x} = \frac{\sum_{i=1}^{n} x_i}{n} \]
mean = sum(mtcars$mpg) / length(mtcars$mpg)
  • The mean in the mpg column is around 20 in this case.

Deviations From Mean

  • Next, we calculate how far each car’s MPG deviates from the mean: \((x_i - \bar{x})\).
deviations <- vector(length = length(mtcars$mpg))
for (i in 1:length(mtcars$mpg)) {
    deviations[i] <- mtcars$mpg[i] - mean
}

Squaring Deviations

  • As you could see from the graph previously, there were negative values within the “Differences From Mean” points.
  • When adding all of those up, the total will always be 0 no matter what data set you use. This is a problem since standard deviation uses all the points to figure out a single value for deviation.
  • To get positive values, we square each deviation to get rid of the negative.

\[ \text{S} = \sum_{i=1}^{n} (x_i - \bar{x})^2 \]

Variance and Standard Deviation

  • Since these deviations are a sample and not the true data set, we average the squared deviations by dividing by the amount of summed numbers minus one.
  • The reason for subtracting one is complicated, so the simple answer is that the value gleamed without doing so is too small. \[ \sigma^2 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1} \]
variance = sum(deviations * deviations) / (length(deviations) - 1)
  • This provides the sample variance. Since these are in mpg\(^2\) (or s\(^2\)), the square root gives the awaited standard deviation in the familiar miles per gallon. \[ \sigma = \sqrt{\frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1}} \]
stdev = sqrt(variance)
  • variance is around 36.32 while standard deviation is around 6.03

Visualizing the Standard Deviation Bracket

  • The plot below displays the data again, but the shaded window representing \(\pm 1\) Standard Deviation from the mean. Most of the car mpg data naturally falls inside this zone as it the mpg data points deviate generally within the standard deviation.

Summary

  • The mean establishes the baseline center-point at around 20.09
  • The deviations from the mean show how individual cars differ from the supposed average.
  • squaring cancels out the negative, and multiplying by \(\frac{1}{n-1}\) provides the variance \(\sigma^2\)
  • The square root of the variance is the standard deviation, finally giving around 6.03
  • As a rule of thumb, the bigger stdev is, the more spread out the data it came from will be as well.

One Small Note

  • This is the standard deviation; not the Mean Absolute Deviance (MAD).
  • The MAD is the deviation value using the actual deviations from the mean, and not squaring them as done previously in these slides.
  • The standard deviation found earlier gets bigger the more outliers there are; MAD finds the straight up mean with the absolute values of the deviations which is 90% of the time smaller than the standard deviation. \[ \text{MAD} = \frac{\sum_{i=1}^{n} |x_i - \bar{x}|}{n} \]
  • While this may be easier to read, and doesn’t have the weid “n - 1”, stdev generally is used more because it’s easier to use in calculus.
  • They both prove the same objective, and stdev is used more, so I would recommend sticking with standard deviation to improve your sanity.