library(ggplot2)
t<-1000 #number of simulation
n<-40 #number of samples
l<-0.2 #lambda
ctr<-1/l
#simulatated results
simdata<-sapply(1:t,function(x){
mean(rexp(n,l))
})
Mean, Standard Deviation and variance of simulated data
mean(simdata)
## [1] 5.002
sd(simdata)
## [1] 0.7763
var(simdata)
## [1] 0.6026
Mean is close enough to the center of distribution 5.
Theoretical Standard Deviation and Variance
(1/l)/sqrt(n)
## [1] 0.7906
((1/l)/sqrt(n))^2
## [1] 0.625
Include both results: exponential distribution and normal distribution
normal<-rnorm(t,mean=ctr,sd=ctr/sqrt(n))
data<-c(simdata,normal)
type<-as.factor(c(rep('simulation', length(simdata)), rep('normal', length(normal))))
df<-data.frame(val=data,type=type)
Draw Simulated Distribution and Normal Distribution
ggplot(df,aes(x=val,fill=type)) +
geom_histogram(aes(y=..density..),binwidth=.1,colour="black", fill="white") +
geom_density(alpha=.2) +
geom_vline(aes(xintercept=ctr),color="red",linetype="solid", size=1) +
geom_vline(aes(xintercept=mean(data, na.rm=T)),color="blue",linetype="solid", size=1) +
scale_x_continuous(breaks = 1:10) +
scale_y_continuous(name = "Density")
t.test(simdata,normal)
##
## Welch Two Sample t-test
##
## data: simdata and normal
## t = 1.251, df = 1992, p-value = 0.2111
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.02538 0.11478
## sample estimates:
## mean of x mean of y
## 5.002 4.957
Both simulated and theoretical diestribution are shown in green and red. The mean of simulaterd mean is presented vertical blue line while the theoretical mean should be in red line and they are very close each other.
The confidence interval is below:
intervals<-mean(data) + c(-1,1) * 1.96 * sd(data)/sqrt(n)
intervals
## [1] 4.732 5.227