{"id":18562965,"url":"https://github.com/josiahparry/sfdep","last_synced_at":"2025-09-01T13:39:23.444Z","repository":{"id":40316363,"uuid":"445850676","full_name":"JosiahParry/sfdep","owner":"JosiahParry","description":"A tidy interface for spatial dependence","archived":false,"fork":false,"pushed_at":"2024-09-05T16:03:56.000Z","size":21884,"stargazers_count":131,"open_issues_count":15,"forks_count":5,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-06T01:27:03.849Z","etag":null,"topics":["r-spatial","rstats","spatial"],"latest_commit_sha":null,"homepage":"https://sfdep.josiahparry.com/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JosiahParry.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["josiahparry"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2022-01-08T15:05:49.000Z","updated_at":"2025-03-17T17:23:43.000Z","dependencies_parsed_at":"2024-11-06T22:21:38.013Z","dependency_job_id":null,"html_url":"https://github.com/JosiahParry/sfdep","commit_stats":{"total_commits":142,"total_committers":3,"mean_commits":"47.333333333333336","dds":"0.021126760563380254","last_synced_commit":"649b90c7ee45fa1dd80bac79a8bf3ec622b16425"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfdep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfdep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfdep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfdep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JosiahParry","download_url":"https://codeload.github.com/JosiahParry/sfdep/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247557767,"owners_count":20958047,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["r-spatial","rstats","spatial"],"created_at":"2024-11-06T22:11:32.457Z","updated_at":"2025-04-06T22:08:34.619Z","avatar_url":"https://github.com/JosiahParry.png","language":"R","funding_links":["https://github.com/sponsors/josiahparry"],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# sfdep\n\n\u003c!-- badges: start --\u003e\n[![CRAN status](https://www.r-pkg.org/badges/version/sfdep)](https://CRAN.R-project.org/package=sfdep)\n[![R-CMD-check](https://github.com/JosiahParry/sfdep/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/JosiahParry/sfdep/actions/workflows/R-CMD-check.yaml)\n[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)\n\u003c!-- badges: end --\u003e\n\nsfdep builds on the great shoulders of spdep package for spatial dependence. sfdep creates an sf and tidyverse friendly interface to the package as well as introduces new functionality that is not present in spdep. sfdep utilizes list columns extensively to make this interface possible. \n\n## Installation\n\nInstall the released version from CRAN \n\n```r\ninstall.packages(\"sfdep\")\n```\n\nYou can install the development version of sfdep like so:\n\n``` r\nremotes::install_github(\"josiahparry/sfdep\")\n```\n\n## Usage\n\nThere are three main categories of functionality relating to geometry neighbors, weights, and local indicators of spatial association (LISAs). \n\n### Neighbors\n\nThe most fundamental usage is to find contiguous neighbors from a polygon. This is done with `st_contiguity()` which, by default creates queen weights. If rook weights are desired, set `queen = FALSE`. Additional arguments can be passed to the underlying `spdep::poly2nb()` via `...`. `st_contiguity()` creates an object of class `nb` as used by `spdep`. \n\n```{r message=FALSE}\nlibrary(sf)\nlibrary(sfdep)\nlibrary(dplyr)\n\n# grab geometry\ngeo \u003c- st_geometry(guerry)\n\nnb \u003c- st_contiguity(geo)\n\nnb\n```\n\nWe can identify higher order neighbors with `st_nb_lag()` and the cumulative higher order neighbors with `st_nb_lag_cumul()`.\n\n```{r}\nst_nb_lag(nb, 2)\nst_nb_lag_cumul(nb, 2)\n```\n\nOther point geometry neighbor functions are `st_knn()`, `st_dist_band()`, `st_nb_dists()`. \n\n\n### Weights\n\n\nPolygon weights are created with `st_weights()` (which calls `spdep::nb2listw`). By default they are row standardized weights. \n\n```{r}\nwt \u003c- st_weights(nb)\n\nwt[1:2]\n```\n\nOther point based weights can be created with `st_nb_dists()`,  `st_kernel_weights()` and `st_inverse_weights()`.\n\n\n### Local Indicators of Spatial Association (LISAs)\n\nLISAs are created from a combination of neighbors and weights and are intended to be used inside of a dplyr pipeline. The below is a worked example of calculating the spatial lag and the local moran.\n\n```{r, message = FALSE, warn = FALSE}\ng \u003c- guerry %\u003e% \n  mutate(\n    nb = st_contiguity(geometry),\n    wt = st_weights(nb)\n    )\n```\n\nThen calculate the spatial lag with `st_lag()`. Given that we've only modified an sf object, we can visualize this with ggplot2.\n\n```{r}\nlibrary(ggplot2)\n\n# create spatial lag\ng %\u003e% \n  mutate(crime_pers_lag = st_lag(crime_pers, nb, wt)) %\u003e% \n  ggplot(aes(fill = crime_pers_lag)) + \n  geom_sf(lwd = 0.2, color = \"black\") +\n  theme_void()\n```\n\nMost users will be interested in local indicators of spatial association (LISA). Utilize `local_moran()` to do this. `local_moran()` will create a data frame column which contains a number of informative variables. For example the cluster that a polygon falls into based on mean, median, or pysal calculations. This will need to be unnested or certain variables hoisted.\n\nCreate the Local Moran data frame column.\n\n```{r}\nlisa \u003c- g %\u003e% \n  mutate(moran = local_moran(crime_pers, nb, wt))\n\npull(lisa, moran) %\u003e% \n  glimpse()\n```\n\nVisualize this by converting insignificant values to NA. This uses a cutoff of 0.1.\n\n```{r}\nlisa %\u003e% \n  tidyr::unnest(moran) %\u003e% \n  mutate(pysal = ifelse(p_folded_sim \u003c= 0.1, as.character(pysal), NA)) |\u003e \n  ggplot(aes(fill = pysal)) +\n  geom_sf() +\n  geom_sf(lwd = 0.2, color = \"black\") +\n  theme_void() +\n  scale_fill_manual(values = c(\"#1C4769\", \"#24975E\", \"#EACA97\", \"#B20016\"))\n```\n\n## Other topics: \n\n* [Emerging Hot Spot Analysis](https://sfdep.josiahparry.com/articles/understanding-emerging-hotspots.html#using-emerging_hotspot_analysis)\n* [Spatio-Temporal Data](https://sfdep.josiahparry.com/articles/spacetime-s3.html)\n* [`{sfnetworks}` integration](https://sfdep.josiahparry.com/reference/index.html#networks)\n\n\u003c!-- ## Neighbor apply function --\u003e\n\n\u003c!-- In the case that you want to create custom local level metrics, you can do so with the function `st_nb_apply()`. This provides a purrr like interface to do calculation on an observation $x_{i}$ using it's neighbors, $x_{ij}$, and their respective weights $w_{ij}$. In the formula syntax utilize `.xij`, `.nb` and `.wt` for these values respectively. We can specify the type of output with the suffix argument. By default this will be `\"dbl\"`. These can be any purrr sufix. To get the default list output use `\"list\"`.  --\u003e\n\n\u003c!-- For example  --\u003e\n\n\u003c!-- ```{r} --\u003e\n\u003c!-- g %\u003e%  --\u003e\n\u003c!--   transmute(x = st_nb_apply(crime_pers, nb, wt,  --\u003e\n\u003c!--                             .f = function(.xij, .wt, ...) sum((.xij - crime_pers)^2) / (length(.wt) + 1), suffix = \"dbl\")) --\u003e\n\u003c!-- ``` --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosiahparry%2Fsfdep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosiahparry%2Fsfdep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosiahparry%2Fsfdep/lists"}