[R-sig-Geo] Adding a Few Neighbour Relationships to a nb List

Stuart Reece @tu@rt@reece @end|ng |rom b|gpond@com
Thu Aug 8 12:48:37 CEST 2019


Thankyou so much Vijay and Barry.

Yes I have found that using these techniques I can add nb relationships nicely.

And they card() nicely too.

And when I nb2listw they transform well and when I test them can.be.simmed(x) they pass this test also.

 

When I draw maps with them Like Roger Bivand showed in his examples for poly2nb – the maps draw just perfectly.

 

However when I use them in the spml or spgm regressions in package splm they fail with an error message usually about indexes being out of bounds.

 

Running this code ….

 

summary(spgm(PC1MI ~ PC1Rx * log(mrjmon), 

+              data=MIdf9dfF2, listw=MIdf9sflww,

+              lag=TRUE, moments="fullweights", method="g2sls",

+              model="random", spatial.error=TRUE))

 

Generates this error:

Error in x[, ii] : subscript out of bounds

 

I feel that I must be missing something here but am not able to put my finger on what it is???

Thanks so much again,

Stuart.

 

 

 

 

From: Dr Stuart Reece <asreece using bigpond.net.au> 
Sent: Thursday, 8 August, 2019 2:01 PM
To: 'Barry Rowlingson' <b.rowlingson using gmail.com>; 'Stuart Reece' <stuart.reece using bigpond.com>
Cc: 'Vijay Lulla' <vijaylulla using gmail.com>; 'R-sig-geo Mailing List' <r-sig-geo using r-project.org>
Subject: RE: [R-sig-Geo] Adding a Few Neighbour Relationships to a nb List
Importance: High

 

Thanks Barry.

That is so perfect and so super helpful!!!

And if I want to add three areas to an area – say I want to add 6,7 and 8 to the area 10??

Please may I have the syntax for that to avoid the integer error??

Is this also the root of the error about not being the correct index??

Many thanks again,

Stuart.

 

From: Barry Rowlingson [mailto:b.rowlingson using gmail.com] 
Sent: Thursday, 8 August 2019 8:51 AM
To: Stuart Reece
Cc: Vijay Lulla; R-sig-geo Mailing List; Stuart Reece
Subject: Re: [R-sig-Geo] Adding a Few Neighbour Relationships to a nb List

 

I recently answered a similar question on Stack Overflow where someone needed to add detached polygons to their connected network by connecting them to their nearest neighbour:

 

https://stackoverflow.com/questions/57269254/how-to-impute-missing-neighbours-of-a-spatial-weight-matrix-queen-contiguity/57378930?noredirect=1#comment101246065_57378930

 

in short, you can treat a `nb` object like a list of vectors: nb[[i]] is a vector of indexes of objects connected to object `i`

 

BUT you have to make sure you store integers:

 

Here's a `nb` object from that question which in summary has this manyneighbours for each region:

 

> card(nb)
[1] 2 3 4 3 2 0 0

 

lets set the 6th feature to be a neighbour of the first:

 

> nb[[6]] = 1

 

then uh-oh...

 

> card(nb)
Error in card(nb) : 
  INTEGER() can only be applied to a 'integer', not a 'double'

 

same again only `as.integer`:

 

> nb[[6]] = as.integer(1)

 

and its happy:

 

> card(nb)
[1] 2 3 4 3 2 1 0

 

if you want to set the nighbours of 6 to several features:

 

> nb[[6]] = as.integer(c(1,2,3))
> card(nb)
[1] 2 3 4 3 2 3 0

 

Barry

 

 


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list