创建一个复制其他函数 100 次的函数
Create a function that replicates other function 100 times
我正在尝试创建一个复制其他两个函数 100 倍的函数,然后计算这些值的平均值。
我有一个这样的矩阵:
str(pref)
#num [1:9, 1:158] 4 9 15 6 7 8 6 11 11 4 ...
#- attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:158] "V1" "V2" "V3" "V4" ...
我尝试用这段代码重现它(可能不是最好的):
pref <- cbind(v = c(6,4,3,5,2,6),
c = c(5,6,7,4,2,5),
d= c(0,2,4,5,12,4))
是包vegan
和SpadeR
的组合,输出o chao
library(vegan)
library("SpadeR")
library(tidyverse)
library(data.table)
pref[is.na(pref)] <- 0
chaox100 <- function(x, y){
replicate(100, {
subsample <- rarefy(x, y)
chao <- ChaoSpecies(subsample)
df <- chao$Species_table
})
}
如果有帮助的话,chao$Species_table
是一个矩阵,因此该函数按应有的方式复制了 100 次,但输出数据一团糟。看看怎么样:
我想要所有数据的所有平均值。我尝试了 cbind
、rbind
、group_by(row.names)
、summarize_all
。
str(test)
# num [1:9, 1:4, 1:100] 262 221 779 530 829 ...
# - attr(*, "dimnames")=List of 3
# ..$ : chr [1:9] " Homogeneous Model" " Homogeneous (MLE)" " Chao1 (Chao, 1984)" " Chao1-bc" ...
# ..$ : chr [1:4] "Estimate" "s.e." "95%Lower" "95%Upper"
# ..$ : NULL
为了 运行 问题中的函数并得到它的 return 平均值,apply
函数 mean
到 replicate
的输出.
chaox100 <- function(x, y){
test <- replicate(100, {
subsample <- rarefy(x, y)
chao <- ChaoSpecies(subsample)
chao$Species_table
})
apply(test, MARGIN = 1:2, mean)
}
chaox100(pref, 70)
# Estimate s.e. 95%Lower 95%Upper
# Homogeneous Model 6.000 0.541 6.000 7.543
# Homogeneous (MLE) 6.467 0.795 6.047 10.595
# Chao1 (Chao, 1984) 6.000 0.541 6.000 7.543
# Chao1-bc 6.000 0.541 6.000 7.543
# iChao1 (Chiu et al. 2014) 6.000 0.541 6.000 7.543
# ACE (Chao & Lee, 1992) 6.000 0.541 6.000 7.543
# ACE-1 (Chao & Lee, 1992) 6.000 0.541 6.000 7.543
# 1st order jackknife 6.000 0.541 6.000 7.543
# 2nd order jackknife 6.000 0.541 6.000 7.543
#There were 50 or more warnings (use warnings() to see the first 50)
我正在尝试创建一个复制其他两个函数 100 倍的函数,然后计算这些值的平均值。
我有一个这样的矩阵:
str(pref)
#num [1:9, 1:158] 4 9 15 6 7 8 6 11 11 4 ...
#- attr(*, "dimnames")=List of 2
# ..$ : NULL
# ..$ : chr [1:158] "V1" "V2" "V3" "V4" ...
我尝试用这段代码重现它(可能不是最好的):
pref <- cbind(v = c(6,4,3,5,2,6),
c = c(5,6,7,4,2,5),
d= c(0,2,4,5,12,4))
是包vegan
和SpadeR
的组合,输出o chao
library(vegan)
library("SpadeR")
library(tidyverse)
library(data.table)
pref[is.na(pref)] <- 0
chaox100 <- function(x, y){
replicate(100, {
subsample <- rarefy(x, y)
chao <- ChaoSpecies(subsample)
df <- chao$Species_table
})
}
如果有帮助的话,chao$Species_table
是一个矩阵,因此该函数按应有的方式复制了 100 次,但输出数据一团糟。看看怎么样:
我想要所有数据的所有平均值。我尝试了 cbind
、rbind
、group_by(row.names)
、summarize_all
。
str(test)
# num [1:9, 1:4, 1:100] 262 221 779 530 829 ...
# - attr(*, "dimnames")=List of 3
# ..$ : chr [1:9] " Homogeneous Model" " Homogeneous (MLE)" " Chao1 (Chao, 1984)" " Chao1-bc" ...
# ..$ : chr [1:4] "Estimate" "s.e." "95%Lower" "95%Upper"
# ..$ : NULL
为了 运行 问题中的函数并得到它的 return 平均值,apply
函数 mean
到 replicate
的输出.
chaox100 <- function(x, y){
test <- replicate(100, {
subsample <- rarefy(x, y)
chao <- ChaoSpecies(subsample)
chao$Species_table
})
apply(test, MARGIN = 1:2, mean)
}
chaox100(pref, 70)
# Estimate s.e. 95%Lower 95%Upper
# Homogeneous Model 6.000 0.541 6.000 7.543
# Homogeneous (MLE) 6.467 0.795 6.047 10.595
# Chao1 (Chao, 1984) 6.000 0.541 6.000 7.543
# Chao1-bc 6.000 0.541 6.000 7.543
# iChao1 (Chiu et al. 2014) 6.000 0.541 6.000 7.543
# ACE (Chao & Lee, 1992) 6.000 0.541 6.000 7.543
# ACE-1 (Chao & Lee, 1992) 6.000 0.541 6.000 7.543
# 1st order jackknife 6.000 0.541 6.000 7.543
# 2nd order jackknife 6.000 0.541 6.000 7.543
#There were 50 or more warnings (use warnings() to see the first 50)