What are the most memory efficient open source packages for calculating a geographically weighted regression (GWR)?

I am in a situation where I need to do a geographically weighted regression on a set of points where training data consists of about 40,000 observations and each observation has about 20,000 variables.

I have implemented a version of GWR myself using a combination of Python Numpy/SciPy and PostGIS. I solve the regression using a matrix algebra approach, but this fails due to memory issues when I have dense, feature rich systems with many observations.

One way to get around the memory issue is use an iterative approach for finding a line of best fit, such as an incremental gradient descent. I'm thinking it should work something like (http://www.eecs.wsu.edu/~cook/dm/lectures/l5/node14.html). Incremental Gradient Descent is described pretty well here in pages 4-7 (http://cs229.stanford.edu/notes/cs229-notes1.pdf).

Obviously I could implement this myself, but I was hoping maybe someone else had already coded something similar.

  • 2
    Welcome to GIS.SE. Can you please update your question (just click edit below the question) with some more details. For example, I guess your GWR doesn't mean Ground-Wave Radar. Perhaps you meant Geographically Weighted Regression, but I'm guessing. Possibly you could say what you've already looked at and disregarded (and why), so we don't suggest things that won't meet your needs. – BradHards Feb 13 '14 at 2:23
  • I've heard that the R package is very good, but never tested it. – radouxju Feb 13 '14 at 7:03
  • 1
    As stated by @martin, the spgwr package in R would work. Please take note that GWR is a frequents approach, with all of the relevant assumptions, within the local regression fit. With 20k independent variables you are facing the curse of dimensionality and will not have a remotely valid model. You may want to perform some sort of variable screening, check for colinearity and apply a data reduction approach (e.g., PCA) before specifying a model. With this many covariates, the decomposition is a memory limiting factor and would be mitigated by reducing the dimensionality of the problem. – Jeffrey Evans Feb 18 '14 at 22:37
  • 2
    Once you are finished with this analysis, you should throw away the results, because they will be utterly meaningless: 40,000 observations for 20,000 variables is not enough even to estimate a non-geographic model with any confidence. Some serious reduction in the number of variables is needed before proceeding with any kind of analysis. – whuber May 29 '14 at 19:17
  • 1
    @WhiteboxDev - Does that (below) help? – Martin F Oct 4 '14 at 17:07

From GWR by Roger Bivand:

Geographically weighted regression (GWR) is an exploratory technique mainly intended to indicate where non-stationarity is taking place on the map, that is where locally weighted regression coefficients move away from their global values. Its basis is the concern that the fitted coefficient values of a global model, fitted to all the data, may not represent detailed local variations in the data adequately – in this it follows other local regression implementations. It differs, however, in not looking for local variation in ‘data’ space, but by moving a weighted window over the data, estimating one set of coefficient values at every chosen ‘fit’ point. The fit points are very often the points at which observations were made, but do not have to be. If the local coefficients vary in space, it can be taken as an indication of non-stationarity.

The technique ... involves first selecting a bandwidth for an isotropic spatial weights kernel, typically a Gaussian kernel with a fixed bandwidth chosen by leave-one-out cross-validation. Choice of the bandwidth can be very demanding, as n regressions must be fitted at each step. Alternative techniques are available, for example for adaptive bandwidths, but they may often be even more compute-intensive.

> library(maptools)
> library(spdep)
> owd <- getwd()
> setwd(system.file("etc/shapes", package = "spdep"))
> NY8 <- readShapeSpatial("NY8_utm18")
> setwd(owd)
> library(spgwr)
> bwG <- gwr.sel(Z ~ PEXPOSURE + PCTAGE65P + PCTOWNHOME, data = NY8, gweight = gwr.Gauss,
+ verbose = FALSE)
> gwrG <- gwr(Z ~ PEXPOSURE + PCTAGE65P + PCTOWNHOME, data = NY8, bandwidth = bwG,
+ gweight = gwr.Gauss, hatmatrix = TRUE)
> gwrG

Once the bandwidth has been found, or chosen by hand, the gwr function may be used to fit the model with the chosen local kernel and bandwidth. If the data argument is passed a SpatialPolygonsDataFrame or a SpatialPointsDataFrame object, the output object will contain a component, which is an object of the same geometry populated with the local coefficient estimates. If the input objects have polygon support, the centroids of the spatial entities are taken as the basis for analysis. The function also takes a fit.points argument, which permits local coefficients to be created by geographically weighted regression for other support than the data points.


If you would like to calculate GWR in R, you should try GWmodel. If you need to do it in Python, you can also use pygwr.

GWmodel contains many geographically-weighted (GW) models including gwr (GW regression), gwpca(GW principal components analysis), gwda(GW Discriminant Analysis), gwr.generalised(Generalised GWR models, including Poisson and Binomial), gwr.mixed(mixed geographically weighted regression), gwr.lcr ( GWR with a locally-compensated ridge term) and etc. There are also some robust version for different GW models in the package. Here are some examples for basic GWR :

if (!require(GWmodel)) install.packages("GWmodel")
## specify an optimum bandwidth by cross-validation appraoch
bw1<-bw.gwr(PURCHASE~FLOORSZ, data=londonhp, kernel = "gaussian",dMat=DM)
gwr.res1<-gwr.basic(PURCHASE~FLOORSZ, data=londonhp, bw=bw1,
kernel = "gaussian",dMat=DM, F123.test=T)

Pygwr support Gaussian and Poisson GWR. You can see the example code from github and fork it.

  • Can you give an example in context please. You are probably correct in your assessment but it would be handy in the scope of this question and for future readers to see how it works in a quick script. – Michael Stimson Jul 29 '15 at 1:25
  • I updated my answer and hope it will be helpful for others @MichaelMiles-Stimson – seifer_08ms Jul 30 '15 at 0:17
  • That's better. It's good to see some code. – Michael Stimson Jul 30 '15 at 0:21

spatial analysis in macroecology might be a good solution. http://www.ecoevol.ufg.br/sam/


I would try GeoDA (http://geodacenter.asu.edu/), as it is a stand-alone one-purpose program.

  • GeoDa does not include a GWR model, only autocorrelation analysis and spatial regression. – Jeffrey Evans Oct 5 '14 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.