Steven J. Pierce pierces1 at msu.edu
Tue Jun 13 04:12:36 CEST 2006

```Hi folks,

I tried Roger's suggested method for adding extra links directly into a
neighbor list. To get it to work, I had to coerce the vector representing
the new link to be an integer vector instead of a numeric vector. Here's
what worked.

# Create an integer vector of the 2 regions (23 & 31) that need to be
connected.

ij <- as.integer(c(23,31))

nb[[ij[1]]] <- sort(unique(c(nb[[ij[1]]], ij[2])))
nb[[ij[2]]] <- sort(unique(c(nb[[ij[2]]], ij[1])))

I just wanted to share that detail back to the list in case anyone else
wants to do this sort of data manipulation.

Steven J. Pierce
E-mail: pierces1 at msu.edu

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: Saturday, June 03, 2006 7:11 AM
To: Steven J. Pierce
Cc: r-sig-geo at stat.math.ethz.ch

On Fri, 2 Jun 2006, Steven J. Pierce wrote:

> Hi folks,
>
> After using spdep to construct a neighbor list from a SpatialPolygons
> object, I plotted the neighbor list and noted that I needed one more link
> that wasn't caught by the poly2nb command. I found the edit.nb() function
> and can successfully use the interactive graphical interface to add the
> link, but would like to know if there is an alternative method for adding
a
> link between two regions. What would I need to do to directly edit the
> neighbor list object to include an additional link between regions 23 and
> 31?

manipulate the list directly. If ij is a two element vector for the

nb[[ij[1]]] <- sort(unique(c(nb[[ij[1]]], ij[2])))
nb[[ij[2]]] <- sort(unique(c(nb[[ij[2]]], ij[1])))

should do it (untried). If there are more links, have a look at drop.links
and see if you can adapt it.

>
>
> > # Create a SpatialPolygons object from a SpatialPolygonsDataFrame
object,
> this
> > # helps set up for creating a neighbor list via commands from spdep
> package
> >
> > CITY.polys <- as.SpatialPolygons.PolygonsList(CITY.ZCTA.data at polygons,
> +
> proj4string=CRS(projection.details))
> >

I think:

CITY.polys <- as(CITY.ZCTA.data, "SpatialPolygons")

should be enough, in time poly2nb() will do that internally.

> > # Create a neighbor list based on shared boundary. We need this to
> facilitate
> > # creating a weights matrix when we go to do spatial regression models.
> >
> > CITY.nb <- poly2nb(CITY.polys, row.names = NULL,
> snap=sqrt(.Machine\$double.eps), queen=TRUE)
> >
> > # Capture coordinates of ZCTA centroids into a matrix we can use in
> plotting
> > # the neighbor list.
> >
> > coords.mat <- cbind(CITY.ZCTA.data\$centroid.x,
CITY.ZCTA.data\$centroid.y)
> >

Could be:

coords.mat <- coordinates(CITY.ZCTA.data)

if the centroid.x|y values correspond to the polygon centroids.

Roger

> > # Plot the neighbor list (reveals we need to add 1 more link)
> >
> > plot(CITY.ZCTA.data, col="grey", border="black", axes=FALSE, asp=1,
> yaxs="i")
> > plot(CITY.nb, coords.mat, add=TRUE, col="black", lwd=2)
> >
> > # Add 1 link to neighbor list. This requires interaction with the
graphics
> > # on screen.
> >
> > CITY.nb <- edit.nb(CITY.nb, coords.mat, polys=CITY.polys)
> Identifying contiguity for deletion ...
> No contiguity between chosen points
> added contiguity between point 23 and 31
> Options: quit[q] refresh[r] continue[c] q
> >
> > # Plot the revised neighbor list (now everything looks right)
> >
> > plot(CITY.ZCTA.data, col="grey", border="black", axes=FALSE, asp=1,
> yaxs="i")
> > plot(CITY.nb, coords.mat, add=TRUE, col="black", lwd=2)
>
>
> Steven J. Pierce
> E-mail: pierces1 at msu.edu
>
> _______________________________________________
> 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