Problem 2.

 

set.seed(1234)
n <- 100
mu0 <- 1 # mean for chisq(1)
m <- 100000   #number of replicates
p <- numeric(m)  #storage for p-values
 
  for (j in 1:m) { 
    x <- rchisq(n,1) #generate from chisq with 1 degree of freedom
    ttest <- t.test(x, mu = mu0) #conduct a t-test for each number generated
    p[j] <- ttest$p.value #store p-values in a vector p
    }

p.hat <- mean(p<.05) #calculates the proportion of p values that are less than .05
se.hat <- sqrt(p.hat * (1 - p.hat) / m) #calculates the standard error
chisq <- c("p.hat","se.hat")
pander(cbind(chisq, c(round(p.hat,3),round(se.hat,3))))
chisq  
p.hat 0.065
se.hat 0.001
n2 <- 100
mu0 <- 1 #mean for U(0,2)
m2 <- 100000          #number of replicates
p2 <- numeric(m2)     #storage for p-values
  
for (j2 in 1:m2) {
    x2 <- runif(n2, 0, 2) #generates random numbers from U(0,2)
    ttest2 <- t.test(x2, mu = mu0) #conducts a t-test for each number generated
    p2[j2] <- ttest2$p.value #stores p-values in a vector p2
    }

p.hat2 <- mean(p2<.05) #calculates the proportion of p values that are less than .05
se.hat2 <- sqrt(p.hat2 * (1 - p.hat2) / m2) #calculates the standard error
Uniform <- c("p.hat","se.hat")
pander(cbind(Uniform, c(round(p.hat2,3),round(se.hat2,3))))
Uniform  
p.hat 0.05
se.hat 0.001

 

The simulated type 1 error rate for the Uniform(0,2) distribution is still 0.05. The error rate for the Chisq(1) distribution has risen to 0.065, which is more than 1 standard error from 0.05.

 

Problem 3.

 

set.seed(1234)
n <- 100 # random sample
m <- 100000 # iterations
mu <- numeric(m)
d <- numeric(m)


for (i in 1:m) { # for loop to generate values of mu
  x <- rlnorm(n)
  mu[i] <- log(median(x))
}

mu1 <- sort(mu)

L <- mu1[5000] # finding a lower limit
U <- mu1[95000] # finding a upper limit

for (i in 1:m){ # for loop to determine whether the mu estimates are contained in the confidence interval
  if (L>mu[i]) {
      d[i] <- 0
  }
  else if (U < mu[i]) {
    d[i] <- 0
  }
  else {
    d[i] <- 1
  }
}

pander(cbind(c("estimate of mu","Lower Limit","Upper Limit","Confidence Level"), round(c(mean(mu),L,U,mean(d)),3)))
estimate of mu 0
Lower Limit -0.203
Upper Limit 0.205
Confidence Level 0.9

  We can be approximately 90% confident that the true value of mu is contained in (-0.203,0.205).