Statistics with DataFrames - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Applications and Example Worksheets : Statistics and Data Analysis : examples/DataFrame/Statistics

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 meta-information 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 Statisitics commands that can be run on data frames, see DataFrames in Statistics.

Getting Started

To begin, the Statistics package is loaded.

with(Statistics):

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] );

dataabc10.8147236863931790.655740699156587220.9057919370756190.0357116785741896330.1269868162935060.849129305868777340.9133758561390190.933993247757551050.6323592462254100.678735154857773260.09754040499940950.757740130578333170.2784982188670480.743132468124916280.5468815192049840.3922270195341683............

(1)

DataFrame Context Menu

The Maple programming language provides many commands that are useful for exploring DataFrames. The right-click context menu provides easy access to a selection of these commands, displaying context specific commands that can be applied to DataFrames or DataSeries.

The DataFrame context menu contains 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 context menu 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 context menu 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 non-numeric DataSeries. In many cases, commands in Statistics assumes an entirely numeric DataFrame. Selectively removing the non-numeric 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:

Variance(data);

a0.108855078661881b0.111867789505438c1.60789473684210

(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:

convert(data[3], set);

0&comma;1&comma;2&comma;3

(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":

Aggregate(data, 3);

abc10.7891769279856010.571571656996847020.5312144700993430.464463409194948130.6209018764709720.609871986015197240.5626664281503380.3490738205606583

(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);

abc10.9354413457866580.739467859252425020.5312144700993430.464463409194948130.6323592462254100.678735154857773240.5468815192049840.3170994800608613

(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);

abcTally10.8287064431334000.9183892024609340620.8673481301998670.5865534427667721230.6786687293758970.4662094831640262540.7887487088955610.81341762729458837

(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]]);

data1&comma;2ab10.5234123348900130.48787864850482020.7994336662807601.3659068859777131.561068756037761.0660790064816940.8224199806258001.3198083363198450.02932119404272030.55662831656409861.650318715152690.79284029419919171.101848569574990.74916576857245080.2883980494739850.299984227596453.........

(7)

data;

abc10.5234123348900130.487878648504820220.7994336662807601.36590688597771331.561068756037761.06607900648169340.8224199806258001.31980833631984050.02932119404272030.556628316564098261.650318715152690.792840294199191171.101848569574990.749165768572450280.2883980494739850.2999842275964533............

(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

pcaRecordvalues&equals;1.766679753320571.515847620999200.325367362522331&comma;varianceproportion&equals;0.4896705370253960.4201474077167730.0901820552578308&comma;stdev&equals;1.329165058719411.231197636855760.570409819798302&comma;rotation&equals;123a0.3007034699680270.6761767679882840.672578918478796b0.1625374384053690.7312350818757620.662477800496566c0.9397653985255090.08989011885110650.329788966260434&comma;principalcomponents&equals;12310.09222743837778690.02862681904415880.79067101428064921.250301816896621.660709166738320.078014345608612031.564824328957391.713761135206780.10152684421422242.012435715683780.5573075685449340.88333427049713450.2472619335579580.3953909212605930.46445922406488260.2434571753256861.754088389658890.79909331040586570.5384800927696791.261399155110440.12934829048980781.404164250133820.1457025906894790.0525117649632190............

(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:

BoxPlot(data);

AreaChart(data);

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);

0.6676285062407730.8224199809565660.8224199809565661.468242742641611.515909372291690.4551668735850430.9958403755046280.4748179281858040.5234123351303701.10184856925804

(10)

This method is however only valid for type numeric DataSeries. In the case that we have a non-numeric 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));

0&comma;1&comma;0&comma;0&comma;1&comma;3&comma;3&comma;0&comma;3&comma;3

(11)

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

e&comma;e&comma;l&comma;p&comma;p&comma;e&comma;p&comma;p&comma;e&comma;p

(12)

More examples

There are several examples for working with DataFrames and DataSeries:

• 

DataFrame Guide : Examples of working with DataFrames

• 

Iris Data : Examples of summarizing data, computing aggregate statistics, and principal component analysis

• 

Subsets of DataFrames : Examples of indexing and filtering columns and rows of a DataFrame


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam