Economics Guy
2008-Mar-11 15:15 UTC
[R] Generating a new matrix using rbinom and a matrix of probabilities.
I am having a little trouble getting R to do something without writing
a couple of very awkward loops.
I have a matrix of probabilities and I want to generate a new matrix
of ones and zeros where each element in the new matrix is the result
of a draw from a binomial distribution where the probability of
getting a 1 is the corresponding element in the matrix of
probabilities.
Example Code:
--------------------------------------------
## First I generate the matrix of probabilities for example purposes.
probMatrix <- matrix(NA,5,5){
for (i in 1:5)
probVectorI <- runif(5,0,1)
probMatrix[i,] <- probVectorI
}
# Now I want to take each element in probMatrix and use it as the
probability parameter in rbinom draw and generate a new matrix.
Something like this:
binomialMatrix <- rbinom(1,1,probMatrix)
# But that does not work. I know I can run a loop across each vector
of the matrix, but this seems like an bad way to do this.
---------------------------End Code--------------------------------
So any help would be appreciated.
Thanks,
EG
Patrick Burns
2008-Mar-11 15:43 UTC
[R] Generating a new matrix using rbinom and a matrix of probabilities.
I think the following does what you want: probMatrix <- matrix(runif(5 * 5), 5, 5) binomialMatrix <- matrix(rbinom(5 * 5, 1, probMatrix), 5, 5) Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Economics Guy wrote:>I am having a little trouble getting R to do something without writing >a couple of very awkward loops. > >I have a matrix of probabilities and I want to generate a new matrix >of ones and zeros where each element in the new matrix is the result >of a draw from a binomial distribution where the probability of >getting a 1 is the corresponding element in the matrix of >probabilities. > >Example Code: >-------------------------------------------- > >## First I generate the matrix of probabilities for example purposes. > >probMatrix <- matrix(NA,5,5){ > >for (i in 1:5) > >probVectorI <- runif(5,0,1) > >probMatrix[i,] <- probVectorI > >} > ># Now I want to take each element in probMatrix and use it as the >probability parameter in rbinom draw and generate a new matrix. >Something like this: > >binomialMatrix <- rbinom(1,1,probMatrix) > ># But that does not work. I know I can run a loop across each vector >of the matrix, but this seems like an bad way to do this. > >---------------------------End Code-------------------------------- > >So any help would be appreciated. > >Thanks, > >EG > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > > > >
Ben Fairbank
2008-Mar-11 15:44 UTC
[R] [PS] Generating a new matrix using rbinom and a matrix of probabilities.
I tried your code and could not get it to run on my installation of R,
so I may be missing something. But if you have a matrix of
probabilities (call it probs) and want to simulate random binomial
draws, can you not simply create a matrix of the same size of uniform
random numbers (runif()) (call it rands), then do comparisons, thus,
draws <- 0 + (rands < probs)
Ben Fairbank
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Economics Guy
Sent: Tuesday, March 11, 2008 10:16 AM
To: r-help at stat.math.ethz.ch
Subject: [PS] [R] Generating a new matrix using rbinom and a matrix of
probabilities.
I am having a little trouble getting R to do something without writing
a couple of very awkward loops.
I have a matrix of probabilities and I want to generate a new matrix
of ones and zeros where each element in the new matrix is the result
of a draw from a binomial distribution where the probability of
getting a 1 is the corresponding element in the matrix of
probabilities.
Example Code:
--------------------------------------------
## First I generate the matrix of probabilities for example purposes.
probMatrix <- matrix(NA,5,5){
for (i in 1:5)
probVectorI <- runif(5,0,1)
probMatrix[i,] <- probVectorI
}
# Now I want to take each element in probMatrix and use it as the
probability parameter in rbinom draw and generate a new matrix.
Something like this:
binomialMatrix <- rbinom(1,1,probMatrix)
# But that does not work. I know I can run a loop across each vector
of the matrix, but this seems like an bad way to do this.
---------------------------End Code--------------------------------
So any help would be appreciated.
Thanks,
EG
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.