How can i draw samples increasing in size(increase is geometric in nature) from a population?

49 Views Asked by At

I am new to R programming and I need help with a problem. I am told that a population is normally distributed with a mean of 4 and a standard deviation of 4. The size of the population is 10000000. Then I am asked to draw 23 samples from this population, starting with a sample of size n = 1 and each successive sample bigger than the previous one by a factor of 2 i.e the first sample is size = 1, then the second is n = 2, then n = 4, then n = 8, then n = 16 .... to n= 2^23. I can do this by manually drawing each sample but I am looking for a way to automate that. Probably using a for loop? I know the factor of increase from one to the next is 2 but I can't seem to reason it out. Please help

1

There are 1 best solutions below

0
On

Here's an approach with base R.

set.seed(1)
Result <- sapply(0:22,function(x){rnorm(n = 2^x, mean = 4, sd = 4)})
Result[1:4]
#[[1]]
#[1] 1.494185
#
#[[2]]
#[1] 4.7345733 0.6574856
#
#[[3]]
#[1] 10.3811232  5.3180311  0.7181265  5.9497162
#
#[[4]]
#[1]  6.953299  6.303125  2.778446 10.047125  5.559373  1.515038 -4.858800  8.499724

This returns Result which is a list with 23 elements, each containing 2^n draws.

I can imagine that someone wants you to run mean on each one of those 23 draws. That's easy too.

sapply(Result,mean)
#[1] 1.494185 2.696029 5.591749 4.599666 4.579945 4.709584 4.289879 3.759900 4.030414 3.806070 3.930797 4.076481 3.939796 3.945899
#[15] 4.058098 3.959156 4.015014 3.989361 4.005028 4.002011 3.999206 3.999296 4.003509