r - R中的混合β分布
问题描述
我得到了一个函数(混合了 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)
只是想知道这是否是正确的方法?我怎样才能使用密度函数来做到这一点?谢谢!
解决方案
您可以像这样在 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
祝你好运!
推荐阅读
- arrays - 无法快速将数组数据加载到类数组中
- facebook - MVC5 Facebook 身份验证 - 帐户/ExternalLoginCallback?error=access_denied#_=_
- ios - 应用程序运行时无法将蓝牙耳机与 AVAudioSessionCategoryPlayAndRecord 连接
- python-3.x - networkx 和 dict_values 导致浮点参数错误
- java - 通过代码将图像加载到 imageview 将不会加载,除非我之前通过 xml 将图像提供给 imageview
- discord.py - 与python不和谐的日期和时间错误
- java - 如何在 .NET 和 android 之间共享大文件
- angular - Angular 6 错误:无法从伪造的异步测试中生成 XHR。请求网址:https://mlj0xk2yy.com/latest/static/videos
- node-sass - node-sass 在所有项目中缺少供应商文件夹
- jquery - 我如何遍历html表单并单独获取后代