我想将伽马分布拟合到由338个元素组成的数据集中,具有固定的低阈值(我使用的是R)。为了表示这个下限,我想使用带有三个参数的伽马,加上位置。这是我的密码:
library(FAdist)
library(fitdistrplus)
fit <- fitdist(mydata,"gamma3",start=list(1,1,mythreshold))每次运行代码时,我都会得到相同的错误:
<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 [3]>
Error in fitdist(Hs, "gamma3", start = list(1, 1, 3)) :
the function mle failed to estimate the parameters,
with the error code 100所以我试着改变起始值,用两个参数的伽玛拟合得到的值,得到了这样的结果:
fit <- fitdist(mydata,"gamma")
fit
Fitting of the distribution ' gamma ' by maximum likelihood
Parameters:
estimate Std. Error
shape 21.417503 1.6348313
rate 5.352422 0.4133735但是它仍然不起作用。如果有两个参数的伽玛不起作用,我会理解的,但事实并非如此,我无法给自己一个解释。此外,对于具有两个参数的伽马,QQ-图和ecdf并不是good...but,如果我在与低阈值有关的原始数据集上拟合分布,则拟合看起来是完美的:
fit <- fitdist(mydata-mythreshold,"gamma")
fit
Fitting of the distribution ' gamma ' by maximum likelihood
Parameters:
estimate Std. Error
shape 1.059540 0.07212832
rate 1.058007 0.09117620但是,我不知道这样做是否正确,因为that...the参数非常不同,我需要它们来计算与我的数据相关的返回周期!这就是为什么我想到了带有位置参数的伽马。
附注:我没有附上这些数据,因为它们太多,但我可以报告这些数据的摘要:
summary(mydata)
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.003 3.282 3.753 4.001 4.444 8.087 发布于 2016-12-26 21:00:28
树立可复制的榜样;
library(FAdist); library(fitdistrplus)
set.seed(101)
x <- rgamma3(1000,shape=1,scale=1,thres=1)对于阈值参数的值大于数据集中的最小值,可能是无限的(因为这些值被认为是不可能的/在截断分布下的值为零)。我们可以通过为该参数设置一个上限来使其正常工作:
fitdist(x,dgamma3,start=list(shape=1,scale=1,thres=0.5),
upper=c(Inf,Inf,min(x)))
## Fitting of the distribution ' gamma3 ' by maximum likelihood
## Parameters:
## estimate Std. Error
## shape 0.9321949 NA
## scale 1.0586079 NA
## thres 1.0000012 NA注(1)在最大似然情况下,这类阈值参数总是以它能取的最大值(即数据集中的最小值)结束;(2)标准误差为NA,因为当参数到达边界时无法计算Wald标准误差。
或者,您可以通过定义包装器函数来修复阈值参数:
dgconstr <- function(x,shape,scale,...) {
dgamma3(x,shape,scale,thres=0.5,...)
}
pgconstr <- function(...) {
pgamma3(...,thres=0.5)
}
fitdist(x,dgconstr,start=list(shape=1,scale=1))https://stackoverflow.com/questions/41334345
复制相似问题