Statistics - Maple Programming Help

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Statistics and Data Analysis : Statistics Package : Data Analysis : Statistics/PCA





principal component analysis on data


Calling Sequence







Calling Sequence

PCA( dataset )

PCA( dataset, options )

PrincipalComponentAnalysis( dataset )

PrincipalComponentAnalysis( dataset, options )




data set or DataFrame ; Matrix or DataFrame of values with 2 or more columns



(optional) equation(s) of the form option=value



method : one of the names eigenvector or svd; controls if the principal component analysis uses the eigenvector method (on either the covariance or correlation matrix) or the singular values method. By default this is set to svd.


columns : non-negative integer; controls the number of dimensions of data to retain. The default is set to the same number of dimensions as the original dataset. Columns of data are discarded based on their effect on the variability in the data with the columns accounting for the most variability being discarded last.


tolerance : float; controls the number of dimensions of data to discard. The default is set to 0.0. Columns are discarded if their standard deviations are less than or equal to the value for tolerance multiplied by the standard deviation of the first component.


correlation : truefalse; controls the choice of the transformation matrix in the computation. By default, correlation is set to false, and the covariance matrix is used.


center : truefalse, numeric, procedure; controls if the returned list of values is centered or not. The default is set to true, which uses the Statistics[Mean] command to compute the center. If this is set to false, the list is not centered and the list of values will have the same central mean value as before. If a numeric value is entered, the list is centered using that value as its center. If a procedure is entered, the list is centered using the value returned from the procedure.  If a procedure is entered as the first value of a list, subsequent arguments in the list are passed to the procedure.


scale : truefalse, numeric, procedure, or identical to the name auto; controls if the returned list of values is scaled or not. The default is set to auto. When set to auto, if the correlation matrix is used in the computation, scale is automatically set to true. If the covariance matrix is used, scale is set to false. If scale is set to false, each column is not scaled and therefore has the same standard deviation as before. When set to true, the Statistics[StandardDeviation] command is used to compute the standard deviation. If a numeric value is entered, the list is scaled using that value as its standard deviation. If a procedure is entered, the list is scaled using the value returned from the procedure.  If a procedure is entered as the first value of a list, subsequent arguments in the list are passed to the procedure.


output : list; controls the form of the returned solution. The default is record. Output options include:


record : a record which contains all of the following output options. Each of the outputs can be queried using :-outputname.


values : the singular values or eigenvalues


varianceproportion : the proportion of the total variance for each value


stdev : the square root of the singular values or eigenvalues


rotation : a matrix whose columns correspond to the eigenvectors


principalcomponents : a matrix whose columns correspond to the resulting principal components


ignore : truefalse; controls how missing data is handled. Missing items are represented by undefined or Float(undefined). If ignore=false and the dataset contains missing data, the PCA command will return undefined. If ignore = true, all rows with any missing items in dataset will be removed. The default value is false. The ignore option is not passed to any procedures entered for either of the center or scale options.


summarize : truefalse; controls the display of a printed summary of the singular values or eigenvalues. The default is false



The PCA command is used to perform a principal component analysis on a set of data. Principal Component Analysis transforms a multi-dimensional data set to a new set of perpendicular axes (or components) that describe decreasing amounts of variance in the data.   


The PCA command returns a record containing values for values, varianceproportion, stdev, rotation and principalcomponents by default. The output option can also be used to return specific elements from the principal component analysis.


The default method is svd, which is generally the recommended method for numerical accuracy.


The PrincipalComponentAnalysis command is provided as an alias.



To print out the results of the principal component analysis, set infolevel[Statistics] to 1, or use the summarize option.



If infolevel[Statistics] is set to 1, the PCA command will return a printed summary for the results.

infolevel[Statistics] := 1:

data := < 2, 4, 5, 6, 8, 9 | -2, -1, 0, 1, 2, 4 >;



PCAnalysis := PCA(data):


Values   proportion of variance  St. Deviation
11.2240     0.9904                 3.3502
0.1093      0.0096                 0.3306

The rotation matrix from the principal component analysis:




The principal components can be returned using :-principalcomponents.




The following plot shows the original data set (in red) and the results from the principal component analysis.

