R语言fitdistrplus包mme法拟合三参数对数正态分布报错求助
报错根因
触发unused argument (mln3 = ...)报错以及后续拟合失败的核心原因有3点:
fitdistrplus::fitdist调用mme矩匹配法拟合自定义分布时,不接收手动传入的理论矩函数参数:它会自动在当前工作环境搜索命名规则为m[分布名]的函数作为理论矩计算逻辑,你设置的分布名是ln3,只要把理论矩函数命名为mln3放在全局环境即可,调用fitdist时额外写mln3=mln3属于多余参数,会直接触发未使用参数报错。- 原代码写的
mln3二阶、三阶矩计算逻辑完全没有纳入阈值threshold的平移影响,就算去掉多余参数也会出现矩匹配不收敛、结果完全错误的问题。 - 原代码自定义的
memp经验矩函数计算的是中心矩,和fitdistmme方法默认匹配的原始矩逻辑不兼容,手动传入反而会干扰优化过程。
修正后可运行代码
修正点包括:移除多余传参、修正三参数对数正态矩计算公式、给阈值参数加合法边界避免优化取无效值,同时保留你需要的分位数置信区间生成逻辑:
library(fitdistrplus) library(EnvStats) # 此处替换为你自己的径流样本y,下面是测试用示例数据 set.seed(123) y <- rlnorm3(1000, meanlog = 2, sdlog = 0.5, threshold = 10) # 三参数对数正态的分布函数,直接调用EnvStats内置逻辑即可 dln3 <- function(x, meanlog, sdlog, threshold) dlnorm3(x, meanlog, sdlog, threshold) pln3 <- function(q, meanlog, sdlog, threshold) plnorm3(q, meanlog, sdlog, threshold) qln3 <- function(p, meanlog, sdlog, threshold) qlnorm3(p, meanlog, sdlog, threshold) # 用EnvStats获取矩匹配初始值 param_init <- elnorm3(y, method = "mme", ci = FALSE)$parameters # 定义三参数对数正态的理论原点矩(函数名必须为mln3,匹配distr="ln3"的命名规则) mln3 <- function(order, meanlog, sdlog, threshold) { # 两参数对数正态部分的k阶原点矩 lnorm_k <- exp(order * meanlog + (order^2 * sdlog^2)/2) if (order == 1) { return(threshold + lnorm_k) } else if (order == 2) { m1 <- exp(meanlog + sdlog^2/2) return(lnorm_k + 2*threshold*m1 + threshold^2) } else if (order == 3) { m1 <- exp(meanlog + sdlog^2/2) m2 <- exp(2*meanlog + 2*sdlog^2) return(lnorm_k + 3*threshold*m2 + 3*threshold^2*m1 + threshold^3) } } # 拟合分布 # 注意:不要手动传memp、mln3参数,给threshold设上界为min(y)-1e-6避免出现阈值≥样本值的非法情况 ajust <- fitdist( y, distr = "ln3", method = "mme", order = 1:3, start = list( meanlog = unname(param_init["meanlog"]), sdlog = unname(param_init["sdlog"]), threshold = unname(param_init["threshold"]) ), lower = c(-Inf, 1e-6, -Inf), upper = c(Inf, Inf, min(y) - 1e-6) ) # 查看拟合结果 summary(ajust) # 生成对应分位数的置信区间(基于bootstrap,为fitdistrplus官方推荐方法) boot_ajust <- bootdist(ajust, niter = 1000) # 示例:输出95%置信水平下1%、50%、99%分位数的置信区间,可自行替换probs参数 quantile(boot_ajust, probs = c(0.01, 0.5, 0.99), conf.level = 0.95)
拟合注意事项
- 如果出现收敛警告,可以手动调整
threshold的初始值,通常设置为比样本最小值小5%~10%的数值更容易收敛。 - 矩匹配法本身对样本异常值敏感度较高,如果拟合效果不符合预期,可以换成
method="mle"做极大似然估计。 - bootstrap迭代次数
niter可以根据自己的精度需求调整,通常设1000~2000次足够稳定。
内容的提问来源于stack exchange,提问作者r-user
相关产品推荐
相关产品推荐

