Trouble using akde as shapefile

461 views
Skip to first unread message

Malena Candino

unread,
Feb 18, 2021, 5:14:38 PM2/18/21
to ctmm R user group

Hi Chris,

I'm trying to work with the akde contours to be able to filter locations and extract only those that fall within the 95% home range. I have calculated the akde's, and wrote them as a shapefile using for example: writeShapefile(AKDE$G01_Winter_2019, folder, file=NULL,level.UD=0.95,level= FALSE) I set level to false because I don't want the confidence intervals.
When I preview this shapefile in QGIS, it looks exactly what it should look like, and I don't have the confidence intervals. If I read that shapefile back into r as an 'sf' to mask locations I don't want, it has the confidence intervals again for some reason, and it also appears to be distorted. If I read it as a spatial polygon data frame it also has the confidence intervals back, and I can't seem to be able to plot the locations on top of it.
Any ideas as to how to best do this and why I may be encountering these problems would be really useful!
I tried attaching my code, the shapefile and the locations I'm using- in case they helped- but I wouldn't let me post this (I can share by regular email if it's useful)

Thank you,

Malena.

datos <- read.csv("G01_ctmm.csv",header=T)
head(datos)
str(datos)
#---------------#
# create AKDE---#       
#---------------#

datos$id<-datos$Burst
burst<-as.telemetry(dat, projection = CRS("+proj=utm +zone=19 +south +datum=WGS84"))

#plot all guanacos, and ind guanacos
plot(burst)
COL <- rainbow(length(burst))
plot(burst,col=COL)
# look back at G01
COLG01 <- color(burst$G01_Winter_2019,by="time")
plot(burst$G01_Winter_2019,col=COLG01)

SVF <- list()
for(i in 1:length(burst)){
  print(i)
  SVF[[i]] <- variogram(burst[[i]])
}
names(SVF) <- names(burst)
zoom(SVF$G01_Winter_2019)
## fit models to all animals
FIT <- list()
for(i in 1:length(burst)){
  print(i)
  GUESS <- ctmm.guess(burst[[i]],interactive=FALSE)
  FIT[[i]] <- ctmm.select(burst[[i]],GUESS,verbose=TRUE,trace=2)
}
names(FIT) <- names(burst)

AKDE <- list()
KDE <- list()
for(i in 1:length(burst)){
  print(i)
  AKDE[[i]] <- akde(burst[[i]],FIT[[i]][[1]])
  IID <- ctmm.fit(burst[[i]],ctmm(isotropic=TRUE))
  KDE[[i]] <- akde(burst[[i]],IID)
}
names(AKDE) <- names(burst)
names(KDE)<-names(burst)
# AKDE versus KDE area
summary(KDE$G01_Winter_2019)
summary(AKDE$G01_Winter_2019)

plot(AKDE$G01_Winter_2019)
# comparison plot
EXT <- extent(list(burst,AKDE$G01_Winter_2019))
par(mfrow=1:2)
plot(burst$G01_Winter_2019,AKDE$G01_Winter_2019,ext=EXT)
title("AKDE")
plot(burst$G01_Winter_2019, KDE$G01_Winter_2019,ext=EXT)
title("KDE")

par(mfrow=c(1,1))
folder <- paste(getwd(),"/shp-output2", sep="")
plot(AKDE$G01_Winter_2019)
str(AKDE$G01_Winter_2019)
writeShapefile(AKDE$G01_Winter_2019, folder, file=NULL,level.UD=0.95,level= FALSE)

cont<-st_read("G01_Winter_2019.shp")
cont<-cont[2,]# I don't know how else to loose the CI
plot(cont)
winter<-subset(datos, datos$id== "G01_Winter_2019")
coordinates(winter) <- c("long","lat")
proj4string(winter) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
plot(winter, add=TRUE) # when adding my locations I see the HR polygon appears to be distorted
pol<-shapefile("G01_Winter_2019.shp")
plot(pol)
plot(winter, add=TRUE) #nothing happens





Christen Fleming

unread,
Feb 18, 2021, 8:02:10 PM2/18/21
to ctmm R user group
Hi Malena,

I don't think level=FALSE would turn off the CIs, I think it would just set them to 0%, which would make them infinitesimally narrow. Also, it sounds like you would rather want to export the AKDE to SpatialPolygonsDataFrame (sp) format and then to sf. I haven't gotten around to making a single function call to export directly to sf, but it's pretty easy. Then, with either an sp or sf format object, it should be pretty easy to extract the point-estimate polygon that you want to work with.

It looks like your plotting at the end is not in ctmm. The distortion could be because you are plotting in long-lat, rather than a locally flat projection? On most of the globe, plotting long-lat is distorted unless you do some calculations to figure out what the correct aspect ratio should be.

Best,
Chris

Christen Fleming

unread,
Feb 19, 2021, 2:58:32 PM2/19/21
to ctmm R user group
I coded up sf exporting with the as.sf() function on GitHub. Feel free to provide feedback on how well that works or what it needs.

Best,
Chris
Reply all
Reply to author
Forward
0 new messages