在我的模型中,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)
uj5u.com熱心網友回復:
在評論中擴展 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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477849.html
