1

我有这个问题。'将 nfsold 中的数据(nfsold 只是一个包含 150 个数字的向量)建模为来自 Gamma(lambda; k) 分布的 150 个独立观察值的集合。使用矩量法来获得 k 和 lambda 的估计值。绘制数据的直方图并叠加拟合伽马分布的 PDF,作为该分布与观测数据匹配的初步检查。

这是我写的代码。

#The first moment of each Xi, i = 1,...,n, is E(Xi) = k/lamda.
#The second moment of each Xi is E(Xi^2) = k(k+1)/(lamda)^2
#Since we have to find 2 two things, k and lamda we require 2 moments to do this.

x_bar = mean = sum(nfsold)/150 #This is the first moment

mean

second_moment = sum(nfsold^2)/150
second_moment
#(1/n)(sum xi) = k/lamda
#(1/n)(sum x^2i) = k(k+1)/(lamda)^2
#By solving these because of the methods of moments we get lambda and k.

lamda_hat = (x_bar)/((second_moment)-(x_bar)^2)

lamda_hat

k_hat = (x_bar)^2/ ((second_moment)-(x_bar)^2)

k_hat

independent_observations = dgamma(x,k_hat, rate = lamda_hat)

hist( independent_observations, breaks = 15, prob = TRUE, main="Histogram for the Gamma Distribution of the data in nfsold", xlab="Independent Observations", ylab="P.D.F")

curve(dgamma(x,k_hat, rate =lamda_hat), add=TRUE, col="green")

我的问题是我的叠加曲线没有跟随我的直方图,所以我觉得我的代码有问题,请问我能得到一些帮助来纠正它吗?

谢谢!

4

0 回答 0