Discussion:
[R] rbinom for a matrix
ACroske
2008-07-09 17:30:25 UTC
Permalink
I have a large matrix full of probabilities; I would like to convert each
probability to a 1 or a 0 using rbinom.
How can I do this on the entire matrix? The matrix was converted from a
raster ArcMap dataset, so the matrix is essentially a map. Because of this,
I have no column headings.
Thanks!
--
View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18366867.html
Sent from the R help mailing list archive at Nabble.com.
Erik Iverson
2008-07-09 18:42:54 UTC
Permalink
Is this what you're looking for?

test <- matrix(runif(100, 0, 1), nrow = 20)
nr <- nrow(test)
matrix(sapply(test, rbinom, n = 1, size = 1), nrow = nr)
Post by ACroske
I have a large matrix full of probabilities; I would like to convert each
probability to a 1 or a 0 using rbinom.
How can I do this on the entire matrix? The matrix was converted from a
raster ArcMap dataset, so the matrix is essentially a map. Because of this,
I have no column headings.
Thanks!
Ben Bolker
2008-07-09 19:13:52 UTC
Permalink
Post by ACroske
I have a large matrix full of probabilities; I would like to convert each
probability to a 1 or a 0 using rbinom.
How can I do this on the entire matrix? The matrix was converted from a
raster ArcMap dataset, so the matrix is essentially a map. Because of this,
I have no column headings.
Thanks!
How about

matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))

or (perhaps marginally more efficiently?)

y <- (runif(m)<m)
storage.mode(y) <- "double"

Ben Bolker
Dylan Beaudette
2008-07-09 19:42:42 UTC
Permalink
Post by Ben Bolker
Post by ACroske
I have a large matrix full of probabilities; I would like to convert each
probability to a 1 or a 0 using rbinom.
How can I do this on the entire matrix? The matrix was converted from a
raster ArcMap dataset, so the matrix is essentially a map. Because of
this, I have no column headings.
Thanks!
How about
matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
or (perhaps marginally more efficiently?)
y <- (runif(m)<m)
storage.mode(y) <- "double"
Ben Bolker
Wait a second. Are you trying to convert each probability into the most
likely '1' or '0' through rounding? The code example above will give you a
different answer every time you run it. Is that what you are looking for?

Just curious,

Dylan
--
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341
Ben Bolker
2008-07-09 19:59:21 UTC
Permalink
Dylan Beaudette wrote:
| On Wednesday 09 July 2008, Ben Bolker wrote:
|> ACroske <Audy3272 <at> yahoo.com> writes:
|>> I have a large matrix full of probabilities; I would like to convert
each
|>> probability to a 1 or a 0 using rbinom.
|>> How can I do this on the entire matrix? The matrix was converted from a
|>> raster ArcMap dataset, so the matrix is essentially a map. Because of
|>> this, I have no column headings.
|>> Thanks!
|> How about
|>
|> matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
|>
|> or (perhaps marginally more efficiently?)
|>
|> y <- (runif(m)<m)
|> storage.mode(y) <- "double"
|>
|> Ben Bolker
|>
|
| Wait a second. Are you trying to convert each probability into the most
| likely '1' or '0' through rounding? The code example above will give
you a
| different answer every time you run it. Is that what you are looking for?
|
| Just curious,
|
| Dylan
|

