Multivariate Analysis - Homework 4 - Multivariate statistical method - Clustering

Patricija Varsek

Source: Data retrived on 27.1.2024, from: https://www.kaggle.com/datasets/teejmahal20/airline-passenger-satisfaction?select=test.csv

Can we group travelers on a plane based on their satisfaction with the flight?

library(readxl)
mydata <- read_excel("~/6_IMB/7_Class/7_MultivariateAnalysis/R_HW/flights.xlsx")
## New names:
## • `` -> `...1`
mydata <- as.data.frame(mydata)
head(mydata)
##    ...1    id Gender     Customer Type Age  Type of Travel    Class
## 1  2593 30133   Male    Loyal Customer  70 Personal Travel      Eco
## 2  3911 29816 Female    Loyal Customer  38 Business travel      Eco
## 3  8263 30212 Female    Loyal Customer  27 Personal Travel Eco Plus
## 4  6444 48682 Female disloyal Customer  22 Business travel      Eco
## 5 17060 50001 Female    Loyal Customer  45 Business travel Business
## 6 21765 50687   Male    Loyal Customer  11 Personal Travel      Eco
##   Flight Distance Inflight wifi service
## 1              31                     2
## 2              31                     4
## 3              31                     3
## 4              56                     1
## 5              56                     1
## 6              56                     3
##   Departure/Arrival time convenient Ease of Online booking
## 1                                 5                      0
## 2                                 2                      2
## 3                                 5                      3
## 4                                 5                      1
## 5                                 2                      2
## 6                                 2                      3
##   Gate location Food and drink Online boarding Seat comfort
## 1             4              4               0            4
## 2             2              4               4            4
## 3             5              4               3            1
## 4             3              5               1            5
## 5             2              5               3            4
## 6             4              2               3            2
##   Inflight entertainment On-board service Leg room service
## 1                      4                3                5
## 2                      4                4                4
## 3                      4                5                5
## 4                      5                3                5
## 5                      4                4                4
## 6                      2                2                3
##   Baggage handling Checkin service Inflight service Cleanliness
## 1                5               3                4           4
## 2                3               5                2           4
## 3                4               3                5           4
## 4                5               4                4           5
## 5                1               4                4           4
## 6                3               3                3           2
##   Departure Delay in Minutes Arrival Delay in Minutes
## 1                          0                      0.0
## 2                          0                      0.0
## 3                          8                     15.0
## 4                          0                     16.0
## 5                          0                      0.0
## 6                         73                     76.0
##              satisfaction
## 1 neutral or dissatisfied
## 2               satisfied
## 3 neutral or dissatisfied
## 4 neutral or dissatisfied
## 5 neutral or dissatisfied
## 6 neutral or dissatisfied
mydata <- mydata[apply(mydata != 0, 1, all), ] #I deleted answers with 0 (not applicable). 
set.seed(1) #Setting initial point of sampling.
mydata <- mydata[sample(nrow(mydata), 500), ] #Random sample of 500 units
any_missing <- any(is.na(mydata)) #checking if there is any missing data
library(tidyr)
mydata <- drop_na(mydata) #Because we got TRUE (there is missing data), I dropped it here
any_missing <- any(is.na(mydata)) #checking again if there is any missing data
mydata$ID <- seq(1,498) #creating ID
head(mydata)
##        ...1     id Gender     Customer Type Age  Type of Travel
## 3387  21356  39904 Female disloyal Customer  27 Business travel
## 20069 18939  10414   Male    Loyal Customer  45 Business travel
## 12531  9923 101014   Male disloyal Customer  23 Business travel
## 25689 15089  23784   Male    Loyal Customer  42 Business travel
## 24088 10457  22234 Female    Loyal Customer  27 Business travel
## 21097  8437 110055 Female    Loyal Customer  66 Personal Travel
##          Class Flight Distance Inflight wifi service
## 3387  Eco Plus             272                     2
## 20069 Business            1883                     2
## 12531      Eco             806                     1
## 25689 Business            3874                     3
## 24088 Business            3076                     4
## 21097      Eco            2173                     1
##       Departure/Arrival time convenient Ease of Online booking
## 3387                                  2                      2
## 20069                                 2                      2
## 12531                                 2                      2
## 25689                                 3                      3
## 24088                                 4                      4
## 21097                                 2                      1
##       Gate location Food and drink Online boarding Seat comfort
## 3387              3              2               2            2
## 20069             2              2               4            5
## 12531             2              2               2            2
## 25689             3              4               5            2
## 24088             4              5               5            5
## 21097             3              4               3            2
##       Inflight entertainment On-board service Leg room service
## 3387                       2                4                3
## 20069                      4                4                4
## 12531                      2                2                1
## 25689                      5                5                5
## 24088                      5                5                2
## 21097                      4                4                1
##       Baggage handling Checkin service Inflight service Cleanliness
## 3387                 3               1                3           2
## 20069                4               3                4           5
## 12531                2               4                3           2
## 25689                5               1                5           3
## 24088                5               3                5           5
## 21097                4               3                4           4
##       Departure Delay in Minutes Arrival Delay in Minutes
## 3387                           9                     10.0
## 20069                         66                     58.0
## 12531                          7                      0.0
## 25689                         16                     12.0
## 24088                         68                     67.0
## 21097                         36                     19.0
##                  satisfaction ID
## 3387  neutral or dissatisfied  1
## 20069               satisfied  2
## 12531 neutral or dissatisfied  3
## 25689               satisfied  4
## 24088               satisfied  5
## 21097 neutral or dissatisfied  6

