dbspitz / migrateR

Shared tools for analyzing animal movement behavior in R
6 stars 0 forks source link

MigrateR error in function mvmt2dt #9

Open GRoncalli opened 4 years ago

GRoncalli commented 4 years ago

Hi, I'm having some problems using mvmt2dt function in order to extract the date of departure of my individuals. Class mvmts calssified my trajectories in disperser, migrant and mixmig. When I run the three functions: mvmt2dt(migrate_duck.rnsd.1, p=0.05, mod="disperser")
mvmt2dt(migrate_duck.rnsd.1, p=0.05, mod="migrant") mvmt2dt(migrate_duck.rnsd.1, p=0.05, mod="mixmig") only the first one run right. In the other two cases, I had these errors: "Error in chol2inv(object$m$Rmat()) : 'a' must be a numeric matrix In addition: Warning messages: 1: In if (class(x) == "try-error") { : the condition has length > 1 and only the first element will be used 2: BACUCCO181633 movements overlap Error in if (jdts[2] > jdts[3]) { : missing value where TRUE/FALSE needed"

So, I tried to select just one of my individuals by this expression: mvmt2dt(migrate_duck.rnsd.1[[15]], p=0.05, mod="mixmig") but I obtained this message: "Warning messages: 1: In if (class(x) == "try-error") { : the condition has length > 1 and only the first element will be used 2: MOROSINA191680 movements overlap ".

Can anybody help me out with this issue? Thank you, All the best, Gianluca

dbspitz commented 4 years ago

Thanks, Ginaluca!

Appreciate the example code and error messages--you found a glitch in the error trapping for mvmt2dt. I've added this to my to-do list, but some other revisions I'm working on for the package may delay my ability to tackle this until toward the end of the month.

In the meantime, this should be something you can work around. It looks like the error is being triggered by bursts in "migrate_duck.rnsd.1" where the model(s) you are requesting weren't successfully fit. As a temporary fix, before running 'mvmt2dt' you could try subsetting "migrate_duck.rnsd.1" to just the bursts where the models you're looking for were successfully fit, e.g., with something like

which(sapply(migrate_duck.rnsd.1, function(z){ class(z@models$disperser)=="nls" }))

to find all of the disperser models. Hope that helps. And let me know if you run into more questions. I'll touch base once I have a chance to revise 'mvmt2dt'.

Best,

Derek Spitz Postdoc, UCSC Bishop, CA (831) 737-3120 CityOfKitesAndCrows.com http://cityofkitesandcrows.com Wilmers Lab http://wildlife.ucsc.edu // Wittmer Lab http://cms.victoria.ac.nz/sbs/research/ecology-biodiversity-research/ecology-and-conservation-biology

On Tue, Apr 14, 2020 at 8:14 AM GRoncalli notifications@github.com wrote:

Hi, I'm having some problems using mvmt2dt function in order to extract the date of departure of my individuals. Class mvmts calssified my trajectories in disperser, migrant and mixmig. When I run the three functions: mvmt2dt(migrate_duck.rnsd.1, p=0.05, mod="disperser") mvmt2dt(migrate_duck.rnsd.1, p=0.05, mod="migrant") mvmt2dt(migrate_duck.rnsd.1, p=0.05, mod="mixmig") only the first one run right. In the other two cases, I had these errors: "Error in chol2inv(object$m$Rmat()) : 'a' must be a numeric matrix In addition: Warning messages: 1: In if (class(x) == "try-error") { : the condition has length > 1 and only the first element will be used 2: BACUCCO181633 movements overlap Error in if (jdts[2] > jdts[3]) { : missing value where TRUE/FALSE needed"

So, I tried to select just one of my individuals by this expression: mvmt2dt(migrate_duck.rnsd.1[[15]], p=0.05, mod="mixmig") but I obtained this message: "Warning messages: 1: In if (class(x) == "try-error") { : the condition has length > 1 and only the first element will be used 2: MOROSINA191680 movements overlap ".

Can anybody help me out with this issue? Thank you, All the best, Gianluca

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dbspitz/migrateR/issues/9, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACTZKUTZP7SEX6OROVHKAYLRMR4UFANCNFSM4MH2KA6A .

briwinkel commented 1 year ago

Has there been any update on this issue? I am also getting this error.

Thank you! -Bri

eldritchmayo commented 1 year ago

I am having a simmilar error:

> mvmt2dt(Base.migrater2, mod = "mixmig") Error in if (class(x) == "try-error") { : the condition has length > 1

I get it even when the model for all the tracks fitted sucesfully.

I tried reruning the model without the problematic tracks and the issue persists for some models.

mod = "disperser" is the only one that runs succesfully.

eldritchmayo commented 1 year ago

I have narrowed down the issue to the theta2 function which is used in mvmt2dt. The rest of the computations used in the command work fine (phi, phi2 and theta). I have been able to curcumanvigate the issue by computing the values seperately:

get.theta <- function(x, mod="mixmig"){
btbl <- coef(x@models[[mod]])
theta <- btbl["theta"]
return(theta)}

get.theta2 <- function(mvmt, mod = "mixmig") {
  res <- lapply(mvmt, function(v) {
    if (mod %in% names(v@models)) {
      m.fit <- v@models[[mod]]
      try(car::deltaMethod(coef(m.fit), "theta+2*phi+rho+2*phi2", vcov(m.fit))[1])
    } else {
      NA
    }
  })
  names(res) <- sapply(mvmt, attr, "burst")
  return(res)
}

get.phi<- function(x, mod="mixmig"){
  btbl <- coef(x@models[[mod]])
  phi <- btbl["phi"]  
  return(phi)}

get.phi2 <- function(x, mod="mixmig"){
  btbl <- coef(x@models[[mod]])
  phi2 <- btbl["phi2"]
  return(phi2)}

You can then use lapply or for get.theta2 directly on the mvmtlistobject. Replace mixmig with migrant depending on what you need. After that you can use those four values and the formulas used in mvmt2dt, which you can find in the source code - the jdts values, to compute start and end dates.

I did it with a lot of clunky, my-data-specific code, so maybe it is best to do the rest based on what you need.

Hope this helped