这是我在一些论坛中发现的常见问题。但答案很简单,就在错误消息中。 “你需要扩展你的网格”。发生这种情况是因为当您申请时getverticeshr(ud, 95)
部分多边形超出网格,因此无法获得面积。
例如,在下面的代码中,估计了两种假设动物的 KDE。我使用从 0 到 100 的随机点,因此我定义了一个 100x100 的网格(域)。
#"""
# Language: R script
# This is a temporary script file.
#"""
# 1. Packages
library(adehabitatHR) # Package for spatal analysis
# 2. Empty Dataframe
points <- data.frame(ID = double())
XY_cor <- data.frame(X = double(),
Y = double())
# 3. Assigning values (this will be our spatial coordinates)
set.seed(17)
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(0, 100, by=1.) # resolution is the pixel size you desire
y <- seq(0, 100, by=1.)
xy <- expand.grid(x=x,y=y)
coordinates(xy) <- ~x+y
gridded(xy) <- TRUE
class(xy)
# 6. Kernel Density
kud_points <- kernelUD(points, h = "href", grid = xy)
image(kud_points)
# 7. Get the Volum
vud_points <- getvolumeUD(kud_points)
# 8. Get contour
levels <- c(50, 75, 95)
list <- vector(mode="list", length = 2)
list[[1]] <- as.image.SpatialGridDataFrame(vud_points[[1]])
list[[2]] <- as.image.SpatialGridDataFrame(vud_points[[2]])
# 9. Plot
par(mfrow = c(2, 1))
image(vud_points[[1]])
contour(list[[1]], add=TRUE, levels=levels)
image(vud_points[[2]])
contour(list[[2]], add=TRUE, levels=levels)
该图显示 50% 的轮廓在网格内部,但 75% 的轮廓被剪切,这意味着该轮廓的一部分在网格之外。
如果您尝试将 KDE 的顶点估计为 50%,您将获得良好的结果:
# 10. Get vertices (It will be fine)
vkde_points <- getverticeshr(kud_points, percent = 50,
unin = 'm', unout='m2')
plot(vkde_points)
但如果你尝试使用 75% 的水平,你将得到经典错误:getverticeshr.estUD(x[[i]],percent,ida=names(x)[i],unin,中的错误:
网格太小,无法估计家庭范围。
您应该使用更大的范围参数重新运行 kernelUD
# 10. Get vertices (Will be an Error)
vkde_points <- getverticeshr(kud_points, percent = 75,
unin = 'm', unout='m2')
plot(vkde_points)
现在,你可以清楚地看到发生了什么,R 无法估计顶点到 75%,因为它们在网格之外,所以你需要增加域(网格)!这里我将域增加到 50(参见 #5. 域)
# 5. Domain HERE GRID IS INCREASED 50 AT X AND Y!!
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
kud_points <- kernelUD(points, h = "href", grid = xy)
image(kud_points)
# 7. Get the Volum
vud_points <- getvolumeUD(kud_points)
# 8. Get contour
levels <- c(50, 75, 95)
list <- vector(mode="list", length = 2)
list[[1]] <- as.image.SpatialGridDataFrame(vud_points[[1]])
list[[2]] <- as.image.SpatialGridDataFrame(vud_points[[2]])
# 9. Plot
par(mfrow = c(2, 1))
image(vud_points[[1]])
contour(list[[1]], add=TRUE, levels=levels)
image(vud_points[[2]])
contour(list[[2]], add=TRUE, levels=levels)
您可以看到所有轮廓都在网格(域)内。所以,现在您将能够估计顶点。
# 10. Get vertices
vkde_points <- getverticeshr(kud_points, percent = 75,
unin = 'm', unout='m2')
plot(vkde_points)