这是我在一些论坛中发现的常见问题。但答案很简单,并且完全在错误消息中。“你需要扩展你的网格”。发生这种情况是因为当您应用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, : The grid is too small to allow家庭范围的估计。您应该使用更大范围参数重新运行 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.Domain)
# 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)