r boot :: boot():返回均值和二手示例的函数
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文档
?boot
中描述统计
参数。boot()
功能只想处理输出单个向量的函数。修改您的代码以返回两个元素的列表
意味着它将不再起作用。 R中实际上有一些有趣的奇数,boot()
函数,这意味着代码几乎可以工作,如果您在<<代码> boot()呼叫,但仍然错了。幸运的是,出于您的目的,作者已经对有用的
boot.array()
函数进行了编程。它输出一个带有r
行的矩阵和nrow(data)
列,指示为ITH Bootstrap采样了jth个人的次数,或者是采样的个体的索引。通过从数据中选择那些个人,可以轻松找到自举数据集。这可能需要一段时间。如果您有多个数据列,则应添加
,drop = false
From the documentation
?boot
, describing thestatistic
argument.The
boot()
function only wants to deal with functions that output a single vector. Modifying your code to return alist
of two elements means it won't work anymore. There's actually a little interesting oddity in R and theboot()
function which means the code almost works if you setR=1
in theboot()
call, but it's still wrong.Fortunately for your purpose, the authors have already programmed the useful
boot.array()
function. It outputs a matrix withR
rows andnrow(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.If you have multiple columns of data you should add
, , drop = FALSE