I am doing bootstrapping for a linear model but how do I edit the printout names of the intercept and the x variable?
Here are the simulated data
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)
Here is the model:
mo <- lm(y ~ x, data=dd)
Find fit and residuals:
fit <- fitted(mo)
resi <- residuals(mo)
Function to retrieve confidence intervals based on the residual bootstrapping:
FUN <- function() {
X <- model.matrix(mo)
ressampy <- fit sample(resi, length(resi), replace = TRUE)
bootmod <- lm(ressampy ~ X-1)
confint(bootmod, level = 0.95)
}
Output of 1 run (notice that the printouts are X(Intercept)
and Xx
but instead I just want them to be (Intercept)
and x
)
FUN()
2.5 % 97.5 %
X(Intercept) 49.74439 50.07817
Xx 24.92904 25.25103
This may be a easy fix but I just couldn't get it to work. Any help will be greatly appreciated!
CodePudding user response:
Simply use rownames()
to change the row names of the matrix containing the confidence intervals, as in:
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)
}