test_tac is a helper function for model_gam, model_gamm, and plot_diagnostics to compute the (partial) autocorrelation functions. It also tests whether residuals show temporal autocorrelation (see details).

test_tac(model_resid)

Arguments

model_resid

A list of residuals from one or many Generalized Additive (Mixed) Models (GAM(M).

Value

The function returns a tibble with one row for each model and three columns:

acf

A list-column with values from the autocorrelation function.

pacf

A list-column with values from the partial autocorrelation function.

tac

logical; if TRUE, temporal autocorrelation was detected

Details

NOTE: if the time series on which the GAM(M) is fitted contains missing values, they need to be accounted for in the residual vector. Observations with one or more NAs in-between will be otherwise considered as having a lag of 1.

The test for temporal autocorrelation is based on the following condition: If any of the acf and any of the pacf values of lag 1 - 5 is greater or lower than 0.4, a TRUE is returned.

See also

Examples

# Using models of the Baltic Sea demo data: # Get model residuals of GAMs model_resid <- purrr::map(model_gam_ex$model, ~mgcv::residuals.gam(., type = "deviance")) # test whether model residuals show significant TAC test_tac(model_resid)
#> # A tibble: 84 × 3 #> acf pacf tac #> <list> <list> <lgl> #> 1 <dbl [15]> <dbl [14]> FALSE #> 2 <dbl [15]> <dbl [14]> FALSE #> 3 <dbl [15]> <dbl [14]> FALSE #> 4 <dbl [15]> <dbl [14]> FALSE #> 5 <dbl [15]> <dbl [14]> FALSE #> 6 <dbl [15]> <dbl [14]> FALSE #> 7 <dbl [15]> <dbl [14]> FALSE #> 8 <dbl [15]> <dbl [14]> FALSE #> 9 <dbl [15]> <dbl [14]> FALSE #> 10 <dbl [15]> <dbl [14]> FALSE #> # … with 74 more rows
# Works also with GAMMs model_resid <- purrr::map_if(model_gamm_ex$model, !is.na(model_gamm_ex$model), ~as.numeric(mgcv::residuals.gam(.$gam, type = "deviance"))) # (exclude those GAMMs that were not fitted) # test whether model residuals show significant TAC test_tac(model_resid)$tac
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [136] TRUE TRUE TRUE