-
Notifications
You must be signed in to change notification settings - Fork 2
/
shiny_likelihoods.R
71 lines (63 loc) · 2.34 KB
/
shiny_likelihoods.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
library('shiny')
# read this: http://alexanderetz.com/2015/04/15/understanding-bayes-a-look-at-the-likelihood/
shinyApp(
ui = shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("p1", label = "p1",
min = 0, max = 1, value = 0.7, step = 0.01),
sliderInput("p2", label = "p2",
min = 0, max = 1, value = 0.5, step = 0.01),
sliderInput("k", label = "k",
min = 1, max = 50, value = 7, step = 1),
sliderInput("N", label = "N",
min = 1, max = 50, value = 10, step = 1),
br(),
br(),
textOutput("LRatio1"),
br(),
textOutput("LRatio2"),
tags$head(tags$style("#LRatio1, #LRatio2 { font-size: 25px; }"))
),
mainPanel(
plotOutput("LRplot", height="500px")
)
)
)),
server = function(input, output) {
plot.LR <- function(k, N, p1, p2) {
# adapted from blog mentioned above
MLE <- dbinom(k, N, k/N)
L1 <- dbinom(k, N, prob = p1) / MLE
L2 <- dbinom(k, N, prob = p2) / MLE
curve((dbinom(k, N, x) / max(dbinom(k, N, x))), xlim = c(0, 1),
ylab = "Likelihood", xlab = "Probability of correct answer",
las = 1, main = "Likelihood function for binomials", lwd = 3,
cex.axis = 1.5, cex.lab = 1.5, cex.main = 1.5)
points(p1, L1, cex = 2, pch = 21, bg = "cyan")
points(p2, L2, cex = 2, pch = 21, bg = "cyan")
lines(c(p1, p2), c(L1, L1), lwd = 3, lty = 2, col = "cyan")
lines(c(p2, p2), c(L1, L2), lwd = 3, lty = 2, col = "cyan")
abline(v = k/N, lty = 5, lwd = 1, col = "grey73")
}
get.LR <- function(k, N, p1, p2) {
MLE <- dbinom(k, N, k/N)
L1 <- dbinom(k, N, prob = p1) / MLE
L2 <- dbinom(k, N, prob = p2) / MLE
L1 / L2
}
output$LRplot <- renderPlot({
k <- input$k
N <- input$N
if (k <= N) plot.LR(k, N, input$p1, input$p2)
})
output$LRatio1 <- renderText({
LR_12 <- get.LR(input$k, input$N, input$p1, input$p2)
paste("L1 / L2: ", round(LR_12, 3))
})
output$LRatio2 <- renderText({
LR_21 <- 1 / get.LR(input$k, input$N, input$p1, input$p2)
paste("L2 / L1: ", round(LR_21, 3))
})
}
)