Learn R Programming

itsadug (version 0.8)

acf_n_plots: Generate N ACF plots of individual or aggregated time series.

Description

Generate N ACF plots of individual or aggregated time series.

Usage

acf_n_plots(x, n = 5, split_by = NULL, max_lag = NULL, fun = mean,
  random = F, mfrow = NULL, add = FALSE, ...)

Arguments

x
A vector with time series data, typically residuals of a regression model.
n
The number of plots to generate.
split_by
List of vectors (each with equal length as x) that group the values of x into trials or timeseries events. Generally other columns from the same data frame.
max_lag
Maximum lag at which to calculate the acf. Default is the maximum for the longest time series.
fun
The function used when aggregating over time series (depending on the value of split_by).
random
Logical: determine randomly which n (aggregated) time series are plotted, or use the quantile function to find a range of different time series to plot. Default is FALSE (not random).
mfrow
A vector of the form c(nr, nc). The figures will be drawn in an nr-by-nc array on the device by rows.
add
Logical: whether to add the plots to an exiting plot window or not. Default is FALSE.
...
Other arguments for plotting, see par.

Value

  • n ACF plots providing information about the autocorrelation in x.

See Also

Use acf for the original ACF function, and acf_plot for an ACF that takes into account time series in the data.

Other functions for model criticism: acf_plot; acf_resid; check_normaldist; resid.gam, resid_gam

Examples

Run this code
data(simdat)
# Separate ACF for each time series:
acf_n_plots(simdat$Y, split_by=list(simdat$Subject, simdat$Trial))
# Average ACF per participant:
acf_n_plots(simdat$Y, split_by=list(simdat$Subject))
# Data treated as single time series. Plot is added to current window.
# Note: 1 time series results in 1 plot.
acf_n_plots(simdat$Y, add=TRUE)
# Plot 4 ACF plots doesn't work without splitting data:
acf_n_plots(simdat$Y, add=TRUE, n=4)
# Plot ACFs of 4 randomly selected time series:
acf_n_plots(simdat$Y, random=TRUE, n=4, add=TRUE,
    split_by=list(simdat$Subject, simdat$Trial))
# See also the vignette for an example:
vignette(topic="plotfunctions", package="itsadug")

#---------------------------------------------
# When using model residuals
#---------------------------------------------

# add missing values to simdat:
simdat[sample(nrow(simdat), 15),]$Y <- NA
# simple linear model:
m1 <- lm(Y ~ Time, data=simdat)

# This will generate an error:
# acf_n_plots(resid(m1), split_by=list(simdat$Subject, simdat$Trial))

# This should work:
el.na <- missing_est(m1)
acf_n_plots(resid(m1),
     split_by=list(simdat[-el.na,]$Subject, simdat[-el.na,]$Trial))

# This should also work:
simdat$res <- NA
simdat[!is.na(simdat$Y),]$res <- resid(m1)
acf_n_plots(simdat$res, split_by=list(simdat$Subject, simdat$Trial))

Run the code above in your browser using DataLab