~ I assumed that since the original poster said "convert each
probability to a 1 or 0 using rbinom" that they did indeed want
a random assignment, rather than rounding ...
Dylan Beaudette
2008-07-09 20:35:30 UTC
Permalink
Post by Ben Bolker
|>> I have a large matrix full of probabilities; I would like to convert
each
|>> probability to a 1 or a 0 using rbinom.
|>> How can I do this on the entire matrix? The matrix was converted from a
|>> raster ArcMap dataset, so the matrix is essentially a map. Because of
|>> this, I have no column headings.
|>> Thanks!
|>
|> How about
|>
|> matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
|>
|> or (perhaps marginally more efficiently?)
|>
|> y <- (runif(m)<m)
|> storage.mode(y) <- "double"
|>
|> Ben Bolker
|
| Wait a second. Are you trying to convert each probability into the most
| likely '1' or '0' through rounding? The code example above will give
you a
| different answer every time you run it. Is that what you are looking for?
|
| Just curious,
|
| Dylan
~ I assumed that since the original poster said "convert each
probability to a 1 or 0 using rbinom" that they did indeed want
a random assignment, rather than rounding ...
Sorry about not including a reference to whom I was addressing. Meant that
comment to go to the original poster.
--
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341
ACroske
2008-07-09 20:24:57 UTC
Permalink
Yes I do want a random assignment, instead of rounding. (From what I
understand of the rbinom command, it will randomly assign 1 or 0, and the
higher the given probability, the higher the likelihood of a 1... Feel free
to correct me if I'm wrong!)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
|>> I have a large matrix full of probabilities; I would like to convert
each
|>> probability to a 1 or a 0 using rbinom.
|>> How can I do this on the entire matrix? The matrix was converted from a
|>> raster ArcMap dataset, so the matrix is essentially a map. Because of
|>> this, I have no column headings.
|>> Thanks!
|> How about
|>
|> matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
|>
|> or (perhaps marginally more efficiently?)
|>
|> y <- (runif(m)<m)
|> storage.mode(y) <- "double"
|>
|> Ben Bolker
|>
|
| Wait a second. Are you trying to convert each probability into the most
| likely '1' or '0' through rounding? The code example above will give
you a
| different answer every time you run it. Is that what you are looking for?
|
| Just curious,
|
| Dylan
|
~ I assumed that since the original poster said "convert each
probability to a 1 or 0 using rbinom" that they did indeed want
a random assignment, rather than rounding ...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFIdRiZc5UpGjwzenMRAuLkAKCA6ZhjrVsg5RJGYFGwq+6vz2pWxgCfetYk
9V/ZPGi2jfVGV5jx+LGPs8s=
=d8c9
-----END PGP SIGNATURE-----
______________________________________________
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.
--
View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18370010.html
Sent from the R help mailing list archive at Nabble.com.
ACroske
2008-07-09 19:48:01 UTC
Permalink
Ben:
Thanks for the reply. One further question, and this is where my novice
status at R shows through. The code makes sense, but what would I put it for
"m"? Is it the same number for all three (that was my first thought since it
was the same placeholder for all three). Number of rows in the matrix is 56.
There are 2576 total cells (observations). Each cell has its own
probability.
Thanks for your help!
Post by Ben Bolker
Post by ACroske
I have a large matrix full of probabilities; I would like to convert each
probability to a 1 or a 0 using rbinom.
How can I do this on the entire matrix? The matrix was converted from a
raster ArcMap dataset, so the matrix is essentially a map. Because of this,
I have no column headings.
Thanks!
How about
matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
or (perhaps marginally more efficiently?)
y <- (runif(m)<m)
storage.mode(y) <- "double"
Ben Bolker
______________________________________________
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.
--
View this message in context: http://www.nabble.com/rbinom-for-a-matrix-tp18366867p18369250.html
Sent from the R help mailing list archive at Nabble.com.
Ben Bolker
2008-07-10 13:58:15 UTC
Permalink
Post by ACroske
Thanks for the reply. One further question, and this is where my novice
status at R shows through. The code makes sense, but what would I put it for
"m"? Is it the same number for all three (that was my first thought since it
was the same placeholder for all three). Number of rows in the matrix is 56.
There are 2576 total cells (observations). Each cell has its own
probability.
Thanks for your help!
Post by Ben Bolker
matrix(rbinom(length(m),prob=m,size=1),nrow=nrow(m))
or (perhaps marginally more efficiently?)
y <- (runif(m)<m)
storage.mode(y) <- "double"
m is your matrix of probabilities; for
example, you might do

m = as.matrix(read.table("mystuff.txt"))

(or import it from ArcGIS, or whatever).

length(m) will be computed automatically (=2576),
as will nrow(m) (=56).

Erik Iverson's solution should work too, but
I think mine will be much more efficient -- picking
a whole slug of random numbers at once is much
faster than looping and picking them one at a time.

Ben Bolker

Loading...