Search code examples
rplotregressiongammgcv

How are the "plot.gam" confidence intervals calculated?


If a model is fitted using mgcv and then the smooth terms are plotted,

m <- gam(y ~ s(x))
plot(m, shade = TRUE)

then you get a plot of the curve with a confidence interval. These are, I presume, pointwise-confidence intervals. How are they computed?

I tried to write

object <- plot(m, shade = true)
object[[1]]$fit +- 2*object[[1]]$se

in order to extract the lower and upper bounds using the standard errors and a multiplier of 2, but when I plot it, it looks a bit different than the confidence intervals plotted by plot.gam?

So, how are those calculated?

I do not use seWithMean = true or anything like that.


Solution

  • It is 1 standard deviation.

    oo <- plot.gam(m)
    oo <- oo[[1]]
    points(oo$x, oo$fit, pch = 20)
    points(oo$x, oo$fit - oo$se, pch = 20)
    

    Reproducible example:

    x <- seq(0, 2 * pi, length = 100)
    y <- x * sin(x) + rnorm(100, 0, 0.5)
    m <- gam(y ~ s(x))