问题描述
我正在尝试使用fitdistrplus
包中的fitdist ()
函数,以使我的数据适合不同的分布.假设我的数据如下:
I'm trying to use fitdist ()
function from the fitdistrplus
package to fit my data to different distributions. Let's say that my data looks like:
x = c (1.300000, 1.220000, 1.160000, 1.300000, 1.380000, 1.240000,
1.150000, 1.180000, 1.350000, 1.290000, 1.150000, 1.240000,
1.150000, 1.120000, 1.260000, 1.120000, 1.460000, 1.310000,
1.270000, 1.260000, 1.270000, 1.180000, 1.290000, 1.120000,
1.310000, 1.120000, 1.220000, 1.160000, 1.460000, 1.410000,
1.250000, 1.200000, 1.180000, 1.830000, 1.670000, 1.130000,
1.150000, 1.170000, 1.190000, 1.380000, 1.160000, 1.120000,
1.280000, 1.180000, 1.170000, 1.410000, 1.550000, 1.170000,
1.298701, 1.123595, 1.098901, 1.123595, 1.110000, 1.420000,
1.360000, 1.290000, 1.230000, 1.270000, 1.190000, 1.180000,
1.298701, 1.136364, 1.098901, 1.123595, 1.316900, 1.281800,
1.239400, 1.216989, 1.785077, 1.250800, 1.370000)
接下来,如果我运行fitdist (x, "gamma")
,一切都很好,但是如果我使用fitdist (x, "beta")
,则会出现以下错误:
Next, if i run fitdist (x, "gamma")
everything is fine, but if I use fitdist (x, "beta")
instead I get the following error:
Error in start.arg.default(data10, distr = distname) :
values must be in [0-1] to fit a beta distribution
好,所以我不是英语母语人士,但据我了解,此方法要求数据的范围为[0,1],因此我使用x_scaled = (x-min(x))/max(x)
对其进行了缩放.这给了我一个向量,该向量的值在那个范围内,与原始向量x
完全相关.
Ok, so I'm not native english but as far as I understand this method requires data to be in the range [0,1], so I scale it by using x_scaled = (x-min(x))/max(x)
. This gives me a vector with values in that range that perfectly correlates the original vector x
.
由于x_scaled
是class matrix
,因此我使用as.numeric()
转换为数值向量.然后用fitdist(x_scale,"beta")
拟合模型.
Because of x_scaled
is of class matrix
, I convert into a numeric vector using as.numeric()
. And then fit the model with fitdist(x_scale,"beta")
.
这次我得到以下错误:
Error in fitdist(x_scale, "beta") :
the function mle failed to estimate the parameters, with the error code 100
因此,在那之后,我一直在进行一些搜索引擎查询,但没有发现任何有用的东西.有人对这里出了什么问题有个想法吗?谢谢
So after that I've been doing some search engine queries but I don't find anything useful. Does anybody ave an idea of whats going on wrong here? Thank you
推荐答案
通过阅读源代码,可以发现fitdist
的默认估算方法为mle
,它将从相同的软件包,这将为您选择的分布构造负对数可能性,并使用optim
或constrOptim
在数值上最小化它.如果数值优化过程有任何问题,您将得到错误消息.
By reading into the source code, it can be found that the default estimation method of fitdist
is mle
, which will call mledist
from the same package, which will construct a negative log-likelihood for the distribution you have chosen and use optim
or constrOptim
to numerically minimize it. If there is anything wrong with the numerical optimization process, you get the error message you've got.
似乎出现错误是因为x_scaled
包含0或1时,在计算beta分布的负对数似然性时会遇到一些问题,因此数值优化方法将被破坏.一个不道德的把戏是让x_scaled <- (x - min(x) + 0.001) / (max(x) - min(x) + 0.002)
起作用,因此x_scaled
中既没有0也没有1,并且fitdist
将起作用.
It seems like the error occurs because when x_scaled
contains 0 or 1, there will be some problem in calculating the negative log-likelihood for beta distribution, so the numerical optimization method will simply broke. One dirty trick is to let x_scaled <- (x - min(x) + 0.001) / (max(x) - min(x) + 0.002)
, so there is no 0 nor 1 in x_scaled
, and fitdist
will work.
这篇关于拟合beta分布时出错:函数mle无法估计错误代码为100的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!