[R-sig-Geo] problem with listw2sn...

Roger Bivand Roger.Bivand at nhh.no
Wed Nov 7 08:22:24 CET 2007


On Tue, 6 Nov 2007, Carson Farmer wrote:

> List,
>
> I am trying to create a GWT file from a shapefile, and all is fine up to
> listw2sn, at which point the region number ids are lost.
> Here is the code for reference, maybe someone can tell me where I went
> wrong?
>
> library(rgdal)
> library(spdep)
> vec <- readOGR(dsn = ".", layer = layerName)
> coords <- coordinates(vec)
> dist <- dnearneigh(coords, 0, 200, row_names, FALSE)
> dlist <- nbdists(dist, coords, FALSE)
> idlist <- lapply(dlist, function(x) 1/x)
> weights = nb2listw(dist, dlist, W, FALSE)
> sn <-listw2sn(weights)
> sn2gwt(sn, outName)
>
> This outputs a GWT file with region ids from 1 to (in this case) 127, not
> those which are specified above(row_names).

As you can see from the documentation, and:

> args(write.sn2gwt)
function (sn, file, shpfile = NULL, ind = NULL)

there is no provision for writing out the region.id attribute, which is 
passed through by listw2sn() - see str(sn). Since your rownames are 
arbitrary, that is not keyed to a given ID field in the shapefile, the 
1-based indices ought to be sufficient, and certainly make it easier to 
write out the output data frame.

You could try to patch write.sn2gwt() to do this, adding an argument, 
which by default is FALSE, but if TRUE replaces the values in sn[,1] and 
sn[,2] with attr(sn, "region.id")sn[,1] etc., and then modify the 
as.data.frame(sn) operation to avoid converting the character region.id to 
factors. A well tried patch would be welcome, but I don't think that it is 
worth the trouble, unless the software on the other side of the GWT knows 
what to do with the values.

Roger

> I've checked (summary) dist,
> dlist, idlist, and weights, and they all contain the right region ids, and
> I've checked (printed it out) sn, and it does not: so the only thing I can
> think of is that they are being lost at listw2sn.
>
> Note: I am running R 2.4.1 through the rpy python bindings in qgis (free
> open source gis).
>
> Carson Farmer
> Spatial Pattern Analysis & Research Lab (SPAR)
> Department of Geography, University of Victoria
> Victoria, BC, Canada
> web: www.geog.uvic.ca/spar
> email: cfarmer at uvic.ca
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> 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