简体   繁体   English

使用R使Gamma分布适合流

[英]Fitting a Gamma Distribution to Streamflows with R

Hello I have a monthly average of stream flow for the last 100 years: 您好,我有过去100年的每月流量平均值:

Sep <-
c(50504.61, 31903.57, 20575.81, 51250, 20282.16, 19932.05, 22302.73, 
24777.73, 27681.94, 18676.9, 28587.23, 29030.58, 24433.3, 18785.24, 
21913.74, 33662.53, 23215.58, 35792.85, 13766.63, 41484.56, 16209.67, 
74759.93, 19730.94, 61532.38, 24404.35, 14988.49, 24505.1, 19911.57, 
10058.62, 20984.86, 24913.8, 21067.56, 37107.83, 23730.15, 18556.58, 
24130.02, 14550.06, 21667.81, 11748.43, 17464.65, 15869.2, 27497.25, 
13161.09, 14842.04, 15144.44, 18796.16, 24514.99, 14315.39, 17463.56, 
12698.22, 10528.03, 35689.17, 15496.81, 11170.44, 11593.22, 30230.25, 
14554.43, 25971.54, 12939.35, 30981.11, 12630.06, 18271.7, 14998.52, 
21462.35, 39960.48, 23928.06, 20261.68, 23808.23, 12809.67, 18762.68, 
17108.04, 12861.55, 14521.27, 14215.91, 21513.8, 18797.29, 37495.3, 
27068.98, 37775.56, 26099.67, 34931.63, 23563.04, 19916.69, 15545.58, 
19366.38, 27494.53, 22375.44, 27635.01, 18280.28, 26641.43, 18894.12, 
42866.85, 25925.63, 38156.57, 18015.56)

I am trying to fit a Gamma Distribution to the data with the function: 我正在尝试使用以下函数将Gamma分布拟合到数据:

fit.gamma <- fitdist(Sep, distr = "gamma", method = "mle")

and it gives an error: 它给出了一个错误:

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 [2]> Error in fitdist(Sep, distr = "gamma", method = "mle") : the function mle failed to estimate the parameters, optim中的simpleError(par = vstart,fn = fnobj,fix.arg = fix.arg,obs = data,gr =梯度,ddistnam = ddistname,hessian = TRUE,方法= meth,lower = Lower,lower = upper,.. 。):非有限的有限差分值[2]> fitdist中的错误(Sep,distr =“ gamma”,方法=“ mle”):函数mle无法估计参数,
with the error code 100 错误代码为100

I am using the packages 我正在使用包装

library(fitdistrplus)
library(sm)
library(Rlab)
library(MASS)

Any help would be much appreciated 任何帮助将非常感激

Gamma distribution has two parameters and both are positive. Gamma distribution有两个参数,并且都是正数。 fitdist function uses optim function to find those parameters. fitdist函数使用optim函数查找那些参数。 However, without any user specified limit, optim will try even (infeasible) negative parameters during iteration resulting in NaNs . 但是,没有任何用户指定的限制, optim将在迭代过程中尝试甚至(不可行)负参数,从而导致NaNs Setting the limit lower = c(0, 0) , will fix this error. 将下限设置为下限lower = c(0, 0)将解决此错误。

Credit to this answer 相信这个答案

library(fitdistrplus)

fit.gamma <- fitdist(Sep, distr = "gamma", method = "mle", lower = c(0, 0))

# Check result
plot(fit.gamma)

# Goodness-of-fit statistics
gofstat(fit.gamma)

#> Goodness-of-fit statistics
#>                              1-mle-gamma
#> Kolmogorov-Smirnov statistic  0.09099753
#> Cramer-von Mises statistic    0.16861125
#> Anderson-Darling statistic    1.07423429
#> 
#> Goodness-of-fit criteria
#>                                1-mle-gamma
#> Akaike's Information Criterion    2004.836
#> Bayesian Information Criterion    2009.944

Created on 2018-03-24 by the reprex package (v0.2.0). reprex软件包 (v0.2.0)于2018-03-24创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM