The Iris data set contains measurements in centimeters for the variables sepal length and width, and petal length and width, for 150 flowers from 3 species of iris, Iris setosa, versicolor, and virginica. The data was collected over several years by Edgar Anderson, who used the data to show that the measurements could be used to differentiate between different species of irises.
The following example discusses techniques for analyzing the Iris data set.
While any command in the package can be referred to using the long form, for example, Statistics:-PCA, it is often easier to load the package and then use the short form command names.
Importing and summarizing the data
The "Iris" dataset is available in the datasets directory of Maple's data directory. By default, the Import command returns a dataframe object when importing csv files.
IrisData := Import("datasets/iris.csv", base = datadir);
DataFrame⁡_rtable36893628111713464060,rows=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,columns=Sepal Length,Sepal Width,Petal Length,Petal Width,Species
The import commands displays a summary of the first 8 rows of the dataset as well as the row and column labels. This dataframe contains 4 columns of floating point data and one column of strings for the plant "Species".
The Describe command prints a brief description for the structure of the imported data:
Describe( IrisData );
IrisData :: DataFrame: 150 observations for 5 variables
Sepal Length: Type: anything Min: 4.300000 Max: 7.900000
Sepal Width: Type: anything Min: 2.000000 Max: 4.400000
Petal Length: Type: anything Min: 1.000000 Max: 6.900000
Petal Width: Type: anything Min: 0.100000 Max: 2.500000
Species: Type: anything Tally: ["versicolor" = 50, "virginica" = 50, "setosa" = 50]
From the dataframe, you can see that the column labels are:
CLabels := ColumnLabels( IrisData );
CLabels≔Sepal Length,Sepal Width,Petal Length,Petal Width,Species
The DataSummary command shows summary statistics for the numeric columns of the dataset:
DataSummary( IrisData[ CLabels[1 .. 4] ], summarize = embed ):
To summarize the column of strings, you can list the distinct elements by collapsing the column into a set:
convert( IrisData[ Species ], set );
Note that DataSummary returns a summary for all rows of the dataframe. The Aggregate command can be used to give aggregate statistics for the three distinct levels (factors) found in the "Species" column. By default, the Aggregate command returns the mean for each factor:
Aggregate( IrisData, Species );
DataFrame⁡5.00603.42801.46200.24600setosa5.93602.77004.26001.3260versicolor6.58802.97405.55202.0260virginica,rows=1,2,3,columns=Sepal Length,Sepal Width,Petal Length,Petal Width,Species
Aggregate can return any summary statistic and tally up the number of observations for each factor level:
Aggregate( IrisData, Species, function = StandardDeviation, tally );
DataFrame⁡0.352490.379060.173660.10539setosa500.516170.313800.469910.19775versicolor500.635880.322500.551890.27465virginica50,rows=1,2,3,columns=Sepal Length,Sepal Width,Petal Length,Petal Width,Species,Tally
In order to visually detect patterns between variables, the variables can be plotted against one another using the GridPlot command. Note that for the upper triangle in the grid of plots, the colorscheme option is passed to plots:-pointplot using the valuesplit option. The valuesplit option splits the "Species" column into three levels and colors points accordingly.
GridPlot(IrisData[ CLabels[1 .. 4] ],
upper = [plots:-pointplot, colorscheme = ["valuesplit", IrisData[Species]], symbol = solidcircle, symbolsize = 20],
lower = '(x) -> Statistics:-PieChart([" " = abs(x), " " = 1 - abs(x)], color = ["CornflowerBlue", "WhiteSmoke"], title = evalf(x), size = [100, 100])',
correlation = [false, true, false], width = 600, widthmode = pixels);
In the above grid of plots, the lower triangle contains a series of piecharts that indicate the value for the correlation between corresponding columns. This type of plot is otherwise known as a correlogram and from this, it can be observed that the "Petal Length" and "Petal Width" columns have a high level of correlation.
Performing a principal component analysis on the data
A principal component analysis can be run on the data to determine which variables explain the majority of the variability in the data.
IrisPCA := PCA(IrisData[ CLabels[1 .. 4] ], summarize):
Values proportion of variance St. Deviation
4.2282 0.9246 2.0563
0.2427 0.0531 0.4926
0.0782 0.0171 0.2797
0.0238 0.0052 0.1544
The principal component analysis command returns a record, which you can query in order to return the principal components, the rotation matrix, and details on the proportion of variance explained by each component. Note that this can also be seen by using the summarize option as above.
For example, the rotation matrix, or loadings for the components can be returned using the rotation option:
DataFrame⁡0.36139−0.656590.582030.31549−0.084523−0.73016−0.59791−0.319720.856670.17337−0.076236−0.479840.358290.075481−0.545830.75366,rows=Sepal Length,Sepal Width,Petal Length,Petal Width,columns=1,2,3,4,datatypes=float8,float8,float8,float8
A ScreePlot is useful in visualizing the variance explained by each component:
ScreePlot( IrisPCA );
From the ScreePlot, it can be seen that the first component accounts for 92.46% of the variance. The second component accounts for a much smaller fraction of the total variance, suggesting that only one component may be enough to summarize the data.
A Biplot can also be used to show the first two components and the observations on the same diagram. The first principal component is plotted on the x-axis and the second on the y-axis.
Biplot(IrisPCA, colorscheme = ["valuesplit", IrisData[ Species ] ]);
From the Biplot, it can be observed that petal width and length are highly correlated and their variability can be primarily attributed to the first component. Likewise, the first component also explains a large part of the Sepal length. The variability in Sepal width is more attributed to the second component.
The data were collected by Anderson, Edgar (1935). The irises of the Gaspé Peninsula, Bulletin of the American Iris Society. 59: 2-5.
Download Help Document
What kind of issue would you like to report? (Optional)