test_tac: Compute (partial) autocorrelation functions and test for...

View source: R/test_tac.R

test_tacR Documentation

Compute (partial) autocorrelation functions and test for significance

Description

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).

Usage

test_tac(model_resid)

Arguments

model_resid

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

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 outside the 0.95 confidence interval (CI), a TRUE is returned. This CI is the same as shown in the acf plot using the default settings and accounts for the length of the time series. It is calculated as follows: qnorm((1-0.95)/2)/sqrt(length(timeseries))

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

See Also

model_gam, model_gamm, plot_diagnostics

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)

# 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

saskiaotto/INDperform documentation built on Feb. 22, 2025, 3:15 a.m.