kuriboh
9/29/2020
I want to simulate the central limit theorem.
With the inputs required are: population mean, population standard deviation, the users can see the effects of the number of simulations and the sample size on the distribution of the sample mean.
nsim = 10
nsam = 4
pop_mean = 0
pop_sd = 1
matrix_data <- matrix(rnorm(nsim * nsam, mean=pop_mean, sd=pop_sd), nrow=nsim, ncol=nsam)
sample_means <- apply(matrix_data, 1, mean)
matrix_data## [,1] [,2] [,3] [,4]
## [1,] -1.7689285 0.9738033 -2.02231692 -0.135686038
## [2,] -0.9769807 -0.7428236 -0.81423799 -0.724810822
## [3,] 0.5753947 -1.1352441 1.48914436 -0.548698817
## [4,] 0.7128147 -0.4303148 2.16688616 -0.471817880
## [5,] 1.4595616 -0.1700032 0.01436222 -1.547878805
## [6,] -0.8428305 0.4616640 1.55886877 -0.472174267
## [7,] 0.1061522 1.3819628 1.74074909 0.007046604
## [8,] -1.4868904 -0.3785397 0.11730153 1.102928537
## [9,] 0.8631169 -0.3459268 0.33262690 0.220310140
## [10,] 1.3356739 1.6246608 -0.88304187 0.485259470
## [1] -0.73828204 -0.81471328 0.09514904 0.49439205 -0.06098954 0.17638202
## [7] 0.80897768 -0.16130000 0.26753179 0.64063808
mainPanel(
h3('Central Limit Theorem Visualization'),
tabsetPanel(type='tabs',
tabPanel('Column plot', br(), plotOutput('output_column')),
tabPanel('Line plot', br(), plotOutput('output_line')),
tabPanel('Instruction', br(), div('Instruction: Use the sliders to modify parameters, which are use to randomly generate sample mean, which centers at the population means. You can see the effects of the number of simulations, as well as the size of sample on the theorem. With this, you can easily see what the Central Limit Theorem is.'))
)
)
sidebarPanel(
h3('Simulation parameters'),
sliderInput('nsim_slider', 'How many simulations?', 100, 10000, 100),
sliderInput('nsam_slider', 'How samples to simulate?', 10, 100, 10),
sliderInput('mean_slider', 'What is the population mean?', 0, 1000, 0),
sliderInput('sd_slider', 'What is the population standard deviation?', 1, 100, 1)
)
create_random_data <- reactive({
nsim <- input$nsim_slider
nsam <- input$nsam_slider
pop_mean <- input$mean_slider
pop_sd <- input$sd_slider
matrix_data <- matrix(rnorm(nsim * nsam, mean=pop_mean, sd=pop_sd), nrow=nsim, ncol=nsam)
sample_means <- apply(matrix_data, 1, mean)
data.frame(PMF=sample_means)
})
output$output_column <- renderPlot({
my_data <- create_random_data()
ggplot(my_data, aes(x=PMF)) + geom_histogram(fill='lightblue', bins=100, aes(y=..density..)) +
geom_vline(xintercept=input$mean_slider, color='red') +
stat_function(fun=dnorm, args=list(mean=mean(my_data$PMF), sd=sd(my_data$PMF)), color='yellow')
})
output$output_line <- renderPlot({
my_data <- create_random_data()
ggplot(my_data, aes(x=PMF)) + geom_density(adjust=0.1, color='orange') +
geom_vline(xintercept=input$mean_slider, color='red') +
stat_function(fun=dnorm, args=list(mean=mean(my_data$PMF), sd=sd(my_data$PMF)), color='green')
})