Search code examples
rloopsdplyrnormal-distribution

Sampling from a normal distribution using a for loop


So I am trying to sample from a uniform distribution 1000 times each time computing the mean of 20 random samples from said uniform distribution.

Now let's loop through 1000 times, sampling 20 values from a uniform distribution and computing the mean of the sample, saving this mean to a variable called sampMean within a tibble called uniformSampleMeans.
{r 2c}

unif_sample_size = 20 # sample size
n_samples = 1000 # number of samples

# set up q data frame to contain the results
uniformSampleMeans <- tibble(sampMean = runif(n_samples, unif_sample_size))


# loop through all samples.  for each one, take a new random sample, 
# compute the mean, and store it in the data frame

for (i in 1:n_samples){
  uniformSampleMeans$sampMean[i] = summarize(uniformSampleMeans = mean(unif_sample_size))
}

I successfully generate a tibble, however the values are "NaN". Additionally when I get to my for loop I get an error.

Error in summarise_(.data, .dots = compat_as_lazy_dots(...)) : argument ".data" is missing, with no default

Any insight would be much appreciated!


Solution

  • Given that you've tagged this as a dplyr question, you can use summarise_all:

    library(dplyr)
    
    n_obs = 20 
    n_samples = 1000 
    
    samples <- data.frame(matrix(runif(n_obs * n_samples), nrow = 20))
    
    summarise_all(samples, mean)
    

    As others have noted, it's possible to do this in base R, too.

    Update Per OP comment
    Yes, possible to use a for loop, although not advisable. Here's one approach:

    unif_sample_size = 20 
    n_samples = 1000 
    total_draws <- unif_sample_size * n_samples
    
    uniformSampleMeans <- 
      tibble(draw_from_uniform = runif(n_samples * unif_sample_size))
    
    sample_means <- vector(length = n_samples)
    
    i <- 1
    for (ix in seq(1, total_draws, by = unif_sample_size)) {
      start <- ix
      end <- ix + unif_sample_size - 1
      sample_means[i] <- mean(uniformSampleMeans$draw_from_uniform[start:end])
      i <- i + 1
    }