何 运行 R 中的分层自举线性回归?

Ho to run stratified bootstrapped linear regression in R?

在我的模型中,x 是具有 3 个类别的分类变量:0、1 和 2,其中 0 是参考类别。但是 0 个类别比其他类别 (1,2) 大,所以为了避免样本偏差我想分层引导,但找不到任何相关的方法

df <- data.frame (x  = c(0,0,0,0,0,1,1,2,2),
                  y = c(10,11,10,10,12,17,16,20,19),
                  m = c(6,5,6,7,2,10,14,8,11)
                  )
df$x <- as.factor(df$x)
df$x <- relevel(df$x,ref = "0")


fit <- lm(y ~ x*m, data = df)

summary(fit)

扩展 Roland 在评论中的回答,您可以使用 boot.ci:

从引导程序中获取置信区间
library(boot)

b <- boot(df, \(DF, i) coef(lm(y ~ x*m, data = df[i,])), strata = df$x, R = 999)

result <- do.call(rbind, lapply(seq_along(b$t0), function(i) {
  m <- boot.ci(b, type = 'norm', index = i)$normal
  data.frame(estimate = b$t0[i], lower = m[2], upper = m[3])
  }))

result
#>               estimate      lower       upper
#> (Intercept) 12.9189189 10.7166127 15.08403731
#> x1           6.5810811  2.0162637  8.73184665
#> x2           9.7477477  6.9556841 11.37390826
#> m           -0.4459459 -0.8010925 -0.07451434
#> x1:m         0.1959459 -0.1842914  0.55627896
#> x2:m         0.1126126 -0.2572955  0.48352616

甚至像这样绘制结果:

ggplot(within(result, var <- rownames(result)), aes(estimate, var)) +
  geom_vline(xintercept = 0, color = 'gray') +
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.1) +
  geom_point(color = 'red') +
  theme_light()