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

标签 r normal-distribution data-fitting poisson model-fitting

我有一个由 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/

相关文章:

c++ - C++ 中具有正常噪声的信号扰动

R:如何计算截断正态分布的均值和协方差

excel - 在Excel中生成正态分布的随机数

python - 使用 scipy.optimize.curve_fit - ValueError 和 minpack.error 拟合 2D 高斯函数

numpy - 使用 numpy 将数据拟合为 y = y = a*x**2 + b*x + c(其中 c=0)

r - 使用 R 将 basemap 添加到 SpatialPointDataFrames

R代码示例/最佳实践

python - 将 3d sigmoid 拟合到数据

r - 如何使用 R 自动重命名宽数据中的列

R 和 Gviz : How to remove (/crop) a region of a plot?