There are a few algorithms to perform spatial clustering in R but most of them focus on point pattern analysis (for example here). I would like to perform spatial constrained clustering for polygon data, something similar to this. There are some interesting Open-Source Tools to do this task like SOMVIS and other tools on the site from this link. But rather than using "another" tool I would like to try this in R since I work a lot in R and I'd like to keep that flexible working environment.

So there seems to be very good information here. Does anybody have expirience with a similar task and can anybody give me some advice on good ressources, packages, books etc?


1 Answer 1


I think you can do this by setting up a dissimilarity matrix between cells such that diss((i,j),(k,l)) is large for non-neighbours, and is the difference between your cell values at (i,j),(k,l) for neighbours. Then you feed the dissimilarity matrix into any clustering algorithm that takes a matrix - hclust, or pam or any of several in the Clustering Task View.

Then, for example, hclust would proceed by starting with each cell in its own cluster, and merging the cells closest in the dissimilarity matrix on the first step. This would have to be two spatially adjacent cells, and all subsequent steps of the cluster algorithm would only ever add adjacent cells to clusters.

Note the difference between spatial distance and dissimilarity. Clustering works on the dissimilarity matrix, which in a conventional clustering problem is the (non-spatial) "distance" between two of the objects you are trying to cluster (eg age difference, blood pressure difference etc). What I'm trying to do here is defining that dissimilarity so that for non-adjacent cells the distance is large, whatever the value of the spatial variable you are trying to cluster.

I had a quick play to see if I could get this going but I'd done something wrong. Part of the problem is that if you have a NxM grid, you end up with an (NxM)x(NxM) dissimilarity matrix (or triangle of it) and I was probably getting a dimension wrong somewhere. Maybe later...

  • Hi Spacedman. Thanks for your answer. Your idea seems very interessting to me. I was discussing this too with a collegue. I see two mayor challenges here. First i don't have homogenous polygons (it's not raster data but a shapefile from census regions). Extracting the coordinates of each polygon and creating a dissimilarity index will give me close distances for the small census regions and high distances for the large polygons. I fear that somehow this will affect my clustering analysis in the sense that first only the small regions will be clusterd.
    – Dspanes
    Commented Jan 29, 2013 at 10:23
  • Second: How would you implement your idea? do you simply put the spatial dissimilarities in the table of the other (statistical) dissimilarities? I have 771 objects, if I'd do it like that, my matrix would be enourmes and spatial distances could be overweighted...
    – Dspanes
    Commented Jan 29, 2013 at 10:28
  • and is this any diffrent from simply attaching the coordinates of the polygon centroids (x,y) as variables to the dataframe that already contains the data used for clustering? its hard for me to understand theses questions...
    – Dspanes
    Commented Jan 29, 2013 at 11:28
  • 2
    Try skater in the spdep package...
    – Spacedman
    Commented Jan 29, 2013 at 12:42
  • 1
    Skater is really adequate for this purpose. Thanks for the indication, I dind't knew it existed within R. Still I will have a carefull look if I can use this function, because skater only implements single linkage algorithms and my data is highly left skewed. That causes problems, so I'd prefer the ward algorithm or maybe partitioning algorithms. An alternative would be to use REDcap (spatialdatamining.org/software/redcap) wich works on the same principles as skater but comes along with Ward (unfortunatly outside of R...)
    – Dspanes
    Commented Jan 29, 2013 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.