Setting up datasets

The original name of the dataset is “Consumer Airfare Report: Table 2 - Top 1,000 City-Pair Markets”. It’s renamed to “Final Proj_Airfare Data”.

# select city, cur_passengers, cur_fare, cur_yield, distance for airfare1 dataset
airfare1 <-read.csv("Final Proj_Airfare Data.csv") %>% select(city, cur_passengers, cur_fare, cur_yield, distance)

# remove " (Metropolitan Area)"
airfare1$city <- gsub("\\s*\\(Metropolitan Area\\)$", "", airfare1$city)

# create variable named `state`
airfare1 <- airfare1 %>%
  mutate(state = substr(city, nchar(city) - 1, nchar(city)))
# diagnostics
airfare_test <- airfare1[, !colnames(airfare1) %in% c('city', 'state')]
pairs(airfare_test)

ggpairs(airfare_test)

# average out the values for each state
# Remove 'city' column 
city_column <- airfare1$city
airfare1 <- airfare1[, -which(names(airfare1) == "city")]

# Aggregate by state and calculate the mean for each column
airfare2 <- aggregate(. ~ state, data = airfare1, mean)

# Set 'state' as row names
rownames(airfare2) <- airfare2$state

head(airfare2)
##    state cur_passengers cur_fare cur_yield  distance
## AL    AL       297179.2 188.4482  23.37630  807.3074
## AR    AR       195453.6 175.8077  23.15202  751.8626
## AZ    AZ      2384860.8 176.4859  16.61170 1066.5266
## CA    CA      4999825.8 185.4397  16.48408 1147.8214
## CO    CO      2961562.6 183.0141  18.19105 1011.1066
## CT    CT       875442.8 197.9015  16.14381 1226.4168

The current number of passengers have a weak, but statistically significant, correlation with the current fares. Therefore, we are using PCA to reduce the multicollinearity as we cluster the states based on their similarity in passenger counts and airfares.

PCA

model.pca <- prcomp(airfare2[, c("cur_passengers","distance")], scale = TRUE)
model.pca
## Standard deviations (1, .., p=2):
## [1] 1.1999793 0.7483646
## 
## Rotation (n x k) = (2 x 2):
##                      PC1        PC2
## cur_passengers 0.7071068 -0.7071068
## distance       0.7071068  0.7071068
model.pca$rotation
##                      PC1        PC2
## cur_passengers 0.7071068 -0.7071068
## distance       0.7071068  0.7071068
summary(model.pca)
## Importance of components:
##                         PC1    PC2
## Standard deviation     1.20 0.7484
## Proportion of Variance 0.72 0.2800
## Cumulative Proportion  0.72 1.0000

Intepretation: * PC1 captures the majority of the variability in the data (72%). * PC2 captures an additional portion of the variability (28%). * The rotation matrix indicates the direction and strength of the relationships between the original variables and the principal components.

airfare2$PC1 <- model.pca$x[,1]
airfare2$PC2 <- model.pca$x[,2]
airfare2 %>% select(PC1, PC2) %>% ggpairs()

mod <- lm(cur_yield ~ PC1 + PC2, data = airfare2)
summary(mod)
## 
## Call:
## lm(formula = cur_yield ~ PC1 + PC2, data = airfare2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.6921 -1.0496  0.0667  1.1269  4.8205 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  19.9695     0.3657  54.603  < 2e-16 ***
## PC1          -1.8479     0.3086  -5.989  5.9e-07 ***
## PC2          -1.7341     0.4948  -3.505  0.00119 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.342 on 38 degrees of freedom
## Multiple R-squared:  0.5589, Adjusted R-squared:  0.5357 
## F-statistic: 24.07 on 2 and 38 DF,  p-value: 1.763e-07
autoplot(mod)

K-Means Clustering

State dissimilarity

# visualize the dissimilarity 
fviz_dist(dist(airfare2[,2:5]))

