Hi, I want to sample from a distribution (say a normal distribution, for example) using vectors of the different parameters (i.e. the mean and standard deviation). That is, I have a list/vector of say 100 means and another of the corresponding 100 SD's, and I want a matrix of 100 rows (one for each mean and SD pair) each having 1000 random samples. Something like: sample_matrix = rnorm(n=1000, mean=vector_of_100_means, sd=vector_of_100_corresponding_SDs) Is this possible? Thanks for any help in advance.
Try something like: out <- matrix( rnorm( 1000 * 100, rep(vector_of_means, 1000), rep(vector_of_sds, 1000) ), nrow=100 ) -- 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 J.K. Bruxer > Sent: Tuesday, September 14, 2010 1:15 PM > To: r-help at r-project.org > Subject: [R] rnorm using vector of means and SDs > > Hi, > > I want to sample from a distribution (say a normal distribution, for > example) using vectors of the different parameters (i.e. the mean and > standard deviation). That is, I have a list/vector of say 100 means > and another of the corresponding 100 SD's, and I want a matrix of 100 > rows (one for each mean and SD pair) each having 1000 random samples. > > Something like: > > sample_matrix = rnorm(n=1000, mean=vector_of_100_means, > sd=vector_of_100_corresponding_SDs) > > Is this possible? Thanks for any help in advance. > > ______________________________________________ > 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.
I think t(mapply(rnorm,1000,vector_of_means,vector_of_sds)) will do what you want. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 14 Sep 2010, J.K. Bruxer wrote:> Hi, > > I want to sample from a distribution (say a normal distribution, for example) using vectors of the different parameters (i.e. the mean and standard deviation). That is, I have a list/vector of say 100 means and another of the corresponding 100 SD's, and I want a matrix of 100 rows (one for each mean and SD pair) each having 1000 random samples. > > Something like: > > sample_matrix = rnorm(n=1000, mean=vector_of_100_means, sd=vector_of_100_corresponding_SDs) > > Is this possible? Thanks for any help in advance. > > ______________________________________________ > 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. >
Hi: Here's a way to do it with mdply() in the plyr package: # Set up a two column data frame of parameters, whose column names are mean and sd: pars <- data.frame(mean = rpois(100, 10), sd = rpois(100, 5)) mysmp <- mdply(pars, rnorm, n = 1000)> dim(mysmp)[1] 100 1001 # 100 rows of samples of 1000 + sample identifier HTH, Dennis On Tue, Sep 14, 2010 at 12:14 PM, J.K. Bruxer < bruxerjk@univmail.cis.mcmaster.ca> wrote:> Hi, > > I want to sample from a distribution (say a normal distribution, for > example) using vectors of the different parameters (i.e. the mean and > standard deviation). That is, I have a list/vector of say 100 means and > another of the corresponding 100 SD's, and I want a matrix of 100 rows (one > for each mean and SD pair) each having 1000 random samples. > > Something like: > > sample_matrix = rnorm(n=1000, mean=vector_of_100_means, > sd=vector_of_100_corresponding_SDs) > > Is this possible? Thanks for any help in advance. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]