Skip to content

How do I sample "cases" within a matrix?

8 messages · jim holtman, Charles C. Berry, Olivier ETERRADOSSI +1 more

#
Hi R community,
I am trying to obtain a sample from a matrix but sample(my.matrix) doesn't
do what I need. I have a matrix of 1287 interactions between the species in
columns and the species in rows and I want to obtain a smaller matrix with
say, 800 interactions, that may or may not have the same number of columns
and/or rows (i.e., some interactions may not be retrieved in a smaller
sample). For example, my original mock matrix M is


     [,1] [,2] [,3] [,4] [,5]
[1,]  140  100   90   40   20
[2,]  126   90   81   36   18
[3,]   84   60   54   24   12
[4,]   70   50   45   20   10
[5,]   42   30   27   12    6

The command sample(my.matrix) samples whole cells from my matrix, such that
if the interaction between species 1 and 1 is included in the sample, they
always show 140 interactions.  But what I want is to sample "cases" within
each cell.  My sample matrix S could have =<140 interactions between species
1 and 1, =<100 between species 1 and 2, etc. Again, if some combination is
absent from the sample matrix, that's OK.

Here's my code, in case it helps:

pla<- c(10, 9, 6, 5, 3) #abundance of pla species
pol<- c(14, 10, 9, 4, 2) #abundance of pol species
m<-pla%*%t(pol) #matrix of interactions according to pla and pol abundance
m
     [,1] [,2] [,3] [,4] [,5]
[1,]  140  100   90   40   20
[2,]  126   90   81   36   18
[3,]   84   60   54   24   12
[4,]   70   50   45   20   10
[5,]   42   30   27   12    6

sample(m) #doesn't give me what I want...

I have searched the forum for an answer but all questions regarding sampling
from matrices refer to sampling whole rows or columns, not "cases" within a
matrix.  
Thanks in advance for any help! Silvia.
#
If you just want to shuffle the row around, this should do it.  By
limiting the number of samples, you can get a smaller matrix:
[,1] [,2] [,3] [,4] [,5]
[1,]  126   90   81   36   18
[2,]   42   30   27   12    6
[3,]   70   50   45   20   10
[4,]   84   60   54   24   12
[5,]  140  100   90   40   20
[,1] [,2] [,3] [,4] [,5]
[1,]  140  100   90   40   20
[2,]  126   90   81   36   18
[3,]   84   60   54   24   12
[4,]   70   50   45   20   10
[5,]   42   30   27   12    6

        
On Wed, Apr 29, 2009 at 1:36 PM, Silvia Lomascolo <slomascolo at gmail.com> wrote:

  
    
#
See below
On Wed, 29 Apr 2009, Silvia Lomascolo wrote:

            
If I understand you,
[1] TRUE
)),nr=5)
Chuck
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
#
Hi Silvia,
excuse me if I'm missing something but sampling the row numbers should make
it :

let M be your matrix, and spM a sample with n rows :
spM <- M[sample(seq(1,dim(M)[1],by=1), n),]

Regards, Olivier
Silvia Lomascolo wrote:

  
    
#
Well Silvia,
I understand that I didn't read your post carefully.
Forget about my previous (unrelevant) post.
regards. olivier
Olivier ETERRADOSSI wrote:

  
    
#
Hi again Silvia (last time... ?),
now : do I understand : you want 
1) to randomly select some intersections between rows and columns
2) randomly select a number of cases for each intersection (being <= the
number of initial cases ?

if yes, here is my solution, using your example :

# select intersections, put them in new matrix mm
mm<-m[sample(seq(1,5,by=1),2),sample(seq(1,5,by=1),3)]

# make a function that samples a number of cases
foo<-function(x) sample(seq(1,x,by=1),1)

# use mapply
mmm<-matrix(mapply(mm,FUN=foo),dim(mm)[1],dim(mm)[2])

On my computer it seems to work... hope this really help, this time !
Regards. Olivier
Silvia Lomascolo wrote:

  
    
#
Both Oliver's and Chuck's code seem to work.  Thank you very much!! I will
keep working on this and might get back to the forum for more help if I get
stuck.  Thanks, really.

PS: Does any of you know why your replies don't show up in the main forum? I
get the message that the message hasn't been posted but all of you did get
it.  I'm subscribed to the list.  I followed all the instructions to do it,
but it still says that I cannot post on the forum... I'm confused
Olivier ETERRADOSSI wrote:

  
    
#
Thank you! This seems to work!!!

Silvia.
Charles C. Berry wrote: