[R-sig-Geo] SpatialFiltering in spdep: How to create an 'nb' object from an imported spatial weights matrix

Roger Bivand Roger.Bivand at nhh.no
Sun Feb 15 12:59:51 CET 2009


On Sat, 14 Feb 2009, Timo Mitze wrote:

> Dear List,
>
> I have a complete (distance/border based binary) spatial weights matrix
> from Stata (e.g. as a txt-file).

Try using read.dta() in foreign of the exported Stata matrix. This reads a 
data.frame, which you coerce to an R matrix with as.matrix().

> I want to apply spatial filtering in
> spdep, however the routine needs an "nb" object. Is there any way to
> extract the information from my matrix automatically (something like
> mat2nb ???) or do I have to edit the "nb" object by hand? (e.g. in terms
> of creating a GAL file by any text editor and then use "read.gal" option
> - this strategy would mean a lot of work since I have a 240 x 240
> matrix). I'd be happy about any help.

Next use mat2listw() in spdep to convert to a listw object, finally using:

x <- mat2listw(mat)
lw <- nb2listw(x$neighbours, glist=x$weights, style="?")

where ? is your choice of style. mat2listw() just does the convsersion, 
assigning a style of "M", which may not be recognised by model fitting 
functions. Drop the glist= argument if the weights are without importance 
- as I understand they are from your description of them as binary.

Hope this helps,

Roger

>
> Best wishes,
>
> Timo
>
> P.S.: I have tried it with a spatial weights matrix in spdep
> (mat2listw), but it does not work - see my code below:
>
> ****************************************************
>
> #### Code for Griffith Eigenvector Filtering ###
>
>
>
> library(spdep)
>
>
>
> migration_spatial <-
> read.table("C:/migration_laptop/spdep_migration.txt", header=TRUE,
> sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
>
>
>
> summary(migration_spatial)
>
>
>
> mat = matrix(0, 240, 240)
>
>
>
> mat[row(mat) >= col(mat)] <-
> scan("C:/migration_laptop/spdep_Wmat_Chun_border.txt")
>
>
>
> # not sure if the following is needed :
>
>
>
> mat <- mat + t(mat)
>
>
>
> list(mat)
>
>
>
> migmat.listw <- mat2listw(mat)
>
>
>
> migmat.listw
>
>
>
> migfilt <- SpatialFiltering(lnmr_i ~ lnmr_i_lag1 + ldwager_ij_lag1 +
> ldur_ij_lag1 + ldyrl_ij_fd_lag1 + ldq_ij_lag1 + ldpland_ij_lag1 +
> ldhc6_ij_lag1, data=migration_spatial, nb=migmat.listw, style="W",
> ExactEV=TRUE, zero.policy=TRUE)
>
>
>
> ****************************************************
>
>
>
>
>
>
>
>
> 	[[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