There are still a lot of things that you did not specify, but maybe this will get you started:
n.groups <- sample(3:6, 1) n.species <- sample(2:10, n.groups, replace=TRUE) n.animals <- sample( 10:25, sum(n.species), replace=TRUE ) mu.g <- rnorm(n.groups, 100, 50) mu.species <- rnorm( sum(n.species), 0, 10 ) species.id <- rep( seq_len(sum(n.species)), n.animals ) group.id <- cut( species.id, c(0,cumsum(n.species)), labels=FALSE ) mydata <- data.frame( group=group.id, species=species.id, y= rnorm( sum(n.animals), mu.g[group.id]+mu.species[species.id], 2 ) ) plot( y ~ factor(species), data=mydata ) abline( v=cumsum(n.species)+0.5, lty=2, col='green' ) On Fri, Nov 9, 2012 at 1:12 PM, Andrea Goijman <[email protected]>wrote: > Thanks Sarah, > > Ok, I'll start over... forget the functions that I wrote before and lets > start from scratch. > Suppose I want to simulate data; for example n species that belong to g > groups. Lets say 10 species, where 4 belong to group 1, 3 to group 2, and > the rest to group 3. > I want to simulate those values... > How would you do? > > > On Fri, Nov 9, 2012 at 2:59 PM, Sarah Goslee <[email protected]> > wrote: > > > Andrea, > > > > I don't understand what you want. Why don't you start over, and > > explain what the inputs are, and what your desired outputs are. > > > > Using your previous code: > > > > # pre-specified number of groups > > groups <- 3 > > # what is species? It needs to be defined before > > # we can assign a value to a component of it > > species <- ??? > > > > # randomly assign between 1 and 10 > > # species per functional group for each group > > # specified in the groups variable > > species$groups<-as.integer(runif(groups,1,10)) > > > > # original code to set up p > > # I assumed this was supposed to create an > > # array of groups dimension, but apparently > > # that isn't true > > p<-array(NA,dim=c(species$groups)) > > > > # you wrote: > > # > I need to have a single value of p per species, and the total number > of > > # > elements in p would be whatever number results from > > # > species(group1)+species(group2)+species(group3) > > # which suggests to me that you do not need an array at all, but > > # simply a vector of the length of the number of species: > > p <- rep(NA, sum(species$groups)) > > > > # but that isn't clear to me because I'm not sure what you > > # mean by species(group1), since there is no species() > > # function defined > > > > > > > > > > On Fri, Nov 9, 2012 at 2:49 PM, Andrea Goijman > > <[email protected]> wrote: > > > Yes, I tried your suggestion, but it didn't help. It just creates a > > > tri-dimentional array for p... and that is not what I want p per > species > > > (within groups) to be an unbalanced array... > > > > > > I need to have a single value of p per species, and the total number of > > > elements in p would be whatever number results from > > > species(group1)+species(group2)+species(group3) > > > > > > > > > > > > > > > On Fri, Nov 9, 2012 at 2:40 PM, Sarah Goslee <[email protected]> > > wrote: > > >> > > >> Andrea, > > >> > > >> I simply meant that I couldn't run your code assigning a value to > > >> species$groups > > >> because the code didn't include any information about creating the R > > >> object species. Thus, I changed the name of that R object to > > >> speciesgroups and altered your code so that it runs by removing the > > >> erroneous c(). > > >> > > >> Did you try my suggestion, and did or did it not help your problem? > > >> > > >> Sarah > > >> > > >> On Fri, Nov 9, 2012 at 2:37 PM, Andrea Goijman > > >> <[email protected]> wrote: > > >> > Hi Sarah, > > >> > > > >> > Maybe I expressed myself wrong, but so far, I don't have the > species; > > >> > I'm > > >> > just wanting to generate simulated data. For example, creating an > > >> > unbalanced > > >> > (and random) number of species per group, and then run the for-loops > > >> > > > >> > for example: > > >> > Group1: 3 species > > >> > Group2: 5 species > > >> > Group3: 8 species > > >> > > > >> > then, I want to create an array "p" to be able to fill the following > > >> > loop > > >> > > > >> > for (g in 1:groups){ > > >> > for (i in 1:species[g]){ > > >> > p[i] <- rnorm(1, mu.p[g], tau.p[g]) > > >> > }#species > > >> > } > > >> > > > >> > > > >> > Andrea > > >> > > > >> > > > >> > > > >> > > > >> > On Fri, Nov 9, 2012 at 2:26 PM, Sarah Goslee < > [email protected]> > > >> > wrote: > > >> >> > > >> >> Hi Andrea, > > >> >> > > >> >> Without knowing what species is, I can't run your code as is, but > try > > >> >> this: > > >> >> > > >> >> > > >> >> groups<-3 > > >> >> speciesgroups <- as.integer(runif(groups,1,10)) > > >> >> # I'd use sample(1:10, groups, replace=TRUE) > > >> >> p<-array(NA,dim=speciesgroups) > > >> >> > > >> >> > > >> >> Mostly you're trying to use c() on something that's already a > vector. > > >> >> > > >> >> Sarah > > >> >> > > >> >> On Fri, Nov 9, 2012 at 2:16 PM, Andrea Goijman > > >> >> <[email protected]> wrote: > > >> >> > I know this seems like a very easy question (and maye it is) but > > I've > > >> >> > been > > >> >> > trying to simulate nested data and been unsucessful so far.. > > >> >> > > > >> >> > I want to simulate a varying number of species within a group; > and > > >> >> > then > > >> >> > create an array to store the results of my for-loop. For example: > > >> >> > > > >> >> > groups<-3 > > >> >> > species$groups<-as.integer(runif(groups,1,10)) #species per > > >> >> > functional > > >> >> > group > > >> >> > > > >> >> > ###create arrays to store results > > >> >> > > > >> >> > p<-array(NA,dim=c(species$groups)) > > >> >> > > > >> >> > So, far this is not working... > > >> >> > > > >> >> > Thanks! > > >> >> > > >> >> > > > > > > -- > > Sarah Goslee > > http://www.functionaldiversity.org > > > > > > -- > --- > Lic. Andrea Paula Goijman > Grupo Ecología y Gestión Ambiental de la Biodiversidad > IRB - INTA Castelar, Argentina > [email protected] > <http://inta.gob.ar/personas/goijman.andrea/> > http://inta.gob.ar/personas/goijman.andrea/ > > PhD Candidate > Georgia Cooperative Fish and Wildlife Research Unit > D.B. Warnell School of Forestry and Natural Resources > University of Georgia > Athens, GA 30602 USA > Tel. +706.206.4805 > [email protected] > > [[alternative HTML version deleted]] > > > ______________________________________________ > [email protected] 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. > > -- Gregory (Greg) L. Snow Ph.D. [email protected] [[alternative HTML version deleted]]
______________________________________________ [email protected] 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.

