Author: Nik KovaÄŤ
mydata <- read.table("./tripadvisor_review.csv", header=TRUE, sep=",", dec=";") ##Creating data frame by importing a csv file
colnames(mydata)<- c("ID","X1","X2","X3","X4","X5","X6","X7","X8","X9","X10") #Renaming the columns
head(mydata)
## ID X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
## 1 User 1 0.93 1.8 2.29 0.62 0.8 2.42 3.19 2.79 1.82 2.42
## 2 User 2 1.02 2.2 2.66 0.64 1.42 3.18 3.21 2.63 1.86 2.32
## 3 User 3 1.22 0.8 0.54 0.53 0.24 1.54 3.18 2.8 1.31 2.5
## 4 User 4 0.45 1.8 0.29 0.57 0.46 1.52 3.18 2.96 1.57 2.86
## 5 User 5 0.51 1.2 1.18 0.57 1.54 2.02 3.18 2.78 1.18 2.54
## 6 User 6 0.99 1.28 0.72 0.27 0.74 1.26 3.17 2.89 1.66 3.66
The data is based upon reviews from TripAdvisor.com on destinations across East Asia.
Explanation of the dataset:
The dataset is called Travel Reviews and I took it from a website called UCI Machine Learning Repository: https://archive.ics.uci.edu/dataset/484/travel+reviews.
set.seed(123) #Setting a seed
mydata <- mydata[sample(nrow(mydata), 200),] #Taking a random sample of 200 units from our original dataset
anyNA(mydata) #Checking for NA values
## [1] FALSE
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata <- mydata %>% #Deleting column named ID (Values are categoric)
select(-ID)
mydata$ID <- seq_along(mydata$X1) #Assigning an "ID" number to each house
library(dplyr)
mydata <- mydata %>%
select(ID, everything()) #Putting ID first
library(dplyr)
mydata <- mutate_all(mydata, as.numeric)
summary(mydata[-1]) #descriptive statistics of all relevant columns
## X1 X2 X3 X4
## Min. :0.3400 Min. :0.000 Min. :0.130 Min. :0.2300
## 1st Qu.:0.6700 1st Qu.:1.080 1st Qu.:0.290 1st Qu.:0.4100
## Median :0.8300 Median :1.280 Median :0.815 Median :0.4900
## Mean :0.8775 Mean :1.336 Mean :1.014 Mean :0.5325
## 3rd Qu.:1.0200 3rd Qu.:1.520 3rd Qu.:1.500 3rd Qu.:0.5700
## Max. :2.0500 Max. :2.960 Max. :2.940 Max. :3.1000
## X5 X6 X7 X8 X9
## Min. :0.160 Min. :0.400 Min. :3.160 Min. :2.54 Min. :0.930
## 1st Qu.:0.620 1st Qu.:1.415 1st Qu.:3.180 1st Qu.:2.77 1st Qu.:1.340
## Median :0.880 Median :1.860 Median :3.180 Median :2.84 Median :1.500
## Mean :0.912 Mean :1.834 Mean :3.181 Mean :2.84 Mean :1.572
## 3rd Qu.:1.150 3rd Qu.:2.160 3rd Qu.:3.180 3rd Qu.:2.91 3rd Qu.:1.760
## Max. :3.300 Max. :3.760 Max. :3.200 Max. :3.26 Max. :3.140
## X10
## Min. :2.260
## 1st Qu.:2.540
## Median :2.760
## Mean :2.808
## 3rd Qu.:3.040
## Max. :3.660
From this table we can gather a lot of information and I will explain some:
Creating a new data frame with only the relevant columns:
clustervars <- mydata %>%
select(ID, X4, X5, X7, X8)
Standardizing all columns except ID:
clustervars$X4_z <- scale(clustervars$X4)
clustervars$X5_z <- scale(clustervars$X5)
clustervars$X7_z <- scale(clustervars$X7)
clustervars$X8_z <- scale(clustervars$X8)
Before I do the clustering itself, I need to check if all assumptions are met:
We can assume there are natural groups of objects in the population and the sample size is also large enough for the second assumption to be met.
Lastly, I need to check the correlation between cluster variables:
library(Hmisc)
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
##
## src, summarize
## The following objects are masked from 'package:base':
##
## format.pval, units
rcorr(as.matrix(clustervars[, c("X4_z","X5_z","X7_z","X8_z")]),
type = "pearson")
## X4_z X5_z X7_z X8_z
## X4_z 1.00 -0.05 0.14 -0.04
## X5_z -0.05 1.00 0.16 0.02
## X7_z 0.14 0.16 1.00 0.07
## X8_z -0.04 0.02 0.07 1.00
##
## n= 200
##
##
## P
## X4_z X5_z X7_z X8_z
## X4_z 0.5063 0.0525 0.5538
## X5_z 0.5063 0.0201 0.8252
## X7_z 0.0525 0.0201 0.3378
## X8_z 0.5538 0.8252 0.3378
As we can see from the correlation matrix, there are no pair of variables whose correlation exceeds absolute value of 0.3, this means there is not too strong correlation between cluster variables
Checking outliers:
clustervars$Dissimilarity <- sqrt(clustervars$X4_z^2 + clustervars$X5_z^2 + clustervars$X7_z^2 + clustervars$X8_z^2) #Finding outliers
head(clustervars[order(-clustervars$Dissimilarity), c("ID", "Dissimilarity")], 10)
## ID Dissimilarity
## 602 15 8.786927
## 593 33 6.432182
## 480 108 6.237929
## 83 162 4.626320
## 89 95 4.046349
## 373 36 4.043416
## 308 93 3.569241
## 309 48 3.483849
## 294 65 3.452571
## 672 196 3.289233
Removing outliers:
library(dplyr)
clustervars <- clustervars %>%
filter(Dissimilarity <= 4) #Removing units with Dissimilarity values over 4
## Warning: Using one column matrices in `filter()` was deprecated in dplyr 1.1.0.
## ℹ Please use one dimensional logical vectors instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Dissimilarity matrix:
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
#Calculating Euclidian distances
distance <- get_dist(clustervars[c("X4_z","X5_z","X7_z","X8_z")],
method = "euclidian")
distance2 <- distance^2
fviz_dist(distance2) #Showing dissimilarity matrix
Hopkins statistics has to be above in order for the data to be clusterable
get_clust_tendency(clustervars[c("X4_z","X5_z","X7_z","X8_z")],
n = nrow(clustervars) - 1,
graph = FALSE)
## $hopkins_stat
## [1] 0.730623
##
## $plot
## NULL
Hierarchical clustering:
library(dplyr)
WARD <- clustervars[c("X4_z","X5_z","X7_z","X8_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: 194
Dendrogram:
library(factoextra)
fviz_dend(WARD) #Dendrogram
## 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.
I decided to do 5 clusters based on the horizontal lines of clusters.
Showing the initial classification of units (hierarchical clustering):
clustervars$ClusterWard <- cutree(WARD,
k = 5) #Cutting the dendrogram
head(clustervars)
## ID X4 X5 X7 X8 X4_z X5_z X7_z X8_z
## 1 1 0.53 0.74 3.18 2.87 -0.008684272 -0.4241367 -0.1129353 0.2333993
## 2 2 0.54 1.18 3.18 2.78 0.025371695 0.6608642 -0.1129353 -0.4656336
## 3 3 0.47 0.46 3.18 2.89 -0.213020072 -1.1145918 -0.1129353 0.3887400
## 4 4 0.49 1.30 3.19 2.70 -0.144908138 0.9567735 1.1419018 -1.0869963
## 5 5 0.40 1.18 3.18 2.82 -0.451411839 0.6608642 -0.1129353 -0.1549523
## 6 6 0.53 1.58 3.18 2.79 -0.008684272 1.6472286 -0.1129353 -0.3879633
## Dissimilarity ClusterWard
## 1 0.4971891 1
## 2 0.8166727 2
## 3 1.2048094 1
## 4 1.8498418 3
## 5 0.8229695 2
## 6 1.6960859 2
Positions of initial leaders:
#Calculating positions of initial leaders
initial_leaders <- aggregate(clustervars[, c("X4_z","X5_z","X7_z","X8_z")],
by = list(clustervars$ClusterWard),
FUN = mean)
initial_leaders #Showing initial leaders
## Group.1 X4_z X5_z X7_z X8_z
## 1 1 -0.13789662 -0.78604544 -0.05757488 -0.42565626
## 2 2 -0.12081977 0.96639659 -0.08232956 -0.01666123
## 3 3 0.10446942 0.48586404 1.62764520 0.05300374
## 4 4 0.01260071 -0.23097368 0.14848906 1.64764664
## 5 5 -0.51611818 0.02137123 -1.49325619 -0.18860946
K-Means clustering:
library(factoextra)
k_means <- hkmeans(clustervars[c("X4_z","X5_z","X7_z","X8_z")],
k = 5,
hc.metric = "euclidean",
hc.method = "ward.D2")
k_means
## Hierarchical K-means clustering with 5 clusters of sizes 60, 41, 30, 29, 34
##
## Cluster means:
## X4_z X5_z X7_z X8_z
## 1 -0.12277176 -0.7299097 0.09620418 -0.54071496
## 2 -0.07762684 0.9808312 -0.11293534 -0.01097804
## 3 0.05715726 0.5392126 1.64383665 0.07029165
## 4 -0.03686852 -0.2914877 0.06014564 1.55379499
## 5 -0.48146122 -0.2297649 -1.47849340 -0.24632917
##
## Clustering vector:
## [1] 1 2 1 3 2 2 1 2 3 4 1 3 3 5 1 1 1 1 1 4 2 5 2 1 1 1 1 1 2 3 2 2 2 5 5 5 1
## [38] 5 4 1 4 1 5 1 4 5 2 4 5 1 5 5 1 3 1 4 5 5 5 4 1 4 1 4 5 4 1 1 3 4 4 4 1 5
## [75] 2 1 2 3 3 4 1 1 2 2 3 3 2 2 1 3 2 3 3 4 2 1 5 2 2 5 4 1 1 3 1 3 5 4 1 5 5
## [112] 1 4 2 2 1 4 2 1 3 3 5 2 1 2 1 1 3 2 4 4 2 1 5 1 1 4 1 1 2 3 5 2 3 2 3 4 1
## [149] 2 1 2 4 1 4 4 1 2 1 5 4 5 1 3 2 5 3 3 3 3 5 1 1 2 4 2 1 1 5 5 3 5 1 3 1 5
## [186] 1 2 2 5 2 2 5 1 3
##
## Within cluster sum of squares by cluster:
## [1] 76.39149 39.96829 39.80471 38.18970 50.12121
## (between_SS / total_SS = 57.9 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault" "data"
## [11] "hclust"
Explanation of results:
We can find out more information from this output alone but it’s better visible in the later graph.
Cluster plot:
fviz_cluster(k_means,
palette = "set1",
repel = FALSE,
ggtheme = theme_classic())
Hierarchical grouping vs K-Means grouping:
clustervars$ClusterK_Means <- k_means$cluster
head(clustervars[c("ID","ClusterWard","ClusterK_Means")])
## ID ClusterWard ClusterK_Means
## 1 1 1 1
## 2 2 2 2
## 3 3 1 1
## 4 4 3 3
## 5 5 2 2
## 6 6 2 2
Number of units in each group (hierarchical clustering)
#Checking for reclassifications
table(clustervars$ClusterWard)
##
## 1 2 3 4 5
## 68 41 31 24 30
Number of units in each group (K-Means clustering)
table(clustervars$ClusterK_Means)
##
## 1 2 3 4 5
## 60 41 30 29 34
Number of units in each group (matrix of both)
table(clustervars$ClusterWard, clustervars$ClusterK_Means)
##
## 1 2 3 4 5
## 1 57 2 0 3 6
## 2 2 38 0 1 0
## 3 1 0 30 0 0
## 4 0 0 0 24 0
## 5 0 1 0 1 28
Explanation of reclassification:
We have 29 units in group 4 performed by K-Means clustering, 24 units were already in group 4, 3 reclassified from group 1, 1 reclassified from group 2 and 1 reclassified from group 5 performed by hierarchical clustering.
We had 31 units in group 3 performed by hierarchical clustering but one unit reclassified to group 1 and 30 stayed in group 3 performed by K-means
Centroids:
Centroids <- k_means$centers
Centroids
## X4_z X5_z X7_z X8_z
## 1 -0.12277176 -0.7299097 0.09620418 -0.54071496
## 2 -0.07762684 0.9808312 -0.11293534 -0.01097804
## 3 0.05715726 0.5392126 1.64383665 0.07029165
## 4 -0.03686852 -0.2914877 0.06014564 1.55379499
## 5 -0.48146122 -0.2297649 -1.47849340 -0.24632917
library(ggplot2)
library(tidyr)
Figure <- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer(Figure, cols = c(X4_z, X5_z, X7_z, X8_z))
Figure$Groups <- factor(Figure$id,
levels = c(1, 2, 3, 4, 5),
labels = c("1", "2", "3", "4", "5"))
Figure$nameFactor <- factor(Figure$name,
levels = c("X4_z","X5_z","X7_z","X8_z"),
labels = c("Restaurants","Museums","Parks","Beaches"))
ggplot(Figure, aes(x = nameFactor, y = value)) +
geom_hline(yintercept = 0) +
theme_bw() +
geom_point(aes(shape = Groups, col = Groups), size = 5) +
geom_line(aes(group = id), linewidth = 1) +
ylab("Averages") +
xlab("Cluster variables") +
ylim(-2.5, 2.5)
Explanation of results:
fit <- aov(cbind(X4_z, X5_z, X7_z, X8_z) ~ as.factor(ClusterK_Means),
data = clustervars)
summary(fit)
## Response 1 :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 4 5.6121 1.40302 9.2646 7.397e-07 ***
## Residuals 189 28.6219 0.15144
## ---
## 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(ClusterK_Means) 4 84.321 21.0803 53.375 < 2.2e-16 ***
## Residuals 189 74.645 0.3949
## ---
## 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(ClusterK_Means) 4 156.552 39.138 182.67 < 2.2e-16 ***
## Residuals 189 40.494 0.214
## ---
## 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(ClusterK_Means) 4 89.593 22.3983 42.033 < 2.2e-16 ***
## Residuals 189 100.714 0.5329
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Explanation of results:
All p-values are below p<0.001, this means we can reject the null hypothesis for all cluster variables (p<0.001) and conclude that they are all successful at classifying units into groups
Research question: Can we divide travelers into homogeneous groups based on their reviews of restaurants, museums, parks and beaches when visiting East Asia?