R语言 如何在R中对数据集拟合伽马分布
伽马分布专门用于确定指数分布、埃朗分布和齐次分布。它也被称为具有连续概率分布的双参数族。
分步实现
第1步: 在R语言中安装并导入fitdistrplus包。
install.package("fitdistrplus")
library(fitdistrplus)
fitdistrplus软件包提供了fitdist函数来拟合一个分布。
语法
fitdist(dataset, distr = “choice”, method = “method”)
这里。
- distr = “choice” : 它代表分布的选择
- method = “method” : 表示拟合数据的方法。
第2步: 现在,我们将在伽马分布的帮助下拟合数据集数据,并在最大似然估计方法的帮助下拟合数据集。
# Generating 20 random values that uses
# a gamma distribution having shape
# parameter as 10
# combined with some gaussian noise
data <- rgamma(20, 3, 10) + rnorm(20, 0, .02)
# Fit the dataset to a gamma distribution
# using mle
ans <- fitdist(data, distr = "gamma", method = "mle")
# Display the summary of ans
summary(ans)
输出
现在我们将制作一些图表,以显示伽马分布对数据集的拟合程度,其语法如下。
# Import the package
library(fitdistrplus)
# Generating 20 random values that uses a
# gamma distribution having shape parameter
# as 10
# combined with some gaussian noise
data <- rgamma(20, 3, 10) + rnorm(20, 0, .02)
# Fitting the dataset to a gamma distribution
# with the help of mle
ans <- fitdist(data, distr = "gamma", method = "mle")
# Display the plot
plot(ans)
输出
例子
# Import the package
library(fitdistrplus)
# Generating 20 random values that uses a
# gamma distribution having shape parameter
# as 10
# combined with some gaussian noise
data <- rgamma(20, 3, 10) + rnorm(20, 0, .02)
# Fitting the dataset to a gamma distribution
# with the help of mle
ans <- fitdist(data, distr = "gamma", method = "mle")
# Display the summary of the ans
summary(ans)
# Display the plot
plot(ans)
输出