rmgcvspdep

GAM with MRF smoother: create neighbourhood structure with duplicate coordinates


I want to use the mgcv package in R to run the model

df <- as.data.frame(spdf)
mod <- gam(y ~ s(geoid, bs = 'mrf', xt = list(nb = nb), k = 20) +     
               s(month, bs = 'cc', k = 12),
                data = df,
                method = 'REML',
                family = Gamma(link = log))

where spdf is a SpatialPolygonDataFrame.

I am having problems understanding how to create the nb object since the data df have duplicates geoid values (one for each month) and when I run

nb <- poly2nb(spdf, row.names = spdf@data@geoid)

I get the error

Error in poly2nb(spdf, row.names = spdf@data@geoid): non-unique row.names given

Thanks


Solution

  • The neighbourhood object doesn't need to have as many rows as the data, just as many rows as there are geoids.

    If you are going to use geoid from the data, you'll want to subset the object first to contain non-duplicated geoid values. Using the typical subsetting methods ([) and duplicated() on the @data element of the SpatialPolygonsDataFrame should get you a unique set of geoids to build the neighbourhood object from.