Created
October 31, 2012 15:52
-
-
Save robbymeals/3987839 to your computer and use it in GitHub Desktop.
Binomial Proportion Post Gists 2
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
### Function: Likelihood Plot Values | |
likelihood <- function(N,Y){ | |
a <- Y + 1 | |
b <- N - Y + 1 | |
dom <- seq(0,1,0.005) | |
val <- dbeta(dom,a,b) | |
return(data.frame('x'=dom, 'y'=val)) | |
} |
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
### Function: Mean of Posterior Beta | |
mean_of_posterior <- function(m,n,N,Y){ | |
a <- Y + (n*m) -1 | |
b <- N - Y + (n*(1-m)) - 1 | |
E_posterior <- a / (a + b) | |
return(E_posterior) | |
} |
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
### Function: Mode of Posterior Beta | |
mode_of_posterior <- function(m,n,N,Y){ | |
a <- Y + (n*m) -1 | |
b <- N - Y + (n*(1-m)) - 1 | |
mode_posterior <- (a-1)/(a+b-2) | |
return(mode_posterior) | |
} |
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
### Function: Posterior Plot Values | |
posterior <- function(m,n,N,Y){ | |
a <- Y + (n*m) -1 | |
b <- N - Y + (n*(1-m)) - 1 | |
dom <- seq(0,1,0.005) | |
val <- dbeta(dom,a,b) | |
return(data.frame('x'=dom, 'y'=val)) | |
} |
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
### Function: Prior Plot Values | |
prior <- function(m,n){ | |
a = n * m | |
b = n * (1 - m) | |
dom <- seq(0,1,0.005) | |
val <- dbeta(dom,a,b) | |
return(data.frame('x'=dom, 'y'=val)) | |
} |
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
### Function: Std Dev of Posterior Beta | |
sd_of_posterior <- function(m,n,N,Y){ | |
a <- Y + (n*m) -1 | |
b <- N - Y + (n*(1-m)) - 1 | |
sigma_posterior <- sqrt((a*b)/(((a+b)^2)*(a+b+1))) | |
return(sigma_posterior) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear robbymeals,
I am using your explanation and code to estimate the posterior beta distribution of a beta-binomial model. First of all, thanks a lot for sharing your post (http://www.r-bloggers.com/to-the-basics-bayesian-inference-on-a-binomial-proportion/), which I found very clear and useful.
Anyway, I would have some concerns about the function calculating the posterior distribution. In particular:
posterior <- function(m,n,N,Y){
a <- Y + (n_m) -1
b <- N - Y + (n_(1-m)) - 1
[...]
}
Why do you add a "-1" term in the calculus of posterior alpha and beta parameters? The relationship between alpha_prior with alpha_posterior, and beta_prior with beta_posterior turns out to be alpha_posterior = Y + alpha_prior and beta_posterior = N - Y + beta_prior, respectively.
Which is the meaning of this "-1"? Maybe am I missing something?
Thanks a lot for any explanation,
kind regards,
Elia