I choosed variables we are interested in for our research:

mydata2 <- mydata[,c(26,8,9,10,13,14,18,19,3,5,6)]
head(mydata2)
##       ID Flight Distance Inflight wifi service
## 3387   1             272                     2
## 20069  2            1883                     2
## 12531  3             806                     1
## 25689  4            3874                     3
## 24088  5            3076                     4
## 21097  6            2173                     1
##       Departure/Arrival time convenient Food and drink
## 3387                                  2              2
## 20069                                 2              2
## 12531                                 2              2
## 25689                                 3              4
## 24088                                 4              5
## 21097                                 2              4
##       Online boarding Leg room service Baggage handling Gender Age
## 3387                2                3                3 Female  27
## 20069               4                4                4   Male  45
## 12531               2                1                2   Male  23
## 25689               5                5                5   Male  42
## 24088               5                2                5 Female  27
## 21097               3                1                4 Female  66
##        Type of Travel
## 3387  Business travel
## 20069 Business travel
## 12531 Business travel
## 25689 Business travel
## 24088 Business travel
## 21097 Personal Travel

and facotrized catagorical variables:

mydata2$GenderF <- factor(mydata2$Gender, 
                         levels = c("Male", "Female"), 
                         labels = c("Male", "Female"))

colnames(mydata2)[colnames(mydata2) == "Type of Travel"] <- "Type"

mydata2$TypeF <- factor(mydata2$Type, 
                         levels = c("Business travel", "Personal Travel"), 
                         labels = c("Business travel", "Personal Travel"))

colnames(mydata2)[colnames(mydata2) == "Flight Distance"] <- "Distance"
colnames(mydata2)[colnames(mydata2) == "Inflight wifi service"] <- "Wifi"
colnames(mydata2)[colnames(mydata2) == "Departure/Arrival time convenient"] <- "Time_convenient"
colnames(mydata2)[colnames(mydata2) == "Leg room service"] <- "Room"
colnames(mydata2)[colnames(mydata2) == "Food and drink"] <- "FoodDrink"
colnames(mydata2)[colnames(mydata2) == "Online boarding"] <- "Boarding"
colnames(mydata2)[colnames(mydata2) == "Baggage handling"] <- "Baggage"
head(mydata2)
##       ID Distance Wifi Time_convenient FoodDrink Boarding Room
## 3387   1      272    2               2         2        2    3
## 20069  2     1883    2               2         2        4    4
## 12531  3      806    1               2         2        2    1
## 25689  4     3874    3               3         4        5    5
## 24088  5     3076    4               4         5        5    2
## 21097  6     2173    1               2         4        3    1
##       Baggage Gender Age            Type GenderF           TypeF
## 3387        3 Female  27 Business travel  Female Business travel
## 20069       4   Male  45 Business travel    Male Business travel
## 12531       2   Male  23 Business travel    Male Business travel
## 25689       5   Male  42 Business travel    Male Business travel
## 24088       5 Female  27 Business travel  Female Business travel
## 21097       4 Female  66 Personal Travel  Female Personal Travel

