In: Math
(Please answer this question accuratelly THANKS)
The following commands in R computes 5000 simulations of sample means of size 12 from a normal distribution with mean µ = 100 and standard deviation σ = 14. require
(fastR2) nsamplesum <- do(5000) * c(sample.mean=mean(rnorm(12,100,14)))
The following commands compute the approximate mean and standard deviation of the sample mean and plot the histogram giving the approximate distribution of the sample mean.
mean(∼ sample.mean, data=nsamplesum) sd(∼ sample.mean, data=nsamplesum) gf dhistogram(∼ sample.mean, data= nsamplesum, bins=20)
(a) Compare the approximate values of mean and standard deviation of the sample mean found above with the expected theoretical ones.
(b) Repeat the same simulation as above using now samples from a uniform distribution in the interval [−2, 4]. Also in this case, run a numerical test over 5000 simulations, compute mean and standard deviation of the sample mean, and compare it to the theoretical result.
a) Let X be a random variable having a normal distribution with mean and standard deviation
Let be the sample mean of a random sample of size n=12, selected from X. Using central limit theorem we know that has a normal distribution with mean and standard error of mean (standard deviation of )
Now we will use simulation to get 5000 values of and get the mean and standard deviations of these s and see if they match with waht we have above.
The following are the R commands (all statements starting with # are comments)
#install the package fastR2, if not already installed
install.packages('fastR2')
library(fastR2)
#set the random seed
set.seed(123)
#compute 5000 simulations of sample means (xbars) of size 12 from a
normal distribution
#with mean µ = 100 and standard deviation σ = 14
nsamplesum <- do(5000) *
c(sample.mean=mean(rnorm(12,100,14)))
#print the mean of sample means
mean(~ sample.mean, data=nsamplesum)
#print the standard deviation of sample means
sd(~ sample.mean, data=nsamplesum)
#plot the histogram of sample means
gf_dhistogram(~ sample.mean, data= nsamplesum, bins=20)
#get this output
The simulated mean of is 99.9918 compared to the theoretical value of 100
The simulated standard deviation of is 4.0658 compared to the theoretical value of 4.0415
We can see that the simulated values are close to the expected theoretical values
The following is the histogram giving the distribution of sample means
b) Let X be a random variable having a uniform in the interval [-2,4].
Using the standard results of uniform distribution we get
the mean of X as
and standard deviation of X as
Let be the sample mean of a random sample of size n=12, selected from X. Using central limit theorem we know that has a normal distribution with mean and standard error of mean (standard deviation of )
Now we will use simulation to get 5000 values of and get the mean and standard deviations of these s and see if they match with waht we have above.
The following are the R commands (all statements starting with # are comments)
#set the random seed
set.seed(123)
#compute 5000 simulations of sample means (xbars) of size 12 from a
uniform distribution
#in the interval [-2,4]
nsamplesum <- do(5000) *
c(sample.mean=mean(runif(12,min=-2,max=4)))
#print the mean of sample means
mean(~ sample.mean, data=nsamplesum)
#print the standard deviation of sample means
sd(~ sample.mean, data=nsamplesum)
#plot the histogram of sample means
gf_dhistogram(~ sample.mean, data= nsamplesum, bins=20)
# get this output
The simulated mean of is 1.0009 compared to the theoretical value of 1
The simulated standard deviation of is 0.4994 compared to the theoretical value of 0.50
We can see that the simulated values are close to the expected theoretical values
Get the following histogram showing the distribution of sample means