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

Roger Bivand Roger.Bivand at nhh.no
Thu Aug 26 21:19:05 CEST 2010


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:
>>
>>> 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?
>
> http://home.elka.pw.edu.pl/~mkwiecin/edit.nb-problem.rar
>
> I uploaded here the code I use, maps and two screens explaining where
> to look for the missing links in question.

Good, now the underlying problem is clear. 1-based units 90 and 379 (to 
take that case first) share no boundaries with any polygon. You need to 
add the 379 polygon to 90 as a hole first. Then poly2nb() will work 
properly. As it is, 379 has no boundaries with any observation, because it 
is simply dropped on top of the map. 378 also seems to have been dropped, 
but not on 377, it landed on 249. For the first case, try:

pols <- slot(pow, "polygons")
p90 <- pols[[90]]
p379 <- pols[[379]]
crds379 <- slot(slot(p379, "Polygons")[[1]], "coords")
crds379
rcrds379 <- crds379[nrow(crds379):1,]
Pl379 <- Polygon(rcrds379)
slot(p90, "Polygons") <- list(slot(p90, "Polygons")[[1]], Pl379)
pols[[90]] <- p90
slot(pow, "polygons") <- pols
pow.nb<-poly2nb(pow)
pow.nb

Once you find out where 377 should be (it lies on 249), do the same, and 
you will be able to work from your repaired shapefile.

Roger


>
> Thanks,
> Michal
>
> _______________________________________________
> 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


More information about the R-sig-Geo mailing list