Learn R Programming

itsadug (version 2.4.1)

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,
  cond = NULL,
  max_lag = NULL,
  fun = mean,
  plot = TRUE,
  random = F,
  mfrow = NULL,
  add = FALSE,
  print.summary = getOption("itsadug_print"),
  ...
)

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.

cond

Named list with a selection of the time series events specified in split_by. Default is NULL, indicating that all time series are being processed, rather than a selection.

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

plot

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

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.

print.summary

Logical: whether or not to print summary. Default set to the print info messages option (see infoMessages).

...

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(), derive_timeseries(), resid_gam(), start_event(), start_value_rho()

Examples

Run this code
# NOT RUN {
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))

# }
# NOT RUN {
# 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))

# }
# NOT RUN {
#---------------------------------------------
# When using model residuals
#---------------------------------------------

# }
# NOT RUN {
# 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))
# }
# NOT RUN {
# see the vignette for examples:
vignette('acf', package='itsadug')
# }

Run the code above in your browser using DataLab