Statistics Sampler

Jim Mahoney, September 2003

Setup

First I'm going to load various packages.
See Add-ons / Standard Packages / Statistics  and / Graphics in the Help Browser under the Help menu.

<< Statistics`DescriptiveStatistics`;
<< Statistics`ConfidenceIntervals`;
<< Statistics`NormalDistribution`;
<< Statistics`DataManipulation`;
<< Statistics`HypothesisTests`;
<< Statistics`ANOVA`;
<< Graphics`MultipleListPlot`;<< 
      Graphics`Graphics`;

Mathematica Basics

Take the tour in the Help Browser, particularly Getting Started and Tour of Mathematica in the Getting Started section.  

Basically you type something, hit enter, it does something, and gives you the result.

The screen uses "cells" which can be input, output, or just text like what I'm typing right now.

Mathematica functions are all CapitalLettersWithWordsStuckTogether.  You use square brackets to apply them to something, like this.

Sqrt[16]

Curly braces are used for lists.

t  =  {10, 20, 50}

Mean[t]

One quirk ofMathematicais that it doesn't convert to decimal numbers unless necessary.The ages in the table above were given as integer values and so that's what we get for an answer.  To see a decmial value, type //N at the end of the expression, or input the numbers with periods, 10., 20., 50.

Mean[{10, 20, 50}] // N

Mean[{10. , 20. , 50. }]

There are Palletes in the File menu or keyboard shortcuts that let you enter formulas as they would typically look.  Or you can type the functions; either works - and the system in fact changes between the two.

Sqrt[(a + b)/5]

(Sqrt[((a + b)/5)])

Looking around

Here are some of the statistics specific functions.  Typing a question mark before something gives you help on that thing.  You can also type things like ?*Dev* to get help on anything with a "Dev" in the middle.

?StandardDeviation

?Mean

?Column

?DropNonNumeric

?Frequencies

?BarChart

?Export

Spreadsheet tables

To enter a table from the keyboard, use control-comma to add another column and control-return to add another row.  The grouping symbols at the left and right sides are just parenthesis which grow as needed.  Use tab to move to the next entry and control-space to move the cursor out of the table.  Mathematica stores this data as a list of lists.  Non-numeric data must be enclosed by double quotes.

data = ( ({{"height", "weight", "age", "eyecolor"}, {70., 150., 22, "blue"}, {72., 200., 30, "blue"}, {60., 160., 20, "green"}, {65., 220., 21, "brown"}, {72., 190., 19, "blue"}}))

We can save this table to a .csv (comma separated values).  Like most computer applications, Mathematica has the notion of a "current directory" where files are put.  Use SetDirectory["name"] to move around.
To read this data in we'd type \(TraditionalForm\`\(data\  = \ \(\(Import\)\([\)\(\*"""sampleData . 
              csv\)\)\*"""]\)\).

Directory[]

Export["sampleData.csv", data]

Here's how we'd extract some values into a list.

heights  =  
        DropNonNumeric[Column[data, 1]];
weights  =  
        DropNonNumeric[Column[data, 2]];
ages  =  
        DropNonNumeric[Column[data, 3]];
colors  =  Rest[Column[data, 4]]

There are also many ways to make pictures from the data.  Here are a couple simple examples.

Frequencies[colors]

PieChart[Frequencies[colors]]

This next one shows how options are added.  The arrow is typed as ->.

BarChart[heights, 
      weights, 
BarOrientation -> Horizontal,  
      Frame  ->  True]

And we can evaluate various statistical formulas.

Mean[heights]

Mathematica has verbose and not always obvious names for the formulas in the text.  The one for s, which estimates a population standard deviation from a given sample is called StandardDeviation.  The actual formula that this calculates is

\(TraditionalForm\`s\  = \ \@\(\(nΣ(x\_i\^2)\  - \ \((Σx\_i)\)\^2\)\/\(n(n - 1)\)\)\)

s  =  StandardDeviation[heights]

If on the other had we want the pure standard deviation of this list, treating it as the entire population, then the formula is slightly different.

\(TraditionalForm\`σ\  = \ \@\(\(nΣ(x\_i\^2)\  - \ \((Σx\_i)\)\^2\)\/n\^2\)\)

σ  =  StandardDeviationMLE[heights]

Note that to type the symbol σ (sigma) from the keyboard,type use the escape key to type esc-s-esc or esc-sigma-esc.Likewise the symbol Σ (capital Sigma) is typed as esc-S-esc or esc-Sigma-esc.

Here's a summary of a variety of the descriptive statistics.

LocationReport[heights]

DispersionReport[heights]

One quirk of Mathematica is that it doesn't convert to decimal numbers unless necessary.  The ages in the table above were given as integer values without a decimal point, and so the Mean formula gives this result.

Mean[ages]

To see a decimal value for an expression like this, append //N (which means Number) to the end.

Mean[ages] // N