我想获取我的数据集bodyfat_trimmed并使用bootstrapping来检索均值和标准错误。但是,我似乎一直在使用相同的数据,因此得到零标准误差和偏差。我怎么解决这个问题?
bsfunc <- function(data) {
set.seed(1)
x <- model.matrix(reduced_BIC_fit)[, -1]
y <- data$density
bootdata <- sample(1:nrow(x), nrow(x)/2)
x.train <- x[bootdata, ]
y.train <- y[bootdata]
bootframe <- data.frame(bodyfat_trimmed[train, ])
fit <- lm(density ~ age + abdomen + wrist, data = bootframe)
stats <- coef(summary(fit))[, "Estimate"]
return(stats)}
strap <- boot(data = bodyfat_trimmed, sim = "parametric", statistic = bsfunc, R=1000)
strap
输出:
PARAMETRIC BOOTSTRAP
Call:
boot(data = bodyfat_trimmed, statistic = bsfunc, R = 1000, sim = "parametric")
Bootstrap Statistics :
original bias std. error
t1* 1.1360858253 0 0
t2* -0.0000889957 0 0
t3* -0.0018446625 0 0
t4* 0.0050609837 0 0
如果种子在函数内,则样本函数将有些重复。
bsfunc<-function(){set.seed(1); sample(1:10,1)}
bsfunc()
[1] 3
bsfunc()
[1] 3
bsfunc()
[1] 3
PS你的bsfunc
也是错误的。如上所述,train
(即bootframe <- data.frame(bodyfat_trimmed[train, ])
)并非来自此功能。通常boot
的重点是进行自举重新采样。而bsfunc
应该是一个直接的统计数据。