Gender: Gender of the passengers (Female, Male)

Age: The actual age of the passengers

Type of Travel: Purpose of the flight of the passengers (Personal Travel, Business Travel)

Flight distance: The flight distance of this journey

Inflight wifi service: Satisfaction level of the inflight wifi service (0:Not Applicable;1: not satisfied -5: very satisfied)

Departure/Arrival time convenient: Satisfaction level of Departure/Arrival time convenient (1-5)

Ease of Online booking: Satisfaction level of online booking (1-5)

Food and drink: Satisfaction level of Food and drink (1-5)

Online boarding: Satisfaction level of online boarding (1-5)

Baggage handling: Satisfaction level of baggage handling (1-5)

Standardization process to make sure every variable contributes to final result equally.

mydata2$Wifi_z <- scale(mydata2$Wifi)
mydata2$Time_z <- scale(mydata2$Time_convenient)
mydata2$Room_z <- scale(mydata2$Room)
mydata2$FoodDrink_z <- scale(mydata2$FoodDrink)
mydata2$Boarding_z <- scale(mydata2$Boarding)
mydata2$Baggage_z <- scale(mydata2$Baggage)
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(mydata2[,c("Wifi_z","Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")]), 
      type = "pearson")
##             Wifi_z Time_z Room_z FoodDrink_z Boarding_z Baggage_z
## Wifi_z        1.00   0.47   0.16        0.21       0.45      0.04
## Time_z        0.47   1.00   0.02        0.05       0.05      0.11
## Room_z        0.16   0.02   1.00        0.01       0.13      0.38
## FoodDrink_z   0.21   0.05   0.01        1.00       0.28      0.07
## Boarding_z    0.45   0.05   0.13        0.28       1.00      0.08
## Baggage_z     0.04   0.11   0.38        0.07       0.08      1.00
## 
## n= 498 
## 
## 
## P
##             Wifi_z Time_z Room_z FoodDrink_z Boarding_z Baggage_z
## Wifi_z             0.0000 0.0004 0.0000      0.0000     0.4108   
## Time_z      0.0000        0.6686 0.2948      0.2707     0.0142   
## Room_z      0.0004 0.6686        0.7669      0.0043     0.0000   
## FoodDrink_z 0.0000 0.2948 0.7669             0.0000     0.1051   
## Boarding_z  0.0000 0.2707 0.0043 0.0000                 0.0814   
## Baggage_z   0.4108 0.0142 0.0000 0.1051      0.0814

We can see that correlation is above 0.3 in cases between variables Wifi_z and Time_z (0.47), Boarding_z and Wifi_z (0.45), Baggage_z and Room_Z (0.38). It would be better if it was bellow, however, we will continue with the process.