fviz_dist(dist(scale(airfare2[,2:5])))

K-clusters

# K-cluster plot using original variables
kcluster1 <- fviz_nbclust(scale(airfare2[,2,5]), kmeans, method = "wss")

kcluster1 + ggtitle("Graph 3. Optimal number of clusters, using original variables")

# wss: total within cluster sum of square

K-means Clustering Model & Visualizations

# Using original data
km.out <- kmeans(scale(airfare2[,2,5]), 3, nstart = 20)
airfare2$C1 <- as.factor(km.out$cluster)
ggplot(airfare2,aes(x = cur_passengers, y = distance, color = C1, label = rownames(airfare2))) + geom_text() + ggtitle("Graph 5. KMean Clusering Results,using original variables") +  labs(x = "Current Passenger Count", y = "Distance")

# use the 2 PC
# Kmean
kcluster2 <- fviz_nbclust(airfare2[,c("PC1","PC2")], kmeans, method = "wss") 
kcluster2 + ggtitle("Graph 4. Optimal number of clusters, using PCA variables")

# wss: total within 
km2.out <- kmeans(airfare2[,c("PC1","PC2")], 3, nstart = 20) 
airfare2$C2 <- as.factor(km2.out$cluster)
ggplot(airfare2,aes(x = PC1, y = PC2, color = C2, label = rownames(airfare2))) + geom_text() + ggtitle("Graph 5. K-Means Clustering Results on PC")

# Using the eclust function from `factoextra` package to directly cluster on the PC's
eclust(model.pca$x, "kmeans", hc_metric="eucliden",k=3)

