这是一种方法。我将首先生成一些虚假数据:
ids <- rep(1:3,times = 10)
values <- rnorm(30)
dat <- data.frame(ids,values)
现在我们有了数据,我们可以生成集群自举功能。这将从每个群集中采样并返回一个新的数据帧。然后,您可以应用测试统计信息:
library(tidyverse)
cluster_boot_function <- function(x){
clusted_boot <- dat %>%
group_by(ids) %>%
nest() %>%
mutate(samps = map(data,~sample(.$values,size = 5,replace = T))) %>%
select(ids,samps) %>%
unnest(cols = samps)
results <- clusted_boot %>%
group_by(ids) %>%
summarise(mu = mean(values))
results
}
现在,您只需要重复应用它即可(还请注意,函数中的“ x”不会执行任何操作,下一步只需要在其中使用它即可。)
在这里,我使用map_dfr
返回每次迭代的摘要统计信息:
out <- map_df(1:100,cluster_boot_function,.id = "iteration")
这将为您提供有关引导程序每次迭代的统计信息:
# A tibble: 300 x 3
iteration ids mu
<chr> <int> <dbl>
1 1 1 0.150
2 1 2 0.150
3 1 3 0.150
4 2 1 0.150
5 2 2 0.150
6 2 3 0.150
7 3 1 0.150
8 3 2 0.150
由此,您可以将其扩展到需要进行的任何类型的建模。
,
我认为通过示例ID引导绝对是可以的。这是使用boot
包的示例。我不确定我是否完全了解您正在引导的内容,因此该功能可能并不完全正确,但是您应该能够或多或少地了解其功能。这不是很有效;鉴于我不确定统计信息,我根本没有对其进行优化。
library("boot")
ids <- rep(1:3,times = 1000)
values <- rnorm(300)
dat <- data.frame(ids,values)
boot_fun <- function(ids,i) {
sapply(ids[i],function(j) mean(dat[dat$ids == j,"values"]))
}
boot_res <- boot(
dat$ids,statistic = boot_fun,R = 100
)
hist(boot_res$t)
由reprex package(v0.3.0)于2019-11-08创建
本文链接:https://www.f2er.com/3139054.html