r - 在 R 中拟合累积分布后,根据拟合参数创建正态分布

标签 r math statistics curve-fitting normal-distribution

在使用 Gompertz 函数成功拟合累积数据后,我需要根据拟合函数创建正态分布。

这是到目前为止的代码:

      df <- data.frame(x = c(0.01,0.011482,0.013183,0.015136,0.017378,0.019953,0.022909,0.026303,0.0302,0.034674,0.039811,0.045709,0.052481,0.060256,0.069183,0.079433,0.091201,0.104713,0.120226,0.138038,0.158489,0.18197,0.20893,0.239883,0.275423,0.316228,0.363078,0.416869,0.47863,0.549541,0.630957,0.724436,0.831764,0.954993,1.096478,1.258925,1.44544,1.659587,1.905461,2.187762,2.511886,2.884031,3.311311,3.801894,4.365158,5.011872,5.754399,6.606934,7.585776,8.709636,10,11.481536,13.182567,15.135612,17.378008,19.952623,22.908677,26.30268,30.199517,34.673685,39.810717,45.708819,52.480746,60.255959,69.183097,79.432823,91.201084,104.712855,120.226443,138.038426,158.489319,181.970086,208.929613,239.883292,275.42287,316.227766,363.078055,416.869383,478.630092,549.540874,630.957344,724.43596,831.763771,954.992586,1096.478196),
                 y = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.00044816,0.00127554,0.00221488,0.00324858,0.00438312,0.00559138,0.00686054,0.00817179,0.00950625,0.01085188,0.0122145,0.01362578,0.01514366,0.01684314,0.01880564,0.02109756,0.0237676,0.02683182,0.03030649,0.0342276,0.03874555,0.04418374,0.05119304,0.06076553,0.07437854,0.09380666,0.12115065,0.15836926,0.20712933,0.26822017,0.34131335,0.42465413,0.51503564,0.60810697,0.69886817,0.78237651,0.85461023,0.91287236,0.95616228,0.98569093,0.99869001,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999))

library(drc)
fm <- drm(y ~ x, data = df, fct = G.3())

options(scipen = 10) #to avoid scientific notation in x axis

plot(df$x, predict(fm),type = "l", log = "x",col = "blue",
           main = "Cumulative function distribution",xlab = "x", ylab = "y")

points(df,col = "red")

legend("topleft", inset = .05,legend = c("exp","fit")
       ,lty = c(NA,1), col = c("red", "blue"), pch = c(1,NA), lwd=1, bty = "n")


summary(fm)

这是下面的情节: enter image description here

我现在的想法是以某种方式将这种累积拟合转换为正态分布。有什么想法我该怎么做吗?

最佳答案

虽然您的初衷可能是非参数的,但我建议使用参数估计方法:矩量法,该方法广泛用于此类问题,因为您有一定的参数分布(正态分布)需要拟合。这个想法很简单,根据拟合的累积分布函数,您可以计算平均值(我的代码中的E1)和方差(我的代码中的SD的平方),以及那么问题就解决了,因为正态分布完全可以由均值和方差决定。

df <- data.frame(x=c(0.01,0.011482,0.013183,0.015136,0.017378,0.019953,0.022909,0.026303,0.0302,0.034674,0.039811,0.045709,0.052481,0.060256,0.069183,0.079433,0.091201,0.104713,0.120226,0.138038,0.158489,0.18197,0.20893,0.239883,0.275423,0.316228,0.363078,0.416869,0.47863,0.549541,0.630957,0.724436,0.831764,0.954993,1.096478,1.258925,1.44544,1.659587,1.905461,2.187762,2.511886,2.884031,3.311311,3.801894,4.365158,5.011872,5.754399,6.606934,7.585776,8.709636,10,11.481536,13.182567,15.135612,17.378008,19.952623,22.908677,26.30268,30.199517,34.673685,39.810717,45.708819,52.480746,60.255959,69.183097,79.432823,91.201084,104.712855,120.226443,138.038426,158.489319,181.970086,208.929613,239.883292,275.42287,316.227766,363.078055,416.869383,478.630092,549.540874,630.957344,724.43596,831.763771,954.992586,1096.478196),
                 y=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.00044816,0.00127554,0.00221488,0.00324858,0.00438312,0.00559138,0.00686054,0.00817179,0.00950625,0.01085188,0.0122145,0.01362578,0.01514366,0.01684314,0.01880564,0.02109756,0.0237676,0.02683182,0.03030649,0.0342276,0.03874555,0.04418374,0.05119304,0.06076553,0.07437854,0.09380666,0.12115065,0.15836926,0.20712933,0.26822017,0.34131335,0.42465413,0.51503564,0.60810697,0.69886817,0.78237651,0.85461023,0.91287236,0.95616228,0.98569093,0.99869001,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999))