plots:-display( dataplot( data, 'points', color="Red" ), dataplot( PCAnalysis:-principalcomponents, 'points' ) );

One use for principal component analysis is to eliminate dimensions from the data. The following plot shows the original two dimensions of data, X and Y, as well as the two resulting principal components. It can be observed that from the principal component analysis, the 2nd component has the least effect on the variance, suggesting that it can be removed.

plotopts := size = [600,100], view=[-10..10,default], symbol=solidcircle, symbolsize=20:

splots := [ [ Statistics:-ScatterPlot( data[..,1], color="Red", plotopts, title = "X" ),
              Statistics:-ScatterPlot( PCAnalysis:-principalcomponents[..,1], color="Orange", plotopts, title = "1st Component" ) ],
            [ Statistics:-ScatterPlot( data[..,2], color="DarkBlue", plotopts, title = "Y" ),
              Statistics:-ScatterPlot( PCAnalysis:-principalcomponents[..,2], color="RoyalBlue", plotopts, title = "2nd Component" ) ] ]:

DocumentTools:-Tabulate( splots );




infolevel[Statistics] := 0:

The following example performs a principal component analysis on multi-dimensional data. The components that have the least impact on the variance are discarded, and the simplified data is reconstructed from the remaining components.

data2 := < 2.5, 2.4, 10.5 ; 0.5, 0.7, 0.785 ; 2.2, 2.9, 1.286 ; 1.9, 2.2, 2.35 ; 3.1, 3.0, 2.202 ; 2.3, 2.7, 1.351 ; 2.0, 1.6, 2.021 ; 1.0, 1.1, 1.247 ; 1.5, 1.6, 2.503 ; 1.1, 0.9, 1.214 >;



The columns option keeps the columns of data with the greatest variance. Here we discard the component with the least amount of impact on the variability of the dataset.

PCAnalysis2 := PCA(data2, columns = 2, summarize = true):


Values   proportion of variance  St. Deviation
8.3208     0.8782                 2.8846
1.1119     0.1173                 1.0545
0.0425     0.0045                 0.2061




The data can be reconstructed using the principal components:

EVectors := PCAnalysis2:-rotation[ 1 .. 3, 1 .. 2];



TempMatrix := EVectors . PCAnalysis2:-principalcomponents^%T:

RecData := Matrix( upperbound( data2 ), (i, j) -> TempMatrix[j, i] + Mean( data2 )[j] );



plots:-display( dataplot(data2, 'points', color = "Red"),
               dataplot(convert(RecData, Matrix), 'points'));

The correlation option is used to compute the principal components using the correlation matrix instead of the covariance matrix. This is often done while using the eigenvector method.

PCA( data2, method=eigenvector, correlation=true, output=[stdev, principalcomponents] );



A Scree Plot is often used to visually determine which principal components explain the majority of the variance.

data3 := DataFrame( < 2.5, 2.4, 10.5, 0.1, 0.5 ; 0.5, 0.7, 0.785, 4.3, 2.0 ; 2.2, 2.9, 1.286, 5.4, 7.0 ; 1.9, 2.2, 2.35, 6.7, 3.1 ; 3.1, 3.0, 2.202, 8.1, 12; 0.1, 0.4, 0.5, 0.6, 0.9 >, columns = [a,b,c,d,e] );



PCAnalysis3 := PCA(data3, summarize = true):


Values   proportion of variance  St. Deviation
31.4897     0.6658                 5.6116
13.2270     0.2797                 3.6369
2.3575      0.0498                 1.5354
0.2180      0.0046                 0.4669
0.0031      0.0001                 0.0558

The following plot indicates that the first three components account for approximately 99.5% of the variance.

ScreePlot( PCAnalysis3 );

The tolerance or removecolumns options can be used to remove the components with the least effect on the overall variance. Using tolerance = 0.01 will remove any principal components whose value is at most 0.01 multiplied by the value of the first principal component, namely the last two.

PCA(data3, tolerance = 0.01):-principalcomponents;



A Biplot can also be used to show the first two components and the observations on the same set of axes. The first principal component is plotted on the x-axis and the second on the y-axis.

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




The Statistics[PCA] command was introduced in Maple 2016.


For more information on Maple 2016 changes, see Updates in Maple 2016.


Iris Data

See Also