Shiny Web App: Cumulative Binomial Probability Curves

shiny

This is an example of a Shiny Web application that can calculate cumulative binomial probabilities on the fly.

If you read my previous post, you’ll remember that the R script invoked a function to calculate binomial probabilities based on lambda (the probability of an event happening), and the power value (or number of trials).

The idea is that while the probability of an individual event happening may be low, the cumulative probability of the event happening increases with the number of trials.

1 – (1 – λ)N

Here is an example of a Shiny Web App that allows us to manipulate the lambda values using a set of sliders and automatically update the probability curve.

To run this app, open the R Studio console and click File -> New File -> Shiny Web App and select either Single File to paste the ui.R and server.R codes together, or Multiple File to paste them separately.

Enjoy!

shiny

Additionally, if you are new to Shiny you can find my full tutorial on Sitepoint that describes how to build and run a Shiny app from scratch.

ui.R

library(shiny)

# Define UI for application that draws a probability plot
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Cumulative Binomial Probability Plot"),
  
  # Sidebar with a slider input for value of lambda
  sidebarLayout(
    sidebarPanel(
      sliderInput("lambda",
                  "Probability 1:",
                  min = 0,
                  max = 0.1,
                  value = 0.01),
      sliderInput("lambda2",
                  "Probability 2:",
                  min = 0,
                  max = 0.1,
                  value = 0.01),
      sliderInput("lambda3",
                  "Probability 3:",
                  min = 0,
                  max = 0.1,
                  value = 0.01)
    ),
    
    # Show a plot of the generated probability plot
    mainPanel(
      plotOutput("ProbPlot")
    )
  )
))

server.R

library(shiny)
library(ggplot2)
library(scales)

# Shiny Application
shinyServer(function(input, output) {
  
  # Reactive expressions
  output$ProbPlot <- renderPlot({
    
    # generate lambda based on input$lambda from ui.R
    l=0:1
    lambda <- seq(min(l), max(l), length.out = input$lambda)
    probability=lambda
    l2=0:1
    lambda2 <- seq(min(l2), max(l2), length.out = input$lambda2)
    probability=lambda
    l3=0:1
    lambda3 <- seq(min(l3), max(l3), length.out = input$lambda3)
    probability=lambda
    
    # generate trials based on lambda value
    muCalculation <- function(lambda, powers) {1 - ((1 - lambda)^powers)}
    probability_at_lambda <- sapply(input$lambda, muCalculation, seq(0, 100, 10))
    probability_at_lambda2 <- sapply(input$lambda2, muCalculation, seq(0, 100, 10))
    probability_at_lambda3 <- sapply(input$lambda3, muCalculation, seq(0, 100, 10))
    
    # draw the probability
    par(bg = '#191661', fg = '#ffffff', col.main = '#ffffff', col.lab = '#ffffff', col.axis = '#ffffff')
    plot(probability_at_lambda, type = "o", col = 'white', border = 'white', xlab="N", ylab ="Probability 1", ylim=c(0.0, 1.0))
    lines(probability_at_lambda2,type="o",col="red", xlab="N", ylab="Probability 2", ylim=c(0.0, 1.0))
    lines(probability_at_lambda3,type="o",col="green", xlab="N", ylab="Probability 3", ylim=c(0.0, 1.0))
    title(main="Cumulative Binomial Probability Plot")
  })
  
})

Leave a Reply

Your email address will not be published. Required fields are marked *