Skip to contents

ar(), ma(), arma(), cosy(), unstr() and fcor() are read inside a model formula, as in bf(y ~ x + ar(week, subj)), and the formula is where they belong. The functions also exist on their own, as they do in brms, so that a call outside a formula returns the term rather than an error that the function does not exist. frmtmb-autocor describes what each term fits.

Usage

ar(time = NA, gr = NA, p = 1, cov = FALSE)

ma(time = NA, gr = NA, q = 1, cov = FALSE)

arma(time = NA, gr = NA, p = 1, q = 1, cov = FALSE)

cosy(time = NA, gr = NA)

unstr(time, gr)

fcor(M)

acformula(autocor, resp = NULL)

Arguments

time

The time variable. NA takes the row order within each group.

gr

The grouping variable. NA puts all rows in one group.

p, q

The autoregressive and moving-average orders.

cov

FALSE, the brms default, for the regression on earlier residuals; TRUE for the residual covariance matrix. See frmtmb-autocor.

M

For fcor(), the name of the known residual correlation or covariance matrix over the observations, which belongs in data2.

autocor

A one-sided formula of autocorrelation terms.

resp

The response the terms belong to, for a multivariate formula.

Value

ar(), ma(), arma(), cosy(), unstr() and fcor() return an object of class frmtmb_ac_term holding the term as a call. acformula() returns the one-sided formula with class frmtmb_acformula.

Details

brms gives two ways to write the terms apart from the location formula, and frmtmb reads both: the autocor argument of bf(), bf(y ~ x, autocor = ~ ar(week, subj)), and acformula() added to a formula, bf(y ~ x) + acformula(~ ar(week, subj)). Each adds the terms to the location formula, so both fit the model bf(y ~ x + ar(week, subj)). They are also the only way to give a nonlinear model a term, bf(y ~ a * exp(b * x), a + b ~ 1, nl = TRUE, autocor = ~ ar(week, subj)): the body's value is the mean the term reads, and a term written in the body is refused, because a body is evaluated and not read as terms. On a body, a later term replaces an earlier one, as in brms, where a linear formula refuses two. A term object added to a formula, as in bf(y ~ x) + ar(week, subj), is refused with brms's own message, because brms refuses it.

See also

Other autocorrelation: frmtmb-autocor

Examples

ar(week, subj, p = 2)
#> Autocorrelation term ar(week, subj, p = 2)
f <- bf(y ~ x) + acformula(~ ar(week, subj))
f$formula
#> y ~ x + ar(week, subj)
#> <environment: 0x563b3e5f6e50>
bf(y ~ x, autocor = ~ cosy(week, subj))$formula
#> y ~ x + cosy(week, subj)
#> <environment: 0x563b3e5f6e50>
try(bf(y ~ x) + ar(week, subj))
#> Error : Autocorrelation terms can only be specified on the right-hand side of a formula, not added to a 'brmsformula' object. Write the term in the formula, bf(y ~ x + ar(time, gr)), or add it with acformula(~ ar(time, gr))