R 3.3.0中spgwr包GWR带宽计算异常问题求助
Alright, let's break down why your GWR bandwidth estimation is dragging on for days (with only two values generated so far) and walk through actionable fixes to get this back on track.
Key Reasons for the Slowdown
- Unprojected Geographic Coordinates: Your spatial points use lat/lon in degrees with
projargs=NA. When working with unprojected data,spgwrcalculates distances using slow great-circle (spherical) formulas instead of fast planar distance math. Plus, bandwidth is measured in degrees—an unintuitive unit that often leads to inefficient search ranges, prolonging computation. - Outdated Software: R 3.3.0 is a 2016 release, and the corresponding
spgwrversion lacks modern performance optimizations like faster distance algorithms, parallel computing support, and streamlined cross-validation logic. - Overly Broad Bandwidth Search: If you’re using a huge range of bandwidth values or tiny step sizes for your search, combined with slow distance calculations, the cross-validation process will grind to a crawl.
Step-by-Step Solutions
1. Assign & Convert to a Planar Projection
This is the biggest fix for performance—planar projections use meters/kilometers as units, making distance calculations exponentially faster:
# Load required libraries library(sp) library(spgwr) # First, assign the WGS84 geographic projection to your unprojected data proj4string(points_map) <- CRS("+proj=longlat +datum=WGS84 +no_defs") # Convert to a planar projection (e.g., UTM—pick the zone matching your study area) # Example: UTM Zone 50N for parts of East Asia points_map_proj <- spTransform(points_map, CRS("+proj=utm +zone=50 +datum=WGS84 +units=m +no_defs"))
2. Upgrade R & spgwr
R 3.3.0 is way out of date. Upgrade to a modern R version (4.x series recommended) and install the latest spgwr:
# After upgrading R, run this in a new session install.packages("spgwr")
Newer spgwr versions often include parallel computing support (check the parallel argument in gwr.sel()) and optimized distance routines.
3. Optimize Bandwidth Search Parameters
Tweak your gwr.sel() call to cut down on unnecessary computation:
- Start with a rough, fast search to narrow down the bandwidth range:
# Quick initial search with fewer iterations rough_bw <- gwr.sel(y ~ x1 + x2, data = points_map_proj, n = 10, method = "cv") - Then run a precise search using the rough estimate as a guide:
# Fine-tune with a smaller range around the rough value final_bw <- gwr.sel(y ~ x1 + x2, data = points_map_proj, bw = seq(rough_bw * 0.8, rough_bw * 1.2, by = 100), method = "cv") - For faster results, consider using
method="aic"instead of cross-validation (AIC is quicker, though CV is more robust).
4. Alternative: Try the GWmodel Package
If spgwr still runs slowly, give GWmodel a shot—it’s optimized for large datasets and has more efficient GWR implementations:
install.packages("GWmodel") library(GWmodel) # Use gw.bw() for faster bandwidth selection gw_bw <- gw.bw(y ~ x1 + x2, data = points_map_proj, approach = "CV")
内容的提问来源于stack exchange,提问作者Belen

