Introduction
I work in consulting. If you're a consultant at a certain type of company, agency, organization, consultancy, whatever, this can sometimes mean travelling a lot.
Many business travelers 'in the know' have heard the old joke that if you want to stay at any type of hotel anywhere in the world and get a great rate, all you have to do is say that you work for IBM.
The point is that my line of business requires travel, and sometimes that is a lot of the time, like say almost all of last year. Inevitable comparisons to George Clooney's character in Up in the Air were made (ironically I started to read that book, then left it on a plane in a seatback pocket), requests about favours involving duty free, and of course many observations and gently probing questions about frequent flier miles (FYI I've got more than most people, but a lot less than the entrepreneur I sat next to one time, who claimed to have close to 3 million).
But I digress.
Background
The point is that, as I said, I spent quite a bit of time travelling for work last year. Apparently the story with frequent fliers miles is that it's best just to pick one airline and stick with it  and this also worked out well as most companies, including my employer, have preferred airlines and so you often don't have much of a choice in the matter.
In my case this means flying Delta.
So I happened to notice in one of my many visits to Delta's website that they have data on all of their aircraft in a certain site section. I thought this would be an interesting data set on which to do some analysis, as it has both quantitative and qualitative information and is relatively complex. What can we say about the different aircraft in Delta's fleet, coming at it with 'fresh eyes'? Which planes are similar? Which are dissimilar?
Aircraft data card from Delta.com
The data set comprises 33 variables on 44 aircraft taken from Delta.com, including both quantitative measures on attributes like cruising speed, accommodation and range in miles, as well as categorical data on, say, whether a particular aircraft has WiFi or video. These binary categorical variables were transformed into quantitative variables by assigning them values of either 1 or 0, for yes or no respectively.
Analysis
As this a data set of many variables (33) I thought this would be an interesting opportunity to practice using a dimensionality reduction method to make the information easier to visualize and analyze.
First let's just look at the intermediary quantitative variables related to the aircraft physical characteristics: cruising speed, total accommodation, and other quantities like length and wingspan. These variables are about in the middle of the data frame, so we can visualize all of them at once using a scatterplot matrix, which is the default for R's output if plot() is called on a dataframe.
data < read.csv(file="delta.csv", header=T, sep=",", row.names=1)
# scatterplot matrix of intermediary (size/noncategorical) variables
plot(data[,16:22])
We can see that there are pretty strong positive correlations between all these variables, as all of them are related to the aircraft's overall size. Remarkably there is an almost perfectly linear relationship between wingspan and tail height, which perhaps is related to some principle of aeronautical engineering of which I am unaware.
The exception here is the variable right in the middle which is the number of engines. There is one lone outlier [Boeing 747400 (74S)] which has four, while all the other aircraft have two. In this way the engines variable is really more like a categorical variable, but we shall as the analysis progresses that this is not really important, as there are other variables which more strongly discern the aircraft from one another than this.
How do we easier visualize a highdimensional data set like this one? By using a dimensionality reduction technique like principal components analysis.
Principal Components Analysis
Next let's say I know nothing about dimensionality reduction techniques and just naively apply principle components to the data in R:
# Naively apply principal components analysis to raw data and plot
pc < princomp(data)
plot(pc)
Taking that approach we can see that the first principal component has a standard deviation of around 2200 and accounts for over 99.8% of the variance in the data. Looking at the first column of loadings, we see that the first principle component is just the range in miles.
# First component dominates greatly. What are the loadings?
summary(pc) # 1 component has > 99% variance
loadings(pc) # Can see all variance is in the range in miles
Importance of components:
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Comp.1 Â Â Â Comp.2 Â Â Â Comp.3 Â Â Â Comp.4
Standard deviation Â Â 2259.2372556 6.907940e+01 2.871764e+01 2.259929e+01
Proportion of Variance Â Â 0.9987016 9.337038e04 1.613651e04 9.993131e05
Cumulative Proportion Â Â 0.9987016 9.996353e01 9.997966e01 9.998966e01
Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â Â Â Â Â Â Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8
Seat.Width..Club. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.144 0.110 Â Â Â Â Â Â Â
Seat.Pitch..Club. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.327 0.248 Â Â Â Â 0.189
Seat..Club. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Seat.Width..First.Class. Â Â Â Â Â Â Â Â 0.250 Â Â Â Â 0.160 Â Â Â Â 0.156 Â 0.136
Seat.Pitch..First.Class. Â Â Â Â Â Â Â Â 0.515 0.110 0.386 Â 0.112 0.130 Â 0.183
Seats..First.Class. Â Â Â Â Â Â Â Â Â Â 0.258 0.124 0.307 0.109 Â 0.160 Â 0.149
Seat.Width..Business. Â Â Â Â Â Â Â Â Â 0.154 Â 0.142 0.108 Â Â Â Â Â Â Â Â Â Â Â
Seat.Pitch..Business. Â Â Â Â Â Â Â Â Â 0.514 Â 0.446 0.298 Â 0.154 0.172 Â 0.379
Seats..Business. Â Â Â Â Â Â Â Â Â Â Â 0.225 Â 0.187 Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Seat.Width..Eco.Comfort. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.285 0.224 Â Â Â Â
Seat.Pitch..Eco.Comfort. Â Â Â Â Â Â Â Â 0.159 Â Â Â Â Â Â Â Â 0.544 0.442 Â Â Â Â
Seats..Eco.Comfort. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.200 0.160 Â Â Â Â
Seat.Width..Economy. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.125 Â 0.110 Â Â Â Â Â Â Â
Seat.Pitch..Economy. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.227 Â 0.190 Â Â Â Â 0.130
Seats..Economy. Â Â Â Â Â Â Â Â Â 0.597 Â Â Â Â 0.136 Â 0.345 0.165 Â Â Â Â 0.168
Accommodation Â Â Â Â Â Â Â Â Â Â 0.697 Â Â Â Â Â Â Â 0.104 Â Â Â Â Â Â Â Â 0.233
Cruising.Speed..mph. Â Â Â Â Â Â Â Â Â Â 0.463 Â 0.809 Â 0.289 0.144 Â 0.115 Â Â Â Â
Range..miles. Â Â Â Â Â Â 0.999 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Engines Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Wingspan..ft. Â Â Â Â Â Â Â Â Â Â 0.215 Â Â Â Â 0.103 0.316 0.357 0.466 0.665
Tail.Height..ft. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0.100 Â Â Â Â 0.187 Â Â Â Â
Length..ft. Â Â Â Â Â Â Â Â Â Â Â 0.275 Â Â Â Â 0.118 0.318 Â 0.467 Â 0.582 0.418
Wifi Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Video Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Power Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Satellite Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Flat.bed Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Sleeper Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Club Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
First.Class Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Business Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Eco.Comfort Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
Economy Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
This is because the scale of the different variables in the data set is quite variable; we can see this by plotting the variance of the different columns in the data frame (regular scaling on the left, logarithmic on the right):
# verify by plotting variance of columns
mar < par()$mar
par(mar=mar+c(0,5,0,0))
barplot(sapply(data, var), horiz=T, las=1, cex.names=0.8)
barplot(sapply(data, var), horiz=T, las=1, cex.names=0.8, log='x')
par(mar=mar)
We correct for this by scaling the data using the scale() function. We can then verify that the variances across the different variables are equal so that when we apply principal components one variable does not dominate.
# Scale
data2 < data.frame(scale(data))
# Verify variance is uniform
plot(sapply(data2, var))

