Bayesian inferenceExample B of section 8.6 of Rice 3rd edition, page 290-293 with known variance 1 |
shinyServer(function(input, output) {
output$distPrior <- renderPlot({
require(mosaic)
require(latticeExtra)
require(readr)
trellis.par.set(theme=theme.mosaic())
mu_data <- parse_number(input$mu_data)
mu_prior <- parse_number(input$mu_prior)
n <- parse_number(input$n)
prior <- list(mu_prior, 1)
posterior <- list((n*mu_data + mu_prior)/(n+1), sqrt(1/(n+1)))
obs <- list(mu_data, sqrt(1/n))
# display the results
plot1 <- plotDist('norm', params=posterior, lwd=2, col="black", xlim=c(0, 10), lty=2)
plot2 <- plotDist('norm', params=prior, col="purple", lwd=2)
plot3 <- plotDist('norm', params=obs, lwd=2, col="green")
tmpplot <- plot1 + as.layer(plot2)
tmpplot + plot3
})
}
)
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Bayesian inference"),
p("Example B of section 8.6 of Rice 3rd edition, page 290-293 with known variance 1"),
sidebarLayout(
sidebarPanel(
p("Prior is purple, observed data are green, posterior is black [dashed]"),
numericInput("mu_prior", "prior mean:", 5,
min = 1, max = 10),
numericInput("mu_data", "mean of the data:", 2,
min = 1, max = 10),
numericInput("n", "number of observations:", 1,
min = 1, max = 20)),
# Show a plot of the generated distribution
mainPanel(plotOutput("distPrior"))
)
))