R语言中mle()与fitdist()极大似然估计报错求助:初始值非有限
Alright, let's tackle this initial value in 'vmmin' is not finite error you're hitting with mle() and fitdist()—I’ve seen this pop up a lot when working with custom CDFs for maximum likelihood estimation, so let’s break down the most likely fixes step by step.
1. First, audit your custom distribution functions (PDF/CDF) for non-finite values
You mentioned using a CDF structured like 1-((1-q)*...)—the first thing to check is whether this calculation returns valid, finite values when paired with your data and initial parameter guesses. Common pitfalls here include:
- Division by zero in hidden parts of your formula
- Logarithms of non-positive numbers (if your PDF/CDF uses logs)
- Exponentiation that blows up to
Infor collapses to-Inf
Test this manually with your initial parameter guesses:
# Replace with your actual CDF/PDF function and parameter values test_cdf_vals <- your_custom_cdf(data, param1 = 0.5, param2 = 2) any(!is.finite(test_cdf_vals)) # If this returns TRUE, your function has a problem
Also, remember: fitdist() requires a valid PDF (specified via ddistname), not just a CDF. If you’ve only defined the CDF and skipped the PDF, or the PDF calculation is incorrect, this will break the likelihood calculation immediately.
2. Fix your initial parameter guesses (vstart)
This error almost always ties back to poor initial parameter values that send the optimization routine (which both mle() and fitdist() use under the hood) into a region where the likelihood is non-finite. Here’s how to fix this:
- Use theoretical bounds: If your parameters have natural limits (e.g., a probability parameter must be between 0 and 1, a scale parameter must be positive), start with values inside those ranges.
- Use moment estimation: Calculate sample moments (mean, variance, median) of your data, then reverse-engineer rough parameter values that align with those moments for your distribution.
- Avoid extreme values: Steer clear of 0, 1e6, or other edge cases that might trigger singularities in your distribution formula.
3. Add parameter bounds to restrict optimization
If your parameters have hard limits, don’t let optim() wander into invalid territory. Use the lower and upper arguments in fitdist() or mle() to enforce these bounds:
# Example: param1 is a probability (0-1), param2 is a positive scale parameter fitdist( data = your_data, distr = "your_custom_dist", start = list(param1 = 0.3, param2 = 1.5), lower = c(0, 0.01), # Prevent param1=0 or param2=0 upper = c(1, Inf) )
This keeps the optimization routine within valid parameter space, eliminating non-finite likelihood calculations at the start.
4. Verify your likelihood function setup (for mle())
If you’re using mle() with a custom likelihood function, double-check that you’re calculating the negative log-likelihood correctly. The standard structure should be:
neg_log_lik <- function(params) { # Extract parameters param1 <- params[1] param2 <- params[2] # Calculate PDF values for all data points pdf_vals <- your_custom_pdf(your_data, param1, param2) # Return negative sum of log-PDFs (optim minimizes this) -sum(log(pdf_vals)) }
If you accidentally use the CDF instead of the PDF, or flip the sign (maximizing instead of minimizing), you’ll get non-finite values or failed optimization.
5. Debug directly with optim()
If you’re still stuck, bypass mle()/fitdist() and run the underlying optim() call manually to get clearer error context:
optim_result <- optim( par = your_vstart_params, fn = neg_log_lik, lower = c(0, 0.01), upper = c(1, Inf), method = "L-BFGS-B" # Use bounded method if you have limits ) # Check the output for clues print(optim_result$message) print(optim_result$value)
This will tell you exactly where the optimization is failing—whether it’s the initial value, a parameter boundary issue, or a problem with your likelihood function.
内容的提问来源于stack exchange,提问作者Elena Glamozdina

