grid argument in the akde function

508 views
Skip to first unread message

Matthew Butler

unread,
Oct 16, 2020, 11:30:09 AM10/16/20
to ctmm R user group
Perhaps I misunderstand the grid argument in the akde function but I thought that if grid was equal to a consistent extent for all akde's then all akde's would be placed on a common grid.

# CTMM version 0.5.11  

In my case, I am using an extent named ee1.

> ee1
             x         y
min -142294.49 -26822.12
max   58180.37 103601.34

I then run all akde's with grid=ee1.

> akde1=akde(data[1],fitModels[1],grid=ee1)
> akde2=akde(data[2],fitModels[2],grid=ee1)

But when I run the overlap function it returns an error that "inconsistent grid resolutions" or "non-conformable arrays" depending upon which version of ctmm I am using

> overlap(c(akde1,akde2),level=0.95)
Error in grid.intersection(UD) : Inconsistent grid resolutions.

Sure enough the grid extents are not the same?

> ee1
             x         y
min -142294.49 -26822.12
max   58180.37 103601.34

> extent(akde1)
            x         y
min -18102.06  3153.251
max -11164.60 12370.169

> extent(akde2)
             x         y
min -21819.422 -15817.64
max   8333.301  14886.21 

Any idea what I am doing wrong?  Is there a different way in which the grid argument should be defined?

I have over 150 akde's to produce and have ran them using a foreach command to fully utilize my computers resources and make processing time greatly reduced.  I understand that if I run:

> akde1=akde(data[1:2],fitModels[1:2])

that these two akde's will be placed on the same grid.

Any help is much appreciated.

Best,

Matt Butler

Christen Fleming

unread,
Oct 17, 2020, 1:11:31 PM10/17/20
to ctmm R user group
Hi Matt,

The grid argument can take a lot of different options, but with just an extent it isn't going to be fully specified, because the resolutions can still differ. I checked my code and, not being fully specified, the extents weren't being set exactly, so I fixed that on GitHub, but it's probably not what you wanted. You probably wanted to do something more like grid=list(extent=ee1,dr=500), for instance, if you wanted 500 meter resolution. The newer code on GitHub is also more memory efficient in that you can do something like grid=list(dr=500,align.to.origin=TRUE), to calculate overlaps with smaller grids with cells that line up but the grids don't have to be all encompassing any longer.

The extent() function on UD objects has confidence and coverage level arguments, because it's designed more for lining up plots, so those aren't going to necessarily going to return the same values. What you would want to check is the range of UD$r$x and UD$r$y, which are the grid coordinates, but with the newer code on GitHub only the resolution and offset needs to be the same.

Best,
Chris

Matthew Butler

unread,
Oct 19, 2020, 10:14:50 AM10/19/20
to Christen Fleming, ctmm R user group
Thanks Chris, I look forward to trying out the more memory efficient grid=list(dr=500,align.to.origin=TRUE) argument.

Best,

Matt  

--
You received this message because you are subscribed to a topic in the Google Groups "ctmm R user group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ctmm-user/Xg8XMg35GRw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ctmm-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ctmm-user/4c083667-96b6-4ed2-b811-0a752f050093o%40googlegroups.com.


--

----------------------------------------------------------------------
Matthew J. Butler

Christen Fleming

unread,
Oct 19, 2020, 10:19:42 AM10/19/20
to ctmm R user group
Hi Matt,

In case I wasn't clear enough, you can try that right now by installing the development version of the package via devtools::install_github("ctmm-initiative/ctmm")

Best,
Chris
To unsubscribe from this group and all its topics, send an email to ctmm-user+unsubscribe@googlegroups.com.

Kristin Engebretsen

unread,
Sep 22, 2023, 4:44:31 PM9/22/23
to ctmm R user group
Hi Chris,

I am struggling with a similar problem, and this thread seems to be closest to my issue. I'm hoping you can help me figure out this "inconsistent grid resolution" mismatch! 

