[R-sig-Geo] Inverse distance weights and spatial error model-update

Roger Bivand Roger.Bivand at nhh.no
Mon Feb 22 09:38:58 CET 2010


On Mon, 22 Feb 2010, Adam Boessen wrote:

> Hi Roger, Thank you very much for your quick reply and for creating the 
> spdep package.  I tried the tweaks to the code as you note below, but I 
> still get a warning message.  I have reposted my code below with a few 
> more questions:
>
>> #making inverse distance weights
>> neigh.dist <- nbdists(neigh.nb, coords, longlat=TRUE)
>> inverse <- lapply(neigh.dist, function(x) (1/(x^2)))
>>
>> #creating row standardized spatial weights from list
>> neigh.listw <- nb2listw(neigh.nb, glist=inverse, style="W", zero.policy=TRUE)
> Warning message:
> In nb2listw(neigh.nb, glist = inverse, style = "W", zero.policy = TRUE) :
>  zero sum general weights
>> warning()
> Warning message:

>From your listing of the neighbour object, one region has no neighbours at 
the 1km threshold. This means that the general weights sum is zero for 
this region. It's a warning, not an error - you've set zero.policy to 
TRUE, so the output is OK assuming that you accept no-neighbour 
observations.

>
> There was nothing listed here.  Is this warning a problem?  I was 
> hesitant about using glist since I didn't think it could be incorporated 
> in the errorsarlm models (hence the error in the code below).  Would 
> using spautolm() help to get around my error below in the model?
>
>> #making crime counts into rates
>> assaul_r <- (assaul/gpop00)*1000
>>
>
>
> Good catch on this one.  I meant to make my crime counts into rates.  I 
> haven't found a package where I can model crime counts as Poisson or 
> negative binomial regressions.  Any suggestions?

Use disease mapping approaches, usually going out to WinBUGS or BayesX, or 
GLMM through one of the avaliable functions elsewhere in R. We discuss 
this in the final two chapters of our book, code on book website 
www.asdar-book.org. These however do not let you use the spatial weights 
in the same way. If you need these kinds of weights, then maybe the 
spatial filtering or Moran eigenvector approach might help (adding 
selected eigenvectors of the centred weights matrix to the right hand side 
variables in a regular GLM). Using a GLM (or spautolm) also lets you use 
case weights, which would likely be relevant in your case - see also 
Waller & Gotway 2004.

>>
>> #SAR MODEL
>> ass05 <- errorsarlm(assaul_r ~ gage2900 + gblack00 + gcrwd00 + gethhet00 + ghhincsdln00 + glatino00 + gocc00+ gowner00 + gpov00,data=buffalo,listw=neigh.listw, zero.policy=TRUE)
> Error in subset.listw(listw, subset, zero.policy = zero.policy) :
>  Not yet able to subset general weights lists

It appears that one or more of your observations has missing values, and 
the function really prefers all observations to be complete. If they are 
not, and the weights are not general, it subsets the data and the weights 
to drop incomplete observations, but cannot do that if the weights are 
general. Use of complete.cases() and subset() before running the model 
will help.

Hope this helps,

Roger