library(drc)
fm <- drm(y ~ x, data = df, fct = G.3())

options(scipen = 10) #to avoid scientific notation in x axis

plot(df$x, predict(fm),type="l", log = "x",col="blue", main="Cumulative distribution function",xlab="x", ylab="y")

points(df,col="red")

E1 <- sum((df$x[-1] + df$x[-length(df$x)]) / 2 * diff(predict(fm)))
E2 <- sum((df$x[-1] + df$x[-length(df$x)]) ^ 2 / 4 * diff(predict(fm)))
SD <- sqrt(E2 - E1 ^ 2)
points(df$x, pnorm((df$x - E1) / SD), col = "green")

legend("topleft", inset = .05,legend= c("exp","fit","method of moment")
       ,lty = c(NA,1), col = c("red", "blue", "green"), pch = c(1,NA), lwd=1, bty="n")


summary(fm)

CDF

估算结果:

## > E1 (mean of fitted normal distribution)
## [1] 65.78474
## > E2 (second moment of fitted normal distribution)
##[1] 5792.767
## > SD (standard deviation of fitted normal distribution)
## [1] 38.27707
## > SD ^ 2 (variance of fitted normal distribution)
## [1] 1465.134

编辑:更新了由 drc 拟合的 cdf 计算矩的方法。下面定义的函数 moment 使用连续旋转的力矩公式计算力矩估计。 E(X ^ k) = k *\int x ^ {k - 1} (1 - cdf(x)) dx。这些是我可以从拟合的 cdf 中得到的最佳估计。当 x 接近零时,拟合效果不是很好,因为我在评论中讨论了原始数据集的原因。

df <- data.frame(x=c(0.01,0.011482,0.013183,0.015136,0.017378,0.019953,0.022909,0.026303,0.0302,0.034674,0.039811,0.045709,0.052481,0.060256,0.069183,0.079433,0.091201,0.104713,0.120226,0.138038,0.158489,0.18197,0.20893,0.239883,0.275423,0.316228,0.363078,0.416869,0.47863,0.549541,0.630957,0.724436,0.831764,0.954993,1.096478,1.258925,1.44544,1.659587,1.905461,2.187762,2.511886,2.884031,3.311311,3.801894,4.365158,5.011872,5.754399,6.606934,7.585776,8.709636,10,11.481536,13.182567,15.135612,17.378008,19.952623,22.908677,26.30268,30.199517,34.673685,39.810717,45.708819,52.480746,60.255959,69.183097,79.432823,91.201084,104.712855,120.226443,138.038426,158.489319,181.970086,208.929613,239.883292,275.42287,316.227766,363.078055,416.869383,478.630092,549.540874,630.957344,724.43596,831.763771,954.992586,1096.478196),
                 y=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.00044816,0.00127554,0.00221488,0.00324858,0.00438312,0.00559138,0.00686054,0.00817179,0.00950625,0.01085188,0.0122145,0.01362578,0.01514366,0.01684314,0.01880564,0.02109756,0.0237676,0.02683182,0.03030649,0.0342276,0.03874555,0.04418374,0.05119304,0.06076553,0.07437854,0.09380666,0.12115065,0.15836926,0.20712933,0.26822017,0.34131335,0.42465413,0.51503564,0.60810697,0.69886817,0.78237651,0.85461023,0.91287236,0.95616228,0.98569093,0.99869001,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999,0.99999999))

library(drc)
fm <- drm(y ~ x, data = df, fct = G.3())

moment <- function(k){
    f <- function(x){
        x ^ (k - 1) * pmax(0, 1 - predict(fm, data.frame(x = x)))
    }
    k * integrate(f, lower = min(df$x), upper = max(df$x))$value
}

E1 <- moment(1)
E2 <- moment(2)
SD <- sqrt(E2 - E1 ^ 2)

关于r - 在 R 中拟合累积分布后,根据拟合参数创建正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44311477/

相关文章:

r - 在接近度上匹配两个 R 数据帧

r - 聚类分析(层次)中如何了解群体信息?

r - 如何使用存储在 data.table 或 tibble 中的线性模型添加预测列?

html - 如何增加 bookdown 中侧边栏菜单的宽度

java四元数3D旋转实现

python - 无需数学 log python 即可获取对数

c# - .NET 的最佳 NoSQL DB 来存储大量带有时间索引的分钟统计数据?

java - 关于整数值

r - 当 R 中的卡方检验生成警告时,如何执行 Fisher 精确检验 `fisher.test()`?

r - 如何使用 ggplot2 为曲线下的区域着色