[R] Sampling a matrix with different probability distributions
Silvia Lomascolo
slomascolo at gmail.com
Tue May 5 17:52:11 CEST 2009
I need to sample a matrix according to different distributions, instead of
just randomly. Here is some code that will hopefully clarify what I need:
I have a matrix M of 1287 interactions between species in rows and species
in columns, according to their abundance:
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 1287 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
Thanks to help from people in this forum, I was able to randomly sample 800
interactions from matrix M and obtain a subset of the interactions in a
smaller matrix called reduced.M:
M.index <- 1:length(M)
reduced.M <- matrix(table( factor( sample(rep(M.index,M),800),
M.index)),nr=5)
reduced.M
[,1] [,2] [,3] [,4] [,5]
[1,] 77 62 56 25 15
[2,] 83 53 51 21 11
[3,] 57 34 28 18 10
[4,] 51 31 21 14 4
[5,] 27 21 19 6 5
Now I need to sample again, not randomly, but according to different
distributions. For example, I need to sample according to the abundance of
species pla, (pla vector written above). The result should be that I sample
my first row more intensely than my second row, and the last row should be
the least intensely sampled, in proportion to my row species abundance. In
the same token, I want to sample with a uniform distribution as well. How
do I do this?
Thanks, as usual! Silvia.
--
View this message in context: http://www.nabble.com/Sampling-a-matrix-with-different-probability-distributions-tp23390324p23390324.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list