MCP家庭范围重叠

时间:2014-03-31 18:13:27

标签: r

我正在尝试通过adehabitatHR软件包中的MCP和kernelUD方法估算一批动物的家庭范围重叠,我估计了家庭范围。我意识到有一个函数用于计算与内核方法的重叠,但有人能告诉我如何最好地计算MCP的重叠吗?

我收集rgeos包有一个gIntersection函数,我已经开始玩了。 有没有人想出一个相对简单的方法/代码 - 无论是在adehabitatHR,regeos还是其他地方?

3 个答案:

答案 0 :(得分:1)

也许这样的事情会有所帮助:

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)

答案 1 :(得分:1)

当您有许多动物或许多项圈时,一次运行单对的重叠可能非常低效。我确信有更简洁的方法可以做到这一点(我欢迎有关改进的建议!),但这对我有用:

#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)中,变量定义如下(调整名称以适合您的数据):

  • file_folder:领奖期/兴趣会议
  • 动物:个人的唯一标识符
  • Easting / Southing:你的lat长坐标(确保根据需要调整投影)

答案 2 :(得分:0)

我修改过一些约翰内斯&#39;回答,也许它会有所帮助:

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)

输出是每个交点的矩阵,要获得总重叠,您只需要在没有自相交的情况下对行或列求和。最后,您可以添加或删除仅修改初始矩阵的个人。

一切顺利,