Sample data:
set.seed(123)
lon <- runif(1200, 72.6, 73.2)
lat <- runif(1200, 18.8, 19.6)
df <- data.frame(lon, lat)
Progress so far:
geo.dist = function(df) {
require(geosphere)
d <- function(i,z){ # z[1:2] contain long, lat
dist <- rep(0,nrow(z))
dist[i:nrow(z)] <- distHaversine(z[i:nrow(z),1:2],z[i,1:2])
return(dist)
}
dm <- do.call(cbind,lapply(1:nrow(df),d,df))
return(as.dist(dm))
}
hc <- hclust(geo.dist(df))
clust <- cutree(hc, k=50)
#Visualise the results
ggplot()+
geom_point(data=df,aes(x=lon, y=lat, col=as.factor(clust)))+
theme(legend.position = 'none')
As visualized from the plot, the results are very inconsistent. (The sample data is giving a bit better still, as the points are scattered well) Is there any way I can cluster them? Without using Hierarchical or K-Means?
Found one similar approach here: https://www.supplychaindataanalytics.com/proximity-based-spatial-customer-grouping-in-r/
But it is providing NA for me. Don't know why.
Any help would mean a lot!
geosphere::distHaversine
when it can take multiple coordinate pairs for p1 and p2 thus, returning a distance matrix. You could then just mutate the matrix to a pairwise data.frame or, just past the distance matrix to kmeans.