add.monthly.annual.cycle: Monthly Annual Cycle State Component

add.monthly.annual.cycleR Documentation

Monthly Annual Cycle State Component


A seasonal state component for daily data, representing the contribution of each month to the annual seasonal cycle. I.e. this is the "January, February, March, ..." effect, with 12 seasons. There is a step change at the start of each month, and then the contribution of that month is constant over the course of the month.

Note that if you have anything other than daily data, then you're probably looking for AddSeasonal instead.

The state of this model is an 11-vector gamma[t, ] where the first element is the contribution to the mean for the current month, and the remaining elements are the values for the 10 most recent months. When t is the first day in the month then

gamma[t+1, 1] = -1 * sum(gamma[t, -1]) + rnorm(1, 0, sigma)

And the remaining elements are gamma[t, ] shifted down one. When t is any other day then gamma[t+1, ] == gamma[t, ].


                      date.of.first.observation = NULL,
                      sigma.prior = NULL,
                      initial.state.prior = NULL,



A list of state components, to which the monthly annual cycle will be added. If omitted, an empty list will be assumed.


The time series to be modeled, as a numeric vector.


The time stamp of the first observation in y, as a Date or POSIXt object. If y is a zoo object with appropriate time stamps then this argument will be deduced.


An object created by SdPrior describing the prior distribution for the standard deviation of the random walk increments.


An object created using NormalPrior, describing the prior distribution of the the initial state vector (at time 1).


The standard deviation of the series to be modeled. This will be ignored if y is provided, or if all the required prior distributions are supplied directly.


  ## Let's simulate some fake daily data with a monthly cycle.
## Not run: 
  residuals <- rnorm(365 * 5)

## End(Not run)

  n <- length(residuals)
  dates <- seq.Date(from = as.Date("2014-01-01"),
                    len = n,
                    by = 1)
  monthly.cycle <- rnorm(12)
  monthly.cycle <- monthly.cycle - mean(monthly.cycle)
  timestamps <- as.POSIXlt(dates)
  month <- timestamps$mon + 1

  new.month <- c(TRUE, diff(timestamps$mon) != 0)
  month.effect <- cumsum(new.month) 
  month.effect[month.effect == 0] <- 12

  response <- monthly.cycle[month] + residuals
  response <- zoo(response, timestamps)

  ## Now let's fit a bsts model to the daily data with a monthly annual
  ## cycle.
  ss <- AddLocalLevel(list(), response)
  ss <- AddMonthlyAnnualCycle(ss, response)

  ## In real life you'll probably want more iterations.
  model <- bsts(response, state.specification = ss, niter = 200)
  plot(model, "monthly")

bsts documentation built on Nov. 10, 2022, 5:53 p.m.