This function computes predicted values and confidence intervals from a fitted GAM model with a log-link (or other link) and optionally back-transforms predictions to the response scale. Five methods are supported: 1. **delta_link**: classic delta method on the linear predictor (link) scale; back-transformed CI is asymmetric. 2. **delta_resp**: delta method applied directly on the response scale using \(\mathrm{Var}[\exp(\eta)] \approx \exp(2\eta)\,\mathrm{Var}(\eta)\). 3. **bootstrap_link**: parametric bootstrap on the linear predictor; quantiles back-transformed. 4. **bootstrap_resp**: parametric bootstrap on the response scale; quantiles computed after exponentiating. 5. **posterior**: Bayesian posterior simulation using the model covariance matrix; quantiles on response scale.

ci_resp(
  model,
  newdata,
  nboot = 100,
  model.ci_method = c("posterior", "delta_link", "delta_resp", "bootstrap_link",
    "bootstrap_resp"),
  level = 0.95
)

Arguments

model

A fitted GAM object from mgcv::gam.

newdata

A data.frame containing values at which to predict.

nboot

Integer. Number of bootstrap or posterior samples. Default 1000.

model.ci_method

Character. One of "delta_link", "delta_resp", "bootstrap_link", "bootstrap_resp", "posterior".

level

Numeric. Confidence level, default 0.95.

Value

A data.table with columns: fit, lwr, upr.

Details

References: - Wood, S.N. (2017) *Generalized Additive Models: An Introduction with R, 2nd Edition*. CRC Press. - Efron, B., & Tibshirani, R. (1993) *An Introduction to the Bootstrap*. Chapman & Hall. - Gelman, A., et al. (2013) *Bayesian Data Analysis, 3rd Edition*. CRC Press.

Examples

# loading processed data
dt.samples_trt <- readRDS(system.file("extdata", "dt.samples_trt.rds", package = "growthTrendR"))
# climate
dt.clim <- data.table::fread(system.file("extdata", "dt.clim.csv", package = "growthTrendR"))
# pre-data for model
dt.samples_clim <- prepare_samples_clim(dt.samples_trt, dt.clim)
dt.m <- dt.samples_clim[ageC >1]
# using gamm_spatial model as an example
m.sp <-gamm_spatial(data = dt.m, resp_scale = "resp_log",
       m.candidates = "bai_cm2 ~ log(ba_cm2_t_1) + s(ageC) + s(FFD)")

dt.m[, uid_site.fac:= as.factor(as.character(uid_site))]
#>      site_id  year uid_radius uid_site species uid_tree uid_sample    sample_id
#>       <char> <int>      <int>    <int>  <char>    <int>      <int>       <char>
#>   1:   X003b  1992          1        1 PSEUMEN        1          1 X003_101_008
#>   2:   X003b  1993          1        1 PSEUMEN        1          1 X003_101_008
#>   3:   X003b  1994          1        1 PSEUMEN        1          1 X003_101_008
#>   4:   X003b  1995          1        1 PSEUMEN        1          1 X003_101_008
#>   5:   X003b  1996          1        1 PSEUMEN        1          1 X003_101_008
#>  ---                                                                           
#> 239:   X011c  2014          9        3 PSEUMEN        9          9 X011_101_005
#> 240:   X011c  2015          9        3 PSEUMEN        9          9 X011_101_005
#> 241:   X011c  2016          9        3 PSEUMEN        9          9 X011_101_005
#> 242:   X011c  2017          9        3 PSEUMEN        9          9 X011_101_005
#> 243:   X011c  2018          9        3 PSEUMEN        9          9 X011_101_005
#>         radius_id rw_mm  ageC ba_cm2_t_1   bai_cm2      FFD uid_site.fac
#>            <char> <num> <int>      <num>     <num>    <num>       <fctr>
#>   1: X003_101_008  2.32     2  0.1618831 0.4999908 24.72527            1
#>   2: X003_101_008  2.21     3  0.6618739 0.7907986 53.59116            1
#>   3: X003_101_008  2.79     4  1.4526724 1.4365906 33.70166            1
#>   4: X003_101_008  1.97     5  2.8892631 1.3089603 41.43646            1
#>   5: X003_101_008  2.07     6  4.1982234 1.6381301 41.75824            1
#>  ---                                                                    
#> 239: X011_101_005  1.66    24 65.4683562 4.8479047 20.99448            3
#> 240: X011_101_005  1.04    25 70.3162609 3.1254574 13.81215            3
#> 241: X011_101_005  1.18    26 73.4417183 3.6284892 16.48352            3
#> 242: X011_101_005  1.08    27 77.0702076 3.3976702 35.35912            3
#> 243: X011_101_005  1.21    28 80.4678777 3.8936994 27.07182            3
dt.ci <- ci_resp(m.sp$model$gam, newdata = dt.m)