[R-sig-Geo] help in constructing spatial weight matrix

Roger Bivand Roger.Bivand at nhh.no
Tue Apr 3 17:11:55 CEST 2012


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
>

-- 
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



More information about the R-sig-Geo mailing list