Section 3.4 Measures of the Spread of Data
One measure of the spread of the data is the inter-quartile range that was introduced in the context of the box plot. However, the most important measure of spread is the standard deviation.
Before dealing with the standard deviation let us discuss the calculation of the variance. If \(x_i\) is a data value for subject \(i\) and \(\bar x\) is the sample mean, then \(x_i-\bar x\) is called the deviation of subject \(i\) from the mean, or simply the deviation. In a data set, there are as many deviations as there are data values. The variance is in principle the average of the squares of the deviations.
Consider the following example: In a fifth grade class, the teacher was interested in the average age and the standard deviation of the ages of her students. Here are the ages of her students to the nearest half a year:
\begin{align*}
\amp 9,\; 9.5,\; 9.5,\; 10,\; 10,\; 10,\; 10,\; 10.5,\; 10.5,\; 10.5,\; 10.5,\; 11,\; 11,\; 11,\; 11,\; 11,\; 11,\;\\
\amp 11.5,\; 11.5,\; 11.5\;.
\end{align*}
In order to explain the computation of the variance of these data let us create an object
x that contains the data:
x <- c(9,9.5,9.5,10,10,10,10,10.5,10.5,10.5,10.5,11,11,11,11,11,11,11.5,11.5,11.5)
length(x)
## [1] 20
Pay attention to the fact that we did not write the β
+β at the beginning of the second line. That symbol was produced by R when moving to the next line to indicate that the expression is not complete yet and will not be executed. Only after inputting the right bracket and the hitting of the Return key does R carry out the command and creates the object βxβ. When you execute this example yourself on your own computer make sure not to copy the β+β sign. Instead, if you hit the return key after the last comma on the first line, the plus sign will be produced by R as a new prompt and you can go on typing in the rest of the numbers.
The function β
lengthβ returns the length of the input sequence. Notice that we have a total of 20 data points.
The next step involves the computation of the deviations:
x.bar <- mean(x)
x.bar
x - x.bar
## [1] 10.525 ## [1] -1.525 -1.025 -1.025 -0.525 -0.525 -0.525 -0.525 -0.025 -0.025 -0.025 ## [11] -0.025 0.475 0.475 0.475 0.475 0.475 0.475 0.975 0.975 0.975
The average of the observations is equal to 10.525 and when we delete this number from each of the components of the sequence
x we obtain the deviations. For example, the first deviation is obtained as 9 - 10.525 = -1.525, the second deviation is 9.5 - 10.525 = -1.025, and so forth. The 20th deviation is 11.5 - 10.525 = 0.975, and this is the last number that is presented in the output.
From a more technical point of view observe that the expression that computed the deviations, β
x - x.barβ, involved the deletion of a single value (x.bar) from a sequence with 20 values (x). The expression resulted in the deletion of the value from each component of the sequence. This is an example of the general way by which R operates on sequences. The typical behavior of R is to apply an operation to each component of the sequence.
As yet another illustration of this property consider the computation of the squares of the deviations:
(x - x.bar)^2
## [1] 2.325625 1.050625 1.050625 0.275625 0.275625 0.275625 0.275625 ## [8] 0.000625 0.000625 0.000625 0.000625 0.225625 0.225625 0.225625 ## [15] 0.225625 0.225625 0.225625 0.950625 0.950625 0.950625
Recall that β
x - x.barβ is a sequence of length 20. We apply the square function to this sequence. This function is applied to each of the components of the sequence. Indeed, for the first component we have that \((-1.525)^2 = 2.325625\text{,}\) for the second component \((-1.025)^2 = 1.050625\text{,}\) and for the last component \((0.975)^2 = 0.950625\text{.}\)
For the variance we sum the square of the deviations and divide by the total number of data values minus one (\(n-1\)). The standard deviation is obtained by taking the square root of the variance:
sum((x - x.bar)^2)/(length(x)-1)
sqrt(sum((x - x.bar)^2)/(length(x)-1))
## [1] 0.5125 ## [1] 0.7158911
If the variance is produced as a result of dividing the sum of squares by the number of observations minus one then the variance is called the sample variance.
The function β
varβ computes the sample variance and the function βsdβ computes the standard deviations. The input to both functions is the sequence of data values and the outputs are the sample variance and the standard deviation, respectively:
var(x)
sd(x)
## [1] 0.5125 ## [1] 0.7158911
In the computation of the variance we divide the sum of squared deviations by the number of deviations minus one and not by the number of deviations. The reason for that stems from the theory of statistical inference that will be discussed in Part II of this book. Unless the size of the data is small, dividing by \(n\) or by \(n-1\) does not introduce much of a difference.
The variance is a squared measure and does not have the same units as the data. Taking the square root solves the problem. The standard deviation measures the spread in the same units as the data.
The sample standard deviation, \(s\text{,}\) is either zero or is larger than zero. When \(s=0\text{,}\) there is no spread and the data values are equal to each other. When \(s\) is a lot larger than zero, the data values are very spread out about the mean. Outliers can make \(s\) very large.
The standard deviation is a number that measures how far data values are from their mean. For example, if the data contains the value 7 and if the mean of the data is 5 and the standard deviation is 2, then the value 7 is one standard deviation from its mean because \(5 + 1 \times 2 = 7\text{.}\) We say, then, that 7 is one standard deviation larger than the mean 5 (or also say βto the right of 5β). If the value 1 was also part of the data set, then 1 is two standard deviations smaller than the mean (or two standard deviations to the left of 5) because \(5 - 2 \times 2 = 1\text{.}\)
The standard deviation, when first presented, may not be too simple to interpret. By graphing your data, you can get a better βfeelβ for the deviations and the standard deviation. You will find that in symmetrical distributions, the standard deviation can be very helpful but in skewed distributions, the standard deviation is less so. The reason is that the two sides of a skewed distribution have different spreads. In a skewed distribution, it is better to look at the first quartile, the median, the third quartile, the smallest value, and the largest value.
