Frequentist inference, Bernoulli model
{r, echo=FALSE}
inputPanel(
sliderInput("p", label = "Probability (true)",
min = 0, max = 1, value = 0.3, step = 0.05),
sliderInput("n", label = "Sample size",
min = 10, max = 1000, value = 10, step = 10),
sliderInput("seed", label = "Random seed",
min = 0, max = 100, value = 0, step = 10)
)
renderPlot({
par(las = 1)
set.seed(input$seed)
y <- rbinom(n = 1000, size = 1, p = input$p)
pt <- seq(0, 1, by = 0.0005)
L <- sapply(pt, function(z)
prod(dbinom(y[1:input$n], size = 1, prob = z)))
plot(pt, L, type = "l", col="#3498db",
ylab = "Likelihood", xlab="p",
sub=paste0("Mean = ", round(mean(y[1:input$n]), 2), " (",
sum(1-y[1:input$n]), " 0s & ", sum(y[1:input$n]), " 1s)"),
main = paste("Estimate =", round(pt[which.max(L)], 2)))
abline(v = input$p, lwd = 2, col = "#c7254e")
abline(v = pt[which.max(L)], lwd = 2, col = "#18bc9c")
})