Created
December 17, 2012 17:52
-
-
Save mrxiaohe/4320327 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) | |
cnorm <- function(n){ | |
val <- rnorm(n) | |
uval <- runif(n) | |
flag <- (uval <= 1 - 0.1) | |
val[!flag] <- 10 * val[!flag] | |
val | |
} | |
shinyServer(function(input, output){ | |
data <- reactive(function(){ | |
Distribution <- switch(input$Distribution, | |
Normal = rnorm, | |
Uniform = runif, | |
Lognormal = rlnorm, | |
Exponential = rexp, | |
"Contaminated Normal" = cnorm, | |
rnorm | |
) | |
Distribution(input$sam_size*2000) | |
}) | |
output$plot <- reactivePlot(function(){ | |
Distribution <- input$Distribution | |
sam_size <- input$sam_size | |
temp <- matrix(data(), ncol=2000) | |
xbars <- colMeans(temp) | |
SD <- sd(xbars) | |
MEAN <- mean(xbars) | |
ylimits <- range(hist(xbars, br=25)$density, density(xbars)$y) | |
hist(xbars, br=25, ylim=ylimits, | |
main=paste("Sampling Distribution of the Mean\nBased on a", Distribution, | |
"Distribution with n =", sam_size), freq=FALSE) | |
lines(density(xbars)) | |
curve(dnorm(x, mean=MEAN, sd=SD), add=T, lty=3) | |
}) | |
}) |
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) | |
# Define UI for application that plots random distributions | |
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("Central Limit Theorem"), | |
# Sidebar with a slider input for number of observations | |
sidebarPanel( | |
selectInput("Distribution", "Distribution:", | |
list("Normal", "Lognormal", "Contaminated Normal", "Exponential")), | |
br(), | |
sliderInput("sam_size", | |
"Sample size:", | |
min = 5, | |
max = 500, | |
value = 5) | |
), | |
mainPanel( | |
tabPanel("Plot", plotOutput("plot") | |
) | |
) | |
)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment