JosiahParry / sfdep

A tidy interface for spatial dependence
https://sfdep.josiahparry.com/
GNU General Public License v3.0
121 stars 5 forks source link

provide skater interface #27

Open JosiahParry opened 1 year ago

luukvdmeer commented 1 year ago

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

JosiahParry commented 1 year ago

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 commented 1 year ago

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.