I have generated seasonal/annual home ranges for 50 mountain lions, and I want to compare the overlap between a cougar's summer and winter home range and also between it's winter range in one year to the next. 

For example, I have two UD objects named EM01_Win19 and EM01_Sum19 for an individual's summer and winter range for 2019. They have the same projection but I cannot calculate overlap because of the grid issue. 


> ctmm::projection(EM01_Sum19)
[1] "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"

> ctmm::projection(EM01_Win19)
[1] "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"

> ctmm::projection(EM01_Win19) == ctmm::projection(EM01_Sum19)
[1] TRUE
>
> extent(EM01_Sum19)
           x         y
min 24706.31 -6562.023
max 38730.28 10042.835
> extent(EM01_Win19)
           x         y
min 21071.03 -3756.547
max 47165.12 18541.262
>
> EM01 <- list(EM01_Sum19,EM01_Win19)
> ctmm::overlap(EM01, level=0.9, debias = TRUE)

Error in grid.intersection(UD) : Inconsistent grid resolutions.

Can I retro-fit these fitted UDs to the same grid so that I can calculate overlap?

Thanks so much for your help!
To unsubscribe from this group and all its topics, send an email to ctmm-user+...@googlegroups.com.

Christen Fleming

unread,
Sep 22, 2023, 7:19:34 PM9/22/23
to ctmm R user group
Hi Kristin,

The easiest way to calculate your UDs in the same grid is to calculate them in one call like akde(TELEMETRY_LIST,FIT_LIST). This will work fine as long as the home ranges aren't radically different sizes.

Best,
Chris

Kristin Engebretsen

unread,
Sep 22, 2023, 7:55:43 PM9/22/23
to ctmm R user group
Hi Chris,

Thanks so much for your response! I'm very grateful that you are so active on here and so helpful! :D 

However, I was afraid of that being your answer. Is there a way to do it after the UDs are already calculated? I am calculating a HR for each cat in winter and summer for several years, so I have 187 home ranges already created. I did these in a batch for each site, and it took several hours to run them all :D I would love to not have to re-run all that code! I just finished writing some code that brings all the AKDE results back in from saved lists and creates the polygons for the 95% UDs, rasterizes the density surface from the PMF, and pulls the area of the UD from summary() so I can look at the differences in HR size and intensity of use between season_years for each individual. But, ideally, I would also like to calculate the pairwise overlap between HRs from summer to winter or from year to year to help identify shifts over time.  Any way to do this without rerunning everything?

Thanks again for your help!!

Christen Fleming

unread,
Sep 22, 2023, 7:57:46 PM9/22/23
to ctmm R user group
Hi Kristin,

There is a way to do it individually with the grid argument, but not after the fact - sorry.

The model fits usually take the longest amount of time and you can reuse those objects.

Best,
Chris

Kristin Engebretsen

unread,
Sep 22, 2023, 10:11:25 PM9/22/23
to ctmm R user group
Bummer!! Glad I didn't waste more time trying to finagle it then :D 

Follow up question for when I rerun these models then! This is how I fit the AKDEs yesterday: 


MantiWinter.telem <- ctmm::as.telemetry(MantiWinter,  drop = FALSE)   

man.win.guess <- lapply(MantiWinter.telem, FUN = ctmm.guess, interactive = FALSE)
length(m.man.win.guess)  ## 31 season-years in manti winter
M.OUF_Lst_Mantiwin <- list()
AKDE_Lst_Mantiwin <- list()
# fit ctmm model for each guy
for(i in 1:length(MantiWinter.telem)){
  M.OUF_Lst_Mantiwin[[i]] <- ctmm.select(MantiWinter.telem[[i]], m.man.win.guess[[i]])
  AKDE_Lst_Mantiwin[[i]] <- akde(MantiWinter.telem[[i]],M.OUF_Lst_Mantiwin[[i]] )  ## akde
}

saveRDS(AKDE_Lst_Mantiwin, "SeasonalAKDE/AKDE_Manti_winter.rds")

I  created these datasets clumped by site because there are far enough apart that I created all my spatial rasters separately (for a different analysis). If I combine these all  and rerun the AKDE, should that save the problem? My sites are pretty spread out in Utah so I didn't think fitting them all to the same grid would be efficient. 

Thanks again!! 

Kristin Engebretsen

unread,
Sep 23, 2023, 12:59:50 AM9/23/23
to ctmm R user group
Oh, thinking about it, I can still fit each site separately cause I don't need to compare across study sites! That should help! We will try it tonight and see!

Christen Fleming

unread,
Sep 23, 2023, 2:42:36 PM9/23/23
to ctmm R user group
Hi Kristin,

Running everything at once will not put them on the same grid, but on grids of the same resolution and alignment (extents can differ). This will work fine unless you have tiny and large home ranges in the same study, where the tiny resolutions applied to the largest home ranges will eat all of your RAM. I have to add some code to be able to handle that in an automated way.

You can reuse the fits,  M.OUF_Lst_Mantiwin, as long as they were calculated in the same projection.

Best,
Chris

Kristin Engebretsen

unread,
Sep 25, 2023, 3:39:51 PM9/25/23
to ctmm R user group
Hi again, Chris,

I am back to trying to sort out this issue. I reran my AKDE fits with each site together, such that the winter and summer home ranges were run on a grid of the same resolution and alignment. I have pulled out two UD objects to try to run the overlap function. However, I am still having the same problem with inconsistent grid resolutions. The projections appear to be the same, but the resolutions aren't.

ctmm::projection(EM01_Sum20) == ctmm::projection(EM01_Win20)
[1] TRUE

> extent(EM01_Sum20)
           x         y
min 18309.61 -8973.308
max 39235.01  8866.493

> extent(EM01_Win20)
           x         y
min 17264.62 -12219.39
max 60396.23  22143.19

If I plot the two UDs, they look like they should be on the same grid, so I don't quite understand how to fix this. If there are a way to modify the extent of one to match the other?

Screen Shot 2023-09-25 at 1.36.16 PM.png

Christen Fleming

unread,
Sep 25, 2023, 7:23:26 PM9/25/23
to ctmm R user group
Hi Kristin,

How did you run akde() here? Did you run it once on a list of telemetry objects and list of model-fit objects? There is an example in help('overlap').

Best,
Chris

Kristin Engebretsen

unread,
Sep 25, 2023, 11:40:39 PM9/25/23
to ctmm R user group
Hi Chris,

I generated a list of telemetry objects for each individual (MantiWinter.telem), fit an automated movement model guess for each one, and used that list of model fits and the telemetry a for loop to fit the AKDE function. I am looking at the help page and see that these were run with lapply instead a for loop (like I used) to iterate through individuals. 

MantiWinter.telem <- ctmm::as.telemetry(MantiWinter,  drop = FALSE)   
man.win.guess <- lapply(MantiWinter.telem, FUN = ctmm.guess, interactive = FALSE)
M.OUF_Lst_Mantiwin <- list()
AKDE_Lst_Mantiwin <- list()
for(i in 1:length(MantiWinter.telem)){
  M.OUF_Lst_Mantiwin[[i]] <- ctmm.select(MantiWinter.telem[[i]], m.man.win.guess[[i]])
  AKDE_Lst_Mantiwin[[i]] <- akde(MantiWinter.telem[[i]],M.OUF_Lst_Mantiwin[[i]] )  
}

I think I might now be realizing the issue. If my code iterates through the ctmm.select and then the AKDE for each individual (each i), it's not actually fitting all the AKDEs in one call... is it?

Christen Fleming

unread,
Sep 26, 2023, 1:25:39 AM9/26/23
to ctmm R user group
Hi Kristin,

Yes, those calls to akde() are independent and so the grid resolutions will not come out to be the same unless you force them to be with the grid argument.

You want to call akde(MantiWinter.telem,M.OUF_Lst_Mantiwin) instead.

Best,
Chris

Kristin Engebretsen

unread,
Sep 26, 2023, 12:03:41 PM9/26/23
to ctmm R user group
I started that process last night, and ran it overnight on an HPC system, but it appeared to time out before finishing. I did save the model-fit objects, so we are trying the call to AKDE() again. Here is the modified code structure:

manti.guess <- lapply(MantiCats.telem, FUN = ctmm.guess, interactive = FALSE)

FITS.manti <- lapply(1:length(MantiCats.telem),
               function(i) ctmm.select(MantiCats.telem[[i]],manti.guess[[i]]) )

UDs_Manti <- akde(MantiCats.telem,FITS.manti)

I modeled the code off the example in help("overlap") to make sure it would be correct. The AKDE() function is currently running, so hopefully we will have some UDs that allow me to calculate the overlap soon! Thanks so much for your help! This is a great package and I'm super excited to be able to use it! 

Kristin Engebretsen

unread,
Sep 26, 2023, 3:43:43 PM9/26/23
to ctmm R user group
Whooo it worked! Brilliant! Thanks so much, Chris! I'm happy to be able to generate the overlaps and the corresponding confidence intervals with this method! So cool!

Kristin Engebretsen

unread,
Oct 6, 2023, 5:45:05 PM10/6/23
to ctmm R user group
Hi Chris,

Follow up to my overlapping home ranges question! 

I found that many of my cats constrict or expand their home range seasonally, but don't actually shift very far. So a low overlap percentage doesn't necessarily tell me that they moved to a new area, just that their two home ranges overlap minimally. Attached plot show 4 years of winter - to - summer overlap comparisons for the same cat. Only in 2023 does it look like she actually shifted her range. However, if I just use a cutoff of the overlap value to make decisions about shifts, I would have wrongly classified 2022 as a shift too. 

Allca14_byyearranges.png

My next thought was that I should look at mean centroid shifts between home ranges as part of the decision process to classify if a cat shifted vs expanded/constricted (if their overlap is small). 

I used ctmm::distance on the FITS objects for each site (as we talked about above), like this:
books_distances <- ctmm::distance(FITS.books, method="Euclidean",  sqrt=TRUE) 

However, all the values are absolutely massive. I tested both  sqrt=TRUE and  sqrt=FALSE, which gave me the same values. So I am not sure if the output units for this are in meters or sq meters or something completely different. After cleaning up the resulting matrices so I only had pairwise comparisons between the same individuals's various seasons, I divided the values by 1e+6 (assuming it was in sq m) to get sq km. 
But comparing the overlap values and the distance results make no sense. For example, one cat (CA07) has an overlap of 93.6% between her Winter 2019 and Summer 2019. However, the distance given for that same pairwise comparison is 2.219973e+10, which (if is supposed to be meters), would be 22,199.73 sq km. That is clearly not correct. 

I think this means I did something wrong in fitting the distance() function, so I was hoping you could see a glaring issue that I missed :D 
Thanks for all your help!!

Kristin

Christen Fleming

unread,
Oct 6, 2023, 7:55:48 PM10/6/23
to ctmm R user group
Hi Kristin,

That output would be in meters - I will update the documentation.

I would try updating the package. There were several updates to distance() in the last version. If that doesn't work, then I can take a look at the case that sounds crazy.

Best,
Chris

Kristin Engebretsen

unread,
Oct 9, 2023, 11:48:08 AM10/9/23
to ctmm R user group
I updated the package and reran the distance function, and I am getting the same results. 

If you are willing to look at this, I would greatly appreciate it! What is the easiest way to share the data with you?

Christen Fleming

unread,
Oct 9, 2023, 12:21:50 PM10/9/23
to ctmm R user group
Hi Kristin,

You can message me a minimal working example (data + script) that will reproduce the error.

