-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ff6d069
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Central Limit Theorem | ||
======================================================== | ||
|
||
This is a simple Shiny web application using R demonstrating the Central Limit Theorem. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
library(shiny) | ||
|
||
shinyServer(function(input, output) { | ||
|
||
data <- reactive({ | ||
if (input$dist=="rpois") { | ||
vals <- do.call(input$dist, list(n=input$n, lambda=1)) | ||
} | ||
else { | ||
vals <- do.call(input$dist, list(n=input$n)) | ||
} | ||
return (list(fun=input$dist, vals=vals)) | ||
}) | ||
|
||
output$plot <- renderPlot({ | ||
distname <- switch(input$dist, | ||
rexp = "Exponential distribution", # (n = ", | ||
rnorm = "Normal distribution", # (n = ", | ||
rlnorm = "Log-normal distribution", # (n = ", | ||
runif = "Uniform distribution", # (n = ", | ||
rpois = "Poisson distribution", | ||
rcauchy = "Cauchy distribution") # (n = ") | ||
|
||
n <- input$n | ||
k <- input$k | ||
pdist <- data()$vals | ||
|
||
if (input$dist=="rpois") { | ||
x <- replicate(k, do.call(input$dist, list(n=n, lambda=1))) | ||
} | ||
else { | ||
x <- replicate(k, do.call(input$dist, list(n=n))) | ||
} | ||
ndist <- rowMeans(x) | ||
expect <- switch(input$dist, | ||
rexp = c(1^-1, 1^-2), | ||
rnorm = c(0, 1), | ||
rlnorm = c(exp(0+(1/2)*1^2), exp(0 + 1^2)*(exp(1^2)-1)), | ||
runif = c(0.5, (1/12)*1), | ||
rpois =c(1, 1), | ||
rcauchy = rep(NA, 2)) | ||
obs <- data.frame(pdist=c(mean(pdist), var(pdist)), ndist=c(mean(ndist), | ||
var(ndist))) | ||
|
||
par(mfrow=c(2,1)) | ||
pdens=density(pdist) | ||
phist=hist(pdist, freq=FALSE, plot=FALSE) | ||
hist(pdist, main=paste(n, " observations from ", distname, sep=""), | ||
xlab="Values", freq=FALSE, ylim=c(0, max(pdens$y, phist$density))) | ||
lines(pdens, col="red", lwd=2) | ||
abline(v=obs$pdist[1], col="blue", lwd=2, lty=2) | ||
abline(v=expect[1], col="red", lwd=2, lty=2) | ||
legend(x="topright", col=c("blue", "red"), lwd=2, lty=2, | ||
legend=c("Observed", "Expected")) | ||
box() | ||
|
||
ndens=density(ndist) | ||
nhist=hist(ndist, freq=FALSE, plot=FALSE) | ||
hist(ndist, main=paste("Distribution of mean values from ", k, | ||
" random variables each\nconsisting of ", n, | ||
" observations from ", distname, sep=""), | ||
xlab="Values", freq=FALSE, ylim=c(0, max(ndens$y, nhist$density))) | ||
lines(ndens, col="red", lwd=2) | ||
abline(v=obs$ndist[1], col="blue", lwd=2, lty=2) | ||
abline(v=expect[1], col="red", lwd=2, lty=2) | ||
legend(x="topright", col=c("blue", "red"), lwd=2, lty=2, | ||
legend=c("Observed", "Expected")) | ||
box() | ||
|
||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
library(shiny) | ||
|
||
shinyUI(pageWithSidebar( | ||
|
||
headerPanel("Central Limit Theorem"), | ||
|
||
sidebarPanel( | ||
radioButtons("dist", "Parent distribution:", | ||
list("Exponential" = "rexp", | ||
"Normal" = "rnorm", | ||
"Log-normal" = "rlnorm", | ||
"Uniform" = "runif", | ||
"Poisson" = "rpois", | ||
"Cauchy (pathological: momentless)" = "rcauchy")), | ||
br(), | ||
|
||
sliderInput("n", | ||
"Number of observations from parent distribution:", | ||
value = 500, | ||
min = 2, | ||
max = 1000), | ||
br(), | ||
|
||
sliderInput("k", | ||
"Number of random variables sampled from parent distribution:", | ||
value = 10, | ||
min = 2, | ||
max = 400) | ||
), | ||
|
||
mainPanel( | ||
plotOutput("plot", height="900px") | ||
) | ||
)) |