fitdistrplus 检验数据的分布
2020-02-27 本文已影响0人
BeeBee生信
一般各种分析其实都是有一定的数学模型和假设的,有些也相应对输入数据的分布有一定要求。如果不确定自己数据是否符合要求,最好先检测一下。比如有时候你纠结使用 FPKM 还是 log2(FPKM + 1) ,此时查半天资料或许不如自己看看2个数据分布的区别。
R包 fitdistrplus 函数 fitdist
检验非删失数据的一元分布,相应的 fitdistcens
则用于检验删失数据。函数默认参数:
fitdist(data, distr, method = c("mle", "mme", "qme", "mge"), start=NULL, fix.arg=NULL, discrete, keepdata = TRUE, keepdata.nb=100, ...)
部分参数解释
- data: 检验的数据,是个向量。
- distr: 检验的分布名。
- method: 检验拟合的方法,"mle"代表"maximum likelihood estimation"(最大似然估计),"mme"代表"moment matching estimation"(矩估计),"qme"代表"quantile matching estimation","mge"代表"maximum goodness-of-estimation"。
-
discrete: 是否为离散型分布。如果不设置会根据
distr
参数来自动设定,在distr
为 "binom", "nbinom", "geom", "hyper", "pois" 这些值时为TRUE
否则FALSE
。
下面给出可以使用的 distr
参数分布名字。所以如果你想检验泊松分布,那么使用 "pois" 而不是 "Poisson" 。
Distribution | Name |
---|---|
beta | beta |
binomial | binom |
Cauchy | cauchy |
chi-squared | chisq |
exponential | exp |
F | f |
gamma | gamma |
geometric | geom |
hypergeometric | hyper |
log-normal | lnorm |
logistic | logis |
negative binomial | nbinom |
normal | norm |
Poisson | pois |
signed rank | signrank |
Student's t | t |
uniform | unif |
Weibull | weibull |
Wilcoxon | wilcox |
"Talk is cheap" 让我们看看代码实例。数据采用R自带的 lung
数据集。
> head(lung, n = 3)
inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
1 3 306 2 74 1 1 90 100 1175 NA
2 3 455 2 68 1 0 90 90 1225 15
3 3 1010 1 56 1 0 90 90 NA 15
检验一下病人年龄分布。比较正态分布 norm
和对数正态分布 lnorm
哪个模型更适合。
正态分布
> fitNorm <- fitdist(lung$age, "norm", method = "mme")
> summary(fitNorm)
Fitting of the distribution ' norm ' by matching moments
Parameters :
estimate
mean 62.447368
sd 9.053537
Loglikelihood: -825.8374 AIC: 1655.675 BIC: 1662.534
> plot(fitNorm)
正态分布
对数正态分布
> fitLnorm <- fitdist(lung$age, "lnorm", method = "mme")
> summary(fitLnorm)
Fitting of the distribution ' lnorm ' by matching moments
Parameters :
estimate
meanlog 4.1239236
sdlog 0.1442254
Loglikelihood: -835.8495 AIC: 1675.699 BIC: 1682.558
> plot(fitLnorm)
对数正态分布
返回的 AIC
, BIC
之类的参数我没搞懂,也不想太深究。从两个分布的图片来看,正态分布要好于对数正态分布。
[参考]
An Introduction to R
https://cran.r-project.org/web/packages/fitdistrplus/vignettes/FAQ.html