## K-means clustering with 3 clusters of sizes 22, 4, 15
## 
## Cluster means:
##          PC1        PC2
## 1  0.3346096  0.4512535
## 2  2.4963758 -1.0834239
## 3 -1.1564609 -0.3729254
## 
## Clustering vector:
## AL AR AZ CA CO CT DC FL GA IA ID IL IN KS KY LA MA ME MI MN MO MS MT NC NE NJ 
##  3  3  1  2  1  1  2  1  3  1  3  2  1  3  3  1  2  3  1  1  1  3  1  3  1  3 
## NM NV NY OH OK OR PA SC TN TX UT VA VT WA WI 
##  1  1  1  1  3  1  1  3  3  3  1  1  3  1  1 
## 
## Within cluster sum of squares by cluster:
## [1] 9.778615 3.368734 8.139671
##  (between_SS / total_SS =  73.4 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
##  [6] "betweenss"    "size"         "iter"         "ifault"       "clust_plot"  
## [11] "silinfo"      "nbclust"      "data"
airfare2
##    state cur_passengers cur_fare cur_yield  distance          PC1          PC2
## AL    AL       297179.2 188.4482  23.37630  807.3074 -1.141088722 -0.098813947
## AR    AR       195453.6 175.8077  23.15202  751.8626 -1.434703970 -0.317508130
## AZ    AZ      2384860.8 176.4859  16.61170 1066.5266  0.825294093  0.329987009
## CA    CA      4999825.8 185.4397  16.48408 1147.8214  2.163839244 -0.257395002
## CO    CO      2961562.6 183.0141  18.19105 1011.1066  0.781625024 -0.138424151
## CT    CT       875442.8 197.9015  16.14381 1226.4168  1.008141626  1.624524006
## DC    DC      7590377.7 196.4963  18.58018 1057.9364  2.702544243 -1.626636922
## FL    FL      2344993.8 168.4034  16.08571 1057.3690  0.768304739  0.302359746
## GA    GA      3228599.8 181.7095  22.58870  805.4378 -0.070227430 -1.186950050
## IA    IA       168407.5 200.5146  18.96762 1063.6614 -0.004154432  1.132960925
## ID    ID       353537.2 140.3638  24.08908  582.3523 -2.159626723 -1.158859792
## IL    IL      8051325.9 183.5323  19.19318  957.5136  2.408336205 -2.260334257
## IN    IN      1186079.4 182.9065  17.90073 1022.2427  0.179250209  0.566848135
## KS    KS       107100.0 198.2900  24.38000  813.2000 -1.183861814 -0.001593337
## KY    KY       410821.7 186.9220  21.01853  887.3431 -0.729475020  0.229101828
## LA    LA      1488789.7 174.7905  18.08800  963.9945  0.021617197  0.186268311
## MA    MA      5650032.8 199.6418  16.46545 1214.3809  2.710783520 -0.189329576
## ME    ME       139262.6 182.2334  30.05038  627.6000 -2.029489308 -0.870908683
## MI    MI      1496632.1 192.9338  18.08423 1072.7207  0.526819461  0.685694641
## MN    MN      2819661.5 213.5445  20.33791 1053.7309  0.926293851  0.110755096
## MO    MO      1666889.5 180.6710  19.76005  914.0227 -0.143667036 -0.110186744
## MS    MS       114894.1 159.7262  23.83844  678.8563 -1.801659347 -0.625131214
## MT    MT       166825.6 236.6333  23.13889 1041.8333 -0.105582530  1.032697951
## NC    NC      1146010.1 200.7526  25.06214  826.2269 -0.741097877 -0.323988831
## NE    NE       524784.6 179.8118  18.01982  997.5147 -0.178515788  0.696127120
## NJ    NJ       153351.7 108.4689  12.38073  878.7659 -0.863915535  0.284288463
## NM    NM       733967.6 174.4263  18.95422  919.6798 -0.461080742  0.259498431
## NV    NV      3091460.1 153.7638  16.78854  968.1027  0.630782074 -0.384936876
## NY    NY      2790912.7 183.8590  19.30672  982.2380  0.585410117 -0.208955064
## OH    OH       855611.8 198.7568  21.29463  942.9369 -0.308837350  0.322150627
## OK    OK       345431.7 184.0074  21.93921  840.7579 -0.968778637  0.037958037
## OR    OR      1856334.3 176.6047  15.39690 1154.0155  1.034862169  0.928815931
## PA    PA      1993986.6 202.4990  19.49605 1052.9564  0.618659791  0.411232361
## SC    SC       226880.7 173.8906  25.31383  689.3577 -1.711903697 -0.617853965
## TN    TN       876611.8 195.8280  23.10154  854.4727 -0.709808428 -0.094286980
## TX    TX      2017247.6 175.7759  21.94839  843.2959 -0.341405209 -0.565964401
## UT    UT      1778850.7 183.5631  18.01682 1014.2936  0.360814106  0.311834688
## VA    VA       387981.5 203.9741  22.28693  953.4037 -0.432686404  0.542712286
## VT    VT       114930.0 140.9050  18.86000  752.8333 -1.459872219 -0.283370554
## WA    WA      1889195.3 159.3519  16.64040 1016.1781  0.410155016  0.279906558
## WI    WI       724162.3 189.0507  17.41493 1089.0732  0.317905535  1.045706324
##    C1 C2
## AL  2  2
## AR  2  2
## AZ  3  3
## CA  1  1
## CO  3  3
## CT  2  3
## DC  1  1
## FL  3  3
## GA  3  2
## IA  2  3
## ID  2  2
## IL  1  1
## IN  2  3
## KS  2  2
## KY  2  2
## LA  3  3
## MA  1  1
## ME  2  2
## MI  3  3
## MN  3  3
## MO  3  3
## MS  2  2
## MT  2  3
## NC  2  2
## NE  2  3
## NJ  2  2
## NM  2  3
## NV  3  3
## NY  3  3
## OH  2  3
## OK  2  2
## OR  3  3
## PA  3  3
## SC  2  2
## TN  2  2
## TX  3  2
## UT  3  3
## VA  2  3
## VT  2  2
## WA  3  3
## WI  2  3