Skip to contents

The number of sunspots rises and falls in a cycle of about 11 years. This article fits that cycle as a damped oscillator, a second order model, to the yearly counts from 1700 to 1988. It shows how to model a single subject, what a pair of complex eigenvalues of the drift matrix means, and how to fit the same model by Bayesian sampling with priors chosen for the data.

Data

ctsem takes long format data with an id column, a time column and the observed variables. Here there is one subject, the sun.

ssdat <- data.frame(id = 1, time = as.numeric(time(sunspot.year)),
  sunspots = as.numeric(sunspot.year))
head(ssdat)
#>   id time sunspots
#> 1  1 1700        5
#> 2  1 1701       11
#> 3  1 1702       16
#> 4  1 1703       23
#> 5  1 1704       36
#> 6  1 1705       58
ggplot(ssdat, aes(time, sunspots)) + geom_line() + theme_bw()

A damped oscillator

A first order model lets a process return towards its equilibrium after a shock, but it cannot overshoot and swing back. To get a cycle, the model needs a second latent process, the rate of change of the first. Call them level and velocity:

  • the level changes at the rate given by the velocity, so DRIFT[1, 2] is fixed to 1 and the level’s own auto effect to 0;
  • the velocity changes according to the level (a21) and to itself (a22). A negative a21 pulls a high level back down, and a negative a22 damps the swings;
  • random shocks act on the velocity only, so DIFFUSION has one free cell;
  • only the level is measured (LAMBDA = c(1, 0)), with an intercept m1 and measurement error.

With the intercept on the measurement, the continuous time intercept CINT is fixed to 0. By default the intercepts and starting values vary over subjects, which one subject cannot show, so the last line turns that off.

ssmodel <- ctModel(type = 'ct',
  manifestNames = 'sunspots',
  latentNames = c('level', 'velocity'),
  LAMBDA = c(1, 0),
  DRIFT = c(0, 1,
    'a21', 'a22'),
  MANIFESTMEANS = 'm1',
  CINT = 0,
  DIFFUSION = c(0, 0,
    0, 'diffusion'))
ssmodel$pars$indvarying <- FALSE

ctModelLatex(ssmodel) writes the model out as equations and opens them as a pdf:

ctModelHigherOrder() adds second order dynamics to an existing model, which is quicker for a model with many processes.

Fitting

fit <- ctFit(ssdat, ssmodel, cores = 2)

With one subject the variance of the starting values (T0VAR) cannot be estimated either, so ctFit() fixes it to a small value and says so in a message. It would also have removed the random effects if the model had kept them.

summary(fit)$popmeans
#>                 mean     sd    2.5%     50%   97.5%    mode
#> T0m_level    -43.620  5.932 -55.214 -43.573 -32.287 -43.428
#> T0m_velocity  -1.266 14.999 -29.805  -1.299  29.230  -1.592
#> a21           -0.372  0.031  -0.430  -0.373  -0.312  -0.373
#> a22           -0.317  0.070  -0.471  -0.311  -0.201  -0.314
#> diffusion     19.309  1.804  15.786  19.297  23.056  19.317
#> mvarsunspots   5.386  0.597   4.221   5.397   6.621   5.370
#> m1            49.436  3.087  43.586  49.472  55.310  49.384

Both drift parameters are negative. The level is pulled back towards its mean of about 49 sunspots, and the swings are damped.

The drift matrix’s eigenvalues say more. A pair of complex eigenvalues means the process oscillates. The imaginary part gives the period of the cycle, and the real part how quickly the amplitude of a cycle decays when no new shocks arrive:

drift <- ctSummaryMatrices(fit)$estimate$DRIFT
drift
#>              level  velocity
#> level     0.000000  1.000000
#> velocity -0.372966 -0.313812
ev <- eigen(drift)$values
ev
#> [1] -0.156906+0.5902089i -0.156906-0.5902089i
2 * pi / abs(Im(ev[1]))   # period, in years
#> [1] 10.6457
log(2) / abs(Re(ev[1]))   # half-life of the amplitude, in years
#> [1] 4.417596

The estimated period is 10.6 years, close to the known sunspot cycle. Without new shocks, the amplitude of a cycle would halve in about 4.4 years. The random shocks to the velocity keep the cycle going, and make each cycle’s height different.

ctDiscretePars() shows the same thing as the effect over time of a one unit shock to each process. A shock to the level swings it below zero after about five years and back up after ten:

ctDiscretePars(fit, times = seq(0, 30, by = 0.5), plot = TRUE)

Predicted and smoothed states

ctPredict() gives the model’s predictions for each time, here every 0.1 years. The predicted values (yprior) use only the earlier observations, so they show what the model would have forecast. The plot is a ggplot, so coord_cartesian() can zoom in on a few cycles:

ctPredict(fit, timestep = 0.1, plot = TRUE, kalmanvec = c('y', 'yprior')) +
  coord_cartesian(xlim = c(1900, 1960))

Between observations the forecast follows the cycle, and its uncertainty grows until the next observation arrives. The smoothed states (etasmooth) use all observations, before and after each time. They give the best estimate of the level and of its velocity, which is not observed:

ctPredict(fit, timestep = 0.1, plot = TRUE, kalmanvec = 'etasmooth') +
  coord_cartesian(xlim = c(1900, 1960))

The velocity is highest while the level rises fastest, and crosses zero at each peak and trough of the level.

Bayesian estimation

priors = TRUE adds a prior to every parameter, and optimize = FALSE samples the posterior. Each parameter is a transform of a standard normal raw parameter, so the transform sets the prior:

ssmodel$pars[!is.na(ssmodel$pars$param), c('matrix', 'param', 'transform')]
#>           matrix                param                              transform
#> 1        T0MEANS            T0m_level                             10 * param
#> 2        T0MEANS         T0m_velocity                             10 * param
#> 7          DRIFT                  a21                                  param
#> 8          DRIFT                  a22 -(1e-06 + 2 * log1p_exp(-(2 * param)))
#> 12     DIFFUSION            diffusion      10 * log1p_exp(2 * param) + 1e-10
#> 13   MANIFESTVAR         mvarsunspots       1e-10 + 5 * log1p_exp(2 * param)
#> 14 MANIFESTMEANS                   m1                             10 * param
#> 17         T0VAR          T0var_level       1e-10 + 5 * log1p_exp(2 * param)
#> 19         T0VAR T0var_velocity_level                                  param
#> 20         T0VAR       T0var_velocity       1e-10 + 5 * log1p_exp(2 * param)

The defaults suit data centred near zero. m1, the mean number of sunspots, gets a normal prior with mean 0 and standard deviation 10, while the data have a mean of about 50. A prior predictive check shows what such priors imply. ctGenerate(fromPriors = TRUE) draws parameters from the priors and generates data with the design of ssdat:

prior <- ctGenerate(ssmodel, fromPriors = TRUE, datastruct = ssdat,
  nsamples = 50)
dim(prior$Y)   # draws by rows of data by variables
#> [1]  50 289   1

The second field of a parameter’s string sets its transform. 'm1|param*20+50' gives m1 a normal prior with mean 50 and standard deviation 20, and 't0_level|param*50' lets the level start up to about 100 from that mean:

ssmodel2 <- ctModel(type = 'ct',
  manifestNames = 'sunspots',
  latentNames = c('level', 'velocity'),
  LAMBDA = c(1, 0),
  DRIFT = c(0, 1,
    'a21', 'a22'),
  T0MEANS = c('t0_level|param*50', 't0_velocity'),
  MANIFESTMEANS = 'm1|param*20+50',
  CINT = 0,
  DIFFUSION = c(0, 0,
    0, 'diffusion'))
ssmodel2$pars$indvarying <- FALSE
prior2 <- ctGenerate(ssmodel2, fromPriors = TRUE, datastruct = ssdat,
  nsamples = 50)
priorlines <- function(gen, label) {
  data.frame(priors = label,
    time = rep(ssdat$time, each = dim(gen$Y)[1]),
    draw = rep(seq_len(dim(gen$Y)[1]), times = nrow(ssdat)),
    sunspots = c(gen$Y[, , 1]))
}
pp <- rbind(priorlines(prior, 'default priors'),
  priorlines(prior2, 'adjusted priors'))
pp$priors <- factor(pp$priors, levels = c('default priors', 'adjusted priors'))
ggplot(pp, aes(time, sunspots)) +
  geom_line(aes(group = draw), colour = 'red', alpha = .2) +
  geom_line(data = ssdat, linewidth = .7) +
  facet_wrap(~priors) +
  coord_cartesian(ylim = c(-150, 250)) +
  theme_bw()

Under the default priors the series is centred near zero. The adjusted priors cover the data and still allow a wide range of other series. Some draws grow without bound, because the prior on a21 allows positive values, which push the level away from its mean. The data rule those out. With 289 years of data the priors matter little for the dynamics, but a prior far from the data can slow the sampler and, with less data, move the estimates.

sfit <- ctFit(ssdat, ssmodel2, priors = TRUE, optimize = FALSE, cores = 2)
summary(sfit)$popmeans
#>                 mean    sd    2.5%     50%   97.5% n_eff  Rhat
#> t0_level     -43.353 5.667 -54.500 -43.312 -32.085   755 1.007
#> t0_velocity   -0.611 8.364 -16.638  -0.677  16.521   437 1.008
#> a21           -0.371 0.032  -0.435  -0.370  -0.313   834 1.004
#> a22           -0.321 0.075  -0.478  -0.316  -0.186   828 1.002
#> diffusion     19.684 1.873  16.397  19.532  23.721   776 1.002
#> mvarsunspots   5.377 0.594   4.257   5.388   6.571  1073 0.999
#> m1            49.345 2.991  43.320  49.352  55.137   797 1.000

The posterior means are close to the maximum likelihood estimates above. n_eff is the effective number of independent draws, and Rhat near 1 says the chains agree. ctPlotPosterior() compares each parameter’s posterior with its prior:

ctGenerateFromFit() generates new data from the fitted model, with the times of the original data. With fullposterior = TRUE each data set uses a different draw from the posterior. Here are three, against the data in black:

gen <- ctGenerateFromFit(sfit, nsamples = 3, fullposterior = TRUE)
post <- data.frame(time = rep(ssdat$time, each = 3),
  draw = factor(rep(1:3, times = nrow(ssdat))),
  sunspots = c(gen$generated$Y[, , 1]))
ggplot(post, aes(time, sunspots)) +
  geom_line(aes(colour = draw), alpha = .7) +
  geom_line(data = ssdat, linewidth = .7) +
  theme_bw()

The generated series cycle at about the same rate as the data, but less regularly, and they often go below zero, which sunspot counts cannot. The data also rise faster than they fall, which a linear model cannot produce. A model of the square root of the counts, or a count measurement model (manifesttype = 3, see vignette('measurement-models')), would be a next step.