gwr fitting using package mgcv and R2Bayesx in R

578 views Asked by At

I want to compare GWR fittings produced between spgwr and mgcv, but I got a error with gam function of mgcv . Here is a example :

require(spgwr)
require(mgcv)
require(R2BayesX)

data(columbus)
col.bw <- gwr.sel(crime ~ income + housing, data=columbus,verbose=F,
                  coords=cbind(columbus$x, columbus$y))
col.gauss <- gwr(crime ~ income + housing, data=columbus,
                 coords=cbind(columbus$x, columbus$y), 
                 bandwidth=col.bw, hatmatrix=TRUE)

#gwr fitting with Intercept
col.gam<-gam(crime ~s(x,y)+s(x,y)*income+s(x,y)*housing, data=columbus)#mgcv ERROR
b1<-bayesx(crime ~sx(x,y)+sx(x,y)*income+sx(x,y)*housing, data=columbus)#R2Bayesx ERROR

Question:

  1. How to fit the same gwr using gam and bayesx function(the smooth functions of location )

  2. How to control the parameters to be similiar as possible including optimal bandwidth

1

There are 1 answers

1
Gavin Simpson On BEST ANSWER

The mgcv error comes from the factor that you are specifying the "interactions" between the spatial smooth and variables income and housing. Read ?gam.models for details on using by terms. I think for this you need

col.gam <- gam(crime ~s(x,y, k = 5) + s(x,y, by = income, k = 5) + 
               s(x,y, by = housing, k = 5), data=columbus)

In this example, as there are only 49 observations, you need to restrict the dimensions of the basis functions, which I do here with k = 5, but you should investigate whether you need to vary these a little, within the constraints of the data.

By the looks of the error from bayesx, you have the same issue of specifying the model incorrectly. I'm not familiar with bayesx(), but it looks like it uses the same s() function as supplied with mgcv, so the model specification should be the same as I show above.

As for 2. can you expand on what you mean here Comparable getween gam() and bayesx() or getting both or one of these comparable with the spgwr() model?