R bayes
library(dplyr)
library(tidyr)
library(ggplot2)
x <- seq(0, 1, 0.01)
n <- 5
y <- 3
alpha <- c(0.5, 0.5, 0.5, 1, 1, 1, 1.5, 1.5, 1.5)
beta <- c(0.5, 1, 1.5, 0.5, 1, 1.5, 0.5, 1, 1.5)
z.post <- as.data.frame(matrix(0, nrow = length(x), ncol = length(alpha)))
z.prio <- as.data.frame(matrix(0, nrow = length(x), ncol = length(alpha)))
for (i in c(1:9)) {
z.post[, i] <- dbeta(x, alpha[i] + y, n - y + beta[i])
z.prio[, i] <- dbeta(x, alpha[i], beta[i])
}
z.post <- select(z.post, post.V1 = V1, post.V2 = V2, post.V3 = V3, post.V4 = V4,
post.V5 = V5, post.V6 =V6, post.V7 = V7, post.V8 = V8, post.V9 = V9)
z.prio <- select(z.prio, prio.V1 = V1, prio.V2 = V2, prio.V3 = V3,
prio.V4 = V4, prio.V5 = V5, prio.V6 = V6, prio.V7 = V7, prio.V8 = V8,
prio.V9 = V9)
z.wide <- cbind(x,z.post,z.prio)
z <- gather(z.wide,bayes,value,post.V1:prio.V9)%>%mutate(group=substr(bayes,7,8))%>%mutate(co=substr(bayes,1,4))
ggplot(z)+
geom_line(aes(x,value,colour = factor(co)))+
facet_wrap(~ group)
library(tidyr)
library(ggplot2)
x <- seq(0, 1, 0.01)
n <- 5
y <- 3
alpha <- c(0.5, 0.5, 0.5, 1, 1, 1, 1.5, 1.5, 1.5)
beta <- c(0.5, 1, 1.5, 0.5, 1, 1.5, 0.5, 1, 1.5)
z.post <- as.data.frame(matrix(0, nrow = length(x), ncol = length(alpha)))
z.prio <- as.data.frame(matrix(0, nrow = length(x), ncol = length(alpha)))
for (i in c(1:9)) {
z.post[, i] <- dbeta(x, alpha[i] + y, n - y + beta[i])
z.prio[, i] <- dbeta(x, alpha[i], beta[i])
}
z.post <- select(z.post, post.V1 = V1, post.V2 = V2, post.V3 = V3, post.V4 = V4,
post.V5 = V5, post.V6 =V6, post.V7 = V7, post.V8 = V8, post.V9 = V9)
z.prio <- select(z.prio, prio.V1 = V1, prio.V2 = V2, prio.V3 = V3,
prio.V4 = V4, prio.V5 = V5, prio.V6 = V6, prio.V7 = V7, prio.V8 = V8,
prio.V9 = V9)
z.wide <- cbind(x,z.post,z.prio)
z <- gather(z.wide,bayes,value,post.V1:prio.V9)%>%mutate(group=substr(bayes,7,8))%>%mutate(co=substr(bayes,1,4))
ggplot(z)+
geom_line(aes(x,value,colour = factor(co)))+
facet_wrap(~ group)
评论
发表评论