r boot :: boot():返回均值和二手示例的函数

发布于 2025-02-07 07:20:05 字数 659 浏览 0 评论 0原文

我正在使用boot()函数从启动软件包中使用来自总体的bootstrap。使用的功能是:

boot_mean <- function(data, i){
  
  ds_m <- data[i]
  return(mean(ds_m))
  
}

工作类似于魅力,但现在我想调整boot_mean函数,以便我可以获取导致均值的样本。我尝试过:

library('boot')

boot_mean <- function(data, i){
  
  ds_m <- data[i]
  ds_m_mean <- mean(ds_m)
  rlist <- list("means" = ds_m_mean, "data" = ds_m)
  return(rlist)
  
}

dummy_data <- rnorm(500)
dummy_boot <- boot(dummy_data, boot_mean, R = 1000) 

哪个导致错误:

t.star [r,]&lt; - res [[r]]中的错误 在矩阵上

这里有什么问题?如何将相应的数据集获取为自举的含义?

I'm using the boot() function from the boot package to bootstrap means from a population. The used function is:

boot_mean <- function(data, i){
  
  ds_m <- data[i]
  return(mean(ds_m))
  
}

Works like charm but now I want to adapt the boot_mean function so that I can get the samples which lead to the mean too. I tried:

library('boot')

boot_mean <- function(data, i){
  
  ds_m <- data[i]
  ds_m_mean <- mean(ds_m)
  rlist <- list("means" = ds_m_mean, "data" = ds_m)
  return(rlist)
  
}

dummy_data <- rnorm(500)
dummy_boot <- boot(dummy_data, boot_mean, R = 1000) 

Which results in an error:

Error in t.star[r, ] <- res[[r]] : incorrect number of subscripts
on matrix

What's wrong here? How can I get the corresponding dataset to the bootstrapped mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2025-02-14 07:20:05

从文档?boot中描述统计参数。

应用于数据时返回向量包含感兴趣的统计数据的函数。 ...

boot()功能只想处理输出单个向量的函数。修改您的代码以返回两个元素的列表意味着它将不再起作用。 R中实际上有一些有趣的奇数,boot()函数,这意味着代码几乎可以工作,如果您在<<代码> boot()呼叫,但仍然错了。

幸运的是,出于您的目的,作者已经对有用的boot.array()函数进行了编程。它输出一个带有r行的矩阵和nrow(data)列,指示为ITH Bootstrap采样了jth个人的次数,或者是采样的个体的索引。通过从数据中选择那些个人,可以轻松找到自举数据集。这可能需要一段时间。

dats <- lapply(1:nrow(boot.array(dummy_boot)), 
    FUN = function(x) dummy_data[boot.array(dummy_boot, indices = TRUE)[x, ]])

如果您有多个数据列,则应添加,drop = false

dats <- lapply(1:nrow(boot.array(dummy_boot)), 
    FUN = function(x) dummy_data[boot.array(dummy_boot, indices = TRUE)[x, ], , drop = FALSE])

From the documentation ?boot, describing the statistic argument.

A function which when applied to data returns a vector containing the statistic(s) of interest. ...

The boot() function only wants to deal with functions that output a single vector. Modifying your code to return a list of two elements means it won't work anymore. There's actually a little interesting oddity in R and the boot() function which means the code almost works if you set R=1 in the boot() call, but it's still wrong.

Fortunately for your purpose, the authors have already programmed the useful boot.array() function. It outputs a matrix with R rows and nrow(data) columns, indicating either how many times the jth individual was sampled for the ith bootstrap, or the indices of the sampled individuals. Getting the bootstrapped datasets can easily be found by selecting those individuals from the data. This can take a little while.

dats <- lapply(1:nrow(boot.array(dummy_boot)), 
    FUN = function(x) dummy_data[boot.array(dummy_boot, indices = TRUE)[x, ]])

If you have multiple columns of data you should add , , drop = FALSE

dats <- lapply(1:nrow(boot.array(dummy_boot)), 
    FUN = function(x) dummy_data[boot.array(dummy_boot, indices = TRUE)[x, ], , drop = FALSE])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文