Bootstrap Confidence Bands for Linear Regression (in R)

I am looking for a way to implement non-parametric bootstrap to confidence bands around my regression line for my linear regression model. I am, however, new to bootstrap, therefore I am unsure how to proceed. I have, thus, the following questions:

1. Is it even possible to implement non-parametric bootstrap to confidence bands?
2. If yes, how do I proceed?
3. How do proceed when I am using R?

Thanks a bunch!

• Is this simple linear regression, or something more complicated? Can you show your data? Or at least part of it, in order to provide context? Why do you need to use a nonparametric procedure? (Residuals not normal?) – BruceET May 24 at 18:48

As a pragmatic solution, for each bootstrapped dataset,

1. Fit the regression model
2. Calculate the predicted values at every desired observation level
3. Calculate the bootstrap interval estimates for each set of predictions at an observation level

Voilà

As always, R code seems to convince people more than anything any more so a small working example:

set.seed(123)
x <- seq(-3, 3, by=0.1)
y <- rnorm(length(x), log(x+4), x+3)

preds <- replicate(1e4, {
i <- sample(seq(x), replace=T)
xi <- x[i]
yi <- y[i]
predict(lm(yi~xi), newdata=data.frame(xi=x))
})

preds <- apply(preds, 1, quantile, c(0.025, 0.975))
plot(x,y)
apply(preds, 1, lines, x=x)


gives 