The p-value of the binomial test

The p-value is estimated by the proportion of simulated successes that are at least as extreme as the data.

show with app
  • app.R
library(shiny)

ui <- fluidPage(
  titlePanel("The p-value of the binomial test"),
  p(HTML("The p-value is estimated by the proportion of simulated successes
          that are at least as extreme as the data.")),
  sidebarLayout(
    sidebarPanel(
      sliderInput("p0", "Hypothesized parameter value",
                  min = 0, max = 1, value = .5, step = .05),
      sliderInput("stat", "Observed number of successes (data)",
                  min = 0, max = 40, value = c(14, 20), step = 1),
      actionButton("action", "Resample")
    ),
    mainPanel(
      plotOutput("scatterPlot")
    )
  )
)

server <- function(input, output) {
  r <- reactive({
    input$action
    p0   <- input$p0
    stat <- input$stat[1]
    n    <- input$stat[2]
    x    <- rbinom(50, n, prob = p0)
    list(
      p0    = p0,
      stat  = stat,
      n     = n,
      x     = x,
      pcond = abs(x - p0*n) >= abs(stat - p0*n)
    )
  })

  output$scatterPlot <- renderPlot({
    p0    <- r()$p0
    stat  <- r()$stat
    n     <- r()$n
    x     <- r()$x
    pcond <- r()$pcond

    par(mai = c(.7, .7, .5, .1), mgp = c(2, 0.7, 0))
    plot(0:50, c(stat, x), ylim = c(0, n), type = "n",
         ylab = "Number of successes",
         xlab =
           bquote("Replications of experiment under "~H[0]: pi == .(p0)))
    abline(h = c(2*p0*n - stat, p0*n, stat), col = "gray")
    points(0, stat, pch = 4, lwd = 2, col = "darkblue")
    text(0, stat, "Data", pos = 3, col = "darkblue", xpd = TRUE)
    points(x, pch = c(1, 16)[pcond + 1])
    title(paste0("Simulated p-value: ",
          mean(pcond),
          ", theoretical p-value: ",
          round(binom.test(stat, n, p = p0)$p.value, 3)))
  })
}

shinyApp(ui, server)