Statistics with DataFrames
A DataFrame is one of the basic data structures in Maple. Data frames are a list of variables, known as DataSeries, which are displayed in a rectangular grid. Every column (variable) in a DataFrame has the same length; however, each variable can have a different type, such as integer, float, string, name, truefalse, etc., which makes data frames an ideal storage device for heterogeneous data.
When printed, Data frames resemble matrices in that they are viewed as a rectangular grid, but a key difference is that the first row corresponds to the column (variable) names and the first column corresponds to the row (individual) names. These row and columns are treated as header metainformation and are not a part of the data. Moreover, the data stored in a DataFrame can be accessed using these header names, as well as by the standard numbered index.
This example page shows a number of common statistical operations with data frames. For more information, including a list of Statistics commands that can be run on data frames, see DataFrames in Statistics.

Getting Started


To begin, the Statistics package is loaded.
A new DataFrame can be created based on randomly generated data. The following generates a DataFrame with 3 DataSeries. The first two columns are generated by randomly sampling from a Uniform(0,1) distribution. The third DataSeries also has randomly generated data, which is sampled from 4 distinct levels: 0, 1, 2, 3.
>

data := DataFrame( < Sample(Uniform(0, 1), [20, 2])  LinearAlgebra:RandomVector(20, generator = rand(0 .. 3) ) >, columns = [a,b,c] );

${\mathrm{data}}{\u2254}\left[\begin{array}{cccc}{}& {a}& {b}& {c}\\ {1}& {0.814723686393179}& {0.655740699156587}& {2}\\ {2}& {0.905791937075619}& {0.0357116785741896}& {3}\\ {3}& {0.126986816293506}& {0.849129305868777}& {3}\\ {4}& {0.913375856139019}& {0.933993247757551}& {0}\\ {5}& {0.632359246225410}& {0.678735154857773}& {2}\\ {6}& {0.0975404049994095}& {0.757740130578333}& {1}\\ {7}& {0.278498218867048}& {0.743132468124916}& {2}\\ {8}& {0.546881519204984}& {0.392227019534168}& {3}\\ {9}& {0.957506835434298}& {0.655477890177557}& {0}\\ {10}& {0.964888535199277}& {0.171186687811562}& {1}\\ {\mathrm{`:`}}& {\mathrm{`:`}}& {\mathrm{`:`}}& {''20\; x\; 3\; DataFrame''}\end{array}\right]$
 (1) 


DataFrames and the Context Panel


The Maple programming language provides many commands that are useful for exploring DataFrames. The Context Panel provides easy access to a selection of these commands, displaying contextspecific commands that can be applied to DataFrames or DataSeries.
The DataFrame contextsensitive options include many commands that can be applied to entire DataFrames as well as to a single DataSeries in a DataFrame. The second section of the DataFrame menu in the Context Panel includes commands for conversions, operations, queries, and visualization of DataFrames and DataSeries. The third section includes more commands relating to statistics and data analysis, including data analysis, data manipulation, properties and quantities, and summary and tabulation.
A useful feature of the contextsensitive options is the ability to quickly filter the DataFrame by value or to select columns to apply operations to. This can be beneficial when dealing with heterogeneous data that includes nonnumeric DataSeries. In many cases, commands in Statistics assumes an entirely numeric DataFrame. Selectively removing the nonnumeric data makes it possible to use the routines natively on the given DataFrame.


Computing Summary Statistics


One of the most fundamental statistical tasks is to generate summary statistics, such as the mean or standard deviation, for a given data set. The DataSummary command returns the mean, standard deviation, skewness and kurtosis, as well as the minimum and maximum values, and the cumulative weight. When all of the elements in the DataSeries have weight = 1, the cumulative weight corresponds to the total number of observations in the DataSeries.
>

DataSummary(data, summarize = embed):


a

b

c

mean

${0.6420332443759758}$

${0.49256167171857845}$

${1.6499999999999997}$

standarddeviation

${0.3299319303460653}$

${0.33446642507946533}$

${1.2680278927697548}$

skewness

${}{0.5350979067702286}$

${}{0.22331966492529112}$

${}{0.2598207055845495}$

kurtosis

${1.6289929083961734}$

${1.412887607415442}$

${1.387479273868869}$

minimum

${0.09754040499940952}$

${0.031832846377420676}$

${0.0}$

maximum

${0.9705927817606157}$

${0.9502220488383549}$

${3.0}$

cumulativeweight

${20.0}$

${20.0}$

${20.0}$



Individual summary statistics can also be returned. For example, the variance of each of the DataSeries in the DataFrame is:
$\left[\begin{array}{cc}{a}& {0.108855078661881}\\ {b}& {0.111867789505438}\\ {c}& {1.60789473684210}\end{array}\right]$
 (2) 
It is important to note that a DataSeries is returned from requesting any individual summary statistic. Each row corresponds to each of the columns in the DataFrame. In this case, column "a" has a variance of 0.11, column "b" has a variance of 0.11, and column "c" has a variance of 1.61.


