Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created July 9, 2018 15:02
Show Gist options
  • Save marcosci/3ffdafe9120c413c1aab0551b35eaf60 to your computer and use it in GitHub Desktop.
Save marcosci/3ffdafe9120c413c1aab0551b35eaf60 to your computer and use it in GitHub Desktop.
clumpiness metric from fragstats
landscape_padded <- pad_raster(landscape)
# landscape_padded <- landscape
adjacent_cells <- raster::adjacent(
landscape_padded,
cells = seq_len(raster::ncell(landscape_padded)),
directions = 4,
pairs = TRUE
)
tb <- table(landscape_padded[adjacent_cells[, 1]],
landscape_padded[adjacent_cells[, 2]])
like_adjacencies <- diag(tb)
like_adjacencies <- like_adjacencies[2:length(like_adjacencies)]
other_adjacencies <- tb[lower.tri(tb, diag = TRUE)]
area_class <- tibble::as.tibble(raster::freq(landscape))
min_e <- dplyr::mutate(
area_class,
value = count * 10000,
n = trunc(sqrt(count)),
m = count - n ^ 2,
min_e = dplyr::case_when(
m == 0 ~ n * 4,
n ^ 2 < count &
count <= n * (1 + n) ~ 4 * n + 2,
count > n * (1 + n) ~ 4 * n + 4
)
) %>%
dplyr::pull(min_e)
g <- like_adjacencies / (colSums(other_adjacencies) - min_e)
prop_class <-
(lsm_c_pland(landscape) %>% dplyr::pull(value)) / 100
clumpy <- purrr::map_dbl(seq_along(g), function(row_ind) {
if (g[row_ind] < (prop_class[row_ind]) & prop_class[row_ind] < .5) {
clumpy <-
(g[row_ind] - prop_class[row_ind]) / prop_class[row_ind]
} else{
clumpy <-
(g[row_ind] - prop_class[row_ind]) / (1 - prop_class[row_ind])
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment