rsf.fit - NA in underlying rasters

200 views
Skip to first unread message

Ryan Gill

unread,
Aug 4, 2022, 9:32:46 PM8/4/22
to ctmm R user group
Hi Chris,

I'm having trouble with rsf.fit run on UDs created from ctmm_fit. I'm using the following code:

j = 1 #test on one individual
  cilla <- DATA[[j]]
  cilla.ud = uds[[j]]

RESULTS <- ctmm::rsf.fit(cilla,
                         UD = cilla.ud,
                         R = rlist,
                         integrated = TRUE,
                         level.UD = 95,
                         error = TRUE)


I have 5 underlying rasters I'm using for the rsf (in rlist). There are no NA values in the rasters, and nodata in the raster is set to -3.40282E+38.

The problem I'm having is that rsf.fit produces the error: 

Error in ctmm::rsf.fit(cilla, UD = cilla.ud, R = rlist, integrated = TRUE,  :  NA values in sampled rasters.

But I'm fairly confident that I've dealt with NA and nan values in the underlying rasters. Furthermore, if I look at one of the UDs that produces this error, it's right in the middle of the extent of the rasters. 

I've downloaded the most recent ctmm package from Github.

It's highly likely I'm blundering this somewhere, but I'm not sure what else to try to get this to work. 

Many thanks for any guidance!

Ryan.

Christen Fleming

unread,
Aug 5, 2022, 7:29:26 AM8/5/22
to ctmm R user group
Hi Ryan,

level.UD is not being used here, but should be less than 1, like 0.95 instead of 95, but I don't think that matters here.

If you can make a dropbox folder for for me with a minimal working example (data + script) then I can see what the issue is. That error is not intended unless one of the sampled locations returns an NA from a raster. NAs can exist outside of the data and are ignored with the default method.

Best,
Chris

Jesse Alston

unread,
Aug 5, 2022, 12:49:52 PM8/5/22
to Christen Fleming, ctmm R user group
Hi Ryan,

I suspect this is a coordinate system issue and the extent of the raster is way too small--I have gotten many errors like this, even when the coordinates seem OK. Use plot(DATA, R=raster) to check rather than checking the extents by hand, and I suspect you will find that rsf.fit() is trying to interpret lat/long as meters or something.

Chris can correct me if I'm wrong, but I think rasters need lat/long data for rsf.fit().

Jesse

--
You received this message because you are subscribed to the Google Groups "ctmm R user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ctmm-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ctmm-user/d2cb0e41-7d84-4eaa-a0b4-612594462da5n%40googlegroups.com.


--

Christen Fleming

unread,
Aug 5, 2022, 4:51:26 PM8/5/22
to ctmm R user group
plot(DATA, R=raster) is a good way to check if the data and raster are in the same place. The raster should only need to be in long-lat with the non-default argument integrator="Riemann",  but I can easily fix that if anyone needs it. With default arguments, it shouldn't matter what projection the rasters are in relative to the data. The projections of the rasters do need to be correctly stored in those objects, though.

Best,
Chris

Ryan Gill

unread,
Aug 5, 2022, 8:32:06 PM8/5/22
to Christen Fleming, ctmm R user group
Hi Chris and Jesse,

Thanks for these solutions. I’ll check in more detail when I’m back at my computer. The points used to generated the UDs are lat/long, rasters are NAD83 Albers. Extracting values from the rasters at each point worked fine, but I’ll try transforming the rasters to WGS84 and see if that resolves the error.

Regards,

Ryan.

Sent from my iPhone

On Aug 5, 2022, at 1:51 PM, Christen Fleming <chris.h...@gmail.com> wrote:


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/2LVntBK3UDE/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/f05bdc27-8f07-4141-837f-761857580e7cn%40googlegroups.com.

Christen Fleming

unread,
Aug 6, 2022, 7:55:27 PM8/6/22
to Ryan Gill, ctmm R user group
Thanks Ryan,

Please tell me if that works, because it shouldn't matter.

Best,
Chris

Sarah Comer

unread,
Aug 22, 2022, 4:05:08 PM8/22/22
to ctmm R user group
Hi Chris & Ryan

I wondered if you had managed to sort this error out?
The same popped up for me after processing three animals that worked perfectly, with three rasters in the list.
I've checked them all and they look fine with NA values, all are in the same projection.
When I tried to run RSF.fit again with the individual rasters they were fine.  
Thanks in advance for any suggestions you might have!!

Cheers
Sarah

>  RSF.c12M06 <- rsf.fit(DATA,UD=UD,R=list(TRI=TRI_SS, Fire12=Fire_2012, WATER = WATER),trace=TRUE)
RSF code is isotropic for the moment.
Maximizing likelihood.
Calculating Hessian.
Calculating REML gradient.
Calculating REML Hessian.
Error in rsf.fit(DATA, UD = UD, R = list(TRI = TRI_SS, Fire12 = Fire_2012,  :
  NA values in sampled rasters.

Christen Fleming

unread,
Aug 22, 2022, 5:27:01 PM8/22/22
to ctmm R user group
Hi Sarah,

In Ryan's case, he had re-imported the data differently at a later point in the calculation so that his telemetry object and UD object were in two different projections, which I had not coded for the possibility of... since UD objects are calculated directly from telemetry objects and come in the same projection.

If you are getting NAs at your sampled locations and shouldn't be, then feel free to setup a dropbox with a minimal working example (data + script) and I will take a look at it.

Best,
Chris

Ryan Gill

unread,
Aug 22, 2022, 8:43:01 PM8/22/22
to Sarah Comer, ctmm R user group
Hi Sarah,

Using projection(UD) = projection(data) resolved the issue for me. However, I was also running into a data storage issue which produced the same error after running through several animals, so it might also be worth checking where your R temp directory is and that you have plenty of room on that drive.

Sent from my iPhone

On Aug 22, 2022, at 1:05 PM, Sarah Comer <sarahc...@gmail.com> wrote:

Hi Chris & Ryan

Sarah Comer

unread,
Aug 23, 2022, 8:22:48 AM8/23/22
to ctmm R user group
Thanks Chris & Ryan
The rasters were ok when I first started running through these animals, which is why I'm a bit confused.
I have changed anything except for the individual ID. 
Had a look at the data storage as suggested by Ryan but this appears to be fine.
Thanks for the offers of help, very much appreciated!
Sarah

Christen Fleming

unread,
Aug 24, 2022, 7:33:53 PM8/24/22
to ctmm R user group
Hi Sarah,

I'm finding NA values along the southern edge of your data. I plotted the data & raster in both raster and ctmm to confirm. This is pretty easy to check in ctmm now, like:

NAS <- is.na(RASTER)
plot(DATA,R=NAS)

Also, you were using the second best model for calculations with FIT <- FITS[[2]] rather than the AIC-best model, which would be FIT <- FITS[[1]]
I'll check my materials to make sure there isn't anything confusing that might cause that.

Best,
Chris

Sarah Comer

unread,
Aug 25, 2022, 9:43:58 AM8/25/22
to ctmm R user group
Hi Chris

Thankyou!! The model fit makes perfect sense, that was a slip on my part.

With the rasters thought they were all masked properly.. a novice mistake (again!), 
I've checked them all again and masked to the coast to remove the NA values there.
For those that do have NAs the values were  -3.40282346639e+38 (which was what I'd originally set). Should they be something else?

I've also tried Ryan's suggestion to set the projection but it's still falling over with the NA in underlying rasters, inconsistently.
Have checked, and there is apparently plenty of free memory.
I've just run another 2 animals with no issues, then the next one came up with both the 'NA in underlying rasters' and another message below.

And this message with the projection.

Error in (function (cl, name, valueClass)  :
  ‘crs’ is not a slot in class “UD”


Thanks heaps for trying to help, I feel like I'm going around in circles, and apologies if I'm missing something really obvious!
Cheers
Sarah

Christen Fleming

unread,
Aug 25, 2022, 4:20:33 PM8/25/22
to ctmm R user group
Hi Sarah,

I'm not sure how things import by default, but you can always fix the NA values with something like:

NAV <- -3.40282346639E+38
RASTER[RASTER <= NAV] <- NA

When I checked your example, the problem wasn't inconsistent projections. That shouldn't happen unless you import the data multiple times in different ways.

If you load raster, then it will take over the projection() function, and there isn't anything that I can do about that because it doesn't define a generic function, but there should be a warning issued by R.
So with raster loaded, you have to call ctmm::projection() for ctmm objects.

Regarding the remaining NA values, I would make sure that your rasters extend far beyond your data. It might be helpful to use

ctmm::extent(UDS,complete=TRUE,level=0.99,level.UD=0.99)

to get a large long-lat extent.

Best,
Chris

Sarah Comer

unread,
Aug 28, 2022, 7:33:02 PM8/28/22
to ctmm R user group
Hi Chris and Ryan
Thanks very much for your help with this - the memory was an issue and with the projection fix and NA code it has been running smoothly.
Really appreciate the support!!  
Cheers
Sarah
Reply all
Reply to author
Forward
0 new messages