Created
October 31, 2012 06:42
-
-
Save robbymeals/3985488 to your computer and use it in GitHub Desktop.
Binomial Proportion Post Gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Pull random sample from population | |
N_samp <- 500 | |
Zsamp <- sample(Zpop,N_samp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######### 1. Unknown Probability of Success ######## | |
## Using built-in R pseudo-random number generator | |
theta_true <- runif(1,0,1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Y_samp <- sum(Zsamp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment