简体   繁体   English

R中的自定义引导置信区间

[英]Custom bootstrap confidence intervals in R

I need to find a way for getting the bootstrap confidence intervals on the estimate I obtain with a custom function. 我需要找到一种方法来获取自定义函数获得的估计值上的引导置信区间。 Now, the problem is that I have one big matrix from which I take rows out at random and then calculate the quantity needed. 现在,问题是我有一个大矩阵,我从中随机取出行,然后计算所需的数量。

Here is (hopefully) reproducible example 这是(希望)可复制的示例

Generate similar random data: 生成类似的随机数据:

mat1 <- matrix(rnorm(300, 80, 20), nrow = 100)

Function to calculate the desired quantity (where R is correlation matrix): 计算所需数量的函数(其中R是相关矩阵):

IIvar <- function(R) { 
d <- eigen(R)$values  
p <- length(d)  
sum((d-1)^2)/(p*(p-1))}

My function that attempts the solution (where omat is the smaller matrix that consists of some mat1 rows, freq is the number of rows in omat, and numR is the number of replicates): 我的函数尝试求解(其中omat是由一些mat1行组成的较小矩阵,freq是omat中的行数,而numR是重复数):

ciint <- function(omat, mat1, freq, numR) {
II <- IIvar(cor(omat))
n <- dim(mat1)[1]
b <- numeric(numR)
for (i in 1:numR) { b[i] <- IIvar(cor(mat1[sample(c(1:n),freq),]))}
hist(b)
abline(v = II, lty = 5, lwd = 3)
return(b) }

The resultant vector b has all the values obtained from matrices of randomly chosen rows (number determined by freq) from mat1 which can be compared with IIvar from omat (the matrix with rows chosen by population membership). 所得向量b具有从mat1中随机选择的行矩阵(由freq确定的数量)中获得的所有值,可以将其与omat的IIvar(具有通过总体成员资格选择的行的矩阵)进行比较。

In mat1 I have ie 5 populations (grouping by rows) and I need to calculate IIvar for all of them separetely and to generate confidence intervals for the obtained value. 在mat1中,我有5个总体(按行分组),我需要分别计算它们的IIvar,并为获得的值生成置信区间。

When I run my ciint function like this 当我像这样运行ciint函数时

ciint(omat, mat1, 61, 1000)

I get the distribution of values, and the position of the "real" IIvar value, but I have no idea how to generate 95% intervals from this point. 我得到了值的分布以及“实际” IIvar值的位置,但是我不知道如何从这一点生成95%的间隔。

All you need is an interval that contains 95% of the generated b values. 您只需要一个包含95%生成的b值的间隔。 You can the highest posterior density from bayesian estimation, it's just that. 您可以从贝叶斯估计中获得最高的后验密度,仅此而已。 There are many packages that compute it, for instance, function emp.hpd from TeachingDemos . 有许多软件包可以对其进行计算,例如,来自TeachingDemos emp.hpd函数。 Add

require(TeachingDemos)

and change the last line ( return(b) ) from ciint to 并将最后一行( return(b) )从ciint

emp.hpd(b)

(No need to use return() .) (无需使用return() 。)

I am not sure exactly what you are trying to accomplish with your function, but if you want to do boostrapping then look at the boot function in the boot package. 我不确定您要完成的功能到底是什么,但是如果您想进行增强处理,请查看boot软件包中的boot功能。 You can pass custom functions to boot and it will take bootstrap samples, pass them to the custom function, then collate the results. 您可以将自定义函数传递给boot ,它将进行引导程序样本,将其传递给自定义函数,然后整理结果。 It also then has multiple options for confidence intervals from the results. 然后,对于结果的置信区间,它还具有多个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM