我正在尝试估计一批动物之间的家乡范围重叠,我已经通过adehabitatHR包中的MCP和kernelUD方法估计了它们的家乡范围。我知道有一个用内核方法计算重叠的函数,但谁能告诉我如何最好地计算MCP的重叠?
我知道rgeos包有一个gIntersection函数,我已经开始尝试使用它了。有没有人想出一个相对简单的方法/代码--无论是在adehabitatHR、regeos还是其他地方?
发布于 2014-04-01 04:49:39
也许像这样的东西会有帮助:
library(adehabitatHR)
library(rgeos)
## Use some simulated data
ani1 <- SpatialPoints(matrix(rnorm(200, mean=2), ncol=2))
ani2 <- SpatialPoints(matrix(rnorm(200, mean=1), ncol=2))
## Function to calculate overlaps
gOverlap <- function(hr1, hr2, ...) {
a <- gIntersection(hr1, hr2, ...)
if (is.null(a)) {
return(0)
}
gArea(a, byid=TRUE) / gArea(hr1, byid=TRUE)
}
## Calcualte homeranges
hr1 <- mcp(ani1)
hr2 <- mcp(ani2)
## Calculate HR overlap
gOverlap(hr1, hr2)发布于 2017-02-03 01:01:52
当您有许多动物或许多领口会话时,一次为单对运行重叠可能是非常低效的。我相信有一种更干净的方法可以做到这一点(我欢迎关于改进的建议!),但这对我很有效:
#Relevant libraries (I think they're all here!)
library(adehabitatHR)
library(geosphere)
library(rgeos)
library(nlme)
#Define an overlap function for a single dyad
dyad.overlap <- function(cp,dyads.df){
p1<-subset(cp, cp@data$id==dyads.df[1])
p2<-subset(cp, cp@data$id==dyads.df[2])
Overlap<- ifelse(is.null(gIntersection(p1,p2)), 0, gArea(gIntersection(p1,p2)) / gArea(p1)) #puts 0 if no overlap between the dyad
return(Overlap)
}
#Define a function for overlap of all animals from a given time period
mcp.overlaps <- function(period.df){
period.df$Animal<-factor(period.df$Animal) #remove any ids not relevant to that period
count <- length(unique(period.df$Animal)) #identify number of individuals for the period
anim <- unique(period.df$Animal) #identify names of individuals/periods
xy<-SpatialPointsDataFrame(period.df[c("Easting","Southing")], data=data.frame(id=period.df$Animal)) #create SPDF
proj4string(xy)<-CRS("+init=epsg:32750") #define projection
cp <- adehabitatHR::mcp(xy, percent=95) #create Spatial Polygons Data Frame of the 95% MCPs
writeOGR(obj=cp, dsn="tempdir", layer=paste(period.df$file_folder[i],"95 Percent MCPs"), driver="ESRI Shapefile") #export shp of 95% MCPs for period, if desired
dyads<-(combn(anim, 2, simplify=T)) #all possible dyad combinations
dyads.df<-data.frame(A1=dyads[1,1:count],A2=dyads[2,1:count]) #creates a data frame of the pairs
dyads.df$Overlap<-apply(dyads.df, 1, dyad.overlap, cp=cp)
dyads.df$Period<-period.df$file_folder[1]
return(dyads.df)
}
#Now run the overlap for each time period included in the data frame
All.Overlaps<-do.call(rbind.data.frame, gapply(df, groups=df$file_folder, FUN=mcp.overlaps))当然,如果你只有一个时间段(例如,一年的数据和每年的家庭范围),你所要做的就是:
All.Overlaps <- mcp.overlaps(df)这假设所有数据都在单个数据框(df)中,变量定义如下(调整名称以适合您的数据):
interest
发布于 2017-04-20 23:35:13
我对约翰斯的答案做了一点修改,也许这会更有帮助:
library(adehabitatHR)
library(rgeos)
data(puechabonsp)
rel <- puechabonsp$relocs
cp <- mcp(rel[,1])
## Set Up a matrix which will store the results
mat <- matrix(NA,4,4,dimnames=list(c("1","2","3","4"),
c("1","2","3","4")))
## Set Up the loop
gOverlap <- function(hr, number, matrix){
for(i in c(1:number)){
for(j in c(1:number)){
a <- gIntersection(hr[i,], hr[j,])
if (is.null(a)){
matrix[i, j] <- 0
} else{matrix[i, j] <- gArea(a)}
}
}
return(matrix)
}
##Test the function
gOverlap(cp, 4, mat)
##Plotting to corroborate
plot(cp)输出是一个具有每个交集的矩阵,要获得总重叠,您只需对没有自交集的行或列求和即可。最后,您只需修改初始矩阵即可添加或删除个人。
万事如意,
https://stackoverflow.com/questions/22769074
复制相似问题