你如何将R分布拟合到R中的数据?

时间:2017-08-06 20:20:39

标签: r distribution gamma-distribution

假设我有使用以下方法生成的变量x:

x <- rgamma(100,2,11) + rnorm(100,0,.01) #gamma distr + some gaussian noise

    head(x,20)
 [1] 0.35135058 0.12784251 0.23770365 0.13095612 0.18796901 0.18251968
 [7] 0.20506117 0.25298286 0.11888596 0.07953969 0.09763770 0.28698417
[13] 0.07647302 0.17489578 0.02594517 0.14016041 0.04102864 0.13677059
[19] 0.18963015 0.23626828

我怎样才能将伽马分布拟合到它?

2 个答案:

答案 0 :(得分:6)

一个很好的选择是ML Delignette-Muller等人的fitdistrplus包。例如,使用您的方法生成数据:

set.seed(2017)
x <- rgamma(100,2,11) + rnorm(100,0,.01)
library(fitdistrplus)
fit.gamma <- fitdist(x, distr = "gamma", method = "mle")
summary(fit.gamma)

Fitting of the distribution ' gamma ' by maximum likelihood 
Parameters : 
       estimate Std. Error
shape  2.185415  0.2885935
rate  12.850432  1.9066390
Loglikelihood:  91.41958   AIC:  -178.8392   BIC:  -173.6288 
Correlation matrix:
          shape      rate
shape 1.0000000 0.8900242
rate  0.8900242 1.0000000


plot(fit.gamma)

enter image description here

答案 1 :(得分:2)

您可以尝试快速适应Gamma分布。作为双参数分布,可以通过查找样本均值和方差来恢复它们。只要平均值为正,您就可以将一些样本设为负值。

set.seed(31234)
x <- rgamma(100, 2.0, 11.0) + rnorm(100, 0, .01) #gamma distr + some gaussian noise
#print(x)

m <- mean(x)
v <- var(x)

print(m)
print(v)

scale <- v/m
shape <- m*m/v

print(shape)
print(1.0/scale)

对我来说它打印

> print(shape)
[1] 2.066785
> print(1.0/scale)
[1] 11.57765
>