2014-08-31 56 views
-1

我想引導的飲食項目7個人的比例發生和計算sd()工作與應用()函數

比方說有菜單上9個獵物。

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

而且這些獵物是由7口不同的同種

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

的人吃我的目標是引導每個單獨的每種飲食項目的比例發生。
下面的循環產生用於每個單獨的5個飲食即用更換採樣(含有N = 20餵養各飲食)。數據存儲爲個人列表,每個列表包含樣本飲食列表。

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的前兩個日糧作爲循環的示例結果

$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()

雖然我的循環運行上面,我懷疑有避免列出一個更好的方法(可能使用boot()函數)...

雖然我只包括5個樣本(引導程序),爲每個在這裏,我希望能產生一個不同的策略或如何申請sd()跨子列表是極大的讚賞10000

建議。

+0

除非存在內存問題,否則可以將數據以長格式「data.frame」與列-say- [ind,No_diet,prey,prop]存儲在一起,然後您可以調用'aggregate(prop〜ind +獵物,mydataframe,SD)'。 – 2014-08-31 20:26:00

回答

2

我會嘗試以這種方式來獲得一個數組(而不是嵌套列表):

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,以確保表格能夠追蹤每個飲食(即使是0頻率的飲食)。

獲得的IndTotboot對象是一個三維數組,其中第一個索引指示Diet,第二個引導程序複製和第三個Inds。從那裏你可以用標準方式使用apply

編輯:

如果試圖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的推廣。

現在,爲了獲得sd每一個DietInds你只需要使用apply

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

很酷的想法@nicola,雖然我無法得到apply()的代碼。 IndTotboot對象的str()表示列表,但lapply(IndTotboot,sd)的結果顯然不正確。我錯過了什麼......? – 2014-08-31 23:32:03

+0

'IndTotboot'是一個數組,而不是一個列表。我將擴展我的答案以展示如何使用apply。 – nicola 2014-09-01 04:27:48

0

以下可能是有用的:

dd = data.frame(sapply(IndTotboot, function(x)x)) 

maindf = data.frame(Var1=as.character(), Freq=as.numeric()) 

for(rr in 1:nrow(dd)) for (cc in 1:ncol(dd)){ 
     maindf= merge(maindf, data.frame(dd[rr,cc]), all=TRUE) 
} 

> head(maindf, 10) 
    Var1 Freq 
1 Beaver 0.05 
2 Beaver 0.10 
3 Beaver 0.15 
4 Beaver 0.20 
5 Beaver 0.30 
6 Bird 0.05 
7 Bird 0.10 
8 Bird 0.15 
9 Bird 0.20 
10 Bird 0.25 


with(maindf, tapply(Freq, Var1, sd)) 
    Beaver  Bird  Bobcat  Coyote  Elk Porcupine Raccoon  SmMamm  Deer 
0.09617692 0.09354143 0.09354143 0.09354143 0.09354143 0.06454972 0.07905694 0.108.07905694 

對於每一個人:

counter=1 
for (cc in 1:ncol(dd)){ 
    maindf = data.frame(Var1=as.character(), Freq=as.numeric()) 
    for(rr in 1:nrow(dd)){ 
     maindf= merge(maindf, data.frame(dd[rr,cc]), all=TRUE) 
    } 
    cat("\nFor individual number: ",counter,"\n"); counter=counter+1 
    print(with(maindf, tapply(Freq, Var1, sd))) 
} 


For individual number: 1 
    Beaver  Bird  Bobcat  Coyote  Elk Porcupine Raccoon  SmMamm  Deer 
0.05000000 0.07637626 0.05000000 0.06454972 0.03535534 0.05000000 0.05000000 0.07637626 0.05000000 

For individual number: 2 
    Beaver  Bird  Bobcat  Coyote  Deer  Elk Porcupine Raccoon  SmMamm 
0.05000000 0.108.03535534 0.05000000 0.09128709   NA 0.03535534 0.07637626 0.13149778 

For individual number: 3 
    Beaver  Bird  Bobcat  Coyote  Deer  Elk Porcupine Raccoon  SmMamm 
0.03535534 0.07637626 0.12583057 0.03535534 0.03535534 0.06454972 0.05000000 0.10606602 0.06454972 

For individual number: 4 
    Beaver  Bird  Bobcat  Coyote  Deer  Elk Porcupine Raccoon  SmMamm 
0.05000000 0.05000000 0.05000000 0.03535534 0.10408330 0.07905694 0.05000000 0.03535534 0.10408330 

For individual number: 5 
    Beaver  Bird  Bobcat  Coyote  Deer  Elk Porcupine Raccoon  SmMamm 
0.05000000 0.03535534 0.05000000 0.03535534 0.03535534 0.03535534 0.05000000 0.05000000 0.07071068 

For individual number: 6 
    Beaver  Coyote  Deer  Elk Porcupine Raccoon  SmMamm  Bobcat  Bird 
0.10000000 0.07637626 0.03535534 0.05000000 0.07071068 0.03535534 0.05000000 0.10408330 0.10606602 

For individual number: 7 
    Beaver  Bird  Bobcat  Coyote  Deer  Elk Porcupine Raccoon  SmMamm 
0.03535534 0.05000000 0.10408330 0.07637626 0.05000000 0.13228757 0.07637626 0.03535534 0.05000000 

對於個人+物種:

maindf = data.frame(Var1=as.character(), Freq=as.numeric(), ind=as.numeric()) 
counter=1 
for (cc in 1:ncol(dd)){ 
    for(rr in 1:nrow(dd)){ 
     maindf= merge(maindf, cbind(data.frame(dd[rr,cc]),ind=counter), all=TRUE) 
    } 
    counter=counter+1 
} 
with(maindf, tapply(Freq, list(Var1,ind), sd)) 

        1   2   3   4   5   6   7 
Beaver 0.05000000 0.05000000 0.03535534 0.05000000 0.05000000 0.10000000 0.03535534 
Bird  0.07637626 0.108.07637626 0.05000000 0.03535534 0.10606602 0.05000000 
Bobcat 0.05000000 0.03535534 0.12583057 0.05000000 0.05000000 0.10408330 0.10408330 
Coyote 0.06454972 0.05000000 0.03535534 0.03535534 0.03535534 0.07637626 0.07637626 
Elk  0.03535534   NA 0.06454972 0.07905694 0.03535534 0.05000000 0.13228757 
Porcupine 0.05000000 0.03535534 0.05000000 0.05000000 0.05000000 0.07071068 0.07637626 
Raccoon 0.05000000 0.07637626 0.10606602 0.03535534 0.05000000 0.03535534 0.03535534 
SmMamm 0.07637626 0.13149778 0.06454972 0.10408330 0.07071068 0.05000000 0.05000000 
Deer  0.05000000 0.09128709 0.03535534 0.10408330 0.03535534 0.03535534 0.05000000 
+0

請參閱我上面的編輯。 – rnso 2014-09-01 03:42:54