{"id":13857329,"url":"https://github.com/JosiahParry/sfweight","last_synced_at":"2025-07-13T21:32:20.661Z","repository":{"id":132838888,"uuid":"370192968","full_name":"JosiahParry/sfweight","owner":"JosiahParry","description":"Calculate spatial weights and lags with {sf} objects and the tidyverse. ","archived":false,"fork":false,"pushed_at":"2022-03-29T17:46:55.000Z","size":7637,"stargazers_count":59,"open_issues_count":8,"forks_count":2,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-11-14T08:02:59.042Z","etag":null,"topics":["gis","rspatial"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JosiahParry.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-05-24T01:16:51.000Z","updated_at":"2024-01-15T15:15:59.000Z","dependencies_parsed_at":"2023-12-03T14:45:46.727Z","dependency_job_id":null,"html_url":"https://github.com/JosiahParry/sfweight","commit_stats":{"total_commits":15,"total_committers":1,"mean_commits":15.0,"dds":0.0,"last_synced_commit":"dafcf176aca8085f4d446111dc5793e5a6c624dc"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfweight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfweight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfweight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JosiahParry%2Fsfweight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JosiahParry","download_url":"https://codeload.github.com/JosiahParry/sfweight/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225920280,"owners_count":17545463,"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":["gis","rspatial"],"created_at":"2024-08-05T03:01:33.545Z","updated_at":"2024-11-22T15:30:31.234Z","avatar_url":"https://github.com/JosiahParry.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\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\n\u003c!-- badges: start --\u003e\n[![CRAN status](https://www.r-pkg.org/badges/version/sfweight)](https://CRAN.R-project.org/package=sfweight)\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)\n\u003c!-- badges: end --\u003e\n\nNOTE: this package is under active and experimental development. Functions are likely to change. \n\nsfweight is an opinionated translation of the wonderful spdep package. The goal is to provide a streamlined method of doing spatial statistics that works with sf objects, data frames, and the tidyverse. spdep is more flexible with the types of input objects and a bit more idiosyncratic in the syntax that is used. \n\n## Installation\n\nYou can install the development version from GitHub with \n```{r eval=FALSE}\nremotes::install_github(\"Josiahparry/sfweight\")\n```\n\n\n## Motivating examples\n\n### Spatial OLS\n\nWe can fit a spatial Durbin model by calculating spatially lagged predictors. \n\n```{r, warning = FALSE, message=FALSE}\nlibrary(sfweight)\nlibrary(tidyverse)\n\nacs_lagged \u003c- acs %\u003e% \n  mutate(nb = st_contiguity(geometry),\n         wts = st_weights(nb),\n         trans_lag = st_lag(by_pub_trans, nb, wts),\n         bach_lag = st_lag(bach, nb, wts))\n\n\ndurbin_lm \u003c- lm(med_house_income ~ trans_lag + by_pub_trans + bach_lag + bach, \n   data = acs_lagged)\n\nbroom::tidy(durbin_lm)\n```\n\n### Local Autocorrelation\n\nWe can create a Moran plot by creating a spatially lagged variable. Additionally the function `categorize_lisa()` will categorize high-high, high-low, etc., groupings of these variables.\n\n```{r}\nacs_lagged %\u003e% \n  mutate(inc_lag = st_lag(med_house_income, nb, wts),\n         lisa_group = categorize_lisa(med_house_income, inc_lag)) %\u003e% \n  ggplot(aes(med_house_income, inc_lag, color = lisa_group)) +\n  geom_vline(aes(xintercept = mean(med_house_income)), lty = 2, alpha = 1/3) +\n  geom_hline(aes(yintercept = mean(inc_lag)), lty = 2, alpha = 1/3) + \n  geom_point() +\n  labs(title = \"Moran Plot\",\n       y = \"Med. HH Income Spatial Lag\",\n       x = \"Median Household Income\") +\n  theme_minimal() +\n  scale_x_continuous(labels = scales::dollar) + \n  scale_y_continuous(labels = scales::dollar)\n```\n\n\nWe can also calculate the Local  Moran's I for each observation using the function `local_moran()` this will create a dataframe column containing the I, expected I, variance, Z-value, and P-value for each observation. You can extract this using `tidyr::unnest()`.\n\n\n```{r}\nacs %\u003e% \n  mutate(nb = st_contiguity(geometry),\n         wt = st_weights(nb),\n    lisa = local_moran(med_house_income, nb, wt)) %\u003e% \n  unnest(lisa) %\u003e% \n  ggplot(aes(fill = lisa_category)) + \n  geom_sf(color = \"black\", lwd = 0.25) +\n  scale_fill_manual(values = c(\"HH\" = \"#528672\",\"LL\" = \"#525586\", \"Insignificant\" = NA))\n  \n```\n\n\n\n## Basic usage \u0026 contiguities\n\n```{r}\nstr(acs)\n```\n\nWe can get neighbors based on Queen contiguities with `st_contiguity()`.\n\n\n```{r}\nnbs \u003c- st_contiguity(acs)\n\nnbs[1:5]\n```\n\nIf needed, we can also identify the cardinalities from the neighbors list as well. \n\n```{r}\nst_cardinalties(nbs)\n```\n\n\nWe can get the weights from the neighbor contiguities as well. By default, `st_weights()` uses row standardization. \n\n```{r}\nwts \u003c- st_weights(nbs)\n\nwts[1:5]\n```\n\nWe can also calculate the spatial lag with the weights and neighbors. \n\n```{r}\ninc_lag \u003c- st_lag(acs$med_house_income, nbs, wts)\n\ninc_lag[1:5]\n```\n\n\n## K-Nearest Neighbor Distances\n\nIf we have point data we can also identify the k-nearest neighbors with `st_knn()`. For an example we can use the `airbnb` dataset that's imported with `sfweight`. \n\n```{r}\nairbnb\n```\n\n```{r message=FALSE, warning=FALSE}\nairbnb_knn \u003c- st_knn(airbnb)\n\nairbnb_knn[1:5]\n```\n\n## Other weights\n\nPoint based weights implemented based on Luc Anselin and Grant Morrison's [notes](https://spatialanalysis.github.io/lab_tutorials/Spatial_Weights_as_Distance_Functions.html#kernal-weights). \n\nInverse distance band\n\n```{r}\nairbnb_idw \u003c- st_inverse_weights(airbnb$geometry, airbnb_knn)\n\nairbnb_idw[1]\n```\n\n### Kernel based weights\n\n\nAvailable kernels are:\n\n- uniform\n- triangular\n- epanechnikov\n- quartic\n- gaussian\n\n```{r}\nairbnb_gauss \u003c- st_kernel_weight(airbnb$geometry, airbnb_knn, \"gaussian\")\n\nairbnb_gauss[1]\n```\n\n\n## Higher order neighbors \n\n```{r}\nacs %\u003e% \n  transmute(nb = st_contiguity(geometry),\n            nb_2 = st_nb_lag(nb, 2),\n            nb_cumul_2 = st_nb_lag_cumul(nb, 2))\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJosiahParry%2Fsfweight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJosiahParry%2Fsfweight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJosiahParry%2Fsfweight/lists"}