Best,
Chris

Kristin Engebretsen

unread,
Oct 9, 2023, 12:45:08 PM10/9/23
to ctmm R user group
Great thanks!

Kristin Engebretsen

unread,
Oct 9, 2023, 12:51:58 PM10/9/23
to ctmm R user group
I'm sorry, I've never used these groups before, and I can't figure out how to attach script and files to a message.

Christen Fleming

unread,
Oct 9, 2023, 2:09:26 PM10/9/23
to ctmm R user group
Hi Kristin,

You can just shoot me an email.

Best,
Chris

Kristin Engebretsen

unread,
Oct 10, 2023, 11:23:16 AM10/10/23
to ctmm R user group
Great, I sent an email to an address I found on a paper of yours, to @si.edu. Is that a good email to use?

Christen Fleming

unread,
Oct 10, 2023, 3:07:13 PM10/10/23
to ctmm R user group
Hi Kristin,

In your script you have sqrt=FALSE, which will output square meters. Square rooted and dividing by 1000, I get point estimates ranging between 0-15km, which matches with their plots.
Setting sqrt=TRUE for output in meters and dividing by 1000, I get point estimates also ranging between 0-15km (slightly different values because of debiasing).

If you are getting something very different after updating, the Github installer probably failed to overwrite the old files. I would remove the package, restart R, and then re-install the package.

Best,
Chris

Kristin Engebretsen

unread,
Oct 10, 2023, 7:03:51 PM10/10/23
to ctmm R user group
Hi Chris, 
I just uninstalled the package, restarted R, and then reinstalled the package. I am still getting huge numbers when I run this function, which are the same whether or not sqrt is TRUE or FALSE.  This is partial output from the test data I sent you, when sqrt=TRUE.

, , est

           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,]          0 1569701596  913291774 2687374260 2044582567   71135161
[2,] 1569701596          0   38264192   81861250  137041451 1202632905
[3,]  913291774   38264192          0   40061927    6935265 1330491303
[4,] 2687374260   81861250   40061927          0   12371691 2396113921
[5,] 2044582567  137041451    6935265   12371691          0 2491078805
[6,]   71135161 1202632905 1330491303 2396113921 2491078805          0

However, if I take the sqrt of the resulting values and then divide by 1000, they make much more sense! I don't understand why the sqrt argument doesn't change the value estimation, but, if we assume the output is coming in square meters, then the numbers make more sense. 

Thanks for your help,
Kristin

Christen Fleming

unread,
Oct 10, 2023, 11:10:35 PM10/10/23
to ctmm R user group
Hi Kristin,

Are you changing the sqrt argument in distance() and re-running it? That's literally all that I had to change in the script you sent me, and the results made sense with both sqrt=FALSE (as square meters) and sqrt=TRUE (as meters), which I summarized.

Best,
Chris

Kristin Engebretsen

unread,
Oct 11, 2023, 12:33:19 AM10/11/23
to ctmm R user group
Yes, that is exactly what I did! I don’t know how or why it is providing me with the same result whether the sqrt argument in ctmm::distance() is TRUE or FALSE, but it is. I checked in both the script I sent you and also in my main working script with all the data. However, now that you confirmed that the result I am getting is actually in sq m, I can easily convert to m and km, and I have useable results now. I was thinking perhaps there is a glitch in the version of ctmm I  have, but I deleted and reinstalled it so it should have the most updated version. Very strange. Regardless, this works now and I’m quite pleased! I’m grateful for all of your help!

Christen Fleming

unread,
Oct 11, 2023, 8:20:22 PM10/11/23
to ctmm R user group
Hi Kristin,

I can only suspect that there is a file locking issue happening, and that you need to delete the package, shutdown R, go into your package directory and make sure the ctmm folder is deleted, reboot your computer, startup R in a new workspace, and then re-install ctmm. Alternatively, you could try a different computer to make sure there's not something weird in your script causing this.

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