Chapter 2

Descriptive Statistics

Needs["Statistics`DescriptiveStatistics`"];

house
       = {70, 65, 80, 91, 105, 143, 150, 145, 120, 92, 100, 95, 83, 139, 510, 
        101, 675, 84, 580}ast = {19, 25, 22, 23, 24, 19, 22, 19, 24, 24, 27, 18, 20, 23, 25, 28, 
        10, 18, 23, 19, 21, 17, 23, 26, 24, 26, 21, 14, 27, 18, 19, 24, 23, 
        21, 23, 20, 22, 25, 25, 16, 24, 19, 22, 15, 22, 26, 19, 8, 22, 24}gpigs
       = {40,  45,  53,  56,  56,  57, 58, 62, 64, 73, 74, 79, 78, 80, 
        81, 81, 81, 82, 83, 83, 84, 85, 89, 89, 91, 92, 92, 97, 99, 99, 99, 
        99, 101, 102, 102, 102, 103, 104, 107, 108, 109, 113, 114, 118, 121, 
        123, 126, 128, 137, 138, 134, 144, 145, 147, 156, 162, 174, 174, 179, 
        184, 191, 198, 208, 214, 247, 249, 328, 383, 403, 511}cereal
       = {1622, 1579, 1603, 1576, 1589, 1625, 1588, 1589, 1562, 1595, 1589, 
          1599, 1603, 1594, 1629, 1606, 1617, 1597, 1616, 1614, 1600, 1616, 
          1596, 1589, 1608, 1594, 1596, 1608, 1606, 1617, 1618, 1610, 1598, 
          1617, 1593, 1587, 1592, 1603, 1605, 1598, 1599, 1592, 1622, 1625, 
          1564, 1603, 1605, 1626, 1614}/100.00

The X-Files data

Mathematica has functions to perform many statistical operations, but they are not generally loaded when Mathematica starts.  Rather related functions are bundled into packages which can be loaded as a group before they are needed.  Measures of center and spread are in a package called `DescriptiveStatistics` and it can be loaded with the following command.

Needs["Statistics`DescriptiveStatistics`"];

Once loaded the descriptive statistic fuctions are available. Consider the data

season1
       = {7.4, 6.9, 6.8, 5.9, 6.2, 5.6, 5.6, 6.2, 6.1, 5.1, 6.4, 6.4, 6.2, 
        6.8, 7.2, 6.8, 5.8, 7.1, 7.2, 7.5, 8.1, 7.7, 7.4, 8.3}

Mean[season1]

Median[season1]

Mode[season1]

Variance[season1]

StandardDeviation[season1]

SampleRange[season1]

InterquartileRange[season1]

Quantile[season1, .5]

Quantile[season1,  .25]

Quantile[season1,  .75]

To make bar charts of the data, the packages Statistics`DataManipulation` and Graphics`Graphics` are needed.

Needs["Statistics`DataManipulation`"]

The Frequencies command counts the number of occurences of each piece of data.

freq = Frequencies[season1]

We complete the histogram by making a bar chart of the frequencies.  This is a slightly different method of histogram construction than mentioned in the text.  There the bars covered the intervals of interest; here the bars are centered on the midpoints of the intervals and the bar widths have no meaning.  Other packages may use still other methods.

Needs["Graphics`Graphics`"]

BarChart[freq];

The BinCounts function can be used to bin the data.  Here we group the data into Nbin bins.

\(Nbin = 7; 
smallBit = .01\)minMin = Min[season1] - smallBitmaxMax = Max[season1] + smallBitfreq = 
      BinCounts[season1, {minMin, Max[season1], (maxMax - minMin)/Nbin}]

We also need the midpoint of each interval.

midpoints = 
      Table[minMin - (Max[season1] - Min[season1])/(2*Nbin) + 
          n (Max[season1] - Min[season1])/Nbin, {n, 1, Nbin}]

BarChart[Transpose[{freq, midpoints}]];

Next we look at constucting box plots of data.  Mathematica does not come with a boxplot function so we create on using graphics elements.  Note that we use Statistics`DescriptiveStatistics` package.  If it has been already loaded, it need not be reloaded.

season1
       = {7.4, 6.9, 6.8, 5.9, 6.2, 5.6, 5.6, 6.2, 6.1, 5.1, 6.4, 6.4, 6.2, 
        6.8, 7.2, 6.8, 5.8, 7.1, 7.2, 7.5, 8.1, 7.7, 7.4, 8.3}

Needs["Statistics`DescriptiveStatistics`"];

\(SingleBoxplot[data_, center_, \ width_] := 
      \((
		Clear[a, b, c, d, e, f, g]; 
		a = Min[data]; 
        b = \(Quartiles[data]\)[[1]]; 
        c = \(Quartiles[data]\)[[2]]; 
        d = \(Quartiles[data]\)[[3]]; 
        e = Max[data]; f = center - width; g = center + width; 
		
        Graphics[{
					Line[{{f, e}, {g, e}}], 
					
            Line[{{f, d}, {g, d}}], 
					Line[{{f, c}, {g, c}}], 

            					Line[{{f, b}, {g, b}}], 
					
            Line[{{f, a}, {g, a}}], 
					Line[{{f, b}, {f, d}}], 

            					Line[{{g, b}, {g, d}}], 
					
            Line[{{center, a}, {center, e}}]}])\)\)

\(\(s1 = SingleBoxplot[season1, 1, .25]; 


    Show[s1, Axes -> True, Ticks -> {{1}, Automatic}, 
      AxesOrigin -> {.5, Automatic}]; 
	\)\)

season2
       = {9.8, 9.3, 8.7, 8.2, 8.5, 9.2, 9, 9.1, 8.6, 9.9, 8.5, 9.7, 8.8, 
        10.2, 10.8, 9.8, 10.7, 9.6, 10.2, 9.8, 7.9, 8.5, 8.1, 9, 9.6}

\(\(s2 = SingleBoxplot[season2, 1, .25]; 


    Show[s2, Axes -> True, Ticks -> {{1}, Automatic}, 
      AxesOrigin -> {.5, Automatic}]; 
	\)\)

SingleBoxPlot can be used to make side-by-side box plots.

\(\(s1 = SingleBoxplot[season1, 1, .25]; 

    s2 = SingleBoxplot[season2, 2, .25]; 

    Show[s1, s2, Axes -> True, Ticks -> {{1, 2}, Automatic}, 
      AxesOrigin -> {.5, Automatic}]; 
	\)\)