Although I've a regular r user I have only ever computed GWR in arcgis, and now I'm using R I'm just a little confused.

I seem to have got my GWR results and now need to go about plotting them on a map. I did this GWR on a spatial points data frame, which I would usually join spatially to a separate polygons. But with my results stored in an object called 'res.binomial', how can I then join them up to my other data?

EDIT: I've seen that results seem to be stored in res.binomial$SDF - so I guess the question is how I use those results??

bwG <- gwr.sel(pop~ demsat + lrscale + relig + euint + econ, data = newest, coords = locationsnew, verbose = FALSE)
bw.gwr.1 <- bw.gwr(pop~ demsat + lrscale + relig + euint + econ, data = newest, approach = "AICc",kernel = "bisquare", adaptive = TRUE)

DM <- gw.dist(dp.locat=locationsnew)

res.binomial<-ggwr.basic(pop~ demsat + lrscale + relig + euint + econ,data=newest, bw=bwG, dMat=DM, family ="binomial")

  • 1
    Just look at the objects contained in the returned object. Using names() on the "res.binomial" object returns several objects, one being "SDF". Looking at class(res.binomial$SDF) indicates that it is a "SpatialPointsDataFrame". To plot the data you can use any method for plotting sp class objects eg., spplot(res.binomial$SDF, "residual") – Jeffrey Evans May 22 '17 at 17:41

I've recently started working with GWR in R as well. This is what I've been doing demonstrated with a reproducible example. Hopefully it is correct and useful for others. You will also find good tutorials here and here.

# install dev. version og ggplot2 so we can use it with sf


# load data
# plot Census Tract map

enter image description here

# calculate Optimal kernel bandwidth
  GWRbandwidth <- gwr.sel( log(med.age) ~ log(white) + log(black), data=oregon.tract, adapt=T)

# detect number of CPU cores to go parallel
  no_cores <- detectCores() - 1 # Calculate the number of cores
  cl <- makeCluster(no_cores)# Initiate cluster 

# run GWR Model
  gwr_fit <- gwr( log(med.age) ~ log(white) + log(black), data=oregon.tract, adapt= GWRbandwidth, hatmatrix=TRUE, se.fit=TRUE, cl=cl)

# Create an object with the value of Quasi-global R2
  globalR2 <- (1 - (gwr_fit$results$rss/gwr_fit$gTSS))

Ok, now we do the plotting. I'm using ggplot2 and the new [sf library]4, which is incredibly efficient.

  # get spatial spatialpolygondataframe from regression results + convert it into sf object. The spatial object brings the regressions results within it's data component
  sp <- gwr_fit$SDF
  sf <- st_as_sf(sp)  

# map local R2
 ggplot() + geom_sf(data = sf, aes(fill=localR2)) +
    coord_sf() +
    theme_map() +
    ggtitle(paste("Local R2")) +
    labs(subtitle = paste("Global R2:", round(globalR2, 2) ) ) 

enter image description here

# map residuals gwr.e
 ggplot() + geom_sf(data = sf, aes(fill=gwr.e)) +
   coord_sf() +
   theme_map() +

enter image description here

| improve this answer | |
  • Hi @rafa.pereira, just seen this. You're work here is really excellent and a really strong learning tool for myself and others. Similarly, I managed to extract the SDF components, however your instruction has given me a way to implement local R-squared which I had noticed wasn't part of SDF. For this I'm really grateful. – Henry Cann May 23 '17 at 13:09
  • As an additional thing I'd add that my own analysis is a bit more, well 'challenged'. I don't have accurate spatial data points and wanted to do see if I could still do some analysis by assigning centroid coordinates to each county. I'd be interested to hear what you and others think about this, however it certainly has its limitations and in particular risks losing a lot of the detail of the data. – Henry Cann May 23 '17 at 13:11
  • I'm learning along, Henry. Regarding your follow up question, I don't know the answer but I would recommend you post a new question explaining the details of what you're doing. – rafa.pereira May 23 '17 at 13:13
  • Thanks - also I'm wondering if my method for specifying the model is equivalent to yours @rafa.pereira - you have used logs - would both methods prelude identical results? – Henry Cann May 24 '17 at 14:46
  • I must say the maps in my answer were created without log transformation. I included the logs after I had posted my answer just because that's what I'm doing with my own data. The conclusions should be the same, but the results and their interpretation is different. more info here: cazaar.com/ta/econ113/interpreting-beta – rafa.pereira May 24 '17 at 15:28

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.