【问题标题】:A mixture of beta distribution in RR中的混合β分布
【发布时间】:2021-07-28 17:51:44
【问题描述】:

我被赋予了一个函数(beta 分布的混合):

f(x) = beta(1,6)/3 + beta(3,6)/3 + beta(10,6)/3

我的任务是绘制 f(x) 的密度。

这是我的代码:

par(mfrow=c(1,1))

x <- seq(0,1,0.001)

plot(x,dbeta(x, shape1 = 1, shape2 = 6)/3 + dbeta(x, shape1 = 3, shape2 = 6)/3 + dbeta(x, shape1 = 10, shape2 = 6)/3,col="blue",lwd=2)

只是想知道这是否是正确的方法吗?我怎样才能使用密度函数来做到这一点?谢谢!

【问题讨论】:

    标签: r density-plot beta-distribution mixture


    【解决方案1】:

    你可以在 ggplot2 中这样做:

    #FIRST WE CREATE THE VECTOR
    x <- seq(0, 1, by = 0.001)
    
    #THEN EACH OF OUR BETAS DISTR
    
    beta1 <- dbeta(x, shape1 = 1, shape2 = 6)/3
    beta2 <- dbeta(x, shape1 = 3, shape2 = 6)/3
    beta3 <- dbeta(x, shape1 = 10, shape2 = 6)/3
    
    #MIXTURE
    fx <- beta1 + beta2 + beta3
    
    #USE ggplot2 TO PLOT
    library(ggplot2)
    mixture <- ggplot()+
                  geom_line(aes(x, beta1, color = "fx1"),
                            size = 1.2,
                            linetype = "dotdash")+
                  geom_line(aes(x, beta2, color = "fx2"),
                            size = 1.2,
                            linetype = "dotdash")+
                  geom_line(aes(x, beta3, color = "fx3"),
                            size = 1.2,
                            linetype = "dotdash")+
                  geom_line(aes(x, fx, color = "mixture"),
                            size = 1.5)+
                  labs(title = "Mixture of 3 Betas",
                       x = "Sequence",
                       y = "Beta",
                       colour = "Beta Distr",
                       caption = "Data generated with dbeta()")
    
    mixture
    

    祝你好运!

    【讨论】:

    • 非常感谢,马里亚诺!这很有帮助!
    猜你喜欢
    • 2018-08-31
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多