Gaurav Kamath
2024-05-08
lambda <- 5 # These are user input in the app.
sample_size <- 5
no_simulations <- 10
# We use rpois to generate the numbers
data <- rpois(sample_size * no_simulations, lambda)
# We reshape into a matrix with each row corresponding to a simulation
simulations <- matrix(data, nrow=no_simulations, ncol = sample_size)
head(simulations)## [,1] [,2] [,3] [,4] [,5]
## [1,] 6 4 3 4 3
## [2,] 6 11 10 3 7
## [3,] 4 5 5 1 4
## [4,] 4 3 4 2 3
## [5,] 3 7 1 5 2
## [6,] 6 8 10 3 8
# We compute the means along the rows to get sample means for each simulation
mean_vector <- apply(simulations, 1, mean)
head(mean_vector)## [1] 4.0 7.4 3.8 3.2 3.6 7.0
Here we plot the histogram with the simulation means and draw a line at theoritical and sample mean
hist(mean_vector,
breaks = 50, # Bins can be varies by user in the app.
prob = TRUE,
col="lightblue",
main = "Means of exponential distributions simulations",
xlab = "Spread")
lines(density(mean_vector))
abline(v = lambda, col = "red")
xfit <- seq(min(mean_vector), max(mean_vector), length = 100)
yfit <- dnorm(xfit, mean = lambda)
abline(v = mean(mean_vector), col = "orange", pch=22)
legend('topright', c("Simulation", "Theoretical Values"),
bty = "n", lty = c(1,2), col = c("orange", "red"))