Skip to content

Instantly share code, notes, and snippets.

@ZeccaLehn
Created October 1, 2017 21:19
Show Gist options
  • Save ZeccaLehn/849ae412247606079169af28b7f003b2 to your computer and use it in GitHub Desktop.
Save ZeccaLehn/849ae412247606079169af28b7f003b2 to your computer and use it in GitHub Desktop.
Binomial Probabilities in R using Manipulate Package
binomProbs <- function(w, n, propStep){

  # print(w)
  # print(n)
  # print(propStep)
  
  plot(seq(0, 1, propStep), dbinom(w, n, prob = seq(0,1, propStep)), xlim = c(0, 1),
       xlab = "", ylab = "")
  points(seq(0, 1, propStep), 
         dbinom(w, n, seq( 0, 1, propStep))/
           sum(dbinom( w, n, seq(0, 1, propStep))), col = "red")

  print(paste0("Sum Unadjusted: ", round(sum(dbinom(w, n, seq(0,1,propStep))),2)))
  print(paste0("Sum Adjusted: ", sum(dbinom(w, n, seq(0,1,propStep))/sum(dbinom(w, n, seq(0,1,propStep))))))

}

## Dynamic Ggplot Output:
library(manipulate)
manipulate(
  
  binomProbs(w, n, propStep),
  w = slider(min = 1, max = 500, step = 1, initial = 6),
  n = slider(min = 2, max = 500, step = 1, initial = 9),
  propStep = slider(min = .001, max = .2, step = .001, initial = .01)
  
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment