【问题标题】:Log-scale transformation of histogram and fittiting gamma curve直方图的对数尺度变换和拟合伽马曲线
【发布时间】:2021-07-01 12:27:36
【问题描述】:

我有一堆数据,我已经拟合了伽马分布。我的直方图和拟合曲线很好,但现在我想绘制一个直方图和带有 x 的对数刻度曲线。使用 scale_x_log10 对直方图效果很好,但我不能让它对 stat_function/geom_line 起作用。

我知道这是因为 stat_function 现在采用日志值,但我不确定如何事先转换 gamma 函数以使其正常工作。以下是相关图片和代码sn-ps:

这是原图:

fit.gamma2 <- fitdist(myvalues[,1],distr="gamma",method="mme")

ggplot(myvalues, aes(x = V1)) +
  geom_histogram(aes(y =..density..),
                 boundary = 0,
                 binwidth = sirina,
                 col="black",
                 fill="blue",
                 alpha=.2) +
  stat_function(fun=dgamma,
                args=list(shape = fit.gamma2$estimate["shape"],
                          rate = fit.gamma2$estimate["rate"])) +
  labs(title="Histogram žarkov + Gama porazdelitev (MM)",
       x = "Medprihodni časi (s)",
       y = "Gostota")

这是使用 scale_x_log10 后的同一张图。红色曲线应该是拟合曲线,但明显偏离了。

ggplot(myvalues, aes(x = V1)) +
  geom_histogram(aes(y =..density..),
                 boundary = 0,
                 binwidth = sirina_log,
                 col="black",
                 fill="blue",
                 alpha=.2) + 
  stat_function(fun=dgamma,
                args=list(shape = fit.gamma2$estimate["shape"],
                          rate = fit.gamma2$estimate["rate"])) +
  # geom_line(aes(x=V1,y=dgamma(V1,fit.gamma2$estimate["shape"], fit.gamma2$estimate["rate"])), color="red", size = 1) +
  scale_x_log10()

我已尝试应用 10**x 形式的值,但由于我的原始数据范围在 0.1 和 800 之间,一些值然后转义为 Inf。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您需要根据log10 的导数转换您的PDF。首先为转换后的 PDF 创建一个函数:

    dgammalog10 <- function(x, shape, rate) {
      return(x*log(10)*dgamma(x, shape, rate))
    }
    

    然后您可以使用 fun=dgammalog10 之前的 fun=dgamma

    【讨论】:

    • 哦,当然,我是个白痴。我确实做了一个单独的函数,但我没有乘以 log(10),而是一直在做 10 的幂。非常感谢。
    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多