Skip to contents

bart_star() accepts known offsets (exposures) ei>0e_i > 0 in the BART-STAR model for counts, for example survey effort or detection proportion in point-count data. The CDF-based transformations are estimated with the offsets through the offsets argument of g_cdf().

Model

Original STAR model with a BART mean:

yi=⌊yi*⌋,zi=g(yi*),zi=μ(xi)+εi,εi∼N(0,σ2)y_i = \lfloor y_i^\ast \rfloor, \qquad z_i = g(y_i^\ast), \qquad z_i = \mu(x_i) + \varepsilon_i, \qquad \varepsilon_i \sim N(0, \sigma^2)

The observation yi=jy_i = j corresponds to yi*∈[aj,aj+1)y_i^\ast \in [a_j, a_{j+1}), with aj=ja_j = j, a0=−∞a_0 = -\infty and aymax+1=∞a_{y_{\max}+1} = \infty.

With offsets, gg is applied to y*/ey^\ast/e and nothing else changes:

yi=⌊yi*⌋,zi=g(yi*/ei),zi=μ(xi)+εi,εi∼N(0,σ2)y_i = \lfloor y_i^\ast \rfloor, \qquad z_i = g(y_i^\ast/e_i), \qquad z_i = \mu(x_i) + \varepsilon_i, \qquad \varepsilon_i \sim N(0, \sigma^2)

Since ei>0e_i > 0 and gg is increasing,

yi=j⇔yi*ei∈[ajei,aj+1ei)⇔g(ajei)≤zi<g(aj+1ei)y_i = j \iff \frac{y_i^\ast}{e_i} \in [\frac{a_j}{e_i}, \frac{a_{j+1}}{e_i}) \iff g(\frac{a_j}{e_i}) \le z_i < g(\frac{a_{j+1}}{e_i})

with ±∞/ei=±∞\pm\infty/e_i = \pm\infty. The cut points [j,j+1)[j, j+1) become [j/ei,(j+1)/ei)[j/e_i, (j+1)/e_i); the upper endpoint is (j+1)/ei(j+1)/e_i, not j/ei+1j/e_i + 1. The likelihood is

ℓ(μ,σ)=∑i=1nlog⁡[Φ(g(ayi+1/ei)−μiσ)−Φ(g(ayi/ei)−μiσ)]\ell(\mu, \sigma) = \sum_{i=1}^n \log[\Phi(\frac{g(a_{y_i+1}/e_i) - \mu_i}{\sigma}) - \Phi(\frac{g(a_{y_i}/e_i) - \mu_i}{\sigma})]

For g=logg = \log this is the usual log-offset: log⁡yi*=log⁡ei+μ(xi)+εi\log y_i^\ast = \log e_i + \mu(x_i) + \varepsilon_i.

In the code, a_y = a_j(y, y_max)/offsets, a_yp1 = a_j(y + 1, y_max)/offsets, z_lower = g(a_y) and z_upper = g(a_yp1). These are used in the zz sampling step (rtruncnormRcpp), in the λ\lambda update for 'box-cox', and in the pointwise log-likelihood, so also in the WAIC. The BART step (dbarts) is unchanged.

Transformation

Box-Cox family ('identity', 'log', 'sqrt', 'box-cox'). g=gλg = g_\lambda is applied to y*/ey^\ast/e. For 'box-cox', λ\lambda is sampled as in the original; its likelihood uses the intervals [ayi/ei,ayi+1/ei)[a_{y_i}/e_i, a_{y_i+1}/e_i).

CDF-based ('np', 'pois', 'neg-bin'). As in the original, g=Φ−1∘Fg = \Phi^{-1}\circ F at a set of knots, infinite values are dropped, and the knots are joined by the same monotone spline (splinefun(..., method = 'monoH.FC')). With offsets, gg is applied to y*/ey^\ast/e, so FF must describe y*/ey^\ast/e. bart_star() calls g_cdf(y, transformation, offsets).

  • 'np': FF is the empirical CDF of the observed rates y/ey/e, evaluated just below each knot tt (the unique nonzero yi/eiy_i/e_i):

Φ(g(t))=1n+1∑i=1n𝟙(yiei<t)\Phi(g(t)) = \frac{1}{n+1}\sum_{i=1}^n \mathbb{1}\left(\frac{y_i}{e_i} < t\right)

It is computed with one call to ecdf(), as n/(n+1)*(1 - ecdf(-y/offsets)(-t)). With e≡1e \equiv 1, yiy_i and tt are integers, so 𝟙(yi<t)=𝟙(yi≤t−1)\mathbb{1}(y_i < t) = \mathbb{1}(y_i \le t - 1) and gg is the original qnorm(F_y(t0 - 1)).

  • 'pois', 'neg-bin': the offsets enter through the standard count models with exposure,

yi∼Pois(eiμ)oryi∼NB(mean eiμ,size r)y_i \sim \text{Pois}(e_i\mu) \quad \text{or} \quad y_i \sim \text{NB}(\text{mean } e_i\mu, \text{size } r)

In the STAR model zi=g(yi*/ei)z_i = g(y_i^\ast/e_i), and the distribution of ziz_i does not depend on eie_i, so y*/ey^\ast/e is the latent count at unit exposure (e=1e = 1). The transformation is therefore built, as in the original, from the CDF FF of the count at unit exposure, Pois(μ\mu) or NB(μ,r\mu, r):

g(t)=Φ−1(F(t−1))g(t) = \Phi^{-1}(F(t - 1))

