acf_resid: Generate an ACF plot of model residuals. Works for lm, lmer,...

View source: R/acf.R

acf_residR Documentation

Generate an ACF plot of model residuals. Works for lm, lmer, gam, bam, ....


Wrapper around acf_plot and acf_n_plots for regression models.


  split_pred = NULL,
  n = 1,
  plot = TRUE,
  check.rho = NULL,
  main = NULL,



A regression model generated by lm, glm, lmer, glmer, gam, or bam. (See examples for how to avoid errors due to missing values.)


Vector with names of model predictors that determine the time series in the data, or should be used to split the ACF plot by. Alternatively, split_pred can be a named list as being used by acf_plot and acf_n_plots. Yet another option is to provide the text string 'AR.start', for a model that includes an AR1 model. The events are derived from the AR.start column if that is provided.


The number of plots to generate. If n=1 (default) then acf_plot is being called. If n>1 then acf_n_plots is being called.


Logical: whether or not to produce plot. Default is TRUE.


Numeric value: Generally leave at NULL. This value does not change anything, but it is used to check whether the model's AR1 coefficient matches the expected value of rho.


Text string, title of plot.


Other arguments as input for acf_plot or acf_n_plots.


An aggregated ACF plot and / or optionally a list with the aggregated ACF values.


Jacolien van Rij

See Also

Use acf for the original ACF function, and acf_plot, or acf_n_plots.

Other functions for model criticism: acf_n_plots(), acf_plot(), derive_timeseries(), resid_gam(), start_event(), start_value_rho()



# add missing values to simdat:
simdat[sample(nrow(simdat), 15),]$Y <- NA

## Not run: 
# Run GAMM model:
m1 <- bam(Y ~ te(Time, Trial)+s(Subject, bs='re'), data=simdat)

# Using a list to split the data:
acf_resid(m1, split_pred=list(simdat$Subject, simdat$Trial))
# ...or using model predictors:
acf_resid(m1, split_pred=c('Subject', 'Trial'))

# Calling acf_n_plots:
acf_resid(m1, split_pred=c('Subject', 'Trial'), n=4)
# add some arguments:
acf_resid(m1, split_pred=c('Subject', 'Trial'), n=4, max_lag=10)

# This does not work...
m2 <- lm(Y ~ Time, data=simdat)
acf_resid(m2, split_pred=c('Subject', 'Trial'))
# ... but this is ok:
acf_resid(m2, split_pred=list(simdat$Subject, simdat$Trial))

# Using AR.start column:
simdat <- start_event(simdat, event=c('Subject', 'Trial'))
r1 <- start_value_rho(m1)
m3 <- bam(Y ~ te(Time, Trial)+s(Subject, bs='re'), data=simdat, 
    rho=r1, AR.start=simdat$start.event)
acf_resid(m3, split_pred='AR.start')
# this is the same:
acf_resid(m3, split_pred=c('Subject', 'Trial'))
# Note: use model comparison to find better value for rho

## End(Not run)
# see the vignette for examples:
vignette('acf', package='itsadug')

itsadug documentation built on June 17, 2022, 5:05 p.m.