Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

addSeason2formula

Function that adds a sine-/cosine formula to an existing formula.


Description

This function helps to construct a formula object that can be used in a call to hhh4 to model seasonal variation via a sum of sine and cosine terms.

Usage

addSeason2formula(f = ~1, S = 1, period = 52, timevar = "t")

Arguments

f

formula that the seasonal terms should be added to, defaults to an intercept ~1.

S

number of sine and cosine terms. If S is a vector, unit-specific seasonal terms are created.

period

period of the season, defaults to 52 for weekly data.

timevar

the time variable in the model. Defaults to "t".

Details

The function adds the seasonal terms

sum_s gamma_s * sin(2*pi*s/period * t) + delta_s * cos(2*pi*s/period * t),

where gamma_s and delta_s are the unknown parameters and t, t = 1, 2, … denotes the time variable timevar, to an existing formula f.

Note that the seasonal terms can also be expressed as

γ_s sin(2*pi*s/period * t) + δ_s cos2*pi*s/period * t) = A_s sin(2*pi*s/period * t + ε_s)

with amplitude A_s=sqrt{γ_s^2 +δ_s^2} and phase shift \tan(ε_s) = δ_s / γ_s. The amplitude and phase shift can be obtained from a fitted hhh4 model via coef(..., amplitudeShift = TRUE), see coef.hhh4.

Value

Returns a formula with the seasonal terms added and its environment set to .GlobalEnv. Note that to use the resulting formula in hhh4, a time variable named as specified by the argument timevar must be available.

Author(s)

M. Paul, with contributions by S. Meyer

See Also

Examples

# add 2 sine/cosine terms to a model with intercept and linear trend
addSeason2formula(f = ~ 1 + t, S = 2)

# the same for monthly data
addSeason2formula(f = ~ 1 + t, S = 2, period = 12)

# different number of seasons for a bivariate time series
addSeason2formula(f = ~ 1, S = c(3, 1), period = 52)

surveillance

Temporal and Spatio-Temporal Modeling and Monitoring of Epidemic Phenomena

v1.19.1
GPL-2
Authors
Michael H<f6>hle [aut, ths] (<https://orcid.org/0000-0002-0423-6702>), Sebastian Meyer [aut, cre] (<https://orcid.org/0000-0002-1791-9449>), Michaela Paul [aut], Leonhard Held [ctb, ths], Howard Burkom [ctb], Thais Correa [ctb], Mathias Hofmann [ctb], Christian Lang [ctb], Juliane Manitz [ctb], Andrea Riebler [ctb], Daniel Saban<e9>s Bov<e9> [ctb], Ma<eb>lle Salmon [ctb], Dirk Schumacher [ctb], Stefan Steiner [ctb], Mikko Virtanen [ctb], Wei Wei [ctb], Valentin Wimmer [ctb], R Core Team [ctb] (A few code segments are modified versions of code from base R)
Initial release
2021-03-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.