summary(mydata2[c("Wifi_z","Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")]) 
##       Wifi_z.V1            Time_z.V1            Room_z.V1      
##  Min.   :-1.3715022   Min.   :-1.5229168   Min.   :-1.8994354  
##  1st Qu.:-0.5735955   1st Qu.:-0.8067576   1st Qu.:-1.1170377  
##  Median : 0.2243111   Median :-0.0905984   Median : 0.4477577  
##  Mean   : 0.0000000   Mean   : 0.0000000   Mean   : 0.0000000  
##  3rd Qu.: 1.0222177   3rd Qu.: 0.6255607   3rd Qu.: 1.2301554  
##  Max.   : 1.8201244   Max.   : 1.3417199   Max.   : 1.2301554  
##     FoodDrink_z.V1       Boarding_z.V1         Baggage_z.V1    
##  Min.   :-1.7349672   Min.   :-1.7873417   Min.   :-2.2883466  
##  1st Qu.:-0.9635264   1st Qu.:-1.0099651   1st Qu.:-0.5391576  
##  Median :-0.1920857   Median :-0.2325886   Median : 0.3354368  
##  Mean   : 0.0000000   Mean   : 0.0000000   Mean   : 0.0000000  
##  3rd Qu.: 0.5793551   3rd Qu.: 0.5447880   3rd Qu.: 0.3354368  
##  Max.   : 1.3507959   Max.   : 1.3221646   Max.   : 1.2100313
#Saving standardized cluster variables into new data frame
mydata2_clu_std <- as.data.frame(scale(mydata2[c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")]))

Calculating the variable Dissimilarity to asses which units are potential outliers and could be removed

mydata2$Dissimilarity <- sqrt(mydata2_clu_std$Wifi_z^2 + mydata2_clu_std$Time_z^2 + mydata2_clu_std$Room_z^2 + mydata2_clu_std$FoodDrink_z^2 + mydata2_clu_std$Boarding_z^2 + mydata2_clu_std$Baggage_z^2 ) #Finding outliers
head(mydata2[order(-mydata2$Dissimilarity), c("ID", "Dissimilarity")]) #Finding units with highest value of dissimilarity
##        ID Dissimilarity
## 15586 479      4.069433
## 5200   83      4.015138
## 4839  160      4.001443
## 17744 404      3.894371
## 1661  184      3.831862
## 21581  17      3.795308
print(mydata2[479, ]) #Showing customer ID479
##        ID Distance Wifi Time_convenient FoodDrink Boarding Room
## 15586 479     1048    5               1         4        5    1
##       Baggage Gender Age            Type GenderF           TypeF
## 15586       1   Male  53 Business travel    Male Business travel
##         Wifi_z    Time_z    Room_z FoodDrink_z Boarding_z Baggage_z
## 15586 1.820124 -1.522917 -1.899435   0.5793551   1.322165 -2.288347
##       Dissimilarity
## 15586      4.069433
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
## 
##     src, summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mydata2 <- mydata2 %>% filter(!ID %in% c(479, 83, 160)) #Removing ID479, ID83 and ID160 from original data frame
 #Saving standardized cluster variables into new data frame
mydata2_clu_std <- as.data.frame(scale(mydata2[c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")])) 

Dissimilarity matrix

#install.packages("factoextra")
library(factoextra) 
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
#Finding Eudlidean distances, based on 6 Cluster variables, then saving them into object Distances

Distances <- get_dist(mydata2_clu_std, 
                      method = "euclidian")  #we are using Euclidian distance as a measure. 

Distances2 <- Distances^2

fviz_dist(Distances2) 

Hopkins statistics

get_clust_tendency(mydata2[c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")], 
                   n = nrow(mydata2) - 1, 
                   graph = FALSE)
## $hopkins_stat
## [1] 0.5472115
## 
## $plot
## NULL

Based on Hopkins statistics, data is clusterable/ it has cluster tendency (H > 0.5).

Hierarchical clustering - Ward’s algorithem and squared Euclidean distances

library(dplyr)
library(factoextra)

WARD <- mydata2[c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")] %>%
  get_dist(method = "euclidean") %>%  
  hclust(method = "ward.D2")          

WARD
## 
## Call:
## hclust(d = ., method = "ward.D2")
## 
## Cluster method   : ward.D2 
## Distance         : euclidean 
## Number of objects: 495

Dendrogram

library(factoextra)

fviz_dend(WARD)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none"
## instead as of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at
##   <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning
## was generated.

We will perform 3 clusters.

fviz_dend(WARD, 
          k = 3,
          cex = 0.1, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)

Initial Leaders (as basis for K-means method)

mydata2$ClassificationWARD <- cutree(WARD, 
                                 k = 3)

head(mydata2[, c("ID", "ClassificationWARD")])
##       ID ClassificationWARD
## 3387   1                  1
## 20069  2                  2
## 12531  3                  1
## 25689  4                  3
## 24088  5                  3
## 21097  6                  1
#Showing the positions of initial leaders, used as starting point for K-means clustering
InitialLeaders <- aggregate(mydata2[, c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")], 
                            by = list(mydata2$ClassificationWARD), 
                            FUN = mean)

round(InitialLeaders, 3)
##   Group.1 Wifi_z Time_z Room_z FoodDrink_z Boarding_z Baggage_z
## 1       1 -0.600 -0.214 -0.323      -0.532     -0.764    -0.250
## 2       2 -1.002 -1.138  0.593       0.194      0.919     0.643
## 3       3  0.764  0.468  0.178       0.433      0.471     0.102

K-means clustering

library(factoextra)
K_MEANS <- hkmeans(mydata2[c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z")], 
                   k = 3, 
                   hc.metric = "euclidian", 
                   hc.method = "ward.D2")

K_MEANS
## Hierarchical K-means clustering with 3 clusters of sizes 169, 145, 181
## 
## Cluster means:
##       Wifi_z     Time_z     Room_z FoodDrink_z Boarding_z  Baggage_z
## 1 -0.3989059 -0.1923015 -0.9781505  -0.2605567 -0.3659846 -0.6323097
## 2 -0.7606909 -0.5548672  0.6959667  -0.2027262 -0.3880639  0.5163874
## 3  0.9693179  0.6374308  0.3872408   0.4046089  0.6478656  0.2098045
## 
## Clustering vector:
##  3387 20069 12531 25689 24088 21097 10919 21832  4100 21206  5421 
##     1     2     1     3     3     1     2     2     3     3     1 
## 24625 20603  3665 22125  2925 21581 19709 15230 16645 13220  8117 
##     3     3     3     2     2     1     3     3     2     3     3 
## 17910 12275 23886  6344  9565  5815  7331  4746 18434 12245 16625 
##     3     2     2     3     1     3     3     3     1     1     3 
## 11598  3918 25460  7988 13935 24473  1851  3535 14209 20566 15018 
##     3     2     3     1     2     2     3     1     1     3     3 
## 15069 16207 14145 23102 18915 22738  5408 24321 12923  1689 20015 
##     3     1     1     1     3     1     3     3     2     1     1 
## 23261 10491 23111 20714  8601 10792 17466 25110 17173  4179 21732 
##     3     2     2     3     3     3     1     3     3     1     2 
## 21133 16519 17759 24055  2869 17171 22750  1272  2500 16048 12852 
##     3     3     1     2     3     3     2     1     2     1     1 
## 13185  6744 23203 16408 10796  1774 16921  1897 17472 18941 18956 
##     3     2     1     2     1     1     3     3     1     1     1 
## 10231 13435    87  5806 12912  5469  6611 23576  5849 21319 11216 
##     3     1     3     3     3     3     3     2     1     1     1 
## 14421 14777 21269 15267 17651 22950 21298 23635 23432 17511 24853 
##     2     1     3     3     3     1     2     3     2     1     1 
##  8011 22180   854 20815  1881  8663 22569 16835  6399 15258 18293 
##     3     2     3     2     1     3     1     1     2     1     3 
## 15252 11850  6328  3712  3593  3970  3909 11042 21101  1578 16726 
##     2     3     3     1     3     3     3     2     1     3     1 
##  2886 11953 10407 24262 15167  2760 12429  2296  2455  1773  8463 
##     1     3     3     2     2     3     1     2     2     1     2 
## 17306 16292 21454 20454 21450 22749  9870 10100 13363 23158 21623 
##     3     3     3     1     3     3     2     3     3     1     2 
##  2685 17231 18073 20178 11740  5501  7061 11334  1243 19831 10969 
##     3     2     1     1     1     1     1     1     1     2     2 
## 18108 21491  8861  1698  4268  5472  5407 21257 20204  8715 22521 
##     2     1     1     1     1     1     3     2     1     1     3 
## 12346 25131 21555 25603 13177  1661 16595 19586  9178 15898 20552 
##     1     2     3     1     1     1     1     1     1     2     3 
## 11151 11926 24417 22918 15390 25952 14032 17276 20489 11002 25287 
##     3     2     2     1     1     3     2     3     3     1     3 
## 17486 10976  6226  3770  2714 25884 24232 10959 13197 18810 25444 
##     3     1     3     3     1     2     3     3     3     1     2 
## 13901  7727 21857  4015 19621 25736  6583  6331 16381 13905 25708 
##     1     3     1     2     1     1     2     1     1     3     1 
## 18122 14057 14749  5333 14688 14649 12300 10966 14096 18972 11305 
##     3     2     2     2     3     1     1     3     3     2     1 
## 25360  8517 13819 18785 23918 23313  1681 25138 16453 10918 16924 
##     3     3     2     2     1     2     2     2     1     1     3 
## 24002 25388 19822  6010  2497 17838  7855 10385 12514  3172 23245 
##     3     2     3     1     2     3     1     1     1     1     3 
## 15847 25300 14382 14916 21141 12477 21039  7089 17482  9345  9411 
##     1     2     2     2     2     3     1     3     3     2     3 
##  7638  8072   161 18213  3078 19235 20330 12173 19803  9729 13330 
##     1     3     3     2     3     3     3     2     1     1     1 
## 17262 11408   489  2452 23895 19417 18795  2335 25235 25903 14921 
##     1     2     2     1     3     3     1     3     2     1     1 
##   585   469 20886 21835  4654  7858 24198  3627 16131  9913  9727 
##     1     2     1     3     2     2     3     2     3     1     1 
## 24700 16640  1618  6299 20407  1884 23729  7703 14383 22394 13499 
##     3     1     1     3     2     1     2     2     1     2     2 
## 16486 20990 22428  9782  1981 17721  5769 14611  5446 19334  7900 
##     3     3     2     2     2     3     1     3     1     2     3 
## 19812 14672 20023 25386 24902 13433 19035 21116 10945 19893 25623 
##     3     3     2     1     2     3     3     3     1     3     3 
##  2470  4246 21597 15797  9037 25427 13519 25111 21650 15450 23401 
##     2     2     2     1     2     2     3     3     2     1     1 
##  5962   513 15835 13263  5075 15558 16444 23809  6691 16166 13159 
##     3     1     1     3     2     1     3     1     2     1     3 
##  5512  9631  5511 24664 13620 24111 21188 11304 23934 16806 23365 
##     1     3     3     1     3     3     1     3     3     1     1 
## 17746 16126  6371  7248 24806 15206 20769 10260 16942  7345 22183 
##     1     2     1     1     3     2     2     3     1     2     3 
## 25794 19922  1510 21948 17851 19903 11294 13392 11536 16375 11383 
##     3     1     2     3     2     1     2     1     3     2     1 
## 25342  7770 25531 18546 15654 18530 23119 11818 15937 14870   386 
##     2     2     2     2     1     3     2     2     2     3     1 
## 19339 20034  8889 10408 20282 17744 14839 19604  3813  4552 13304 
##     3     1     1     2     3     1     2     1     1     1     2 
##  5362   453 18580 24991  8749  5298  4172  5539 23348  3391 25606 
##     3     1     1     1     2     2     1     1     3     1     3 
##  1485 22124  2629 13538  1730 12194 16471 10534  7287 24493   168 
##     3     2     1     3     1     3     2     1     3     2     3 
## 24023 12593  5971 25314  6384  5429 23794  3652  1252 17232  2777 
##     2     2     2     3     3     1     2     3     3     2     1 
## 16854 14109 13350  9330 24963   317  9231  8670  6448 13582 14603 
##     2     2     1     3     3     3     3     2     1     2     3 
##  7808 16513  7762 18169  4368 21479 14425 19985 24708  9285 21012 
##     2     3     2     3     1     3     1     3     3     2     2 
##  5379 19273 11465 20652 12203  3570 21950  5586 25707 18634 19832 
##     2     2     3     3     2     3     3     3     1     2     1 
## 24867  8327 10361  3786 20557  3505 10119 16447 18057  6570  4736 
##     2     2     3     3     2     3     1     1     2     1     1 
## 16389 14946  6154 25577 22250 17781 21783  8453 16549 12113 19700 
##     2     2     1     2     2     3     2     2     1     2     3 
## 
## Within cluster sum of squares by cluster:
## [1] 744.0474 604.4673 638.7168
##  (between_SS / total_SS =  32.2 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"    
##  [5] "tot.withinss" "betweenss"    "size"         "iter"        
##  [9] "ifault"       "data"         "hclust"
library(factoextra)
fviz_cluster(K_MEANS, 
             palette="Set1",
             repel = FALSE, 
             ggtheme=theme_bw())

Reclassification on units

mydata2$ClassificationK_MEANS <- K_MEANS$cluster
head(mydata2[, c("ID", "ClassificationWARD", "ClassificationK_MEANS")])
##       ID ClassificationWARD ClassificationK_MEANS
## 3387   1                  1                     1
## 20069  2                  2                     2
## 12531  3                  1                     1
## 25689  4                  3                     3
## 24088  5                  3                     3
## 21097  6                  1                     1
table(mydata2$ClassificationWARD)
## 
##   1   2   3 
## 209  54 232
table(mydata2$ClassificationK_MEANS)
## 
##   1   2   3 
## 169 145 181
table(mydata2$ClassificationWARD, mydata2$ClassificationK_MEANS)
##    
##       1   2   3
##   1 124  79   6
##   2   2  50   2
##   3  43  16 173

Based on Ward classifiction there has been 54 units in the second group. After the reclassification there is 145 units in the group (79 from the first one, 50 from the second and 16 from the third).

Profile of the groups

Averages <- K_MEANS$centers
round(Averages, 3)
##   Wifi_z Time_z Room_z FoodDrink_z Boarding_z Baggage_z
## 1 -0.399 -0.192 -0.978      -0.261     -0.366    -0.632
## 2 -0.761 -0.555  0.696      -0.203     -0.388     0.516
## 3  0.969  0.637  0.387       0.405      0.648     0.210
library(ggplot2)
library(tidyr)
Picture <- as.data.frame(Averages)
Picture$id <- 1:nrow(Picture)
Picture <- pivot_longer(Picture, cols = c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z"))

Picture$Group <- factor(Picture$id, 
                        levels = c(1, 2, 3, 4, 5), 
                        labels = c("1", "2", "3", "4", "5"))

Picture$nameFactor <- factor(Picture$name, 
                             levels = c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z"), 
                             labels = c("Wifi_z", "Time_z", "Room_z", "FoodDrink_z", "Boarding_z", "Baggage_z"))

ggplot(Picture, aes(x = nameFactor, y = value)) +
  geom_hline(yintercept = 0) +
  theme_bw() +
  geom_point(aes(shape = Group, col = Group), size = 3) +
  geom_line(aes(group = id, linetype = Group, col = Group), linewidth = 1) +
  ylab("Average") +
  xlab("Cluster variables")

fit <- aov(cbind(Wifi_z, Time_z, Room_z, FoodDrink_z, Boarding_z, Baggage_z) ~ as.factor(ClassificationK_MEANS), 
           data = mydata2)

summary(fit)
##  Response 1 :
##                                   Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(ClassificationK_MEANS)   2 280.85 140.425  332.75 < 2.2e-16
## Residuals                        492 207.63   0.422                  
##                                     
## as.factor(ClassificationK_MEANS) ***
## Residuals                           
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 2 :
##                                   Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(ClassificationK_MEANS)   2 124.42  62.212   83.28 < 2.2e-16
## Residuals                        492 367.53   0.747                  
##                                     
## as.factor(ClassificationK_MEANS) ***
## Residuals                           
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 3 :
##                                   Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(ClassificationK_MEANS)   2  259.0 129.503  280.55 < 2.2e-16
## Residuals                        492  227.1   0.462                  
##                                     
## as.factor(ClassificationK_MEANS) ***
## Residuals                           
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 4 :
##                                   Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(ClassificationK_MEANS)   2  47.06  23.532  26.031 1.794e-11
## Residuals                        492 444.77   0.904                  
##                                     
## as.factor(ClassificationK_MEANS) ***
## Residuals                           
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 5 :
##                                   Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(ClassificationK_MEANS)   2 120.44  60.221  80.107 < 2.2e-16
## Residuals                        492 369.87   0.752                  
##                                     
## as.factor(ClassificationK_MEANS) ***
## Residuals                           
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 6 :
##                                   Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(ClassificationK_MEANS)   2 114.13  57.064  75.813 < 2.2e-16
## Residuals                        492 370.33   0.753                  
##                                     
## as.factor(ClassificationK_MEANS) ***
## Residuals                           
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The ANOVA test for each of the respective variables has the following hypotheses:

H0: μ of the respective variables are equal across all group. H1: at least one μ is different.

ANOVA tests are significant for all tested variables (p<0.001). This means that the means differ across the cluster groups.

Demografic variables:

Gender

#Pearson Chi2 test, for checking the validity of the classification

chisq_results <- chisq.test(mydata2$GenderF, as.factor(mydata2$ClassificationK_MEANS))
chisq_results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata2$GenderF and as.factor(mydata2$ClassificationK_MEANS)
## X-squared = 1.0545, df = 2, p-value = 0.5902
  • H0: there is no association between gender and cluster
  • H1: there is association between gender and cluster

BSD we can’t reject H0. There is no statistically significant difference in the gender structure between groups.

round(chisq_results$res, 2)
##                
## mydata2$GenderF     1     2     3
##          Male   -0.59  0.45  0.16
##          Female  0.53 -0.41 -0.15
addmargins(round(prop.table(chisq_results$observed, 2), 3), 1) 
##                
## mydata2$GenderF     1     2     3
##          Male   0.420 0.476 0.459
##          Female 0.580 0.524 0.541
##          Sum    1.000 1.000 1.000

Age

aggregate(mydata2$Age, 
          by = list(mydata2$ClassificationK_MEANS), 
          FUN = mean)
##   Group.1        x
## 1       1 41.20710
## 2       2 39.28276
## 3       3 42.61326
fit <- aov(Age ~ as.factor(ClassificationK_MEANS), 
           data = mydata2)

summary.aov(fit)
##                                   Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClassificationK_MEANS)   2    894   446.8   2.101  0.123
## Residuals                        492 104632   212.7
  • H0: there is no association between age and cluster
  • H1: there is association between age and cluster

BSD we can’t reject H0. There is no statistically significant differences in age between groups.

Type

chi_square <- chisq.test(mydata2$TypeF, as.factor(mydata2$ClassificationK_MEANS))
chi_square
## 
##  Pearson's Chi-squared test
## 
## data:  mydata2$TypeF and as.factor(mydata2$ClassificationK_MEANS)
## X-squared = 6.6463, df = 2, p-value = 0.03604
round(chi_square$res,2)
##                  
## mydata2$TypeF         1     2     3
##   Business travel -1.05  0.17  0.86
##   Personal Travel  1.68 -0.27 -1.38
  • H0: there is no association between type and cluster
  • H1: there is association between type and cluster

BSD we can reject H0 at p<0.05. There is statistically significant difference types between groups.

addmargins(round(prop.table(chi_square$observed, 2),3),1)
##                  
## mydata2$TypeF         1     2     3
##   Business travel 0.651 0.731 0.773
##   Personal Travel 0.349 0.269 0.227
##   Sum             1.000 1.000 1.000

(flying) Distance

aggregate(mydata2$Distance, 
          by = list(mydata2$ClassificationK_MEANS), 
          FUN = mean)
##   Group.1        x
## 1       1 1162.438
## 2       2 1483.117
## 3       3 1369.779
fit <- aov(Distance ~ as.factor(ClassificationK_MEANS), 
           data = mydata2)

summary(fit)
##                                   Df    Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClassificationK_MEANS)   2   8428568 4214284    3.89 0.0211
## Residuals                        492 533065088 1083466               
##                                   
## as.factor(ClassificationK_MEANS) *
## Residuals                         
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  • H0: there is no association between (flying) distance and cluster
  • H1: there is association between (flying) distance and cluster

BSD we can reject H0 at p<0.05. There is statistically significant difference in distance between groups.

Groups description

Group 1 (169/495=34.14%) consists of travelers that are mostly Business travelers (65.1%). They have the lowest miles of flying (on average 1162.43). There are 42% male and 58% women. On average they are 41.21 years old. They are characterized by being below average with wifi on the plane and time convenience. They are at least satisfied with room on the plane, food and drinks and baggage handling. Regarding online boarding they are also below average.