在引导函数中引用索引

时间:2013-12-24 03:30:23

标签: r

我有一个大型数据集(DF),其子集如下所示:

Site    Event   HardwareID  Species Day1    Day2    Day3    Day4    Day5    Day6    
1       1       16_11       x       0       0       0       0       0       0   
1       1       29_11       y       0       0       6       2       0       1       
1       1       36_11       d       0       0       0       0       0       1       
1       1       41_11       y       0       0       2       4       1       1                
1       1       41_11       x       0       0       0       0       0       1            
1       1       58_11       a       0       0       1       0       0       0             
1       1       62_11       y       0       0       0       1       0       0           
1       1       62_11       z       0       0       0       0       0       0           
1       1       62_11       x       0       0       0       0       0       1            
2       1       40_AR       b       0       0       0       0       0       0           
2       1       12_11       z       0       0       1       0       0       0           

我想通过计算每个不同站点的每个HardwareID的物种累积曲线(本质上包含Days列)和boostrapping来检查在最短时间内生成大多数物种的最小HardwareID数量HardwareID选择部分(因此,请在每个站点使用两个HardwareID,然后是3,然后是4等)查看累积曲线。

我写了一个函数来为这些子集创建物种积累曲线(使用specaccum),例如:

Sites<-subset(DF,DF$Site==1)
samples<-function (x) {               
              specurve_sample<-(ddply(Sites[,4:length(colnames(Sites))],"Species",numcolwise(sum)))
              specurve_sample<-specurve_sample[-1,]
              n<-specurve_sample$Species
              n<-drop.levels(n,reorder=FALSE)                   
              specurve_sample<-specurve_sample[,-1]
              specurve_sample <-t(specurve_sample)
              colnames(specurve_sample)<-n
              specurve_sample<-as.data.frame(specurve_sample)
              sample_k<-specaccum(specurve_sample)
              out<-rbind(sample_k$richness,sample_k$sd)
              outnames<-c("Richness","SD")
              st<-rep(Sites$Site[1],2)
              out<-as.data.frame(cbind(outnames,st,out))          
              colnames(out)<-c("label","site","Days")
              out
              }

如果我事先对我的数据进行子集,那么该函数可以正常工作,但是boostrapping部分不起作用。我知道我需要创建一个函数(x,j),但无法弄清楚j在我的函数中的位置。这是我的其余代码。非常感谢您的帮助。詹姆斯

all_data<-c()
for (i in 1:length(unique(DF$Site))) {
Sites<-subset(DF,DF$Site==i)
boots<-boot(Sites,samples, strata=Sites$HardwareID,R=1000)   
all_data<-rbind(all_data,boots)
all_data
}

1 个答案:

答案 0 :(得分:1)

这样做的一种简单方法是创建xj的函数(正如您已经开始做的那样),并让该函数的第一行标识相关的bootstrap子集。整个集合,bootsub <- x[j, ]。然后,您可以在函数的其余部分引用此子集bootsub,而无需再次引用j

在您的情况下,您不希望您的功能引用回原始数据框Site。因此,您在函数中Site的每个位置都将其更改为bootsub。例如:

samples <- function(x, j) {
    bootsub <- x[j, ]
    specurve_sample <- (ddply(bootsub[, 4:length(colnames(bootsub))], "Species", numcolwise(sum)))
    specurve_sample <- specurve_sample[-1, ]
    n <- specurve_sample$Species
    n <- drop.levels(n, reorder=FALSE)                   
    specurve_sample <- specurve_sample[, -1]
    specurve_sample <- t(specurve_sample)
    colnames(specurve_sample) <- n
    specurve_sample <- as.data.frame(specurve_sample)
    sample_k <- specaccum(specurve_sample)
    out <- rbind(sample_k$richness, sample_k$sd)
    outnames <- c("Richness", "SD")
    st <- rep(bootsub$Site[1], 2)
    out <- as.data.frame(cbind(outnames, st, out))          
    colnames(out) <- c("label", "site", "Days")
    out
    }

...

跟进前两条评论。没有数据就很难排除故障,但这是我最好的猜测。可能是您的subset()功能存在问题,因为您使用i作为for()循环中唯一网站的索引,但随后将i视为subset()调用do.call()时网站的 。此外,在for()循环之后对rbind()运行一次调用可能更有效,而不是在循环内多次调用# vector of unique sites usite <- unique(DF$Site) # empty list in which to put the bootstrap results alldatlist <- vector("list", length(usite)) # loop through every site separately, save the bootstrap replicates ($t) for(i in 1:length(usite)) { Sites <- subset(DF, DF$Site==usite[i]) alldatlist[[i]] <- boot(Sites, samples, strata=Sites$HardwareID, R=1000)$t } # combine the list of results into a single matrix all_data <- do.call(rbind, alldatlist) 。试试这个未经测试的代码。

{{1}}