[R] Copying matrices with certain probabilities

David Winsemius dwinsemius at comcast.net
Sun Oct 4 17:39:15 CEST 2009


On Oct 4, 2009, at 9:36 AM, William Aue wrote:

> Hi all! Below is the problem I am struggling with.
>
> Let’s say I have a matrix (x) of values:
>
> #     [,1] [,2] [,3] [,4]
> #[1,]    1    3    2    1
> #[2,]    2    1    3    2
>
> I need to copy the items into a new matrix (y) with certain
> probability guidelines. I need to set it up such that:
>
> 1. for each item there is a probability (a) that the item will or will
> not be copied at all from x to y. If nothing is copied then the
> corresponding value in y should be 0.

That would be simple enough with binomial sampling using the specified  
probabilities.

?sample
?rbinom

Matrices in R can be thought of as folded vectors so the "*" operator  
acts element wise. If you populated a probability matrix, P, you could  
get useful results with M * P

> 2. If something is copied from x to y, then there should be a separate
> probability (b) that the items will be correctly copied to y with
> items that are incorrectly copied being randomly drawn from a
> geometric distribution.

So, why then did you not offer two 2 x 4 matrices of correct and  
incorrect probabilities to specify what you mean? And while you are at  
it can you also specify what sort of geometric distribution parameters  
you have in mind to go along with the "incorrect" draws?

?rgeom


>
> So y may look like the below example. Row 1 shows that [1,2] was
> copied and subsequently correctly copied but nothing else in [1,] was
> copied. In [2,], [2,1] was copied but subsequently incorrectly copied,
> [2,2] & [2,3] were not copied at all, and [2,4] was copied and
> correctly copied.
>
> #     [,1] [,2] [,3] [,4]
> #[1,]    0    3    0    0
> #[2,]    5    0    0    2
>
> I would also like to setup a loop for this process wherein if
> something doesn’t get copied at all during a previous pass, then there
> is the opportunity for it to be copied on a second/third/etc pass.

And can you clarify whether this would be the same matrix that I above  
suggested should have been offered in your original posting should the  
probabiliies increase if the item was not copied at all. If the  
probabilies should get altered , i.e, the (inhomogeneous) process has  
memory, then in what way should thoe probabilities change?

>
> I hope this is clear enough. I am still a novice at R, so any
> help/guidance that can be offered will be greatly appreciated.
>
OK, ... "and provide commented, minimal, self-contained, reproducible  
code." That would mean specifying your matrices along the lines of:

M <- matrix(c( 0, 3, 0,  0,  5, 0, 0, 2), nrow=2, byrow=TRUE)

...rather than expecting the responders to repeat that work.

> Best,
>
> Billy Aue


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list