Aggregate Statistics


The defined DataFrame has a third column which has 3 distinct levels: 0, 1, 2, and 3. The distinct levels in a DataSeries can easily be seen by collapsing the DataSeries into a set:
$\left\{{0}{\,}{1}{\,}{2}{\,}{3}\right\}$
 (3) 
When a DataFrame contains a DataSeries that has a fixed number of distinct levels, it can be useful to compute aggregate statistics based on the levels in a given DataSeries. Using the example DataFrame, since the column "c" has 4 distinct levels, aggregate statistics can be computed using the Aggregate command, that show values for a given statistic for each of the levels.
The following shows the mean for each of the levels in column "c":
$\left[\begin{array}{cccc}{}& {a}& {b}& {c}\\ {1}& {0.789176927985601}& {0.571571656996847}& {0}\\ {2}& {0.531214470099343}& {0.464463409194948}& {1}\\ {3}& {0.620901876470972}& {0.609871986015197}& {2}\\ {4}& {0.562666428150338}& {0.349073820560658}& {3}\end{array}\right]$
 (4) 
The default summary statistic for the Aggregate command is the mean. By specifying a function, any summary statistic can be returned for the levels in column "c":
>

Aggregate(data, 3, function = Median);

$\left[\begin{array}{cccc}{}& {a}& {b}& {c}\\ {1}& {0.935441345786658}& {0.739467859252425}& {0}\\ {2}& {0.531214470099343}& {0.464463409194948}& {1}\\ {3}& {0.632359246225410}& {0.678735154857773}& {2}\\ {4}& {0.546881519204984}& {0.317099480060861}& {3}\end{array}\right]$
 (5) 
The tally option can be optionally specified in order to tally up the number of observations in each of the levels:
>

Aggregate(data, 3, function = Range, tally);

$\left[\begin{array}{ccccc}{}& {a}& {b}& {c}& {\mathrm{Tally}}\\ {1}& {0.828706443133400}& {0.918389202460934}& {0}& {6}\\ {2}& {0.867348130199867}& {0.586553442766772}& {1}& {2}\\ {3}& {0.678668729375897}& {0.466209483164026}& {2}& {5}\\ {4}& {0.788748708895561}& {0.813417627294588}& {3}& {7}\end{array}\right]$
 (6) 


Data Manipulation


The Statistics package has many commands for manipulating statistical data including data selection, scaling and more. For example, the Scale command can be used to center and scale numeric DataSeries:
>

data[[1, 2]] := Scale(data[[1, 2]]);

${{\mathrm{data}}}_{\left[{1}{\,}{2}\right]}{\u2254}\left[\begin{array}{ccc}{}& {a}& {b}\\ {1}& {0.523412334890013}& {0.487878648504820}\\ {2}& {0.799433666280760}& {}{1.36590688597771}\\ {3}& {}{1.56106875603776}& {1.06607900648169}\\ {4}& {0.822419980625800}& {1.31980833631984}\\ {5}& {}{0.0293211940427203}& {0.556628316564098}\\ {6}& {}{1.65031871515269}& {0.792840294199191}\\ {7}& {}{1.10184856957499}& {0.749165768572450}\\ {8}& {}{0.288398049473985}& {}{0.299984227596453}\\ {9}& {0.956177811366793}& {0.487092892568428}\\ {10}& {0.978551213532616}& {}{0.960858728437875}\\ {\mathrm{`:`}}& {\mathrm{`:`}}& {''20\; x\; 2\; DataFrame''}\end{array}\right]$
 (7) 
$\left[\begin{array}{cccc}{}& {a}& {b}& {c}\\ {1}& {0.523412334890013}& {0.487878648504820}& {2}\\ {2}& {0.799433666280760}& {}{1.36590688597771}& {3}\\ {3}& {}{1.56106875603776}& {1.06607900648169}& {3}\\ {4}& {0.822419980625800}& {1.31980833631984}& {0}\\ {5}& {}{0.0293211940427203}& {0.556628316564098}& {2}\\ {6}& {}{1.65031871515269}& {0.792840294199191}& {1}\\ {7}& {}{1.10184856957499}& {0.749165768572450}& {2}\\ {8}& {}{0.288398049473985}& {}{0.299984227596453}& {3}\\ {9}& {0.956177811366793}& {0.487092892568428}& {0}\\ {10}& {0.978551213532616}& {}{0.960858728437875}& {1}\\ {\mathrm{`:`}}& {\mathrm{`:`}}& {\mathrm{`:`}}& {''20\; x\; 3\; DataFrame''}\end{array}\right]$
 (8) 


Data Analysis


There are many commands in Maple for analyzing data, including commands for regression analysis, principal component analysis, analysis of variance and more.
Principal Component Analysis (PCA) aims to identify patterns in data by reducing the dimensionality of multivariate data to a few key explanatory variables called principal components. Performing a principal component analysis on our data returns a record, which can be queried for the principal components, transformation matrix, singular values and more.
>

