【问题标题】:view values used by function boot to bootstrap estimates查看函数引导使用的值以引导估计
【发布时间】:2015-06-30 17:56:00
【问题描述】:

我编写了下面的代码来获得平均值的引导估计。我的目标是通过boot 包中的函数boot 查看从数据集中选择的数字,理想情况下是按照它们被选择的顺序。

数据集只包含三个数字:1、10 和 100,我只使用了两个引导样本。

估计的平均值是23.5,下面的R 代码表示这六个数字包括一个“1”、四个“10”和一个“100”。但是,这些数字有 30 种可能的组合会导致平均值为 23.5。

我有没有办法确定这 30 种可能的组合中的哪一种是实际出现在两个 bootstrap 样本中的组合?

library(boot)

set.seed(1234)

dat <- c(1, 10, 100)
av  <- function(dat, i) { sum(dat[i])/length(dat[i]) }
av.boot <- boot(dat, av, R = 2)
av.boot
#
# ORDINARY NONPARAMETRIC BOOTSTRAP
#
#
# Call:
# boot(data = dat, statistic = av, R = 2)
#
#
# Bootstrap Statistics :
#     original  bias    std. error
# t1*       37   -13.5    19.09188
#

mean(dat) + -13.5 
# [1] 23.5

# The two samples must have contained one '1', four '10' and one '100',
# but there are 30 possibilities.
# Which of these 30 possible sequences actual occurred?

# This code shows there must have been one '1', four '10' and one '100'
# and shows the 30 possible combinations

my.combos <- expand.grid(V1  = c(1, 10, 100),
                         V2  = c(1, 10, 100),
                         V3  = c(1, 10, 100),
                         V4  = c(1, 10, 100),
                         V5  = c(1, 10, 100),
                         V6  = c(1, 10, 100))

my.means <- apply(my.combos, 1, function(x) {( (x[1] + x[2] + x[3])/3 + (x[4] + x[5] + x[6])/3 ) / 2 })

possible.samples <- my.combos[my.means == 23.5,]
dim(possible.samples)

n.1   <- rowSums(possible.samples == 1)
n.10  <- rowSums(possible.samples == 10)
n.100 <- rowSums(possible.samples == 100)

n.1[1]
n.10[1]
n.100[1]

length(unique(n.1))   == 1
length(unique(n.10))  == 1
length(unique(n.100)) == 1

【问题讨论】:

    标签: r statistics-bootstrap


    【解决方案1】:

    我认为您可以使用下面的代码确定采样的数字和采样的顺序。您必须从 boot 包中提取函数 ordinary.array 并将该函数粘贴到您的 R 代码中。然后指定nRstrata 的值,其中n 是数据集中的观察数,R 是您想要的重复样本数。

    我不知道这种方法有多普遍,但它适用于我尝试过的几个简单示例,包括下面的示例。

    library(boot)
    
    set.seed(1234)
    
    dat <- c(1, 10, 100, 1000)
    av  <- function(dat, i) { sum(dat[i])/length(dat[i]) }
    av.boot <- boot(dat, av, R = 3)
    av.boot
    #
    # ORDINARY NONPARAMETRIC BOOTSTRAP
    #
    #
    # Call:
    # boot(data = dat, statistic = av, R = 3)
    #
    #
    # Bootstrap Statistics :
    #     original  bias    std. error
    # t1*   277.75  -127.5    132.2405
    # 
    # 
    
    mean(dat) + -127.5
    # [1] 150.25
    
    # boot:::ordinary.array
    
    ordinary.array <- function (n, R, strata) 
    {
        inds <- as.integer(names(table(strata)))
        if (length(inds) == 1L) {
            output <- sample.int(n, n * R, replace = TRUE)
            dim(output) <- c(R, n)
        }
        else {
            output <- matrix(as.integer(0L), R, n)
            for (is in inds) {
                gp <- seq_len(n)[strata == is]
                output[, gp] <- if (length(gp) == 1) 
                    rep(gp, R)
                else bsample(gp, R * length(gp))
            }
        }
        output
    }
    
    # I think the function ordinary.array determines which elements 
    # of the data are sampled in each of the R samples
    
    set.seed(1234)
    ordinary.array(n=4,R=3,1)
    
    #      [,1] [,2] [,3] [,4]
    # [1,]    1    3    1    3
    # [2,]    3    4    1    3
    # [3,]    3    3    3    3
    #
    # which equals:
    
    ((1+100+1+100) / 4  +  (100+1000+1+100) / 4  +  (100+100+100+100) / 4) / 3
    
    # [1] 150.25
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 2017-02-22
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多