[R-sig-Geo] problem with edit.nb

Michał Kwieciński jamesbond6 at gmail.com
Thu Aug 26 20:39:47 CEST 2010

2010/8/26 Roger Bivand <Roger.Bivand at nhh.no>:
> On Thu, 26 Aug 2010, Michał Kwieciński wrote:
>> 2010/8/26 Roger Bivand <Roger.Bivand at nhh.no>:
>>> On Thu, 26 Aug 2010, Michał Kwieciński wrote:
>>>> Hi all,
>>>> I am just about to finish my thesis. The spatial model I want to use
>>>> there is an extension of some work I did back in April. I used R 2.9.2
>>>> then and in order to include 3 additional administrative areas for
>>>> Poland, I edited the shp files (the borders aren't perfectly aligned).
>>>> Then in R I created the nb class object and edited it with edit.nb
>>>> adding three new connections. Everything worked perfect, I had no
>>>> regions with no links and I generated weight matrices with no
>>>> problems.
>>>> However, I'd been doing exactly the same thing entire night in R 2.11
>>>> and it did not work (I use the same code I did 4 months ago) and I
>>>> have no idea what is the reason for it. I've been looking for some
>>>> other way to do it, I tried nb2mat and editing the matrix, but I
>>>> surrendered having no idea where and what values I should use.
>>>> Before editing nb object R claims that regions 377 and 378 have no
>>>> links. However in edit.nb the 378 and 379 are visible as having no
>>>> links (378 and 379 are cities added on top of bigger shapes, whereas
>>>> 377 was just split from a bigger shape into two smaller ones and only
>>>> the link between these two parts is missing). I connect the circles,
>>>> quit and in the new object there are some new links - the overall
>>>> number has increased - but 377 and 378 are still listed as having no
>>>> links. Editing nb again shows the links, so they have been saved for
>>>> sure.
>>>> I am not an advanced R user and most of my code was based on my
>>>> professor's book. However, I think I have spent enough time with
>>>> spatial models and those matrices in order to call this problem really
>>>> weird. Especially since it worked perfectly last time...
>>>> I can attach shp files and my code if it will be of any help in order
>>>> to properly investigate this problem. I would really appreciate some
>>>> help, I need to finish the project over the weekend.
>>> Maybe you are using the wrong indices, as FIDs are 0-base but nb objects
>>> are
>>> 1-base. So you may be editing the wrong ones. If this doesn't resolve the
>>> problem, zip the shapefile and post a link to it, don't attach the
>>> shapefile, as it would be sent to 1700 people.
>> I must admit I did not understand your hint (I do not know what "base"
>> is, assuming FID is Field ID - header in shp file). How is it possible
>> I edited some other layer of information by function edit.nb? Could
>> you please clarify what should I do to check it?
> Google "0-based" gets you to Wikipedia:
> "0 (zero-based indexing)
>    The first element of the array is indexed by subscript of 0.
> 1 (one-based indexing)
>    The first element of the array is indexed by subscript of 1."
> So the FIDs in the shapefile are 0, ..., (n-1), and identify the
> observations, so are set in the region.id attribute of the nb object. Then
> if print(nb) says that "377" and "378" have no neighbours, and the region.id
> values are from the shapefile:
> which(card(nb) == 0)
> will likely say 378 379, and
> attr(nb, "region.id")[which(card(nb) == 0)]
> will say "377" "378".
> The indices used internally in edit.nb are the 1-based indices. They
> probably should be the ones stored in the region.id attribute, but this
> would involve an extra level of indexing. If you don't understand, put the
> shapefile on a website and post the link.
Ok, now I understand. This is true in my case - it explains why I see
different numbers in listing of "no-links regions" and on the map in
edit.nb. That brings me only to the main question: why, even after
connecting the nodes (and verifying they are connected with plot.nb),
the print(nb) still says that those two regions remain unconnected?


I uploaded here the code I use, maps and two screens explaining where
to look for the missing links in question.


More information about the R-sig-Geo mailing list