>> traceback()
> 4: stop("Not yet able to subset general weights lists")
> 3: subset.listw(listw, subset, zero.policy = zero.policy)
> 2: subset(listw, subset, zero.policy = zero.policy)
> 1: errorsarlm(assaul_r ~ gage2900 + gblack00 + gcrwd00 + gethhet00 +
>       ghhincsdln00 + glatino00 + gocc00 + gowner00 + gpov00, data = buffalo,
>       listw = neigh.listw, zero.policy = TRUE)
>
> Thanks again for your help!
>
> Adam
>
> Adam Boessen
> Doctoral Student
> Department of Criminology, Law and Society
> University of California, Irvine
> aboessen at uci.edu
>
>
> On Feb 21, 2010, at 11:38 PM, Roger Bivand wrote:
>
>> On Sun, 21 Feb 2010, Adam Boessen wrote:
>>
>>> My apologies for the duplicate list, but I forgot to use plain text in my last post, and thus there was a problem posting to archive.  Here is the post now:
>>
>> Thanks for cooperating about this, keeping to plain text really helps!
>>
>>>
>>> Hi Everyone,
>>> I'm new to R, so thank you in advance for your patience.  I'm using US census block groups in Buffalo New York to examine how neighborhood characteristics affect crime.  I would like to use an inverse distance weights (distance decay) of block group centroids that is banded at 1 kilometer.  In other words, I would like to create a one kilometer buffer around each of the centroids, then use row standardized inverse distance weights.  Finally, I would like to run a spatial error model using these weights.
>>>
>>> Here is my code:
>>>> library(foreign)
>>>> library(spdep)
>>>>
>>>> buffalo <- read.dta("buffalo_blkgrps.dta")
>>>> attach(buffalo)
>>>> names(buffalo)
>>> [1] "bgidfp00"     "gage2900"     "gavghhinc00"  "gblack00"     "gcrwd00"
>>> [6] "gethhet00"    "ghhincsdln00" "glatino00"    "gocc00"       "gowner00"
>>> [11] "gpop00"       "gpov00"       "assaul"       "robber"       "burglr"
>>> [16] "motveh"       "murder"       "larcen"       "x"            "y"
>>>>
>>>> #distance based neighbors - making a neighbor list - bounded to 1 kilometer
>>>> coords <-(cbind(x,y))
>>>> neigh.nb <- dnearneigh(coords, 0, 1, longlat=TRUE)
>>
>> I'm assuming that you know definitely that your coordinates are geographical not projected - I've seen users adding longlat=TRUE when not appropriate.
>>
>>>> summary(neigh.nb)
>>> Neighbour list object:
>>> Number of regions: 409
>>> Number of nonzero links: 5298
>>> Percentage nonzero weights: 3.167126
>>> Average number of links: 12.95355
>>> 1 region with no links:
>>> 12
>>> Link number distribution:
>>>
>>> 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
>>> 1  1  3  3  4  5 14 25 28 23 19 26 32 44 29 24 29 25 24 13 15 12  3  4  1  1  1
>>> 1 least connected region:
>>> 3 with 1 link
>>> 1 most connected region:
>>> 382 with 26 links
>>>>
>>>> #making inverse distance weights
>>>> neigh.dist <- nbdists(neigh.nb, coords, longlat=TRUE)
>>>> inverse <- lapply(neigh.dist, function(x) (1/(x^2)))
>>>>
>>>> #creating row standardized spatial weights from list
>>>> neigh.listw <- nb2listw(inverse, style="W",zero.policy=TRUE)
>>
>> No, use:
>>
>> neigh.listw <- nb2listw(neigh.nb, glist=inverse, style="W",
>>  zero.policy=TRUE)
>>
>> You pass the list of weights through the glist= argument, as in the example for col.w.d in ?nb2listw.
>>
>>> Error in nb2listw(inverse, style = "W", zero.policy = TRUE) :
>>> Not a neighbours list
>>>> traceback()
>>> 2: stop("Not a neighbours list")
>>> 1: nb2listw(inverse, style = "W", zero.policy = TRUE)
>>>>
>>>>
>>>> assault <- errorsarlm(assaul_r ~ gage2900 + gblack00 + gcrwd00 + gethhet00 + ghhincsdln00 + glatino00 + gocc00+ gowner00 + gpov00,
>>> + data=buffalo,listw=neigh.listw, zero.policy=TRUE)
>>> Error in eval(expr, envir, enclos) : object 'assaul_r' not found
>>>
>>
>> Well, there is no assaul_r in buffalo, is there?
>>
>> Hope this helps,
>>
>> Roger
>>
>>> The model will run when I do not include the code with the inverse weights and only interpoint distances, but it's unclear to me why I can't include the inverse distances when using nb2listw.  Any ideas on why this is occurring and/or help to alleviate this issue would be much appreciated.  The spatial error model from errorsarlm (package=spdep) will also run fine when I don't include the inverse weights.  Is there a better way to go about running this spatial error model using row standardized inverse distance weights?   Would spautolm from the spdep package be better suited for these data?  Any comments or suggestions are welcome.
>>> Thank you for your time!
>>>
>>> Adam
>>>
>>> Adam Boessen
>>> Doctoral Student
>>> Department of Criminology, Law and Society
>>> University of California, Irvine
>>> aboessen at uci.edu
>>>
>>> p.s. If it's helpful, here is my version of R:
>>>> version
>>>              _
>>> platform       i386-apple-darwin9.8.0
>>> arch           i386
>>> os             darwin9.8.0
>>> system         i386, darwin9.8.0
>>> status
>>> major          2
>>> minor          10.1
>>> year           2009
>>> month          12
>>> day            14
>>> svn rev        50720
>>> language       R
>>> version.string R version 2.10.1 (2009-12-14)
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> --
>> Roger Bivand
>> Economic Geography Section, Department of Economics, Norwegian School of
>> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
>> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>> e-mail: Roger.Bivand at nhh.no
>>
>
>
>
>
>
>
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list