[R-sig-Geo] subset grid and neighbour list

Roger Bivand Roger.Bivand at nhh.no
Tue Jan 10 08:42:58 CET 2006


On Mon, 9 Jan 2006, Ronnie Babigumira wrote:

> Dear listers
> This may be obvious however I have thrown in the towel after numerous futile attempts. Here is my problem. I have a grid 
> with 5 levels including NA. I would like to perform join count stats on it. Prof. Bivand advised me to either add NA as 
> a factor, or subset the grid and eliminate the NA's (I feel the later makes more sense)
> 
> I think the steps I need to take are
> 
> 1. Drop the NA's in mygrid (and save the ids of the cells dropped)
> 
> 2. subset the neighbors list using information from step1
> 
> Is there anyone familiar with this especially step 1

library(spdep)
set.seed(20060110)
input <- data.frame(expand.grid(x=1:10, y=1:10), z=rpois(100, 3))
coordinates(input) <- c("x", "y")
table(input$z)
is.na(input$z) <- input$z < 1 | input$z > 5
table(input$z)
input$z <- as.factor(input$z)
rook <- dnearneigh(coordinates(input), 0, 1)
rook
plot(rook, coordinates(input))
joincount.multi(input$z, nb2listw(rook, 
style="B"))
# Error in joincount.multi(input$z, nb2listw(rook, style = "B")) : 
#        NA in factor
keep <- !is.na(input$z)
rook_sub <- subset(rook, subset=keep)
input_sub <- subset(input, subset=keep)
rook_sub
# look out for no-neighbour cells - if present, use zero.policy=TRUE below
plot(rook_sub, coordinates(input_sub))
joincount.multi(input_sub$z, nb2listw(rook_sub, style="B"))

This is for a full grid, but if you have SpatialPixels or SpatialPolygons 
the order of operations will be the same, I think. You need to watch out 
for no-neighbour cells after subsetting, and use zero.policy=TRUE in both 
joincount.multi() and nb2listw() if they have been introduced.

Roger


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