The offsets change only the estimates of μ\mu and rr, which solve the moment equations of the exposure model,

∑iyi=μ∑iei,s2=μe‾+μ2re2¯\sum_i y_i = \mu\sum_i e_i, \qquad s^2 = \mu\bar e + \frac{\mu^2}{r}\overline{e^2}

where the second is Var(yi)=eiμ+ei2μ2/r\text{Var}(y_i) = e_i\mu + e_i^2\mu^2/r averaged over ii, s2s^2 is the sample variance of yi−eiμ̂y_i - e_i\hat\mu, e‾=mean(e)\bar e = \text{mean}(e) and e2¯=mean(e2)\overline{e^2} = \text{mean}(e^2). This gives

μ̂=∑iyi∑iei,r̂=μ̂2e2¯s2−μ̂e‾\hat\mu = \frac{\sum_i y_i}{\sum_i e_i}, \qquad \hat r = \frac{\hat\mu^2\overline{e^2}}{s^2 - \hat\mu\bar e}

μ̂\hat\mu is also the Poisson MLE. With e≡1e \equiv 1, these are the original estimates.

'ispline' is not available with offsets. The function stops if any offsets or offsets_test differs from 1; otherwise it calls the original bart_star_ispline().

Predictions, conditional expectation, scores

Posterior predictive draws. At each saved iteration, z̃i∼N(μi,σ2)\tilde z_i \sim N(\mu_i, \sigma^2) and

ỹi=⌊g−1(z̃i)⋅ei⌋\tilde y_i = \lfloor g^{-1}(\tilde z_i) \cdot e_i \rfloor

computed as round_floor(g_inv(z_tilde) * offsets, y_max). Test draws (post.pred.test) use offsets_test. As in the original, g−1g^{-1} is exact for the Box-Cox family and a grid approximation (g_inv_approx()) for 'np', 'pois' and 'neg-bin'.

Conditional expectation.

𝔼(yi∣xi,ei)=∑j=0Jij[Φ(g(aj+1/ei)−μ(xi)σ)−Φ(g(aj/ei)−μ(xi)σ)]\mathbb{E}(y_i \mid x_i, e_i) = \sum_{j=0}^{J_i} j[\Phi(\frac{g(a_{j+1}/e_i) - \mu(x_i)}{\sigma}) - \Phi(\frac{g(a_j/e_i) - \mu(x_i)}{\sigma})]

Here JiJ_i is the 0.9999 quantile of yiy_i, capped at ymaxy_{\max}. Since aymax+1=∞a_{y_{\max}+1} = \infty, the term j=ymaxj = y_{\max} is ymaxP(yi=ymax)y_{\max} P(y_i = y_{\max}). expectation_gRcpp() sums up to Jmax −1- 1, so the code uses Jmax =Ji+1= J_i + 1 and the cut points a_j(., y_max).

Without offsets, the original single call to expectation_gRcpp() is used. With offsets, the cut points g(aj/ei)g(a_j/e_i) differ by observation, while expectation_gRcpp() takes one vector of cut points for all observations. To avoid a loop over observations, the same sum is computed in R for all observations at once, over one long vector of the pairs (i,j)(i, j), j=Jmin⁡,i,…,Jij = J_{\min,i}, \dots, J_i, where Jmin⁡,iJ_{\min,i} is the 0.0001 quantile of yiy_i. This is used for every transformation. The checks of this design are in README_check.md.

Log-predictive scores for the test data use the intervals [a_j(y_test, y_max)/offsets_test, a_j(y_test + 1, y_max)/offsets_test), so a test value at ymaxy_{\max} has the cell [ymax/e,∞)[y_{\max}/e, \infty), as in the likelihood.

Initialization

The original rules, applied to the scale y/ey/e:

Quantity Code
Initial z_star g((y + abs(rnorm(n)))/offsets)
sigest (when NULL) sd(g((y + 1)/offsets))
sigest, 'box-cox' the original pilot MCMC if all offsets equal 1; otherwise the rule above at the initial λ\lambda, because the pilot MCMC does not take offsets
Grid for g_inv_approx() 250 equally spaced points on [0,min⁡(2max⁡(y/e),ymax/min⁡(e))][0, \min(2\max(y/e), y_{\max}/\min(e))], plus 250 quantiles of the unique (y+1)/e(y+1)/e over y<ymaxy < y_{\max}

Usage

library(countSTAR)

fit = bart_star(y = y, X = X, offsets = e,         # e: known exposures
                X_test = X, offsets_test = 1,
                transformation = 'np', save_y_hat = TRUE)

fit$post.pred                          # predictive draws of y at the observed exposures e
fit$fitted.values                      # E(y | x, e) at the observed exposures e
colMeans(fit$post.fitted.values.test)  # E(y | x, e = 1): expected count per unit exposure
  • offsets: n x 1 vector or scalar, e>0e > 0; the default is 1. offsets_test: n_test x 1 vector or scalar; the default is 1. Scalars are recycled, and wrong lengths or values ≤0\le 0 stop with an error.
  • post.pred, post.log.like.point and the fitted values are at offsets. post.pred.test, post.fitted.values.test and post.log.pred.test are at offsets_test.
  • post.mu.test and post.sigma are on the scale of z=g(y*/e)z = g(y^\ast/e). With 'np' they do not depend on the units of ee. With 'log', using cece instead of ee is the same model with μ\mu shifted by −log⁡c-\log c. With 'pois' and 'neg-bin', gg is the CDF transformation at e=1e = 1, so the units of ee define the reference exposure.