SpatialFiltering in spdep: How to create an 'nb' object from an imported spatial weights matrix
Dear Roger, it works now, many Thanks! Timo -----Urspr?ngliche Nachricht----- Von: Roger Bivand [mailto:Roger.Bivand at nhh.no] Gesendet: Sonntag, 15. Februar 2009 23:00 An: Timo Mitze Cc: r-sig-geo at stat.math.ethz.ch Betreff: Re: AW: [R-sig-Geo] SpatialFiltering in spdep: How to create an 'nb' object from an imported spatial weights matrix
On Sun, 15 Feb 2009, Timo Mitze wrote:
Dear Roger and List, thanks for your advice. I tried it with the code as (as well as some related with different style parameter etc.): ******************************** x <- mat2listw(mat) lw <- nb2listw(x$neighbours, style="B", zero.policy=TRUE) migfilt <- SpatialFiltering(lnmr_i ~ 1, data=migration_spatial, nb=lw, style="B", ExactEV=TRUE, zero.policy=TRUE) ******************************** ... but unfortunately (always) got the following error message: ******************************** Fehler in card(neighbours) : INTEGER() can only be applied to a 'integer', not a 'character' ******************************** Can it be the problem the the nb object is still missing, since I try to directly create a listw object?
x$neighbours is the nb object, just use that. Roger
According to Yongwan Chun (one of the author of Spatial Filtering), the routines needs an nb and NOT(!!!) a listw object. Can I generate such an nb object from my binary 240x240 matrix? Greetings, Timo -----Urspr?ngliche Nachricht----- Von: Roger Bivand [mailto:Roger.Bivand at nhh.no] Gesendet: Sonntag, 15. Februar 2009 13:31 An: Timo Mitze Cc: r-sig-geo at stat.math.ethz.ch Betreff: Re: [R-sig-Geo] SpatialFiltering in spdep: How to create an 'nb' object from an imported spatial weights matrix 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
-- 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