Geographically Weighted Models in R

Introduction to Geographically Weighted Models

Geographically Weighted Regression (GWR) is a spatial analysis technique that allows relationships between variables to vary across space. Unlike traditional regression models that estimate a single set of coefficients for the entire study area, GWR computes local regression equations for each location in space.

Key Concept: GWR recognizes that spatial processes might not be stationary and allows the relationships between dependent and independent variables to vary across space.

When to Use GWR

  • When you suspect relationships between variables vary across space
  • When you want to explore spatial heterogeneity in your data
  • When you need to account for local variations in your model
  • When traditional regression shows spatial autocorrelation in residuals

R Packages for GWR

Several R packages are available for implementing geographically weighted models. Here are the most commonly used ones:

spgwr

The standard package for basic GWR modeling. Provides functions for bandwidth selection, model fitting, and diagnostics.

install.packages("spgwr") library(spgwr)

GWmodel

More advanced package that includes various geographically weighted models beyond regression (PCA, discriminant analysis, etc.).

install.packages("GWmodel") library(GWmodel)

mgwr

Implements multiscale geographically weighted regression (MGWR) where different bandwidths can be used for different variables.

install.packages("mgwr") library(mgwr)

sf

For handling spatial data (modern replacement for sp package). Often used in conjunction with GWR packages.

install.packages("sf") library(sf)

Example Code

Basic GWR Example using spgwr

# Load required packages library(spgwr) library(sp) library(spData) # for example dataset # Load example data (house prices in London) data(house) data(london_borough) # Create SpatialPointsDataFrame coordinates(house) <- c("EASTING", "NORTHING") proj4string(house) <- CRS("+init=epsg:27700") # Calculate adaptive bandwidth bw <- gwr.sel(price ~ bedrooms + bathrooms + sq_mt, data = house, coords = coordinates(house), adapt = TRUE) # Fit GWR model gwr_model <- gwr(price ~ bedrooms + bathrooms + sq_mt, data = house, coords = coordinates(house), adapt = bw, hatmatrix = TRUE) # View results summary(gwr_model$SDF) print(gwr_model) # Plot coefficients spplot(gwr_model$SDF, "bedrooms", main = "Local coefficients for bedrooms")

Advanced GWR Example using GWmodel

# Load required packages library(GWmodel) library(sf) library(tmap) # Convert data to sf object data(LondonHP) london_sf <- st_as_sf(LondonHP, coords = c("EASTING", "NORTHING"), crs = 27700) # Create distance matrix DM <- gw.dist(dp.locat = st_coordinates(london_sf)) # Bandwidth selection using cross-validation bw <- bw.gwr(price ~ bedrooms + bathrooms + floor_size, data = london_sf, approach = "CV", kernel = "gaussian", dMat = DM) # Fit GWR model gwr_model <- gwr.basic(price ~ bedrooms + bathrooms + floor_size, data = london_sf, bw = bw, kernel = "gaussian", dMat = DM) # View results gwr_model # Visualize coefficients tm_shape(gwr_model$SDF) + tm_fill(col = "bedrooms", style = "quantile", palette = "RdBu", title = "Bedrooms Coefficient") + tm_borders(alpha = 0.5) + tm_layout(legend.position = c("right", "bottom"))

Multiscale GWR Example using mgwr

# Load required packages library(mgwr) library(spData) # Prepare data data(house) coordinates(house) <- c("EASTING", "NORTHING") proj4string(house) <- CRS("+init=epsg:27700") # Select bandwidths bw <- mgwr.sel(price ~ bedrooms + bathrooms + sq_mt, data = house, coords = coordinates(house)) # Fit MGWR model mgwr_model <- mgwr(price ~ bedrooms + bathrooms + sq_mt, data = house, coords = coordinates(house), bandwidths = bw) # View results summary(mgwr_model) print(mgwr_model) # Compare with standard GWR gwr_model <- gwr(price ~ bedrooms + bathrooms + sq_mt, data = house, coords = coordinates(house), adapt = bw[1]) # Compare AIC values cat("GWR AIC:", gwr_model$results$AIC, "\n") cat("MGWR AIC:", mgwr_model$results$AIC, "\n")

Additional Resources

Books

  • Geographically Weighted Regression

    by A. Stewart Fotheringham, Chris Brunsdon, Martin Charlton

  • Spatial Regression Models

    by Michael Ward and Kristian Gleditsch

Online Resources

Videos

Datasets