using for statement to find variance of the sample mean

Hi!! How do I write a for statement that goes through all three steps and calculates var(X)

  1. Takes 100 samples from a standard normal distribution to calculate the sample mean
  2. store the value at vector X
  3. Repeat the above process 10,000 times to generate a vector X of length 10,000 times

I am currently in statistics course and studying R programming alone. I saw this question on a textbook and am completely stuck

Thanks in advance!

Hey,

you have to break this task into a few smaller ones. A for loop "loops" over an iterator (lets say i) for as often as you wish. In your case, you want to have one result per loop, so with 10,000 replications you have 10,000 results. To store them, you have to initialize a vector in advance, which can hold all your results. You can take this snippet, to find a working answer for your problem:

rep_len <- ... # insert a number of replications, like 10,000 instead of '...'

results <- ... # use a base R function to create an arbitrary vector with length 10,000

# seq_along(...) creates a counter, from 1 to length(...)
for (i in seq_along(results)){

  results[[i]] <- mean( ... ) # assign the mean of your random draws (which you have to create with a function that outputs random standard normal values) to the ith value in your results vector

}

var(results)
#> [1] 0.009846492

Created on 2022-11-01 with reprex v2.0.2

Good luck in figuring out the remaining bits! Feel free to ask further related questions if there are any left.

Kind regards

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.