本文介绍了使用apply()函数跨子列表工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试引导7个人的饮食项目按比例出现,并计算sd()

I am trying to the bootstrap the proportional occurrence of diet items for 7 individuals and calculate a sd()

让我们说菜单上有9个猎物.

Lets say there are 9 prey items on the menu.

Diet <- c("Beaver","Bird", "Bobcat","Coyote", "Deer", "Elk",
    "Porcupine", "Raccoon",   "SmMamm")

这些猎物被同一物种的7个不同个体食用

And that these prey items are eaten by 7 different individuals of the same species

Inds <- c("P01", "P02", "P03", "P04", "P05", "P06", "P07")

我的目标是引导每个饮食项目对每个人的比例发生.
下面的循环为每个样本生成了五种饮食(每种饮食含有N = 20个喂养),并进行了替换采样.数据以个人列表的形式存储,每个人都包含样本饮食的列表.

My goal is the bootstrap the proportional occurrence of each diet item for each individual.
The loop below generates five diets for each individual (each diet containing N = 20 feedings) 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.

BootIndDiet <- list()
IndTotboot <- list()
for(i in Inds){
    for(j in 1:5){
        BootIndDiet[[j]] <- prop.table(table(sample(Diet, 20 ,replace = T)))
                        }
            IndTotboot[[i]] <- BootIndDiet
            }

下面,我将个人P07的前两种饮食作为循环结果的一个例子

Below I have included the first two diets of individual P07 as an example of the loop results

$P07
$P07[[1]]

   Beaver      Bird    Bobcat      Deer       Elk
     0.05      0.15      0.20      0.10      0.15
Porcupine   Raccoon    SmMamm
     0.15      0.15      0.05

$P07[[2]]

   Beaver      Bird    Bobcat    Coyote      Deer
     0.15      0.10      0.20      0.05      0.05
      Elk Porcupine   Raccoon    SmMamm
     0.05      0.20      0.10      0.10

然后,我想计算每个个体的每个物种的比例的sd().模棱两可地,对于每个个体(P01-P07),我希望在5种饮食中每个捕食物种的比例发生率sd().

I then want to calculate the sd() of the proportional of each 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.

虽然上面的循环运行,但我怀疑还有一种更好的方法(可能使用boot()函数)来避免使用列表...

While my loop above runs, I suspect there is a better way (possibly using the boot() function) that avoids lists...

虽然我在这里只为每个人提供了5个样本(引导程序),但我希望能产生10000个样本.

While I have only included 5 samples (bootstraps) for each individual here, I hope to generate 10000.

对不同策略的建议或如何在子列表中应用sd()表示赞赏.

Suggestions on a different strategy or how to apply sd() across sub-lists is greatly appreciated.

推荐答案

我将尝试以这种方式获取数组(而不是嵌套列表):

I'd try to obtain an array (instead of a nested list) in this way:

   IndTotboot <-array(replicate(5*length(Inds),prop.table(table(sample(as.factor(Diet), 20 ,replace = T))),simplify=T), dim=c(length(Diet),5,length(Inds)), dimnames=list(Diet,NULL,Inds))

使用replicate,您可以执行一次给定次数的表达式并将结果存储为数组(如果可能).我在Diet之前添加了as.factor,以确保该表可以跟踪每个Diet(甚至是频率为0的Diet).

With replicate you can execute an expression a given number of times and store the result as an array (if possible). I added an as.factor before Diet to make sure that the table takes trace of every Diet (even the ones with a 0 frequency).

获得的IndTotboot对象是一个3维数组,其中第一个索引表示Diet,第二个索引是自举复制,第三个索引是Inds.从那里可以按标准方式使用apply.

The IndTotboot object obtained is a 3-dimensional array where the first index indicates the Diet, the second the bootstrap replications and the third the Inds. From there you can use apply in the standard way.

如果尝试str(IndTotboot),您会得到:

    > str(IndTotboot)
     num [1:9, 1:5, 1:7] 0.1 0.15 0.15 0.1 0.1 0.1 0.15 0.05 0.1 0.15 ...
     - attr(*, "dimnames")=List of 3
       ..$ : chr [1:9] "Beaver" "Bird" "Bobcat" "Coyote" ...
       ..$ : NULL
       ..$ : chr [1:7] "P01" "P02" "P03" "P04" ...

第一行是最重要的.它表示num [1:9, 1:5, 1:7],表示9x5x7阵列.其余的表示dimnames,即尺寸的名称,它是一个列表.它们是矩阵的rownamescolnames的概括.

The first line is the most important. It says num [1:9, 1:5, 1:7], which means a 9x5x7 array. The rest indicates the dimnames, the names of the dimensions, which is a list. They are the generalization of the rownames and the colnames for a matrix.

现在,要获取每个DietIndssd,您只需使用apply:

Now, to obtain the sd for every Diet and Inds you just use apply:

    apply(IndTotboot,MARGIN=c(1,3),sd)

这篇关于使用apply()函数跨子列表工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:58