[R] Sampling a matrix with different probability distributions
Greg Snow
Greg.Snow at imail.org
Tue May 5 18:57:57 CEST 2009
The sample function has a prob argument that can be used to sample with unequal probabilities. It sounds like you can just pass in the species abundance vector to prob and it will do what you want.
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Silvia Lomascolo
> Sent: Tuesday, May 05, 2009 9:52 AM
> To: r-help at r-project.org
> Subject: [R] Sampling a matrix with different probability distributions
>
>
> 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list