# GAM: Confidence interval for the covariates in gratia (R)

I am using gratia to fit a gam model such as:

formula = as.formula(paste(Region, "~ s(date, k=20) + ID"))


you can extract the CI of Region using:

ciGP2 <- confint(mod, parm = "s(date)",  type = "confidence")

mod <- gam(formula, data = dat, method = "REML")


I was wondering:

1. I would like to know the confidence interval for the covariate "Region"

2. Maybe it is the solution to 1 =); how do I get fitted "date" value for a specific "Region" value? (that would probably allow me to calculate the CI of "date" based on the CI of "Region"?)

3. Finally, the estimated values of the GAM are different from the original. Is there a way to obtain the estimated values in their original metric?

Thank you very much

• What does your gam() call look like? 3. is likely due to either i) using a non-Gaussian distribution for family or ii) not appreciating that smooths are (mostly) subject to identifiability constraints (in this case a sum-to-zero constraint) which means the smooths are centred about the model constant term (Intercept). Apr 28, 2022 at 7:40
• Hello, I just added two of the gam. What are you thoughts about? how can I have the actual values rather than an estimation centered on 0? thank you again! Apr 28, 2022 at 16:14
• I also added the gam call: mod <- gam(formula, data = dat, method = "REML") Apr 28, 2022 at 17:03
• also, mod <- gam(formula, data = dat) does'nt change the estimated values Apr 28, 2022 at 21:00

## Q1

By the convention with confint() (the generic in base R), the coverage of the interval is given by argument level, which is typically set to 0.95 such that the interval is a 95% confidence interval. Check out ?confint.gam for more detail.

## Q2

You have this back to front; you don't get a fitted value for date, you get one for Region that is a function of date and ID. If you want to know what value of Region were estimated for input date values then look at the est column in the outputm with the date column indicating what values of date the estimate and interval are for:

library("mgcv")
library("gratia")
dat <- gamSim(1, n = 500, dist = "normal", scale = 2)
mod <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, method = "REML")

## point-wise interval
ci <- confint(mod, parm = "s(x1)", type = "confidence")
ci


which gives

#> # A tibble: 200 x 8
#>    smooth by_variable      x1   est    se  crit lower upper
#>    <chr>  <fct>         <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 s(x1)  NA          0.00251 -2.04 0.296  1.96 -2.62 -1.46
#>  2 s(x1)  NA          0.00752 -2.04 0.287  1.96 -2.60 -1.47
#>  3 s(x1)  NA          0.0125  -2.03 0.279  1.96 -2.58 -1.49
#>  4 s(x1)  NA          0.0175  -2.03 0.270  1.96 -2.56 -1.50
#>  5 s(x1)  NA          0.0225  -2.02 0.262  1.96 -2.54 -1.51
#>  6 s(x1)  NA          0.0276  -2.02 0.254  1.96 -2.52 -1.52
#>  7 s(x1)  NA          0.0326  -2.01 0.246  1.96 -2.50 -1.53
#>  8 s(x1)  NA          0.0376  -2.01 0.239  1.96 -2.48 -1.54
#>  9 s(x1)  NA          0.0426  -2.01 0.232  1.96 -2.46 -1.55
#> 10 s(x1)  NA          0.0476  -2.00 0.226  1.96 -2.44 -1.56
#> # ... with 190 more rows


If you want to get point estimates and intervals for the observed data, pass the original data to the newdata argument of confint.gam

# Q3

This is likely due to either

1. your using a non-Gaussian distribution for family, or
2. not appreciating that smooths are (mostly) subject to identifiability constraints (in this case a sum-to-zero constraint) which means the smooths are centred about the model constant term (Intercept)

Note that the est in the output is the estimated partial effect on the response of the smooth of date for specified values of date. It isn't an estimated value in the sense that it is scaled as per the original response.

• thank you for answering my question so quickly! for question 1: I guess, this is the same problem as question 2: "you don't get a fitted value for date, you get one for Region that is a function of date and ID". So that is why I can't get any confidence interval too for date? which actually make sense... Apr 28, 2022 at 16:09