You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言fitdist拟合分布mle参数估计错误码100问题求解

问题原因
  • 你使用的fitdist默认采用极大似然估计(MLE),你的数据数值跨度极大(最小值仅为6,最大值超过13000)、偏态极强,默认的初始参数设置不合理,导致optim优化迭代过程中出现非有限数值,参数估计失败返回错误码100。
  • 代码存在冗余问题:重复定义两次fg对象、基础绘图参数par(mfrow)对后续ggplot类绘图完全不生效,虽不直接触发报错但属于不规范写法。
修复方案

方案1:换用矩估计法拟合(最稳定,无需设置初始值)

矩估计根据样本矩直接推导分布参数,不需要优化迭代,对这类偏态数据适配性更强,修改fitdist的method参数为"mme"即可,完整可运行代码如下:

library(fitdistrplus)
library(patchwork)
library(ggplot2)

# 统一使用矩估计拟合三类分布
fg <- fitdist(d$df, "gamma", method = "mme")
fln <- fitdist(d$df, "lnorm", method = "mme")
fw <- fitdist(d$df, "weibull", method = "mme")

plot.legend <- c("Weibull", "lognormal", "gamma")

a <- denscomp(list(fw, fln, fg), legendtext = plot.legend, plotstyle = "ggplot")
b <- qqcomp(list(fw, fln, fg), legendtext = plot.legend, plotstyle = "ggplot")
c <- cdfcomp(list(fw, fln, fg), legendtext = plot.legend, plotstyle = "ggplot")
d <- ppcomp(list(fw, fln, fg), legendtext = plot.legend, plotstyle = "ggplot")

a+b+c+d

方案2:保留MLE拟合,手动设置初始参数

如果需要使用极大似然估计的结果,可以根据样本统计量手动推导gamma分布的初始参数传入,避免优化初始值偏离合理区间:

# 计算gamma分布初始参数:shape = (均值^2)/方差,rate = 均值/方差
mu <- mean(d$df)
var <- var(d$df)
start_params <- list(shape = mu^2/var, rate = mu/var)

# 传入初始参数拟合
fg <- fitdist(d$df, "gamma", start = start_params)
# 其余两类分布拟合、绘图代码和方案1一致

方案3:更换优化算法

也可以指定更稳定的优化算法、设置参数边界避免出现非有限值:

fg <- fitdist(d$df, "gamma", 
              optim.method = "Nelder-Mead", 
              lower = c(shape = 0, rate = 0))

内容的提问来源于stack exchange,提问作者stats_noob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 07:15:07