在并行化流程中使用对数正态分布运行moveHMM的起始参数配置疑难
Let’s break down your problem step by step, starting with how to choose valid initial parameters for a log-normal HMM, then fix the errors you’re seeing.
1. Correct Initial Parameter Selection for Log-Normal Distributions
Unlike gamma distributions (which use shape/scale parameters), log-normal distributions are parameterized by mu (mean of the logged data) and sigma (standard deviation of the logged data). This means you can’t directly repurpose gamma parameters—they belong to a completely different parameter space.
Here’s the right approach:
- First, transform your step data by taking the natural log:
log_steps <- log(your_data$step_column) # Replace with your actual step column name - Calculate summary stats of this logged data to set realistic bounds for your initial parameters:
log_mean <- mean(log_steps, na.rm = TRUE) log_sd <- sd(log_steps, na.rm = TRUE) - For your two-state HMM, assign distinct ranges to each state:
- State 1 (e.g., "resting" with small steps): Set mu values slightly below the overall log mean
- State 2 (e.g., "moving" with large steps): Set mu values slightly above the overall log mean
- Sigma values for both states should stay within a reasonable positive range (based on your log_sd, e.g., 0.5×log_sd to 1.5×log_sd)
2. Fixing the "Parameter Bounds" Error
The error Check the step parameters bounds happens because your initial sigma values are likely non-positive (log-normal sigma must be strictly positive) or fall outside the valid parameter space.
Looking at your code:
stepSD0 <- runif(2, min = c(x,y), max = c(y,z))
If x or y are ≤ 0, you’ll generate invalid sigma values. Update this to use strictly positive bounds based on your logged step data’s standard deviation (like the example above).
3. Fixing the "Embedded Nul in String" Error
This serialization error occurs because you’re using gamma-distribution parameters (shape/scale) as log-normal parameters (mu/sigma). These parameters are incompatible—gamma shape is a positive value, but log-normal mu can be any real number, and sigma must be positive. Mismatched parameter types break the parallel cluster’s data serialization.
The fix is simple: never reuse gamma parameters for log-normal HMMs. Build initial parameters specifically for the log-normal distribution using the logged step data stats.
Revised Code Example
Here’s how to adjust your code with valid initial parameters:
library(moveHMM) # Assuming you're using moveHMM for fitHMM library(parallel) # Preprocess step data to get log stats log_steps <- log(data$step) # Replace "step" with your actual column name log_mean_overall <- mean(log_steps, na.rm = TRUE) log_sd_overall <- sd(log_steps, na.rm = TRUE) # Set up parallel cluster ncores <- detectCores() - 1 cl <- makeCluster(getOption("cl.cores", ncores)) clusterExport(cl, list("data", "fitHMM")) niter <- 20 allPar0 <- lapply(as.list(1:niter), function(x) { # State 1: lower mu (small steps), State 2: higher mu (large steps) stepMean0 <- c( runif(1, min = log_mean_overall - 1, max = log_mean_overall), runif(1, min = log_mean_overall, max = log_mean_overall + 1) ) # Sigma must be strictly positive; use bounds based on your data's log SD stepSD0 <- runif(2, min = 0.5 * log_sd_overall, max = 1.5 * log_sd_overall) angleMean0 <- c(0, 0) angleCon0 <- runif(2, min = a, max = b) # Ensure a/b are positive (concentration params) stepPar0 <- c(stepMean0, stepSD0) # Order: mu1, mu2, sigma1, sigma2 anglePar0 <- c(angleMean0, angleCon0) return(list(step = stepPar0, angle = anglePar0)) }) # Fit 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 best model likelihoodL <- unlist(lapply(logP, function(m) m$mod$minimum)) whichbestpL <- which.min(likelihoodL) bestL <- logP[[whichbestpL]] # Clean up cluster stopCluster(cl) bestL
Extra Tips
- Ensure your step data has no zeros (log(0) is undefined). If you have zeros, add a tiny constant (e.g.,
log(data$step + 0.001)) or filter those rows. - Double-check the parameter order for
fitHMMwithstepDist="lnorm": it should bec(mu_state1, mu_state2, sigma_state1, sigma_state2)to match yourstepPar0setup.
内容的提问来源于stack exchange,提问作者ToriAlma

