Struggling to run moveHMM using lognormal function in parallelised routines

Viewed 23

I am attempting to run a two state HMM using a lognormal distribution. I have read Michelot and Langrock (2019) regarding choosing starting parameters through inspecting the data in a histogram and then running iterations in parallel, which has worked for my gamma distribution. Identifying the starting parameters for the lognormal distribution is troubling me however. Do I plot the log of my step length distribution then attempt extracting starting parameters or use the same starting parameters as my gamma distribution and rely on stepDist="lnorm"?

My code for the lognormal attempt currently looks like this:

ncores <- detectCores() - 1
cl <- makeCluster(getOption("cl.cores", ncores))

clusterExport(cl, list("data", "fitHMM"))

niter <- 20

allPar0 <- lapply(as.list(1:niter), function(x) {
  stepMean0 <- runif(2,
                     min = c(x,y),
                     max = c(y,z))
  stepSD0 <- runif(2,
                   min = c(x,y),
                   max = c(y,z))
  angleMean0 <- c(0, 0)
  angleCon0 <- runif(2,
                     min = c(a,b),
                     max = c(a,b))
  stepPar0 <- c(stepMean0, stepSD0)
  anglePar0 <- c(angleMean0, angleCon0)
  return(list(step = stepPar0, angle = anglePar0))
})

# Fit the niter models in parallel
logP <- parLapply(cl = cl, X = allPar0, fun = function(par0) {
  m <- fitHMM(data = data, nbStates = 2, stepDist = "lnorm", stepPar0 = par0$step,
              anglePar0 = par0$angle)
  return(m)
})

# Extract likelihoods of fitted models
likelihoodL <- unlist(lapply(logP, function(m) m$mod$minimum))
likelihoodL

# Index of best fitting model (smallest negative log-likelihood)
whichbestpL <- which.min(likelihoodL)

bestL <- logP[[whichbestpL]]
bestL

If I use negative values from plotting the log of the step length of the data then I get the error:

Error in checkForRemoteErrors(val) : 7 nodes produced errors; first error: Check the step parameters bounds (the initial parameters should be strictly between the bounds of their parameter space).

If I use the same starting parameter values that I used for my gamma distribution then I get the error

Error in unserialize(node$con) : embedded nul in string: 'X\n\0\0\0\003\0\004\002\0\0\003\005\0\0\0'

Please could someone shed some light on how I'm failing at this? Thank you!

1 Answers

Unfortunately, I can't tell for sure what the problem is from the code you included. If you don't get an error when you run fitHMM outside of parLapply, then it suggests that the problem is in how you choose the values of x, y, and z in your code.

The first parameter of the log-normal distribution can be negative or positive, and it is actually the mean of the logarithm of the step length. So, to find good starting values for this, you should look at a histogram of the log step lengths (e.g., following the dedicated moveHMM vignette). The second parameter is the standard deviation of the log step lengths, and this should be strictly positive (but could also be chosen based on the spread of the histogram of log step lengths).

To summarise, you should choose all the initial values based on plots of the log step lengths (rather than the step lengths themselves), and you should not use the same ranges of values for stepMean0 and stepSD0 (because the former can be negative or positive, whereas the latter is positive). Hopefully, this should help you choose x, y, and z.

Related