[R-sig-Geo] Neighborhood weights spam matrix

Roger Bivand Roger.Bivand at nhh.no
Thu Feb 18 14:36:57 CET 2010


On Thu, 18 Feb 2010, Nikhil Kaza wrote:

> Hello,
>
> I am not sure if this is the right forum to ask as this is mostly a sparse 
> matrix question, nevertheless here it is.
>
> I am using R - 2.10.1 and spdep 0.4-54 on Windows Vista
>
> I have a large shapefile for which I need to create a neighborhood matrix 
> file, I used the following command to create a neighborhood matrix
>
> shp_file <- readShapePoly(file.choose())
> grid_nb_mat <- as.spam.listw(nb2listw(poly2nb(shp_file, 
> row.names=shp_file$CELL_ID)))
>
> Now I would like to subset this matrix based on a subset of the shapefile and 
> I have a vector of CELL_IDs. However, I cannot figure out a way to assign row 
> and column names to grid_nb_mat, so that I can subset this matrix. Could 
> someone point me in the right direction? If it is were a simple matrix 
> row.names and colnames would work.

If you had saved the output of poly2nb as an nb object, you could have 
used the subset method for that class. You are using default row 
standardisation in the nb2listw() step, so only subsetting grid_nb_mat - 
by using "[" with keep vectors:

keep <- which(shp_file$CELL_ID "... to keep")

kgrid_nb_mat <- grid_nb_mat[keep, keep]

will not have correct row sums. str(grid_nb_mat) shows no row or column 
names stored (probably to save space). If possible start again from 
poly2nb() and save the output object. Otherwise:

example(columbus)
col.listw <- nb2listw(col.gal.nb)
col.sp <- as.spam.listw(col.listw)
str(col.sp)
keep <- c(1:20, 25:40)
kcol.sp <- col.sp[keep, keep]
apply(kcol.sp, 1, sum)
str(kcol.sp)
kcol.sp at entries <- rep(1, 150)
apply(kcol.sp, 1, sum)
rsum <- apply(kcol.sp, 1, sum)
kkcol.sp <- diag.spam(1/rsum) %*% kcol.sp
apply(kkcol.sp, 1, sum)

Roger

>
> regards
> Nikhil
>
> _______________________________________________
> 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