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

Fitting a lognormal or poisson distribution

我有一个包含 1096 个数字的向量,是测量站 3 年内测得的 NOx 日平均浓度。 可以观察图中的分布类型:

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

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")

我的教授建议我用泊松分布拟合直方图 - 注意过渡:离散 -> 连续(我不知道那是什么意思) - 或者 "Lognormal" 分布。

我尝试使用她在课上给我们的一些命令行来进行泊松拟合,但是在执行以下最后一行代码后 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 经验来自己弄清楚。

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

R自带的MASS包里有一个built-in函数fitdistr:

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

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

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

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

f1f2 对象在打印时会给出估计系数(meanlogsdlog for log-Normal,shape rate 用于 Gamma)和系数的标准误差。

画一张图(在密度刻度上,而不是计数刻度上):红色是 log-Normal,蓝色是 Gamma(在这种情况下 log-Normal 更合适,因为这就是我生成 "data" 首先)。 [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))