After applying the scale() function the variance is now constant across variables 
Now we can apply principal components to the scaled data. Note that this can also be done automatically in call to the prcomp() function by setting the parameter scale=TRUE. Now we see a result which is more along the lines of something we would expect:
# Proceed with principal components
pc < princomp(data2)
plot(pc)
plot(pc, type='l')
summary(pc) # 4 components is both 'elbow' and explains >85% variance
Great, so now we're in business. There are various rules of thumb for selecting the number of principal components to retain in an analysis of this type, two of which I've read about are:
 Pick the number of components which explain 85% or greater of the variation
 Use the 'elbow' method of the scree plot (on right)
Here we are fortunate in that these two are the same, so we will retain the first four principal components. We put these into new data frame and plot.
# Get principal component vectors using prcomp instead of princomp
pc < prcomp(data2)
# First for principal components
comp < data.frame(pc$x[,1:4])
# Plot
plot(comp, pch=16, col=rgb(0,0,0,0.5))
So what were are looking at here are twelve 2D projections of data which are in a 4D space. You can see there's a clear outlier in all the dimensions, as well as some bunching together in the different projections.
Normally, I am a staunch opponent of 3D visualization, as I've spoken strongly
about previously. The one exception to this rule is when the visualization is interactive, which allows the user to explore the space and not lose meaning due to three dimensions being collapsed into a 2D image. Plus, in this particular case, it's a good excuse to use the very cool, very awesome
rgl package.
Click on the images to view the interactive 3D versions (requires a modern browser). You can better see in the 3D projections that the data are confined mainly to the one plane one the left (components 13), with the exception of the outlier, and that there is also bunching in the other dimensions (components 1,3,4 on right).
library(rgl)
# Multi 3D plot
plot3d(comp$PC1, comp$PC2, comp$PC3)
plot3d(comp$PC1, comp$PC3, comp$PC4)
So, now that we've simplified the complex data set into a lower dimensional space we can visualize and work with, how do we find patterns in the data, in our case, the aircraft which are most similar? We can use a simple unsupervised machine learning technique
like clustering.
Cluster Analysis
Â
Here because I'm not a data scientist extraordinaire, I'll stick to the simplest technique and do a simple
kmeansÂ  this is pretty straightforward to do in R.
First how do we determine the number of clusters? The simplest method is to look at the within groups sum of squares and pick the 'elbow' in the plot, similar to as with the scree plot we did for the PCA previously. Here I used the code from
R in Action:
# Determine number of clusters
wss < (nrow(mydata)1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] < sum(kmeans(mydata,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
However, it should be noted that it is very important to set the nstart parameter and iter.max parameter (I've found 25 and 1000, respectively to be okay values to use), which the example in QuickR fails to do, otherwise you can get very different results each time you run the algorithm, as below.


Clustering without the nstart parameter can lead to variable results for each run 


Clustering with the nstart and iter.max parameters leads to consistent results, allowing proper interpretation of the scree plot 
So here we can see that the "elbow" in the scree plot is at k=4, so we apply the kmeans clustering function with k = 4 and plot.
# From scree plot elbow occurs at k = 4
# Apply kmeans with k=4
k < kmeans(comp, 4, nstart=25, iter.max=1000)
library(RColorBrewer)
library(scales)
palette(alpha(brewer.pal(9,'Set1'), 0.5))
plot(comp, col=k$clust, pch=16)
We can see that the one outlier is in its own cluster, there's 3 or 4 in the other and the remainder are split into two clusters of greater size. We visualize in 3D below, as before (click for interactive versions):
# 3D plot
plot3d(comp$PC1, comp$PC2, comp$PC3, col=k$clust)
plot3d(comp$PC1, comp$PC3, comp$PC4, col=k$clust)
We look at the exact clusters below, in order of increasing size:
# Cluster sizes
sort(table(k$clust))
clust < names(sort(table(k$clust)))
# First cluster
row.names(data[k$clust==clust[1],])
# Second Cluster
row.names(data[k$clust==clust[2],])
# Third Cluster
row.names(data[k$clust==clust[3],])
# Fourth Cluster
row.names(data[k$clust==clust[4],])
[1] "Airbus A319 VIP"
[1] "CRJ 100/200 Pinnacle/SkyWest" "CRJ 100/200 ExpressJet"
[3] "E120" Â Â Â Â Â Â Â Â Â Â Â Â "ERJ145"
[1] "Airbus A330200" Â Â Â Â Â "Airbus A330200 (3L2)"
[3] "Airbus A330200 (3L3)" Â Â "Airbus A330300"
[5] "Boeing 747400 (74S)" Â Â "Boeing 757200 (75E)"
[7] "Boeing 757200 (75X)" Â Â "Boeing 767300 (76G)"
[9] "Boeing 767300 (76L)" Â Â "Boeing 767300 (76T)"
[11] "Boeing 767300 (76Z V.1)" "Boeing 767300 (76Z V.2)"
[13] "Boeing 767400 (76D)" Â Â "Boeing 777200ER"
[15] "Boeing 777200LR"
[1] "Airbus A319" Â Â Â Â Â Â "Airbus A320" Â Â Â Â Â Â "Airbus A320 32R"
[4] "Boeing 717" Â Â Â Â Â Â "Boeing 737700 (73W)" Â "Boeing 737800 (738)"
[7] "Boeing 737800 (73H)" Â "Boeing 737900ER (739)" "Boeing 757200 (75A)"
[10] "Boeing 757200 (75M)" Â "Boeing 757200 (75N)" Â "Boeing 757200 (757)"
[13] "Boeing 757200 (75V)" Â "Boeing 757300" Â Â Â Â "Boeing 767300 (76P)"
[16] "Boeing 767300 (76Q)" Â "Boeing 767300 (76U)" Â "CRJ 700"
[19] "CRJ 900" Â Â Â Â Â Â Â Â "E170" Â Â Â Â Â Â Â Â Â "E175"
[22] "MD88" Â Â Â Â Â Â Â Â Â "MD90" Â Â Â Â Â Â Â Â Â "MDDC950"
The first cluster contains a single aircraft, the Airbus A319 VIP. This plane is on its own and rightly so  it is not part of Delta's regular fleet but one of Airbus'
corporate jets. This is a plane for people with money, for private charter. It includes "club seats" around tables for working (or not). Below is a picture of the inside of the A319 VIP:
Ahhh, that's the way fly (some day, some day...). This is apparently the plane professional sports teams and the American military often charter to fly  this articleÂ in the Sydney Morning Herald has more details.
The second cluster contains four aircraft  the two CRJ 100/200's and the Embraer E120 and ERJ145. These are the smallest passenger aircraft, with the smallest accommodations  28 for the E120 and 50 for the remaining craft. As such, there is only economy seating in these planes which is what distinguishes them from the remainder of the fleet. The E120 also has the distinction of being the only plane in the fleet with turboprops. Photos below.

Top: CRJ100/200. Bottom left: Embraer E120. Bottom right: Embraer ERJ145. 
I've flown many times in the venerable CRJ 100/200 series planes, in which I can assure you there is only economy seating, and which I like to affectionately refer to as "little metal tubes of suffering."
The other two clusters comprise the remainder of the fleet, the planes with which most commercial air travellers are familiar  your Boeing 7whatever7's and other Airbus and McDonnellDouglas planes.
These are split into two clusters, which seem to again divide the planes approximately by size (both physical and accommodation), though there is crossover in the Boeing craft.
# Compare accommodation by cluster in boxplot
boxplot(data$Accommodation ~ k$cluster,
xlab='Cluster', ylab='Accommodation',
main='Plane Accommodation by Cluster')
# Compare presence of seat classes in largest clusters
data[k$clust==clust[3],30:33]
data[k$clust==clust[4],30:33]

First.Class 
Business 
Eco.Comfort 
Economy 
Airbus A330200 
0 
1 
1 
1 
Airbus A330200 (3L2) 
0 
1 
1 
1 
Airbus A330200 (3L3) 
0 
1 
1 
1 
Airbus A330300 
0 
1 
1 
1 
Boeing 747400 (74S) 
0 
1 
1 
1 
Boeing 757200 (75E) 
0 
1 
1 
1 
Boeing 757200 (75X) 
0 
1 
1 
1 
Boeing 767300 (76G) 
0 
1 
1 
1 
Boeing 767300 (76L) 
0 
1 
1 
1 
Boeing 767300 (76T) 
0 
1 
1 
1 
Boeing 767300 (76Z V.1) 
0 
1 
1 
1 
Boeing 767300 (76Z V.2) 
0 
1 
1 
1 
Boeing 767400 (76D) 
0 
1 
1 
1 
Boeing 777200ER 
0 
1 
1 
1 
Boeing 777200LR 
0 
1 
1 
1 






First.Class 
Business 
Eco.Comfort 
Economy 
Airbus A319 
1 
0 
1 
1 
Airbus A320 
1 
0 
1 
1 
Airbus A320 32R 
1 
0 
1 
1 
Boeing 717 
1 
0 
1 
1 
Boeing 737700 (73W) 
1 
0 
1 
1 
Boeing 737800 (738) 
1 
0 
1 
1 
Boeing 737800 (73H) 
1 
0 
1 
1 
Boeing 737900ER (739) 
1 
0 
1 
1 
Boeing 757200 (75A) 
1 
0 
1 
1 
Boeing 757200 (75M) 
1 
0 
1 
1 
Boeing 757200 (75N) 
1 
0 
1 
1 
Boeing 757200 (757) 
1 
0 
1 
1 
Boeing 757200 (75V) 
1 
0 
1 
1 
Boeing 757300 
1 
0 
1 
1 
Boeing 767300 (76P) 
1 
0 
1 
1 
Boeing 767300 (76Q) 
1 
0 
1 
1 
Boeing 767300 (76U) 
0 
1 
1 
1 
CRJ 700 
1 
0 
1 
1 
CRJ 900 
1 
0 
1 
1 
E170 
1 
0 
1 
1 
E175 
1 
0 
1 
1 
MD88 
1 
0 
1 
1 
MD90 
1 
0 
1 
1 
MDDC950 
1 
0 
1 
1 
Looking at the raw data, the difference I can ascertain between the largest two clusters is that all the aircraft in the one have first class seating, whereas all the planes in the other have business class instead [the one exception being the Boeing 767300 (76U)].
Conclusions
This was a little analysis which for me not only allowed me to explore my interest in commercial aircraft, but was also educational about finer points of what to look out for when using more advanced data science techniques like principal components, clustering and advanced visualization.
All in all, the techniques did a pretty admirable job in separating out the different type of aircraft into distinct categories. However I believe the way I structured the data may have biased it towards categorizing the aircraft by seating class, as that quality was replicated in the data set compared to other variables, being represented both in quantitative variables (seat pitch & width, number of seat in class) and categorical (class presence). So really the different seating classes where represented in triplicate within the data set compared to other variables, which is why the methods separated the aircraft in this way.
If I did this again, I would structure the data differently and see what relationships such analysis could draw out using only select parts of the data (e.g. aircraft measurements only). The interesting lesson here is that it when using techniques like dimensionality reduction and clustering it is not only important to be mindful of applying them correctly, but also what variables are in your data set and how they are represented.
For now I'll just keep on flying, collecting the miles, and counting down the days until I finally get that seat in first class.
References & Resources
Delta Fleet at Delta.com
The Little Book of R for Multivariate Analysis
Quick R: Cluster Analysis
Plane Luxury: how US sports stars fly (Syndney Morning Herald)