0

So... I have some issue...

Using R, I'm trying to simulate a Binomial distribution with n=10 and p=0.6, with size 1, and then, getting how many 1s and 0s I get, and this simulation should be repeated 200 times, so as to take a matrix of 200 rows and two columns with the number of 1s in the first column and the number of 0s in the second column.

I defined a function so as:

binom10.2 <- function(i){
  x <- rbinom(10,1,0.6)
  y <- as.vector(table(x))
  return(y)
}

which gives me a result such as:

> binom10.2(i)
[1] 5 5
> binom10.2(i)
[1] 7 3
> binom10.2(i)
[1] 5 5
> binom10.2(i)
[1] 4 6

I need this result, replicated 200 times, in a matrix, like this:

     [,1] [,2]
  [1,]    1    9
  [2,]    7    3
  [3,]    5    5
  [4,]    4    6
  [5,]    2    8
  [6,]    5    5
  [7,]    4    6
  [8,]    4    6
  [9,]    5    5
 [10,]    3    7
 [11,]    1    9
 [12,]    3    7
 [13,]    4    6
 [14,]    5    5
 [15,]    2    8

The thing is, that I wrote this code so as to get it:

proportions10 <- t(sapply(1:200, binom10.2)
proportions10 <- t(replicate(200, binom10.2(i)))

I think both ways are the same, BUT, when running both of them, I sometimes get the desired matrix, and sometimes get a list, like this:

     [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]      [,10]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,11]     [,12]     [,13]     [,14]     [,15]     [,16]     [,17]     [,18]     [,19]     [,20]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,21]     [,22]     [,23]     [,24]     [,25]     [,26]     [,27]     [,28]     [,29]     [,30]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,31]     [,32]     [,33]     [,34]     [,35]     [,36]     [,37]     [,38]     [,39]     [,40]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,41]     [,42]     [,43]     [,44]     [,45]     [,46]     [,47]     [,48]     [,49]     [,50]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,51]     [,52]     [,53]     [,54]     [,55]     [,56]     [,57]     [,58]     [,59]     [,60]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,61]     [,62]     [,63]     [,64]     [,65]     [,66]     [,67]     [,68]     [,69]     [,70]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,71]     [,72]     [,73]     [,74]     [,75]     [,76]     [,77]     [,78]     [,79]     [,80]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,81]     [,82]     [,83]     [,84]     [,85]     [,86]     [,87]     [,88]     [,89]     [,90]    
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,91]     [,92]     [,93]     [,94]     [,95]     [,96]     [,97]     [,98]     [,99]     [,100]   
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,101]    [,102]    [,103]    [,104]    [,105]    [,106]    [,107]    [,108]    [,109]    [,110]   
[1,] Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2 Integer,2
     [,111]    [,112]    [,113]    [,114]    [,115]    [,116]    [,117]    [,118]    [,119]    [,120]   

For me it has no sense that sometimes, depending on how many times I run the code, I get the first desired matrix, and some other times, I get the second list, useless. I would like the first matrix to always appear no matter how many times I run the code...

Could anyone help?

Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • The `i` in your function does not refer to any expression. – Pierre L Nov 28 '16 at 17:30
  • Try removing the i from the header `function()` so that the function takes no argument. Then use `t(replicate(200, binom10.2()))` – Pierre L Nov 28 '16 at 17:34
  • hmmm... I still get the same error, eliminating the (i) in the function I still get the second list instead of the desired matrix... and when trying running the code many times, I finally get the desired matrix, but this is what drives me crazy as this should not happen... I should get either one or another, but not both depending on random code running – Emilio Mármol Sánchez Nov 28 '16 at 17:47

3 Answers3

1

Just try this:

# define parameters
n <- 10     # number of trials
p <- 0.6    # probability of success
nexp <- 200 # number of experiments

# run the simulation
res <- replicate(200, rbinom(1, size=n, prob=p))
cbind(res, n-res)

to get the output:

        res  
  [1,]   7 3
  [2,]   8 2
  [3,]   6 4
  [4,]   7 3
  [5,]   5 5
  [6,]   6 4
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
0

Sums of the 1's show the number of successes in each replication. subtracting that number from 10 gives the number failures.

Here is my way:

 x = replicate(200, expr={sum(rbinom(10, 1, .6))})
 m = matrix(0, nrow=200, ncol=2)
 m[,2]=x
 m[,1]=10-x
A.Yazdiha
  • 1,336
  • 1
  • 14
  • 29
  • Wow thanks, I should have tried creating a matrix and then adding the values... Very useful way to do that ! ! ! But still dont know why the way I was trying to do sometimes did showed the desired results and some others didnt. Anyway, thanks for the simple way to handle it – Emilio Mármol Sánchez Nov 28 '16 at 17:55
  • So I see why `t(replicate(200, binom10.2()))` should work, but the `1:200` argument in the `sapply` is argument to the function `binom10.2`, where actually is not an argument, since the function `binom10.2` should take no argument. Maybe also edit your main post about the `i` as input to the function. – A.Yazdiha Nov 28 '16 at 19:52
0

There is no need for replicate here. rbinom(n, size, prob) directly gives you the number of successes for n independent trials. You want n = 200 observations: each observation is a trial of size = 10 and the prob is 0.6.

r <- rbinom(200, 10, 0.6)

Since you want this in a matrix with the second column = number of zeros observed: subtract r from 10 to obtain the second column:

bin10.2 <- matrix(c(r, 10 - r), nrow = 200)
Vidhya G
  • 2,250
  • 1
  • 25
  • 28
  • agree with you, this is just to satisfy OP's requirement of running 200 experiments, which is generally done through replication, not by specifying number of observations, just a different way of doing the same thing. – Sandipan Dey Nov 29 '16 at 06:41