Using R loop to find probability

Viewed 116

I know the basic loop format, but I'm unsure how to incorporate 'population' into the loop to find the probability of collecting a sample with a mean of 42 or larger.

Use a loop to find out the probability of collecting a sample (n=10) with a mean of 42 (or larger) from the dataset produced by the following code:

set.seed(1)
population<-rnorm(n=500,mean=35,sd=10)
1 Answers

One approach to this problem is to repeatedly sample from population and compute the frequency that the mean of these samples is greater than or equal to 42.

set.seed(1); 
population <- rnorm(n=500, mean=35, sd=10)

nsim <- 100000             # the number of time we will do this
vec_mean <- numeric(nsim)  # a vector to hold the sample means

for (i in 1:nsim) {
  
  samp <- sample(population, size = 10, replace = TRUE)
  vec_mean[i] <- mean(samp)
}
  
sum(vec_mean >= 42) / nsim

# [1] 0.01727

This can be interpreted as the (frequentist) probability of collecting a sample of size 10 from this population with a mean of 42 or larger.

Related