I have a code that generates polygons from a MCMC model. For each iteration in the model, I get a polygon of the 95% credible interval for the area. The idea here is that if I generate 10 polygons, I'm able to compute the area for each and then calculate the average area for all polygons.
So,
- is it possible to compute the average polygon from all the polygons generated?
- Would this "average polygon" be of equivalent size to the average area of all polygons?
Here is the code:
# 1. Packages
library(adehabitatHR)
par(mfrow=c(1,1))
kernel.poly.all = NULL
# 2. Assigning values
set.seed(13456789)
for (potatoe in 1:10) {
# 3. Empty Dataframe
points <- data.frame(ID = double())
XY_cor <- data.frame(X = double(),
Y = double())
for(i in c(1:100)){
if(i >= 50){points[i, 1] <- 1}
else {points[i, 1] <- 2}
XY_cor[i, 1] <- runif(1, 0, 100)
XY_cor[i, 2] <- runif(1, 0, 100)}
# 4. Transform to SpatialDataframe
coordinates(points) <- XY_cor[, c("X", "Y")]
class(points)
# 5. Domain
x <- seq(-50, 150, by=1.) # resolution is the pixel size you desire
y <- seq(-50, 150, by=1.)
xy <- expand.grid(x=x,y=y)
coordinates(xy) <- ~x+y
gridded(xy) <- TRUE
class(xy)
# 6. Kernel Density
locs = SpatialPoints(points)
kud_points <- kernelUD(locs, h = "href", grid = xy)
# image(kud_points)
kernel.ref <- kernelUD(xy = locs,
# grid = xy ,
grid = 250,
extent = 10,
# same4all = TRUE,
h = "href")
area.save = kernel.area(x = kernel.ref,
unin = 'm', unout='m2',
percent = 95)
kernel.poly <- getverticeshr(kernel.ref,
percent = 95,
unin = 'm', unout='m2')
kernel.poly.all = c(kernel.poly.all,kernel.poly)
if(potatoe==1){
plot(kernel.poly,
border = "black",
lwd=1,
axes = TRUE,
xlim = range(x),
ylim = range(y))
}
if(potatoe>1){
plot(kernel.poly,
border = "black",
lwd=1,
add = TRUE)
}
print(potatoe)
}
The object kernel.poly.all
contains the 10 iterations of loop, so a total of 10 polygons.
The code above was derived from this question and adapted to make a reproducible example.