0

I was given a function (a mixture of beta distribution):

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

My task is to plot the density of f(x).

Here is my code:

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)

Just wondering if this is the right way to go about it? How can I use density function to do this? Thanks!

1 Answers1

0

You can do it in ggplot2 like this:

#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

Good luck!

Beta Mixture