Bayesian inferenceExercise 8 Chapter 8 of Rice 3rd edition, page 314 beta prior, likelihood is geometric, posterior is beta Rice problem: 363 hops/trials (including successes), 130 birds/samples |
shinyServer(function(input, output) {
output$distPrior <- renderPlot({
require(mosaic)
require(latticeExtra)
trellis.par.set(theme=theme.mosaic())
prior <- list(input$alpha, input$beta)
posterior <- list((input$alpha + input$n), (input$beta + input$y - input$n))
xvals <- seq(0, 1, length=101)
eps <- 0.05
maxy1 <- max(dbeta(xvals, prior[[1]], prior[[2]]))
maxy2 <- max(dbeta(xvals, posterior[[1]], posterior[[2]]))
plot1 <- plotDist('beta', params=prior,
ylim=c(0, min(12, max(maxy1, maxy2)+eps)), xlim=c(0, 1), lwd=2)
plot2 <- plotDist('beta', params=posterior,
lwd=2, col="green")
plot1 + as.layer(plot2)
})
}
)
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Bayesian inference"),
p("Exercise 8 Chapter 8 of Rice 3rd edition, page 314"),
p("beta prior, likelihood is geometric, posterior is beta"),
p("Rice problem: 363 hops/trials (including successes), 130 birds/samples"),
sidebarLayout(
sidebarPanel(
p("Prior is black, posterior is green"),
numericInput("alpha", "alpha parameter for Beta prior:", 1,
min = 1, max = 100),
numericInput("beta", "beta parameter for Beta prior:", 1,
min = 1, max = 100),
numericInput("y", "number of trials required (including successes):", 2,
min = 1, max = 400),
numericInput("n", "out of this many samples:", 1,
min = 1, max = 200)),
# Show a plot of the generated distribution
mainPanel(plotOutput("distPrior"))
)
))