An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20120612/eede4b75/attachment.pl>
help in constructing spatial weight matrix
2 messages · Fabricio Vasselai, Roger Bivand
On Tue, 12 Jun 2012, Fabricio Vasselai wrote:
Dear list, Given a following up to this question, I have myself a somewhat similar question to that of Alsulami. I have a shp of Finnish cities and would like to generate for them a weight matrix of neighborhood based on inverse distance (for further use in Moran's I calculus). Here is the code I've been thinking of: UF.shp <- readShapePoly(myshpfile.shp, IDvar="OBJECTID") UF.nb <- poly2nb(UF.shp, queen=TRUE) dsts <- nbdists(UF.nb, coordinates(UF.shp)) idw <- lapply(dsts, function(x) 1/(x/1000)) UF.listw <- nb2listw(UF.nb, glist = idw, style="B", zero.policy=TRUE) But it does not work and I can't figure how to fully adapt the idea to the shp I have. Anyone please could give me any tips?
Always provide the error messages, the output of sessionInfo(), and demonstrate the problem in a reproducible way with built-in data sets. If myshpfile.shp is not a character string, then the call to readShapePoly() will fail - did you mean: "myshpfile.shp"? Roger
Best, FABRICIO 2012/4/3 Roger Bivand <Roger.Bivand at nhh.no>
On Tue, 3 Apr 2012, dwl Al_Sulami wrote: Dear there
I am trying to construct spatial weight matrix using inverse distance, which means that high weight is given to near observations and ,vies versa, where w_ij=w_ji and w_ii=0, Since, I am a new R user, I've tried many functions in sp,spdep packages and I find some functions to find weight matrix by using nearest neighbours and by distance-based neighbours, but I want to use all observations as neighbours and based on the inverse distance, I can construct the weights. Before I write the code I'll give some information about my data, I have the coordinates of 51 states (US states) , thus latitude and longitude of each states and based on the distance between each state I will give a weight for states where the weight is the the inverse of the distance. obviously , the weight matrix are symmetric. Then I've tried the following code data(location)# data.frame with 2 col and 51 row loc.sp = SpatialPoints(cbind(location$**latitude,location$longitude)) sy8<-knn2nb(knearneigh(loc.sp,**k=50)) # for each state all other states are considered as neighbour dsts<-nbdists(sy8, loc.sp) idw<-lapply(dsts, function(x) 1/(x)) sy<-nb2listw(sy8, glist=idw, style="B") col.mat <- listw2mat(sy) after running this code, I got symmetric matrix (51* 51), where w_ii=0 and w_ij=w_ji but I am not sure if this is true!!!
What do you mean by true - correct? There is at least one problem, that
you are not ensuring that the distances are computed by Great Circle. More
or less reproducing your code (data(locations) must be wrong):
library(maps)
sts <- map("state", fill=TRUE, plot=FALSE)
library(maptools)
IDs <- sapply(strsplit(sts$names, ":"), function(x) x[1])
SPl <- map2SpatialPolygons(sts, IDs=IDs,
proj4string=CRS("+proj=longlat +datum=WGS84"))
crds <- SpatialPoints(coordinates(SPl)**,
proj4string=CRS("+proj=longlat +datum=WGS84"))
is.projected(crds)
library(spdep)
nbs <- dnearneigh(crds, 0, 10000)
# either crds a SpatialPoints object !is.projected(), or use longlat=TRUE
nbs
dsts <- nbdists(nbs, crds)
# either crds a SpatialPoints object !is.projected(), or use longlat=TRUE
summary(unlist(dsts))
# in km
idw <- lapply(dsts, function(x) 1/(x))
sy <- nb2listw(nbs, glist=idw, style="B")
col.mat <- listw2mat(sy)
all.equal(col.mat, t(col.mat), check.attributes=FALSE)
I think that your "distances" were planar measures in geographical
coordinates.
Hope this helps,
Roger
any commands and helps are appreciated
Regards,
Alsulami
[[alternative HTML version deleted]]
______________________________**_________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
-- Roger Bivand Department of Economics, NHH Norwegian School of Economics, 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
______________________________**_________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
Roger Bivand Department of Economics, NHH Norwegian School of Economics, 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