Specification document - a mathematical description of models used by bage.
Note: some features described here have not been implemented yet.
Let yi be a count of events in cell i = 1, ⋯, n and let wi be the corresponding exposure measure, with the possibility that wi ≡ 1. The likelihood under the Poisson model is then using the shape-rates parameterisation of the Gamma distribution. Parameter ξ governs dispersion, with and We allow ξ to equal 0, in which case the model reduces to
For ξ > 0, Equations @ref(eq:lik-pois-1) and @ref(eq:lik-pois-2) are equivalent to (Norton, Christen, and Fox 2018; Simpson 2022). This is the format we use internally for estimation. When values for γi are needed, we generate them on the fly, using the fact that
The likelihood under the binomial model is Parameter ξ again governs dispersion, with and
We allow ξ to equal 0, in which case the model reduces to Equations @ref(eq:lik-binom-1) and @ref(eq:lik-binom-2) are equivalent to which is what we use internally. Values for γi can be generated using
The normal model is where ỹi is a standardized version of outcome yi, and w̃i is a standardized version of weight wi. The standardization is carried out using where
Standardizing allows us to apply the same priors as we use for the Poisson and binomial models.
Let μ = (μ1, ⋯, μn)⊤. Our model for μ is where
Each β(m), m > 0, can be a main effect, involving a single dimension, or an interaction, involving two dimensions. Some priors, when applied to an interaction, treat one dimension, referred to as the ‘along’ dimension, differently from the remaining dimensions, referred to as ‘by’ dimensions. A random walk prior (Section @ref(sec:pr-rw)), for instance, consists of an independent random walk along the ‘along’ dimension, within each combination of the ‘by’ dimensions.
We use v = 1, ⋯, Vm to denote position within the ‘along’ dimension, and u = 1, ⋯, Vm to denote position within a classification formed by the ‘by’ dimensions. When there are no sum-to-zero constraints (see below), Um = ∏kdk where dk is the number of elements in the kth ‘by’ variable. When there are sum-to-zero constraints, Um = ∏k(dk − 1).
If a prior involves an ‘along’ dimension but the user does not specify one, the procedure for choosing a dimension is as follows:
Priors that involve ‘along’ dimensions can optionally include sum-to-zero constraints. If these constrains are applied, then within each element v of the ‘along’ dimension, the sum of the βj(m) across each ‘by’ dimension is zero. For instance, if β(m) is an interaction between time, region, and sex, with time as the ‘along’ variable, then within each combination of time and region, the values for females and males sum to zero, and within each combination of time and sex, the values for regions sum to zero.
Except in the case of dynamic SVD-based priors (eg Sections @ref(sec:pr-svd-rw)), the sum-to-zero constraints are implemented internally by drawing values within an unrestricted lower-dimensional space, and then transforming to the restricted higher-dimensional space. For instance, a random walk prior for a time-region interaction with R regions consists of R − 1 unrestricted random walks along time, which are converted into R random walks that sum to zero across region. Matrices for transforming between the unrestricted and restricted spaces are constructed using the QR decomposition, as described in Section 1.8.1 of Wood (2017). With dynamic SVD-based priors, we draw values for the SVD coefficients with no constraints, convert these to unconstrained values for β(m), and then subtract means.
The intercept term β(0) can only be given a fixed-normal prior (Section @ref(sec:pr-fnorm)) or a Known prior (Section @ref(sec:pr-known)).
Exchangeable normal
N(s = 1)
s
is Aτ(m).
Defaults to 1.Exchangeable normal, with fixed standard deviation
NFix(sd = 1)
sd
is Aτ(m).
Defaults to 1.Random walk
A0(m) can be 0, implying that βu, 1(m) is fixed at 0.
When Um > 1, sum-to-zero constraints (Section @ref(sec:sum-to-zero)) can be applied.
If the prior includes sum-to-zero constraints, means are subtracted from the forecasted values within each combination of ‘along’ and ‘by’ variables.
RW(s = 1, along = NULL, zero_sum = TRUE)
s
is Aτ(m).
Defaults to 1.sd
is A0(m).
Defaults to 1.along
used to identify ‘along’ and ‘by’
dimensions.zero_sum
is TRUE
, sum-to-zero
constraints are applied.Second-order random walk
A0(m) can be 0, implying that βu, 1(m) is fixed at 0.
When Um > 1, sum-to-zero constraints (Section @ref(sec:sum-to-zero)) can be applied.
If the prior includes sum-to-zero constraints, means are subtracted from the forecasted values within each combination of ‘along’ and ‘by’ variables.
RW2(s = 1, sd = 1, sd_slope = 1, along = NULL, zero_sum = TRUE)
s
is Aτ(m)sd
is A0(m)sd_slope
is Aη(m)along
used to identify ‘along’ and ‘by’ dimensionszero_sum
is TRUE
, sum-to-zero
constraints are appliedRandom walk with seasonal effect
A0(m) can be 0, implying that αu, 1(m) is fixed at 0.
Aω(m)2 can be set to zero, implying that seasonal effects are fixed over time.
When Um > 1, sum-to-zero constraints (Section @ref(sec:sum-to-zero)) can be applied.
RW_Seas(n_seas, s = 1, sd = 1, s_seas = 0, sd_seas = 1, along = NULL, zero_sum = FALSE)
n_seas
is Sms
is Aτ(m)sd
is A0(m)s_seas
is Aω(m)sd_seas
is Aλ(m)along
used to identify ‘along’ and ‘by’ dimensionszero_sum
is TRUE
, sum-to-zero
constraints are appliedSecond-order random work, with seasonal effect
A0(m) can be 0, implying that αu, 1(m) is fixed at 0.
Aω(m)2 can be set to zero, implying that seasonal effects are fixed over time.
When Um > 1, sum-to-zero constraints (Section @ref(sec:sum-to-zero)) can be applied.
RW2_Seas(n_seas, s = 1, sd = 1, sd_slope = 1, s_seas = 0, sd_seas = 1, along = NULL, zero_sum = FALSE)
n_seas
is Sms
is Aτ(m)sd
is A0(m)sd_slope
is Aη(m)s_seas
is Aω(m)sd_seas
is Aλ(m)along
used to identify ‘along’ and ‘by’ dimensionszero_sum
is TRUE
, sum-to-zero
constraints are appliedInternally, TMB derives values for βu, v(m), v = 1, ⋯, Km, and for ωm, that imply a stationary distribution, and that give every term βu, v(m) the same marginal variance. We denote this marginal variance τm2, and assign it a prior Each of the ϕk(m) has prior
where p(βu, 1(m), ⋯, βu, Vm(m) ∣ ϕ1(m), ⋯, ϕKm(m), τm) is calculated internally by TMB.
AR(n_coef = 2,
s = 1,
shape1 = 5,
shape2 = 5,
along = NULL,
zero_sum = FALSE)
n_coef
is Kms
is Aτ(m)shape1
is S1(m)shape2
is S2(m)along
is used to indentify the ‘along’ and ‘by’
dimensionsSpecial case or AR()
, with extra options for
autocorrelation coefficient.
This is adapted from the specification used for AR1 densities in TMB. It implies that the marginal variance of all βu, v(m) is τm2. We require that −1 < a0m < a1m < 1.
AR1(s = 1,
shape1 = 5,
shape2 = 5,
min = 0.8,
max = 0.98,
along = NULL,
zero_sum = TRUE)
s
is Aτ(m)shape1
is S1(m)shape2
is S2(m)min
is a0mmax
is a1malong
is used to identify ‘along’ and ‘by’
dimensionsThe defaults for min
and max
are based on
the defaults for function ets()
in R package
forecast (Hyndman and Khandakar
2008).
Note that $\sum_{v=1}^{V_m} \alpha_{u,v}^{(m)} = 0$.
Lin(s = 1, mean_slop = 0, sd_slope = 1, along = NULL, zero_sum = FALSE)
s
is Aτ(m)mean_slope
is Bη(m)sd_slope
is Aη(m)along
is used to indentify ‘along’ and ‘by’
dimensionsNote that $\sum_{v=1}^{V_m} \alpha_{u,v}^{(m)} = 0$.
Internally, TMB derives values for ϵu, v(m), v = 1, ⋯, Km, and for ωm, that provide the ϵu, v(m) with a stationary distribution in which each term has the same marginal variance. We denote this marginal variance τm2, and assign it a prior Each of the individual ϕk(m) has prior
where p(ϵu, 1(m), ⋯, ϵu, Vm(m) ∣ ϕ1(m), ⋯, ϕKm(m), τm) is calculated internally by TMB.
Lin_AR(n_coef = 2,
s = 1,
shape1 = 5,
shape2 = 5,
mean_slope = 0,
sd_slope = 1,
along = NULL,
zero_coef = FALSE)
n_coef
is Kms
is Aτ(m)shape1
is S1(m)shape2
is S2(m)mean_slope
is Bη(m)sd_slope
is Aη(m)along
is used to indentify ‘along’ and ‘by’
variablesTODO - WRITE DETAILS
Penalised spline (P-spline)
where βu(m) is the subvector of β(m) composed of elements from the uth combination of the ‘by’ variables, B(m) is a Vm × Km matrix of B-splines, and αu(m) has a second-order random walk prior (Section @ref(sec:pr-rw2)).
B(m) = (b1(m)(v), ⋯, bKm(m)(v)), with v = (1, ⋯, Vm)⊤. The B-splines are centered, so that 1⊤bk(m)(v) = 0, k = 1, ⋯, Km.
Terms with a P-Spline prior cannot be forecasted.
Sp(n = NULL, s = 1)
n
is Km. Defaults to
max (0.7Jm, 4).s
is the Aτ(m)
from the second-order random walk prior. Defaults to 1.along
is used to identify ‘along’ and ‘by’
variablesAge but no sex or gender
Let βu be the age effect for the uth combination of the ‘by’ variables. With an SVD prior, where F(m) is a Vm × Km matrix, and g(m) is a vector with Vm elements, both derived from a singular value decomposition (SVD) of an external dataset of age-specific values for all sexes/genders combined. The construction of F(m) and g(m) is described in Appendix @ref(app:svd). The centering and scaling used in the construction allow use of the simple prior
Joint model of age and sex/gender
In the joint model, vector βu represents the interaction between age and sex/gender for the uth combination of the ‘by’ variables. Matrix F(m) and vector g(m) are calculated from data that separate sexes/genders. The model is otherwise unchanged.
Independent models for each sex/gender
In the independent model, vector βs, u represents age effects for sex/gender s and the uth combination of the ‘by’ variables, and we have Matrix Fs(m) and vector gs(m) are calculated from data that separate sexes/genders. The prior is
for the age-only and joint models, and for the independent model
Terms with an SVD prior cannot be forecasted.
SVD(ssvd, n_comp = NULL, indep = TRUE)
where - ssvd
is an object containing F and g - n_comp
is
the number of components to be used (which defaults to
ceiling(n/2)
, where n
is the number of
components in ssvd
- indep
determines whether
and independent or joint model will be used if the term being modelled
contains a sex or gender variable.
The SVD_RW()
prior is identical to the
SVD()
prior except that the coefficients evolve over time,
following independent random walks. For instance, in the
combined-sex/gender and joint models with Km SVD
components,
In the combined-sex/gender and joint models,
and in the independent model,
SVD_RW(ssvd,
n_comp = NULL,
indep = TRUE,
s = 1,
sd = 1,
zero_sum = FALSE)
where - ssvd
is an object containing F and g - n_comp
is
Km -
indep
determines whether and independent or joint model
will be used if the term being modelled contains a sex or gender
variable. - s
is Aτ(m)
- sd
is A0(m)
Same structure as SVD_RW()
. TODO - write details.
Same structure as SVD_RW()
. TODO - write details.
Same structure as SVD_RW()
. TODO - write details.
Elements of β(m) are treated as known with certainty.
Known priors make no contribution to the posterior density.
Main effects with a known prior cannot be forecasted.
Known(values)
values
is a vector containing the βj(m).The columns of matrix Z are assumed to be standardised to have mean 0 and standard deviation 1. Z does not contain a column for an intercept.
We implement two priors for coefficient vector ζ. The first prior is designed for the case where P, the number of colums of Z, is small, and most ζp are likely to distinguishable from zero. The second prior is designed for the case where P is large, and only a few ζp are likely to be distinguishable from zero.
Regularized horseshoe prior (Piironen and Vehtari 2017)
where p0 is an initial guess at the number of ζp that are non-zero, and σ̂ is obtained as follows:
The quantities used for Poisson and binomial likelihoods are derived from normal approximations to GLMs (Piironen and Vehtari 2017; Gelman et al. 2014, sec. 16.2).
TODO - write
TODO - write
set_covariates(formula, data = NULL, n_coef = NULL)
formula
is a one-sided R formula describing the
covariates to be useddata
A data frame. If a value for data
is
supplied, then formula
is interpreted in the context of
this data frame. If a value for data
is not supplied, then
formula
is interpreted in the context of the data frame
used for the original call to mod_pois()
,
mod_binom()
, or mod_norm()
.n_coef
is the effective number of non-zero
coefficients. If a value is supplied, the shrinkage prior is used;
otherwise the standard prior is used.Examples:
set_covariates(~ mean_income + distance * employment)
set_covariates(~ ., data = cov_data, n_coef = 5)
Use exponential distribution, parameterised using mean,
set_disp(mean = 1)
mean
is μξRandom rounding to base 3 (RR3) is a confidentialization method used by some statistical agencies. It is applied to counts data. Each count x is rounded randomly as follows:
RR3 data models can be used with Poisson or binomial likelihoods. Let yi denote the observed value for the outcome, and yi* the true value. The likelihood with a RR3 data model is then
The data that we supply to TMB is a a filtered and aggregated version
of the data that the user provides through the data
argument.
In the filtering stage, we remove any rows where (i) the offset is 0 or NA, or (ii) the outcome variable is NA.
In the aggregation stage, we identify any rows in the data that
duplicated combinations of classification variables. For instance, if
the classification variables are age
and sex
,
and we have two rows where age
is "20-24"
and
sex is "Female"
, then these rows would count as duplicated
combinations. We aggregate offset and outcome variables across these
duplicates. With Poisson and binomial models, the aggregation formula
for outcomes is and the aggregation formula for exposure/size is where
D is the number of times a
particular combination is duplicated. With normal models, the
aggregation formula for outcomes is and the aggregation formuala for
weights is
Select variables to be used in inner model. By default, these are the age, sex, and time variables in the model. All remaining variables are ‘outer’ variables.
Aggregate the data using the classification formed by the inner variables. (See Section @ref(sec:filter-ag) on aggregation procedures.) Remove all terms not involving ‘inner’ variables, other than the intercept term, from the model. Set dispersion to 0. Fit the resulting model.
Let μ̂iin be point estimates for the linear predictor μi obtained from the inner model.
Poisson model
Aggregate the data using the classification formed by the outer variables. Remove all terms involving the ‘inner’ variables, plus the intercept, from the model. Set dispersion to 0. Set exposure to wiout = μ̂inwi. Fit the model.
Binomial model
Fit the original model, but set dispersion to 0, and for all terms from the ‘inner’ model, use Known priors using point estimates from the inner model.
Normal model
Aggregate the data using the classification formed by the outer variables. Remove all terms involving the ‘inner’ variables, plus the intercept, from the model. Set the outcome variable to to $y_i^{} = y_i - ^{}. Fit the model.
Concatenate posterior distributions for the inner terms from the inner model to posterior distributions for the outer terms from the outer model.
If the original model includes a dispersion term, then estimate dispersion. Let μ̂icomb be point estimates for the linear predictor obtained from the concatenated estimates.
Poisson model
Use the original disaggregated data, or, if the original data contains more then 10,000 rows, select 10,000 rows at random from the original data. Remove all terms from the original model except for the intercept. Set exposure to wiout = μ̂combwi.
Binomial model
Fit the the original model, but with all terms except the intercept having Known priors, where the values are obtained from point estimates from the concatenated estimates.
Normal model
Use the original disaggregated data, or, if the original data contains more then 10,000 rows, select 10,000 rows at random from the original data. Remove all terms from the original model except for the intercept. Set the outcome to yiout = yi − μ̂comb.
Running TMB yields a set of means m, and a precision matrix Q−1, which together define the approximate joint posterior distribution of
We use $\tilde{\pmb{\theta}}$ to denote a vector containing all these quantities.
We perform a Cholesky decomposition of Q−1, to obtain R such that We store R as part of the model object.
We draw generate values for $\tilde{\pmb{\theta}}$ by generating a vector of standard normal variates z, back-solving the equation and setting
Next we convert any transformed hyper-parameters back to the original units, and insert values for β(m) for terms that have Known priors. We denote the resulting vector θ.
Finally we draw from the distribution of γ ∣ y, θ using the methods described in Sections @ref(sec:pois)-@ref(sec:norm).
To generate one set of simulated values, we start with values for exposure, trials, or weights, w, and possibly covariates Z, then go through the following steps:
which is equivalent to
If the overall model includes a data model for the outcome, then a further set of draws is made, deriving values for the observed outcomes, given values for the true outcomes.
replicate_data(x, condition_on = c("fitted", "expected"), n = 20)
Quantity | Definition |
---|---|
i | Index for cell, i = 1, ⋯, n. |
yi | Value for outcome variable. |
wi | Exposure, number of trials, or weight. |
γi | Super-population rate, probability, or mean. |
μi | Cell-specific mean. |
ξ | Dispersion parameter. |
g() | Log, logit, or identity function. |
m | Index for intercept, main effect, or interaction. m = 0, ⋯, M. |
j | Index for element of a main effect or interaction. |
u | Index for combination of ‘by’ variables for an interaction. u = 1, ⋯Um. UmVm = Jm |
v | Index for the ‘along’ dimension of an interaction. v = 1, ⋯Vm. UmVm = Jm |
β(0) | Intercept. |
β(m) | Main effect or interaction. m = 1, ⋯, M. |
βj(m) | jth element of β(m). j = 1, ⋯, Jm. |
X(m) | Matrix mapping β(m) to y. |
Z | Matrix of covariates. |
ζ | Parameter vector for covariates Z(m). |
A0 | Scale parameter in prior for intercept β(0) or initial value. |
τm | Standard deviation parameter for main effect or interaction. |
Aτ(m) | Scale parameter in prior for τm. |
α(m) | Parameter vector for P-spline and SVD priors. |
αk(m) | kth element of α(m). k = 1, ⋯, Km. |
V(m) | Covariance matrix for multivariate normal prior. |
hj(m) | Linear covariate |
η(m) | Parameter specific to main effect or interaction β(m). |
ηu(m) | Parameter specific to uth combination of ‘by’ variables in interaction β(m). |
Aη(m) | Standard deviation in normal prior for ηm. |
ωm | Standard deviation of parameter ηc in multivariate priors. |
ϕm | Correlation coefficient in AR1 densities. |
a0m, a1m | Minimum and maximum values for ϕm. |
B(m) | B-spline matrix in P-spline prior. |
bk(m) | B-spline. k = 1, ⋯, Km. |
F(m) | Matrix in SVD prior. |
g(m) | Offset in SVD prior. |
βtrend | Trend effect. |
βcyc | Cyclical effect. |
βseas | Seasonal effect. |
φ | Global shrinkage parameter in shrinkage prior. |
Aφ | Scale term in prior for φ. |
ϑp | Local shrinkage parameter in shrinkage prior. |
p0 | Expected number of non-zero coefficients in ζ. |
σ̂ | Empirical scale estimate in prior for φ. |
π | Vector of hyper-parameters |
Let A be a matrix of age-specific estimates from an international database, transformed to take values in the range (−∞, ∞). Each column of A represents one set of age-specific estimates, such as log mortality rates in Japan in 2010, or logit labour participation rates in Germany in 1980.
Let U, D, V be the matrices from a singular value decomposition of A, where we have retained the first K components. Then
Let mk and sk be the mean and sample standard deviation of the elements of the kth row of V, with m = (m1, ⋯, mk)⊤ and s = (s1, ⋯, sk)⊤. Then is a standardized version of V.
We can rewrite @ref(eq:svd1) as where F = UDdiag(s) and g = UDm.
Let $\tilde{\pmb{v}}_l$ be a randomly-selected column from $\tilde{\pmb{V}}$. From the construction of $\tilde{\pmb{V}}$ we have E[ṽkl] = 0 and var[ṽkl] = 1. If z is a vector of standard normal variables, then should look approximately like a randomly-selected column from the original data matrix A.