This question is a continuation from an earlier post, but with different a data set and more specifics added.
I am trying to the bootstrap the proportional occurrence of diet items for 7 individuals and calculate a sd() using the data below.
data <- structure(list(IndID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("P01",
"P02", "P03", "P04", "P05", "P06", "P07"), class = "factor"),
PreyGen = structure(c(1L, 1L, 1L, 1L, 6L, 5L, 4L, 5L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 4L, 5L, 5L, 5L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 5L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 2L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 1L, 4L, 1L, 5L, 4L, 5L, 4L, 4L, 4L, 5L, 4L,
4L), .Label = c("Beaver", "Bobcat", "Coyote", "Deer", "Elk",
"Raccoon"), class = "factor")), .Names = c("IndID", "PreyGen"
), class = "data.frame", row.names = c(NA, -100L))
The summary
of which look like this.
> summary(data)
IndID PreyGen
P01: 6 Beaver : 6
P02:23 Bobcat : 2
P03:12 Coyote : 4
P04:20 Deer :71
P05:21 Elk :16
P06: 7 Raccoon: 1
P07:11
There are 7 different individuals (IndID) of the same species and 6 prey species (PreyGen). Each individual ate a different number of prey in different proportions (this is the main difference from the earlier post).
My goal is to bootstrap the proportional occurrence of each diet item for each individual. The loop below generates five diets for each individual that were sampled with replacement. The data are stored as a list of the individuals, each of which contains a list of the sample diets.
EDIT added set.seed()
and full output for P01
set.seed(1)
BootIndDiet <- list()
IndTotboot <- list()
for(i in unique(data$IndID)){
for(j in 1:5){
BootIndDiet[[j]] <- prop.table(table(sample(data$PreyGen[data$IndID == i],
length(data$PreyGen[data$IndID == i]),replace = T)))
}
IndTotboot[[i]] <- BootIndDiet
}
The bootstrapped diets are specific to each individual (i) in proportion and sample size. The five bootstrapped samples for P01 are shown below as an example.
> IndTotboot[[1]]
[[1]]
Beaver Bobcat Coyote Deer Elk Raccoon
0.6666667 0.0000000 0.0000000 0.0000000 0.3333333 0.0000000
[[2]]
Beaver Bobcat Coyote Deer Elk Raccoon
0.8333333 0.0000000 0.0000000 0.0000000 0.1666667 0.0000000
[[3]]
Beaver Bobcat Coyote Deer Elk Raccoon
0.3333333 0.0000000 0.0000000 0.0000000 0.1666667 0.5000000
[[4]]
Beaver Bobcat Coyote Deer Elk Raccoon
0.6666667 0.0000000 0.0000000 0.0000000 0.1666667 0.1666667
[[5]]
Beaver Bobcat Coyote Deer Elk Raccoon
0.8333333 0.0000000 0.0000000 0.0000000 0.1666667 0.0000000
I am trying to calculate the sd()
of the proportional occurrence of each prey species for each individual. Equivocally, for each individual (P01 - P07) I want the sd()
of the proportional occurrence of each prey species across the 5 diets.
While my loop produces the correct results, am wondering how to calculate the sd()
of the bootstrapped diets for each prey species when the data are contained in nested lists.
I have only included 5 samples (bootstraps) for each individual here, but hope to generate 10000.
EDIT As per the suggestion by @MrFlick
An example output would look like this
P01 P02 P03 P04 P05 P06 P07
Beaver A
Bobcat B
Coyote C
Deer D
Elk E
Raccoon F
Where "A" is the sd of the proportions of beaver eaten by P01 in all five samples. Using output of P01 from above, "A" = sd(0.6666667, 0.8333333, 0.3333333, 0.6666667, 0.8333333). Moving down, "B" would represent the sd of the proportions of Bobcat eaten by P01 in all five samples, and so on for each prey species and individual.
Thanks in advance.