Created
April 7, 2014 01:40
-
-
Save stephensrmmartin/10013670 to your computer and use it in GitHub Desktop.
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) | |
library(ggvis) | |
colors <- c('black','red','green','grey') | |
shinyServer(function(input, output,session) { | |
population0 <- reactive({ | |
data.frame(Value = rnorm(n = 100000,mean = input$mu0,sd = input$sigma0)) | |
}) | |
population1 <- reactive({ | |
data.frame(Value = rnorm(n=100000,mean = input$mu1, sd = input$sigma1)) | |
}) | |
subsample <- function(d,ssize){ | |
ind <- sample(1:nrow(d),size = ssize, replace = TRUE) | |
mean(d[ind,'Value']) | |
} | |
samplingDist0 <- reactive({ | |
# invalidateLater(input$refresh,session = session); | |
ds0 <- population0() | |
data.frame(Means = replicate(n = input$sNum,expr = subsample(ds0,input$ssize))) | |
}) | |
samplingDist1 <- reactive({ | |
ds1 <- population1() | |
data.frame(Means = replicate(n = input$sNum, expr = subsample(ds1,input$ssize))) | |
}) | |
samplingDists <- reactive({ | |
s0 <- samplingDist0() | |
s1 <- samplingDist1() | |
ds <- data.frame(Means = rbind(s0,s1), group = rep(c('Null','Effect'),each=input$sNum)) | |
ds | |
}) | |
gv <- reactive({ | |
ggvis(samplingDists(), | |
by_group(factor(group)), | |
props(x = ~Means, | |
fillOpacity := input_slider(min = 0, | |
max = 1, | |
value = .7, | |
step = .1, | |
label = 'Opacity')), | |
layer_histogram()) | |
# dscale('x','numeric',domain=c(-5,5)) + | |
# guide_axis('x',title='Sample Mean') + | |
# guide_axis('y',title="Frequency") | |
}) | |
output$samplingDist <- observe_ggvis(gv, 'samplingDist', session) | |
output$plotControls <- renderControls(gv, session) | |
}) |
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) | |
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("Distribution of sample means"), | |
# Sidebar with a slider input for number of observations | |
sidebarPanel( | |
sliderInput("sigma0", | |
"Null Population Standard Deviation", | |
min = .01, | |
max = 10, | |
value = 1, | |
step = .05), | |
sliderInput("mu0", | |
"Null Population Mean", | |
min = -10, | |
max = 10, | |
value = 0, | |
step = 1), | |
sliderInput('sigma1', | |
'Effect Population Standard Deviation', | |
min = .01, | |
max = 10, | |
value = 1, | |
step = .05), | |
sliderInput('mu1', | |
'Effect Population Mean', | |
min = -10, | |
max = 10, | |
value = 0, | |
step = 1), | |
sliderInput("sNum", | |
"Number of samples", | |
min = 1, | |
max = 10000, | |
value = 100, | |
step = 2), | |
sliderInput("ssize", | |
"Sample Size", | |
min = 5, | |
max = 1000, | |
value = 20, | |
step = 5), | |
numericInput('refresh', | |
min = 500, | |
max = 60000, | |
label = 'Refresh rate (ms)', | |
value = 3000) | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
ggvis_output('samplingDist'), | |
ggvisControlOutput('plotControls') | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment