y <- rnorm(10)
x <- rnorm(10)
model1 <- lm(y ~ x)
# formal output
pre <- l_predict(model1, newdata = data.frame(x = sort(x)),
interval = "conf")
head(pre)
if(interactive()) {
p <- with(cars, l_plot(speed, dist))
# Example taken from
# https://stackoverflow.com/questions/23852505/how-to-get-confidence-interval-for-smooth-spline
#
l_predict.smooth.spline <- function(model, interval = c("confidence", "none"),
level = 0.95, ...) {
# confidence interval of `smooth.spline`
interval <- match.arg(interval)
res <- (model$yin - model$y)/(1 - model$lev) # jackknife residuals
sigma <- sqrt(var(res)) # estimate sd
std <- stats::qnorm(level / 2 + 0.5)
upper <- model$y + std * sigma * sqrt(model$lev) # upper 95% conf. band
lower <- model$y - std * sigma * sqrt(model$lev) # lower 95% conf. band
data.frame(y = model$yin, lower = lower, upper = upper)
}
l <- l_layer_smooth(p, method = "smooth.spline", interval = "confidence")
}
Run the code above in your browser using DataLab