[R-sig-Geo] help with spatial neighbors/weights

Roger Bivand Roger.Bivand at nhh.no
Thu Jan 15 19:22:24 CET 2009


On Thu, 15 Jan 2009, Michael Denslow wrote:

> Dear R-sig-geo,
>
> I am fairly new to R and very new to spatial regression models. I am 
> attempting to compare an OLS model to some sort of spatial regression 
> such as SAR. However, I am unclear about how to properly designate the 
> spatial neighbors and weights and hope that someone can provide 
> guidance.
>
> My data set consists of variables taken from a series of irregularly 
> shaped and spaced polygons, some are spatial nested (one completely 
> inside another). I have approximate centroids for the polygons as well 
> as some values for the max and min latitude and longitude of each. I am 
> using the centroids in the example below. I have been using Bivand et 
> al. (Applied Spatial Data Analysis with R) as a guide but my data is a 
> bit different then the examples provided.
>
> I am attempting to make a weight file based on the distance based 
> neighbors. I have pasted some example code below. My thought was that 
> using distance based neighbors would associate the nested as well as 
> some of the areas that are close by. This method seems to leave me with 
> some areas without neighbors. This seems to cause a problem when I 
> attempt to convert the 'nb' to the 'listw'.
>
> Can anyone provide me with guidance as to how to proceed or how to best 
> associated the areas in my data set. Thank you in advance for any help!
>
>
> library(spdep)
>
> my.lat <- c(36.4000, 36.3908, 35.0550, 36.0226, 36.4472, 36.1450, 35.2680, 35.1335,
>     35.3225, 35.0050, 35.0450, 35.0600, 35.0500, 35.6120, 35.4330, 36.4580,
>     35.8850, 35.7647, 35.8842, 35.6135, 36.3915, 35.1290, 35.4769, 34.9881,
>     35.4267, 35.5986, 35.8927, 35.4721, 35.4684, 35.6802, 35.6413, 36.2405,
>     35.3650, 35.8183, 36.5495, 35.7250, 36.1955, 35.5960, 35.6300, 35.8930,
>     35.2360, 35.1400, 35.1970, 36.5000, 35.1540, 35.5432, 35.8898, 35.5389,
>     35.8641, 35.6340, 35.4560, 35.3630, 35.4270, 35.7080, 35.7930, 36.1020,
>     35.0127, 35.1380, 35.1310, 35.5689, 35.5417, 35.5206, 35.5634, 35.5157,
>     35.9020, 35.8975, 35.8843, 35.8872)
>
> my.long <-  c(-81.4614, -81.5717, -83.4550, -81.8264, -81.0608, -81.8070, -82.4525,
>     -79.8415, -82.5035, -83.1400, -83.0000, -82.9500, -82.9500, -83.7800, -82.2475,
>     -81.4715, -81.8815, -82.2653, -78.5282, -79.1614, -81.0700, -79.1500, -76.8119,
>     -79.5515, -80.0516, -81.6531, -78.5834, -76.9099, -78.9180, -80.9803, -80.5191,
>     -77.8615, -80.0825, -78.8575, -76.3840, -78.7000, -78.8034, -79.1300, -83.5200,
>     -79.0300, -77.8800, -79.2200, -77.8700, -76.1245, -79.1400, -79.3216, -79.0232,   
>     -79.2881, -78.7555, -83.4000, -83.1300, -82.9200, -82.7600, -82.3700, -82.2900,
>     -82.1100, -82.8264, -81.3900, -81.3700, -83.6629, -83.4933, -83.8661, -83.7328,
>     -83.8830, -79.0390, -79.0305, -79.0469, -79.0166)
>
>
> my.coords <- as.matrix(cbind(my.long,my.lat))
> nb40km <- dnearneigh(my.coords,0,40, longlat = TRUE)
> plot(my.coords)
> plot(nb40km, my.coords, add = TRUE)

plot(SpatialPoints(my.coords, proj4string=CRS("+proj=longlat")),
   axes=TRUE)
plot(nb40km, my.coords, add = TRUE)

is probably more legible, because it corrects for stretching.

>
> nb50km.w <- nb2listw(nb40km, glist=NULL, style="W", zero.policy=TRUE)
> # this causes an error because of the regions with no neighbours found
>

The nb2listw() completes OK, but you need to use the zero.policy=TRUE in 
all subsequent functions needing this specified

You could include all by increasing the distance threshold to:

k1nb <- knn2nb(knearneigh(my.coords, k=1, longlat=TRUE))
max(unlist(nbdists(k1nb, my.coords, longlat=TRUE)))

but a graph-based neighbour scheme may suit you better - see ?graph2nb.

Hope this helps,

Roger

PS. Thanks for a clear report with example data - makes the problem much 
clearer than just a description.

> I am using R 2.8.1, spdep 0.4-29 on Windows XP
>
>
> Michael Denslow
>
> I.W. Carpenter Jr. Herbarium [BOON]
> Appalachian State University
> Boone, North Carolina U.S.A.
>
> -- AND --
>
> Communications Manager
> Southeastern Regional Network of Expertise and Collections
> sernec.org
>
>
>
>
>
> _______________________________________________
> 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