Created
October 4, 2016 17:44
-
-
Save ismayc/63438e63ebd18472c770eef27bb98b42 to your computer and use it in GitHub Desktop.
Shiny App for Checking n, p assumptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
inputPanel( | |
numericInput("n", label = "Sample size:", value = 300), | |
sliderInput("p", label = "Population proportion:", | |
min = 0, max = 1, value = 0.1, step = 0.01), | |
numericInput("x_min", label = "Min for x-axis:", value = 0, min = 0, max = 1), | |
numericInput("x_max", label = "Max for x-axis:", value = 1, min = 0, max = 1) | |
) | |
library(ggplot2) | |
library(shiny) | |
server <- function(input, output) { | |
output$histPlot <- renderPlot({ | |
pp <- data.frame(p_hat = rep(0, 5000)) | |
for(i in 1:5000){ | |
samp <- sample(c(TRUE, FALSE), input$n, replace = TRUE, | |
prob = c(input$p, 1 - input$p)) | |
pp$p_hat[i] <- sum(samp == TRUE) / input$n | |
} | |
bw <- diff(range(pp$p_hat)) / 30 | |
ggplot(data = pp, aes(x = p_hat)) + | |
geom_histogram(binwidth = bw) + | |
xlim(input$x_min, input$x_max) + | |
ggtitle(paste0("Distribution of p_hats, drawn from p = ", input$p, ", n = ", input$n)) | |
}) | |
} | |
ui <- fluidPage( | |
inputPanel( | |
numericInput("n", label = "Sample size:", value = 300), | |
sliderInput("p", label = "Population proportion:", | |
min = 0, max = 1, value = 0.1, step = 0.01), | |
numericInput("x_min", label = "Min for x-axis:", value = 0, min = 0, max = 1), | |
numericInput("x_max", label = "Max for x-axis:", value = 1, min = 0, max = 1) | |
), | |
mainPanel( | |
plotOutput("histPlot") | |
) | |
) | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment