【问题标题】:Bootstrapping multiple columns with R用 R 引导多列
【发布时间】:2016-02-02 23:03:16
【问题描述】:

我在 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

我也尝试过使用引导包,它可以很好地迭代这些方法,但我无法让它对置信区间做同样的事情。上面的“普通”代码还具有可以轻松检索引导结果的优点,该结果可能用于其他计算。为了完整起见,这里是启动代码:

#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 

我已经浏览了所有我能找到的资源,但似乎无法让事情正常进行。我想要输出的是每列具有相关置信区间的自举平均值。谢谢!

【问题讨论】:

  • 当您说输出由均值和置信区间组成时,您是什么意思?您的意思是您想要计算统计量的均值、0.025 和 0.975 个百分位数?
  • 另外,对于 50k 次重复 * n 列,您可能希望使用 boot::boot,因为它提供了与 parallel 包的集成
  • 当我说输出时,我的意思是我希望将结果显示在一个表中,该表由每列的列名、自举平均值和相关的置信区间组成。感谢您对 boot::boot 的建议。
  • ...你的回答根本没有回答我的问题,但很高兴你得到了你需要的答案
  • 恐怕我误解了你的问题。我在我的代码示例中提供了一个我正在寻找的作为所需输出的示例,它显示了具有 95% 置信区间的自举均值。打扰一下,我得了重感冒(而且我服用了感冒药),所以我显然不知何故搞砸了。感谢您的帮助,不胜感激。

标签: r statistics-bootstrap


【解决方案1】:

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

对于引导均值和置信区间,试试这个:

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])
})

【讨论】:

  • 这非常有效。我的代码(不是您的代码)仍然出现一些奇怪的琐碎错误,但您提供的内容产生了我正在寻找的确切结果。非常感谢! :)
猜你喜欢
  • 1970-01-01
  • 2014-01-24
  • 2020-08-27
  • 2013-07-31
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
相关资源
最近更新 更多