使用R引导多个列

时间:2016-02-02 23:03:16

标签: r statistics-bootstrap

我在R处相对较新,而我正在尝试构建一个函数,该函数将遍历导入表中的列并生成包含均值和95%置信区间的输出。理想情况下,应该可以引导具有不同样本大小的列,但首先我想让迭代工作。我有一些类似的东西,但我无法在那里得到它。这就是代码的样子,包括样本数据和输出:

#cdata<-read.csv(file.choose(),header=T)#read data from selected file, works, commented out because data is provided below
#cdata #check imported data

#Sample Data
#   WALL NRPK CISC WHSC LKWH YLPR
#1    21    8    1    2    2    5
#2    57    9    3    1    0    1
#3    45    6    9    1    2    0
#4    17   10    2    0    3    0
#5    33    2    4    0    0    0
#6    41    4   13    1    0    0
#7    21    4    7    1    0    0
#8    32    7    1    7    6    0
#9     9    7    0    5    1    0
#10    9    4    1    0    0    0

x<-cdata[,c("WALL","NRPK","LKWH","YLPR")] #only select relevant species

i<-nrow(x) #count number of rows for bootstrapping 
g<-ncol(x) #count number of columns for iteration

#build bootstrapping function, this works for the first column but doesn't iterate

bootfun <- function(bootdata, reps) {

  boot <- function(bootdata){

    s1=sample(bootdata, size=i, replace=TRUE)
    ms1=mean(s1)
    return(ms1)

  } # a single bootstrap

  bootrep <- replicate(n=reps, boot(bootdata))

  return(bootrep)

} #replicates bootstrap of "bootdata" "reps" number of times and outputs vector of results

cvr1 <- bootfun(x$YLPR,50000) #have unsuccessfully tried iterating the location various ways (i.e. x[i])
cvrquantile<-quantile(cvr1,c(0.025,0.975))
cvrmean<-mean(cvr1)
vec<-c(cvrmean,cvrquantile) #puts results into a suitable form for output
vecr<-sapply(vec,round,1) #rounds results
vecr

      2.5% 97.5% 
 28.5  19.4  38.1 

#apply(x[1:g],2,bootfun) ##doesn't work in this case

#desired output:

#Species    Mean LowerCI UpperCI
#WALL       28.5    19.4      38.1
#NRPK       6.1 4.6    7.6
#YLPR       0.6 0.0    1.6

我也尝试使用启动软件包,并且它可以很好地迭代通过这些方法,但我无法让它在置信区间内做同样的事情。 &#34;普通&#34;上面的代码还有一个优点,即您可以轻松检索自举结果,这可能会用于其他计算。为了完整起见,这里是启动代码:

#Bootstrapping using boot package
library(boot)
#data<-read.csv(file.choose(),header=TRUE) #read data from selected file
#x<-data[,c("WALL","NRPK","LKWH","YLPR")] #only select relevant columns
#x #check data

#Sample Data

#  WALL NRPK LKWH YLPR
#1    21    8    2    5
#2    57    9    0    1
#3    45    6    2    0
#4    17   10    3    0
#5    33    2    0    0
#6    41    4    0    0
#7    21    4    0    0
#8    32    7    6    0
#9     9    7    1    0
#10    9    4    0    0

i<-nrow(x) #count number of rows for resampling 
g<-ncol(x) #count number of columns to step through with bootstrapping
boot.mean<-function(x,i){boot.mean<-mean(x[i])} #bootstrapping function to get the mean

z<-boot(x, boot.mean,R=50000) #bootstrapping function, uses mean and number of reps
boot.ci(z,type="perc") #derive 95% confidence intervals
apply(x[1:g],2, boot.mean) #bootstrap all columns

#output:
#WALL NRPK LKWH YLPR 
#28.5  6.1  1.4  0.6 

我已经查看了我能找到的所有资源,似乎无法让事情发挥作用。我希望输出的是自举方式,每列的相关置信区间。谢谢!

1 个答案:

答案 0 :(得分:1)

注意:apply(x[1:g],2, boot.mean) #bootstrap all columns没有做任何引导程序。您只是计算每列的平均值。

对于bootstrap均值和置信区间,请尝试以下方法:

apply(x,2,function(y){ 
   b<-boot(y,boot.mean,R=50000); 
   c(mean(b$t),boot.ci(b,type="perc", conf=0.95)$percent[4:5])
})