## package 'factoextra' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\asus\AppData\Local\Temp\RtmpaaDd1T\downloaded_packages
## package 'cluster' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\asus\AppData\Local\Temp\RtmpaaDd1T\downloaded_packages
Fetching the data, I toke the data from the Data.gov. the dataset is about the information of us house price.
US_House_Price<-read.csv("C:\\Users\\asus\\Desktop\\Data Science\\Unsupervised Learning\\Unsupervised learning_R projects\\Data_Clustering Project\\US_House_Price.csv")
head(US_House_Price)
## building_permits const_price_index delinquency_rate GDP
## 1 1808 144.4 1.97 14614.14
## 2 1854 145.2 1.97 14614.14
## 3 1757 145.2 1.97 14614.14
## 4 1803 145.9 1.83 14743.57
## 5 1835 145.8 1.83 14743.57
## 6 1875 146.1 1.83 14743.57
## house_for_sale_or_sold housing_subsidies income interest_rate mortgage_rate
## 1 76 25.93 10710.4 1.24 5.9160
## 2 82 25.93 10674.0 1.26 5.8425
## 3 98 25.93 10696.5 1.25 5.7450
## 4 91 25.93 10752.7 1.26 5.8125
## 5 101 25.93 10832.0 1.26 5.4840
## 6 107 25.93 10860.6 1.22 5.2300
## construction_unit total_houses total_const_spending unemployment_rate
## 1 1654 111278 1.5 5.8
## 2 1688 111278 -0.1 5.9
## 3 1638 111278 -1.1 5.9
## 4 1662 111278 1.7 6.0
## 5 1733 111278 0.2 6.1
## 6 1641 111278 2.3 6.3
## urban_population home_price_index
## 1 79.583 128.461
## 2 79.583 129.355
## 3 79.583 130.148
## 4 79.583 130.884
## 5 79.583 131.735
## 6 79.583 132.649
Checking if the data set has any NA values and any other data type other than numeric data.
any_na <- any(is.na(US_House_Price))
print(any_na)
## [1] FALSE
overview of class and structure of the data set, the data type of each variable.
class(US_House_Price)
## [1] "data.frame"
str(US_House_Price)
## 'data.frame': 240 obs. of 15 variables:
## $ building_permits : int 1808 1854 1757 1803 1835 1875 1885 1966 1961 2012 ...
## $ const_price_index : num 144 145 145 146 146 ...
## $ delinquency_rate : num 1.97 1.97 1.97 1.83 1.83 1.83 1.73 1.73 1.73 1.76 ...
## $ GDP : num 14614 14614 14614 14744 14744 ...
## $ house_for_sale_or_sold: int 76 82 98 91 101 107 99 105 90 88 ...
## $ housing_subsidies : num 25.9 25.9 25.9 25.9 25.9 ...
## $ income : num 10710 10674 10696 10753 10832 ...
## $ interest_rate : num 1.24 1.26 1.25 1.26 1.26 1.22 1.01 1.03 1.01 1.01 ...
## $ mortgage_rate : num 5.92 5.84 5.75 5.81 5.48 ...
## $ construction_unit : int 1654 1688 1638 1662 1733 1641 1680 1570 1719 1728 ...
## $ total_houses : int 111278 111278 111278 111278 111278 111278 111278 111278 111278 111278 ...
## $ total_const_spending : num 1.5 -0.1 -1.1 1.7 0.2 2.3 4.1 1 1.2 2.4 ...
## $ unemployment_rate : num 5.8 5.9 5.9 6 6.1 6.3 6.2 6.1 6.1 6 ...
## $ urban_population : num 79.6 79.6 79.6 79.6 79.6 ...
## $ home_price_index : num 128 129 130 131 132 ...
since the data type of all the variable is integer and numerical , we are good to go for the clustering
is the method of finding the most optimal number of groups that the data can be categorized depending on some similarity of characteristics.
For Example If the number of cluster is too small it may not be enough to categorize all the values depending on their difference, in other words variables with different characteristics may be in the same cluster due to small number of clusters. on the other hand if we have too many number of groups, the variance between the group will be very small, which means the variables which have almost the same characteristics will be categorized to different groups.
which plots the variance explained as a function of the number of clusters and looking for an “elbow” point where the rate of decrease sharply changes. this graph shows the variance of the data within the group as the number of group changes. so, we chose the number of groups in which increasing the number of group will not result in decreasing of the variance.
# Create an empty vector to store the total within-cluster sum of squares
wss <- vector("numeric", length = 10) # Assuming a maximum of 10 clusters
# Calculate the within-cluster sum of squares for different values of k
for (i in 1:10) {
kmeans_result <- kmeans(US_House_Price, centers = i)
wss[i] <- kmeans_result$tot.withinss
}
# Plot the elbow plot
plot(1:10, wss, type = "b", pch = 19, frame = FALSE,
xlab = "Number of Clusters (k)",
ylab = "Total Within-Cluster Sum of Squares",
main = "Elbow Method for Optimal k")
# Add a line to indicate the elbow point
elbow_point <- fviz_nbclust(US_House_Price, kmeans, method = "wss")$chisq
abline(v = elbow_point, col = "red", lty = 2)
As we see from from the graph, Increasing the number of cluster from 1 to 2 decrease the variance within group highly. That means observations with different characteristics categorized to different group which decrease the variations with group in each group.
After that point increasing the number of clusters doesn’t decrease the variance within the groups much. that means the observations in different group are not very different or are almost the same. so, No need to categorize them in different groups.
Conclusion: we can say that the optimal number of cluster for this data is 2.
Gap Statistic is a statistical method for estimating the optimal number of clusters in a dataset. The optimal number of clusters is typically associated with the point where the Gap Statistic is maximized (Gap Statistic starts to plateau), indicating that the clustering structure is better than what would be expected by random chance
gap_stat <- clusGap(US_House_Price, FUN = kmeans, nstart = 25, K.max = 10, B = 50)
# Plot the Gap statistic
fviz_gap_stat(gap_stat)
As we see from our graph, it doesn’t exhibit a clear elbow or plateau, and it appears as a straight line or a monotonically increasing trend, determining the optimal number of clusters can be more challenging in this method.
In such cases, the Gap Statistic may not provide a clear signal regarding the number of clusters, we need to consider other methods or criteria. we should stick to the first one.
library(factoextra)
k <- 2
kmeans_result <- kmeans(US_House_Price, centers = k)
# Add cluster assignment to the data frame
US_House_Price$Cluster <- kmeans_result$cluster
# Visualize the clusters using fviz_cluster
fviz_cluster(kmeans_result, data = US_House_Price,
geom = "point", stand = FALSE,
main = "K-Means Clustering Results",
)
As the graph shows, the first half of the data (120) are on the same group, they posses some common behavior. half of the data are belongs to group 2. and posses some similar properties. let’s check the number if data in each group as below.
US_House_Price$Cluster <- kmeans_result$cluster
head(US_House_Price$Cluster)
## [1] 1 1 1 1 1 1
tail(US_House_Price$Cluster)
## [1] 2 2 2 2 2 2
Number of observation in each cluster (Cluster 1 and Cluster 2)
# Count the number of observations in each cluster
cluster_counts <- table(US_House_Price$Cluster)
# Display the cluster counts
print(cluster_counts)
##
## 1 2
## 120 120
Check the Quality of the cluster (the clusterability of the data set), how well the data can be clustered.
The Hopkins statistic is used to assess the clustering tendency of a data set. It compares the distribution of data points with a uniform distribution.it is a measure that quantifies the likelihood of the data being suitable for clustering. A higher Hopkins statistic indicates a higher tendency to cluster.
numeric_data <- US_House_Price
# Calculate Hopkins statistic
hopkins_statistic <- get_clust_tendency(numeric_data, n = 100)
# Print the Hopkins statistic
print(hopkins_statistic)
## $hopkins_stat
## [1] 0.9778368
##
## $plot
since the hopkins_statistic value is 0.9759417 the data set has high likelihood of clustering. It implies that the data points are not randomly distributed and exhibit a tendency to form clusters.
We can also see from the plot that the clusters are visible.
Visual inspection of a dendrogram can provide insights into the clustering tendency. A well-defined dendrogram with clear branches may indicate the presence of clusters.
# Calculate hierarchical clustering
hierarchical_result <- hclust(dist(US_House_Price))
# Plot dendrogram
plot(hierarchical_result, hang = -1, cex = 0.8, main = "Dendrogram for Clustering Tendency")
As we see from the Dendogram, it is well structured and has well defined branches which indicates that the data set is clusterable.