Skip to content

Simulate nested data

9 messages · Andrea Goijman, Sarah Goslee, Greg Snow

#
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
<agoijman at cnia.inta.gov.ar> wrote:
--
Sarah Goslee
http://www.functionaldiversity.org
#
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
<agoijman at cnia.inta.gov.ar> wrote:
Sarah Goslee
http://www.functionaldiversity.org
#
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
<agoijman at cnia.inta.gov.ar> wrote:
--
Sarah Goslee
http://www.functionaldiversity.org
#
On Fri, Nov 9, 2012 at 3:12 PM, Andrea Goijman
<agoijman at cnia.inta.gov.ar> wrote:
Okay.

What do you know beforehand?
That you have 10 species?
That you have 3 groups?
That 4 species belong to group 1?

You don't really explain which are the inputs and which are the
outputs, and what form either will take.

Say you know just the first two:
ngroups <- 3
nspecies <- 10

species.groups <- sample(seq(1, ngroups, by=1), nspecies, replace=TRUE)
# gives nspecies assigned randomly to ngroups

# I still don't know what p is supposed to be: a vector for
# storing further simulations, one per species?
p <- rep(NA, nspecies)

# and for neatness:
mysims <- data.frame(species = seq(1, nspecies, by = 10), groups =
species.groups, p = p)