[R-sig-Geo] kernel density mean value for superimposed polygon

Marco Helbich marco.helbich at gmx.at
Mon Aug 17 08:22:52 CEST 2009


Hello,

I am sorry to post this question a second time, but I still got no solution. My starting point are the local densities from a kernel density estimation (my object called k1) using the splancs package, now I want to calculate the mean value for every superimposed polygon (called tsids). The example code below should help to reproduce my question.

Any help would be greatly appreciated!!!

Thanks and kind regards
Marco

-----------

library(rgdal); library(splancs); library(sp); 
library(maptools); library(gpclib)

# test data + window study site
sids <- readOGR(system.file("shapes/sids.shp", package = "maptools"), "sids")
crs <- CRS("+proj=longlat +datum=NAD27")
proj4string(sids) <- crs
plot(sids)
ncrs <- CRS("+proj=utm +zone=18 +datum=NAD27")
tsids <- spTransform(sids, ncrs)
plot(tsids)
win <- unionSpatialPolygons(tsids, rep("x", length(slot(tsids, "polygons"))))
proj4string(win) <- ncrs
tcoord <- cbind(coordinates(tsids)[,1], coordinates(tsids)[,2])
tpoi <- SpatialPoints(tcoord)
proj4string(tpoi) <- ncrs
plot(win)
plot(tpoi, add=T)
# define splancs objects
tpoi.pts <- as.points(coordinates(tpoi))
win.pol = as.points(list(x=win at polygons[[1]]@Polygons[[1]]@coords[,1],
  y=win at polygons[[1]]@Polygons[[1]]@coords[,2]))

# define grid, bandwith for kernel density estimation
pixs <- 15000
dimx <- ceiling(abs((bbox(win)[1,1] - bbox(win)[1,2])/pixs))
dimy <- ceiling(abs((bbox(win)[2,1] - bbox(win)[2,2])/pixs))
grd <- GridTopology(cellcentre.offset=c(bbox(win)[1,1], bbox(win)[2,1]),
  cellsize=c(pixs, pixs), cells.dim=c(dimx,dimy))
summary(grd)

# kernel density estimation
kde <- spkernel2d(tpoi.pts, win.pol, 2000, grd)
kdf <- data.frame(k0=kde)
k <- SpatialGridDataFrame(grd, data=kdf)
spplot(k, sp.layout = list(list("sp.polygons", tsids)))
k1 <- as(k, "SpatialPointsDataFrame")
k1[["x"]] <- coordinates(k1)[,1]
k1[["y"]] <- coordinates(k1)[,2]

# the following part shows my problem: how can i aggregate the kernel estimations (using the mean) for every polygon in the tsids object? using the overlay function: overlay(k1, tsids, fn = mean) ?

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser



More information about the R-sig-Geo mailing list