#Generate a sample of 40 random variables from exponential distribution
#Find the mean of each sample
#Repeat the simulation 10000 times
#Take the mean of the samples (distribution mean)
#Create the data frame
sim_data <- data.frame(nrow=10000, ncol=2)
names(sim_data)=c("Index", "Mean")
for (i in 1:10000)
{
sim_data[i,1] <- i
sim_data[i,2] <- mean(rexp(40, 0.8)) #Mean of each round (sample) of simulation
}
#Calculate the mean of the simulated data
sim_mean <- mean(sim_data$Mean)
print(sim_mean)
## [1] 1.249556
#Theorical mean is 1/lambda
theo_mean <- 1/0.8
theo_mean
## [1] 1.25
#Plot a histogram of the mean distribution
library(ggplot2)
p<- ggplot(data = sim_data, aes(sim_data$Mean))+ geom_histogram(binwidth=0.1, color='blue')
p<- p +labs(title="Exponential Simulation") + labs(x="Sample Mean")+labs(y="Frequency")
p <- p+ theme(plot.title = element_text(hjust = 0.5))
p<- p+ geom_vline(aes(xintercept=sim_mean, colour = "Simulated Mean"))
p<- p+ geom_vline(aes(xintercept= theo_mean, colour = "Theorical Mean"))
p<- p+ scale_colour_manual("",breaks = c("Simulated Mean", "Theorical Mean"), values = c("red", "yellow"))
print(p)
## Warning: Use of `sim_data$Mean` is discouraged. Use `Mean` instead.

#Variance of the observed (simulated) values
sim_var <- var(sim_data$Mean)
print(sim_var)
## [1] 0.03988849
#For theorical values the variance is
theo_var<- (1/(0.8^2))/40
print(theo_var)
## [1] 0.0390625
#Plot density function
p<- ggplot(data = sim_data, aes(sim_data$Mean))+ geom_histogram(aes(x=sim_data$Mean, ..density..), binwidth=0.2, fill= 'red', color='black')
p<- p+ stat_function(fun=dnorm, color="blue", args=list(mean=mean(sim_data$Mean), sd=sd(sim_data$Mean)))
p<- p +labs(title="Exponential Simulation Density") + labs(x="Sample Mean")+labs(y="Density")
p <- p+ theme(plot.title = element_text(hjust = 0.5))
print(p)
## Warning: Use of `sim_data$Mean` is discouraged. Use `Mean` instead.
## Warning: Use of `sim_data$Mean` is discouraged. Use `Mean` instead.
