Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to solve the following exercise with R (I am not allowed to use anot

ID: 2930829 • Letter: I

Question

I am trying to solve the following exercise with R (I am not allowed to use another software), but my R code doesn't really work (see below):

here is my code in R:

set.seed(2)

sims <- 200

s.size <- c(1, 5, 10, 1000)

means <- rep(NA, length(s.size))

sd <- rep(NA, length(s.size))

maxs <- rep(NA, length(s.size))

mins <- rep(NA, length(s.size))

M <- matrix(NA, nrow = sims, ncol = length(s.size))

for (j in 1:length(s.size)) {

for (i in 1:sims) {

    x <- runif(n = s.size[j], min=0, max=1)

    M[i,j] <- mean(x)

}

means[j] <- mean (M[ , j])

sd[j] <- sd(M[ , j])

maxs[j] <- max(M[ , j])

mins[j] <- min(M[ , j])

}

for (i in 1:4) {

h <- hist(M[ , i],

            freq = FALSE,

            ylab = "Frequency" , xlab = "",

            main = paste("Sample Size =", s.size[i]))

            x <- seq(mins[i], maxs[i], length = 1000)

            y <- dunif(x, min=0, max=1)

            lines(x, y, col="blue")

}

SampleSize <- as.character(s.size)

names(mins) <- SampleSize

names(maxs) <- SampleSize

maxs <- round(maxs, digits = 3)

mins <- round(mins, digits = 3)

maxs

mins

I used the uniform distribution instead of the exponential one (this is actually wrong) because I don't know how to integrate the exponential function in my code.

1. In this empirical exercise, we will illustrate the impact of sample size on the variance of the sample mean using what are called "Monte carlo methods". In monte carlo methods you create your own data and then evaluate the properties of functions of that data While the concepts at play in this question are (fairly) easy, it is not necessarily as easy to program the computer to have it do exactly what you want it to. Thus this question is about having you develop some of your programming skills In this question, we will work with data that are drawn from an exponential distribu- tion. If you are not familiar with the exponential distribution, look it up on Wikipedia or Wolfram MathWorld. If a random variable, xi, is distributed as an exponential, we denote this, xi ~ exp(A), where is the parameter governing the shape of the distri bution. For ri ~ exp(A), you can show (or look up) that E(r.) and V(ri) For the rest of this question, we will assume riexp(1) This question asks you to draw many samples of data from the distribution of ri. Each sample is distinguished by its number of observations, which we denote (as usual) N But in each question below, I will ask you to draw samples of size N many times. We will call these different samples replications and index them by the letter r- 1,... Thus the ith draw of x from the rth replication can be denoted rf. And the sample average of the N values of in the rth replication can be denoted . We can also take the sample average and variance across the R replications of , which we will denote 7 (note no r) and sz. s is our estimate of the variance of the sample mean from a sample of size N discussed extensively in lecture (a) Let N = 1 and R = 200. Calculate Tr for r = 1, . . . , 200 show them in a histogram Also calculate the across-replication average, 7, and sample variance, Sbarz Also calculate the across-replication average, 7, and sample variance, Sbara histogram. Also calculate the across-replication average, T, and sample variance, (b) Let N = 5 and R-200. Calculate Tr for r 1, . . . , 200 show thern in a histogram (c) Let N = 20 and R = 200. Calculate rr for r = 1, 200 show them in a (d) Let N = 1,000 and R = 200. Calculate rr for r = 1, , 200 show them in a histogram. Also calculate the across-replication average, 7, and sample variance, e) Based on your answers to the previous parts of this question, i. For each of N = 1, N = 5, N = 20, and N = 1,000: Does the distribution of T look more like an exponential distribution or a normal distribution? ii. Is your estimate of x close to E(ri-1 in each experiment? If not, why not? iii. Is your estimate of sa close to V(i) n each experiment? If not, why not

Explanation / Answer

> set.seed(1000)
> # We have lambda = 1 so mean= 1/lambda=1
> #to draw data from exponential distribution use rexp(n=5,rate =1). rate is same as lambda
> #An example of that
> x <- rexp(n=10,rate= 1)
> #so we got sample of 10 obs which follows exponential distribution.
> mean(x)
[1] 0.985424
> #Our first objective is to draw 1 obs from exponential with 200 replications.
> N1 <- rexp(n=1,rate = 1)
> matrixN1 <- matrix(N1, nrow =200, ncol =1)
> # thus we have 200 replications of sample size 1. This is because nrow uses formula from N1 to fill the remaining 199 places in the matrix ncol places each of them in different columns
> str(matrixN1)
num [1:200, 1:4] 0.752 0.752 0.752 0.752 0.752 ...
> #Thus we have matrix of 200 replications of N1
> means.N1 <- apply(matrixN1, 1, mean)
> mean(means.N1)
[1] 0.7519662
> var(means.N1)
[1] 0
> hist(means.N1)
> N2 <- rexp(n=5,rate = 1)
> matrixN2 <- matrix(N2, nrow =200, ncol =5)
> dim(matrixN2)
[1] 200 5
>#thus we can see that each of our sample of size 5 has been placed on different rows of the matrix
> str(matrixN2)
num [1:200, 1:5] 1.6899 0.6099 0.8516 2.2278 0.0966 ...
> means.N2 <- apply(matrixN2, 1, mean)
> mean(means.N2)
[1] 1.095163
> var(means.N2)
[1] 0.5886336
> hist(means.N2)
> N4 <- rexp(n=1000,rate = 1)
> matrixN4 <- matrix(N4, nrow =200, ncol =1000)
> str(matrixN4)
num [1:200, 1:1000] 3.7681 0.5671 0.1688 0.9623 0.0762 ...
> means.N4 <- apply(matrixN4, 1, mean)
> mean(means.N4)
[1] 1.00371
> var(means.N4)
[1] 0.2022453
> hist(means.N4)

If you need any further help with the code you can comment on the answer.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote