使用 fitdistrplus 拟合 trunnorm
Fitting truncnorm using fitdistrplus
我正在尝试将截断的正态分布拟合到某些数据。但是,我一直 运行 陷入以下错误:
<simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data, gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth, lower = lower, upper = upper, ...): non-finite finite-difference value [1]>
Error in fitdist(testData, "truncnorm", start = list(a = 0, mean = 0.8, :
the function mle failed to estimate the parameters,
with the error code 100
我不确定出了什么问题 - 我读到在某些情况下如果初始猜测错误或高于实际值可能会出现拟合问题,但我尝试了多种不同的开始值和 none 似乎有效。
这是我的一小部分数据样本,以及我用来获取错误的代码:
library(fitdistrplus)
library(truncnorm)
testData <- c(3.2725167726, 0.1501345235, 1.5784128343, 1.218953218, 1.1895520932,
2.659871271, 2.8200152609, 0.0497193249, 0.0430677458, 1.6035277181,
0.2003910167, 0.4982836845, 0.9867184303, 3.4082793339, 1.6083770189,
2.9140912221, 0.6486576911, 0.335227878, 0.5088426851, 2.0395797721,
1.5216239237, 2.6116576364, 0.1081283479, 0.4791143698, 0.6388625172,
0.261194346, 0.2300098384, 0.6421213993, 0.2671907741, 0.1388568942,
0.479645736, 0.0726750815, 0.2058983462, 1.0936704833, 0.2874115077,
0.1151566887, 0.0129750118, 0.152288794, 0.1508512023, 0.176000366,
0.2499423442, 0.8463027325, 0.0456045486, 0.7689214668, 0.9332181529,
0.0290242892, 0.0441181842, 0.0759601229, 0.0767983979, 0.1348839304
)
fitdist(testData, "truncnorm", start = list(a = 0, mean = 0.8, sd = 0.9))
问题在于,随着下限 a
趋于零(请注意,后者不得在 start
参数,但在 fix.arg
内):
fitdist(testData, "truncnorm", fix.arg=list(a=-.5),
start = list(mean = mean(testData), sd = sd(testData)))
fitdist(testData, "truncnorm", fix.arg=list(a=-.2),
start = list(mean = mean(testData), sd = sd(testData)))
fitdist(testData, "truncnorm", fix.arg=list(a=-.15),
start = list(mean = mean(testData), sd = sd(testData)))
防止 mean
出现较大负值的一种可能性是使用下限进行优化:
fitdist(testData, "truncnorm", fix.arg=list(a=0),
start = list(mean = mean(testData), sd = sd(testData)),
optim.method="L-BFGS-B", lower=c(0, 0))
然而,这改变了估算过程;事实上,您对参数施加了额外的约束,并且可能会获得具有不同下限的不同答案。
我正在尝试将截断的正态分布拟合到某些数据。但是,我一直 运行 陷入以下错误:
<simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data, gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth, lower = lower, upper = upper, ...): non-finite finite-difference value [1]>
Error in fitdist(testData, "truncnorm", start = list(a = 0, mean = 0.8, :
the function mle failed to estimate the parameters,
with the error code 100
我不确定出了什么问题 - 我读到在某些情况下如果初始猜测错误或高于实际值可能会出现拟合问题,但我尝试了多种不同的开始值和 none 似乎有效。
这是我的一小部分数据样本,以及我用来获取错误的代码:
library(fitdistrplus)
library(truncnorm)
testData <- c(3.2725167726, 0.1501345235, 1.5784128343, 1.218953218, 1.1895520932,
2.659871271, 2.8200152609, 0.0497193249, 0.0430677458, 1.6035277181,
0.2003910167, 0.4982836845, 0.9867184303, 3.4082793339, 1.6083770189,
2.9140912221, 0.6486576911, 0.335227878, 0.5088426851, 2.0395797721,
1.5216239237, 2.6116576364, 0.1081283479, 0.4791143698, 0.6388625172,
0.261194346, 0.2300098384, 0.6421213993, 0.2671907741, 0.1388568942,
0.479645736, 0.0726750815, 0.2058983462, 1.0936704833, 0.2874115077,
0.1151566887, 0.0129750118, 0.152288794, 0.1508512023, 0.176000366,
0.2499423442, 0.8463027325, 0.0456045486, 0.7689214668, 0.9332181529,
0.0290242892, 0.0441181842, 0.0759601229, 0.0767983979, 0.1348839304
)
fitdist(testData, "truncnorm", start = list(a = 0, mean = 0.8, sd = 0.9))
问题在于,随着下限 a
趋于零(请注意,后者不得在 start
参数,但在 fix.arg
内):
fitdist(testData, "truncnorm", fix.arg=list(a=-.5),
start = list(mean = mean(testData), sd = sd(testData)))
fitdist(testData, "truncnorm", fix.arg=list(a=-.2),
start = list(mean = mean(testData), sd = sd(testData)))
fitdist(testData, "truncnorm", fix.arg=list(a=-.15),
start = list(mean = mean(testData), sd = sd(testData)))
防止 mean
出现较大负值的一种可能性是使用下限进行优化:
fitdist(testData, "truncnorm", fix.arg=list(a=0),
start = list(mean = mean(testData), sd = sd(testData)),
optim.method="L-BFGS-B", lower=c(0, 0))
然而,这改变了估算过程;事实上,您对参数施加了额外的约束,并且可能会获得具有不同下限的不同答案。