控制与线性模型函数相关的置信区间的打印输出

Control the printout of confidence intervals related to a linear model function

我正在为线性模型进行自举,但如何编辑截距和 x 变量的打印输出名称?

这里是模拟数据

set.seed(42) 
n <- 100
x <- rnorm(n)
e <- rnorm(n)
y <- as.numeric(50 + 25*x + e)
dd <- data.frame(id=1:n, x=x, y=y)

这是模型:

mo <- lm(y ~ x, data=dd)

求拟合和残差:

fit <- fitted(mo)
resi <- residuals(mo)

根据残差引导检索置信区间的函数:

FUN <- function() {
  X <- model.matrix(mo)
  ressampy <- fit + sample(resi, length(resi), replace = TRUE)
  bootmod <- lm(ressampy ~ X-1)
  confint(bootmod, level = 0.95)
}

1 运行 的输出(请注意打印输出是 X(Intercept)Xx 但我只是希望它们是 (Intercept)x

FUN()
                2.5 %   97.5 %
X(Intercept) 49.74439 50.07817
Xx           24.92904 25.25103

这可能是一个简单的修复,但我就是无法让它工作。任何帮助将不胜感激!

只需使用 rownames() 更改包含置信区间的矩阵的行名称,如:

FUN <- function() {
  X <- model.matrix(mo)
  ressampy <- fit + sample(resi, length(resi), replace = TRUE)
  bootmod <- lm(ressampy ~ X-1)
  ci <- confint(bootmod, level = 0.95)
  rownames(ci) <- c("(Intercept)", "x")
  return(ci)
}