pca := PCA(data, summarize = true);

summary:
Values proportion of variance St. Deviation
1.7667 0.4897 1.3292
1.5158 0.4201 1.2312
0.3254 0.0902 0.5704
 
${\mathrm{pca}}{\u2254}{\mathrm{Record}}{}\left({\mathrm{values}}{\=}\left[\begin{array}{c}{1.76667975332057}\\ {1.51584762099920}\\ {0.325367362522331}\end{array}\right]{\,}{\mathrm{varianceproportion}}{\=}\left[\begin{array}{c}{0.489670537025396}\\ {0.420147407716773}\\ {0.0901820552578307}\end{array}\right]{\,}{\mathrm{stdev}}{\=}\left[\begin{array}{c}{1.32916505871941}\\ {1.23119763685576}\\ {0.570409819798302}\end{array}\right]{\,}{\mathrm{rotation}}{\=}\left[\begin{array}{cccc}{}& {1}& {2}& {3}\\ {a}& {}{0.300703469968028}& {0.676176767988283}& {}{0.672578918478796}\\ {b}& {}{0.162537438405368}& {}{0.731235081875763}& {}{0.662477800496566}\\ {c}& {0.939765398525509}& {0.0898901188511082}& {}{0.329788966260434}\end{array}\right]{\,}{\mathrm{principalcomponents}}{\=}\left[\begin{array}{cccc}{}& {1}& {2}& {3}\\ {1}& {0.0922274383777869}& {0.0286268190441588}& {}{0.790671014280649}\\ {2}& {1.25030181689662}& {1.66070916673832}& {}{0.0780143456086121}\\ {3}& {1.56482432895739}& {}{1.71376113520678}& {}{0.101526844214222}\\ {4}& {}{2.01243571568378}& {}{0.557307568544938}& {}{0.883334270497134}\\ {5}& {0.247261933557959}& {}{0.395390921260593}& {}{0.464459224064882}\\ {6}& {}{0.243457175325683}& {}{1.75408838965889}& {0.799093310405865}\\ {7}& {0.538480092769681}& {}{1.26139915511044}& {0.129348290489807}\\ {8}& {1.40416425013382}& {0.145702590689482}& {}{0.0525117649632190}\\ {9}& {}{1.91730972437505}& {0.142047114829403}& {}{0.421641472019027}\\ {10}& {}{0.748925738102532}& {1.30585863058459}& {0.192757488198852}\\ {\mathrm{`:`}}& {\mathrm{`:`}}& {\mathrm{`:`}}& {''20\; x\; 3\; DataFrame''}\end{array}\right]\right)$
 (9) 
More information on PCA can be captured using a biplot:
>

Biplot(pca, pointlabels = true, points = false);



Visualizing DataFrames


Many statistical visualizations support DataFrames and DataSeries:
The colorscheme option is useful when a DataSeries has a fixed number of distinct levels in a given column. In the following, the points are colored based on the 4 levels {0, 1, 2, 3} found in the column "c".
>

ScatterPlot(data[.., 1], data[.., 2], symbolsize = 20, symbol = solidbox, colorscheme = ["valuesplit", data[.., 3], [0 = "Red", 1 = "Blue", 2 = "Green", 3 = "Purple"]]);



Sampling a DataFrame


A typical way of sampling a dataset stored in a Matrix is to make a new empirical distribution based on the dataset and sample it directly. The same process can be used for numeric DataSeries:
>

Sample(EmpiricalDistribution(data[1]), 10);

$\left[\begin{array}{cccccccccc}{}{0.667628506240773}& {0.822419980956566}& {0.822419980956566}& {}{1.46824274264161}& {}{1.51590937229169}& {0.455166873585043}& {0.995840375504628}& {}{0.474817928185804}& {0.523412335130370}& {}{1.10184856925804}\end{array}\right]$
 (10) 
This method is however only valid for type numeric DataSeries. In the case that we have a nonnumeric OR numeric DataSeries, the RandomTools[Generate] command can be used to sample the DataSeries. This does however require a conversion of the DataSeries to a list:
>

RandomTools:Generate('list'('choose'(convert(data[3], list)), 10));

$\left[{0}{\,}{1}{\,}{0}{\,}{0}{\,}{1}{\,}{3}{\,}{3}{\,}{0}{\,}{3}{\,}{3}\right]$
 (11) 
>

RandomTools:Generate('list'('choose'(convert(DataSeries(["m", "a", "p", "l", "e"]), list)), 10));

$\left[{''e''}{\,}{''e''}{\,}{''l''}{\,}{''p''}{\,}{''p''}{\,}{''e''}{\,}{''p''}{\,}{''p''}{\,}{''e''}{\,}{''p''}\right]$
 (12) 


More examples


There are several examples for working with DataFrames and DataSeries:

