【问题标题】:Fit Gamma distribution and plot by factor in R在 R 中按因子拟合 Gamma 分布和绘图
【发布时间】:2018-04-02 19:55:40
【问题描述】:

我有 data.frame 具有数字列金额和分类列欺诈的对象:

amount <- [60.00, 336.38, 119.00, 115.37, 220.01, 60.00, 611.88, 189.78 ...]

fraud <- [1,0,0,0,0,0,1,0, ...]

我想将伽马分布拟合到数量,但要通过factor(fraud) 绘制它。 我想要一个图表,它将向我显示 2 种不同颜色的 2 条曲线,以区分这 2 组(欺诈/非欺诈组)。

这是我到目前为止所做的:

fit.gamma1 <- fitdist(df$amount[df$fraud == 1], distr = "gamma", method = "mle")
plot(fit.gamma1)

fit.gamma0 <- fitdist(df$amount[df$fraud == 0], distr = "gamma", method = "mle")
plot(fit.gamma0)

我使用了这个参考: How would you fit a gamma distribution to a data in R?

【问题讨论】:

    标签: r statistics


    【解决方案1】:

    也许你想要的是

    curve(dgamma(x, shape = fit.gamma0$estimate[1], rate = fit.gamma0$estimate[2]), 
          from = min(amount), to = max(amount), ylab = "")
    curve(dgamma(x, shape = fit.gamma1$estimate[1], rate = fit.gamma1$estimate[2]), 
          from = min(amount), to = max(amount), col = "red", add = TRUE)
    

    ggplot2

    ggplot(data.frame(x = range(amount)), aes(x)) + 
      stat_function(fun = dgamma, aes(color = "Non fraud"),
                    args = list(shape = fit.gamma0$estimate[1], rate = fit.gamma0$estimate[2])) +
      stat_function(fun = dgamma, aes(color = "Fraud"),
                    args = list(shape = fit.gamma1$estimate[1], rate = fit.gamma1$estimate[2])) +
      theme_bw() + scale_color_discrete(name = NULL)
    

    【讨论】:

    • 我很高兴@Julius!非常感谢!
    猜你喜欢
    • 2019-09-17
    • 1970-01-01
    • 2019-06-04
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多