public
Created

Binomial Proportion Post Gists

  • Download Gist
Y_samp.R
R
1
Y_samp <- sum(Zsamp)
betaplot.R
R
1 2 3 4 5 6 7 8
betaplot <- function(a,b){
theta = seq(0,1,0.005)
p_theta = dbeta(theta, a, b)
p <- qplot(theta, p_theta, geom='line')
p <- p + theme_bw()
p <- p + ylab(expression(paste('p(',theta,')', sep = '')))
p <- p + xlab(expression(theta))
return(p)}
genPop.R
R
1 2 3 4 5
## Generate population
N = sample(seq(100000,400000),1)
A = round(theta_true*N)
B = N - A
Zpop <- sample(c(rep(1,A),rep(0,B)))
med_high_prior.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 0.95
n = 100
pr <- prior(m,n)
lk <- likelihood(N_samp,Y_samp)
po <- posterior(m,n,N_samp,Y_samp)
model_plot <- data.frame('Dist'=c(rep('Prior',nrow(pr)),
rep('Likelihood',nrow(lk)),
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist <- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po <- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po <- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po <- sd_of_posterior(m,n,N_samp,Y_samp)
med_low_prior.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 0.05
n = 100
pr <- prior(m,n)
lk <- likelihood(N_samp,Y_samp)
po <- posterior(m,n,N_samp,Y_samp)
model_plot <- data.frame('Dist'=c(rep('Prior',nrow(pr)),
rep('Likelihood',nrow(lk)),
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist <- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po <- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po <- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po <- sd_of_posterior(m,n,N_samp,Y_samp)</code>
popSamp.R
R
1 2 3
### Pull random sample from population
N_samp <- 500
Zsamp <- sample(Zpop,N_samp)
str_eq_prior.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 0.5
n = 500
pr <- prior(m,n)
lk <- likelihood(N_samp,Y_samp)
po <- posterior(m,n,N_samp,Y_samp)
model_plot <- data.frame('Dist'=c(rep('Prior',nrow(pr)),
rep('Likelihood',nrow(lk)),
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist <- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po <- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po <- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po <- sd_of_posterior(m,n,N_samp,Y_samp)</code>
thetaTrue.R
R
1 2 3
######### 1. Unknown Probability of Success ########
## Using built-in R pseudo-random number generator
theta_true <- runif(1,0,1)
unif_prior.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 0.5
n = 2
pr <- prior(m,n)
lk <- likelihood(N_samp,Y_samp)
po <- posterior(m,n,N_samp,Y_samp)
model_plot <- data.frame('Dist'=c(rep('Prior',nrow(pr)),
rep('Likelihood',nrow(lk)),
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist <- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po <- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po <- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po <- sd_of_posterior(m,n,N_samp,Y_samp)
weak_eq_prior.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 0.5
n = 10
pr <- prior(m,n)
lk <- likelihood(N_samp,Y_samp)
po <- posterior(m,n,N_samp,Y_samp)
model_plot <- data.frame('Dist'=c(rep('Prior',nrow(pr)),
rep('Likelihood',nrow(lk)),
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist <- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po <- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po <- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po <- sd_of_posterior(m,n,N_samp,Y_samp)</code>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.