Search code examples
rsimulationprobability

Why does replicate() return a matrix?


> n_toss = 3; # Number of Tosses
> n_trial = 10; # Number of Trials
> 
> mySamples <- sample(c(1,0), n_toss, replace = T, prob=c(0.5,0.5))
> mySamples
[1] 1 0 1
> 
> mySamples <- replicate(n_trial, 
+              {
+                  mySamples <- sample(c(1,0), n_toss, replace = T, prob=c(0.5,0.5))
+              })
> mySamples
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    1    1    0    1    0    0    0    0     0
[2,]    1    0    1    1    1    0    0    0    0     0
[3,]    0    0    0    0    1    1    1    1    0     1
> 

I have two questions here:

  1. Why does mySamples return a matrix? Shouldn't it return the last (10th) vector as R keeps the last statement being evaluated?
  2. Is it possible to keep mySamples <- sample(c(1,0), n_toss, replace = T, prob=c(0.5,0.5)) this statement in a separate function and still obtain a same result?

Solution

  • replicate is a wrapper around sapply, so it will essentially in your case:

    • evaluate and return the results of the expression
    • do this n times - in your case n_trial = 10 times
    • combine those 10 results together - by default it will simplify them (into a matrix in your case). If you do not want the results simplified, use simplify = FALSE - in which case you would get a list (of 10 elements), one for each of the 10 replications.

    So to answer your questions directly:

    1) Yes, R does return the result of the last statement, but does this n_trial times and combines those results together. This is why you get a matrix (or a list, if you use simplify = FALSE in the call to replicate).

    2) Yes, you can do that, for example like so:

    n_toss <- 3
    n_trial <- 10
    
    sampleFun <- function(n_toss) {
      sample(c(1, 0), n_toss, replace = TRUE, prob = c(0.5, 0.5))
    }
    
    mySamples <- replicate(n_trial, sampleFun(n_toss = n_toss))