gpt4 book ai didi

r - 拟合对数正态分布或泊松分布

转载 作者:行者123 更新时间:2023-12-02 20:29:17 24 4
gpt4 key购买 nike

我有一个由 1096 个数字组成的向量,即测量站 3 年来测量的 NOx 日平均浓度。您可以观察图像中的分布类型:

Histogram of NOx concentration

我使用这些命令来制作直方图:

NOxV<-scan("NOx_Vt15-17.txt")
hist.NOxVt<-hist(NOxV, plot = FALSE, breaks = 24)
plot(hist.NOxVt, xlab = "[NOx]", ylab = "Frequenze assolute", main = "Istogramma freq. ass. NOx 15-17 Viterbo")
points(hist.NOxVt$mids, hist.NOxVt$counts, col= "red")

我的教授建议我用泊松分布拟合直方图 - 注意转变:离散 -> 连续(我不知道这意味着什么) - 或用“对数正态”分布。

我尝试使用她在类(class)中给我们的一些命令行进行泊松拟合,但在执行以下最后一行代码后,R 给了我一个错误:

  my_poisson = function(params, x){
exp(-params)*params^x/factorial(x)
}

y<-hist.NOxVt$counts/1096;
x<-hist.NOxVt$mids;
z <- nls( y ~ exp(-a)*a^x/factorial(x), start=list(a=1) )

Error in numericDeriv(form[[3L]], names(ind), env) : Missing value or an infinity produced when evaluating the model In addition: There were 50 or more warnings (use warnings() to see the first 50)"

在这个问题之后我无法解决(甚至在互联网上搜索类似的问题)我决定用对数正态拟合分布,但我不知道该怎么做,因为教授没有向我们解释,而且我仍然没有足够的 R 经验来自己解决这个问题。

如果有任何有关如何进行对数正态拟合和/或泊松拟合的建议或示例,我将不胜感激。

最佳答案

R 自带的 MASS 包中有一个内置函数 fitdistr:

生成要查看的数据示例(观察参数以获取与您的图片类似的内容):

set.seed(101)
z <- rlnorm(1096,meanlog=4.5,sdlog=0.8)

拟合(基于统计,我不推荐泊松拟合 - 可能可以采用离散分布,例如泊松(或更好的负二项式)来拟合此类连续数据,但对数正态分布或 Gamma 分布是更自然的选择。

library(MASS)
f1 <- fitdistr(z,"lognormal")
f2 <- fitdistr(z,"Gamma")

f1f2 对象在打印时给出估计系数(log 的 meanlogsdlog - Gamma 的正态、形状)以及系数的标准误差。

画一张图(在密度尺度上,而不是在计数尺度上):红色是对数正态分布,蓝色是 Gamma 分布(在这种情况下对数正态分布更合适,因为这就是我首先生成“数据”的方式) 。 [with(as.list(coef(...)) 的东西是一些 R 的奇特东西,允许使用系数的名称 (meanlogsdlog 等)在后续的 R 代码中。]

hist(z,col="gray",breaks=50,freq=FALSE)
with(as.list(coef(f1)),
curve(dlnorm(x,meanlog,sdlog),
add=TRUE,col="red",lwd=2))
with(as.list(coef(f2)),
curve(dgamma(x,shape=shape,rate=rate),
add=TRUE,col="blue",lwd=2))

enter image description here

关于r - 拟合对数正态分布或泊松分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49219579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com