Question

In: Math

I used this code for parts a-d: x=runif(1e6,1,5) mean(x) var(x) hist(x,main="Histogram of x",xlab="x",ylab="f(x)",border="blue",col="green",freq=F) Write R code...

I used this code for parts a-d:

x=runif(1e6,1,5)
mean(x)
var(x)
hist(x,main="Histogram of x",xlab="x",ylab="f(x)",border="blue",col="green",freq=F)

Write R code that does the following: Let Xi be a random variable uniformly distributed between 1 and 5. (a) Find E[Xi ] and Var(Xi).

(b) Generate 1000000 samples from a random variable X that has a uniform density on [1,5]. x=runif(1e6,1,5)

(c) Create a histogram of the distribution. hist(x,freq=F) Explain the shape of histogram. Is it consistent with the uniformly distributied rv between 1 and 5? See how to add details such as colors and labels in Homeworks 3, 5.

d) Now we will withdraw n samples out of distribution and take the average. We will repeat the process 1e6 times! and make a histogram of the sampling distribution.

I used this R code for part e through f:Sample_Avg <- function(t){mean(runif(t,1,5))}
avg(30) > 3

rep(avg(30),1e6)

e) In a script file create a function Sample_Avg that will withdraw n samples and take the average. Sample Avg <- function(t){ mean(runif(t,1,5) } Run Sample Ave(30) This will be the average of 30 samples

f) Use replicate command to create 1000000 of these sample averages and save results in Xavg. Be patient it will take about a min.

g) Create a histogram of Xavg. Wow! it looks different compared to the original distribution. Why is that? What is the shape of this sampling distribution? What are the parameters.

h) Overlay the distribution function from g) onto the histogram of Xave. See Homeworks 3,5 for commands for overlaying function on top of a histogram.

i) Compute the theoretical probability P(X30 > 3.5) .In R how would you find the percentage of Xavg that is greater than 3.5?? Execute the code and compare it to the theoretical calculation.

Please answer g, h and i. Thank you

Solutions

Expert Solution

(e)

Created the function Sample_Avg with the given script.

> Sample_Avg <- function(t) { mean(runif(t,1,5))}
> Sample_Avg(30)
[1] 3.04055

The output of Sample_Avg(30) is 3.04055

(b)

Using replicate command to create 1000000 samples of Sample_Avg.

Xave <- replicate(n = 1000000, Sample_Avg(30))

(c)

The histogram of Xave is,

hist(Xave, col = "green")

Because of Central Limit theorem, the shape of the sampling distribution of sample mean is Normal distribution with parameters = (1 + 5)/2 = 3 and = (b - a) / = (5 - 1) / = 0.2108185   

(h)

Run the below R code to overlay the distribution function.

h <- hist(Xave, col = "green")

xfit <- seq(min(Xave), max(Xave), length = 40)

yfit <- dnorm(xfit, mean = mean(Xave), sd = sd(Xave))

yfit <- yfit * diff(h$mids[1:2]) * length(Xave)

lines(xfit, yfit, col = "blue", lwd = 2)

(i)

The theoretical probability is,

P( > 3.5) = P[Z > (3.5 - 3) / 0.2108185] = P[Z > 2.37] = 0.0089

Code to find percentage of Xave that is greater than 3.5 is,

> sum(Xave > 3.5) * 100 / length(Xave)
[1] 0.8671

The percentage of Xave that is greater than 3.5 is 0.8671% 0.0087

The value 0.0087 is very near to the theoretical calculation (0.0089)


Related Solutions

Write a R script that calculates the mean and variance of two random variables X~F(m=2,n=5) and...
Write a R script that calculates the mean and variance of two random variables X~F(m=2,n=5) and Y~F(m=10,n=5) from their density functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT