sfdep icon indicating copy to clipboard operation
sfdep copied to clipboard

provide skater interface

Open JosiahParry opened this issue 2 years ago • 3 comments

JosiahParry avatar Jul 15 '22 15:07 JosiahParry

Any ideas on this already? I am thinking of including skater in sfnetworks, as a function group_skater() to complement the community detection functions of tidygraph (https://tidygraph.data-imaginist.com/reference/group_graph.html), but not sure yet what the best approach is. Probably relying on sfdep/spdep for doing this is the best way

luukvdmeer avatar Sep 12 '22 10:09 luukvdmeer

Here is what I've got so far for an implementation. The resultant object is rather wild and wouldn't fit into a data frame mentality without a fair amount of restructuring.


.skater <- function(x, nb, k, .method = "euclidean",
                    .ini = NULL, scale = TRUE, ...) {

  if (inherits(x, "numeric")) x <- list(x)
  m <- Reduce(cbind.data.frame, x)

  if (scale) m <- scale(m)

  costs <- spdep::nbcosts(nb, m, method = .method)

  listw <- spdep::nb2listw(nb, costs, style = "B")

  tree <- spdep::mstree(listw, ini = .ini)


  skater(tree[,1:2], m, ncuts = k - 1 , ...)

}

res <- .skater(list(bh$HLCI, bh$ELCI), nb, 5)

# The result is still super messy
plot(st_geometry(bh), col = res$groups)
image

JosiahParry avatar Sep 12 '22 12:09 JosiahParry

It could be a super easy implementation if we only return info regarding the cluster assignment. If we return info about edges etc that cannot conform to a dataframe structure and will not work too well.

JosiahParry avatar Sep 12 '22 15:09 JosiahParry