Title: | Generalised Additive Models in greta using mgcv |
---|---|
Description: | A module for greta that lets you use mgcv's smoother functions and formula syntax to define smooth terms for use in a greta model. You can then define your own likelihood to complete the model, and fit it by MCMC. |
Authors: | Nick Golding [aut] , Nicholas Tierney [aut] , David L Miller [aut, cre] |
Maintainer: | David L Miller <[email protected]> |
License: | GPL(>=2) |
Version: | 0.1.0 |
Built: | 2024-10-08 06:03:25 UTC |
Source: | https://github.com/greta-dev/greta.gam |
Evaluate a set of smooths at new data locations
evaluate_smooths(x, newdata)
evaluate_smooths(x, newdata)
x |
a greta array created with greta.gam::smooths |
newdata |
a dataframe with the same column names and datatypes as that used to create x, with data at which to evauate the smooths |
greta array
Nick Golding
greta.gam is a module for greta that lets you use mgcv's smoother functions and formula syntax to define smooth terms for use in a greta model. You can then define your own likelihood to complete the model, and fit it by MCMC.
Takes a GAM defined by formula
and returns the corresponding greta
model via the power of jagam
. Response variable is generated from dummy data and not used.
jagam2greta(formula, data, newdata, sp = NULL, knots = NULL, tol = 0)
jagam2greta(formula, data, newdata, sp = NULL, knots = NULL, tol = 0)
formula |
a GAM formula representing the smooth terms, as in
|
data |
a data frame or list containing the covariates required by the formula. These covariates cannot be greta arrays. |
newdata |
new dataset |
sp |
an optional vector of smoothing parameters, two per smooth term in
the model, in the same order as the formula. If |
knots |
an optional list containing user specified knot values to be
used for basis construction, as in |
tol |
a non-negative scalar numerical tolerance parameter. You can try increasing this if the model has numerical stability issues |
a list
with the following elements: betas
a greta array for the coefficients to be estimated (with appropriate priors applied), X
design matrix for this model, X_pred
prediction matrix.
smooths
translates the right hand side of a mgcv GAM
formula into a corresponding Bayesian representation of smooth terms. This
formula may include multiple combined smooths of different types, as well
as fixed effect terms and intercepts. The resulting greta array
representing the combined smooth can then be used in a greta model.
smooths(formula, data = list(), knots = NULL, sp = NULL, tol = 0)
smooths(formula, data = list(), knots = NULL, sp = NULL, tol = 0)
formula |
a GAM formula representing the smooth terms, as in
|
data |
a data frame or list containing the covariates required by the formula. These covariates cannot be greta arrays. |
knots |
an optional list containing user specified knot values to be
used for basis construction, as in |
sp |
an optional vector of smoothing parameters, two per smooth term in
the model, in the same order as the formula. If |
tol |
a non-negative scalar numerical tolerance parameter. You can try increasing this if the model has numerical stability issues |
Only the right hand side of formula
will be used to define
the smooth terms. The user must complete the gam model by specifying the
link and likelihood term in greta. A warning will be issued if the formula
has a left hand side.
Note that by default, GAM formulas add an intercept term. If you have
already specified an intercept for your greta model, you can remove the
intercept from the smooth term by adding -1
as a term in your
formula.
Like mgcv::jagam
, smooths
translates a
mgcv GAM formula into a Bayesian representation of the smooth terms, using
the GAM smoothing penalty matrix as a multivariate normal prior to penalise
model fitting. Unlike gam
, smooths
does not perform the
integration required to penalise model fitting. The model must be fitted by
MCMC to carry out this integration - it does not make sense to do maximum
likelihood optimisation on a greta model that uses smooths
.
## Not run: n <- 30 x <- runif(n, 0, 10) f <- function(x) { sin(x * 2) + 1.6 * (x < 3) - 1.4 * (x > 7) } y <- f(x) + rnorm(n, 0, 0.3) x_plot <- seq(0, 10, length.out = 200) z <- smooths(~ s(x), data = data.frame(x = x)) distribution(y) <- normal(z, 0.3) z_pred <- evaluate_smooths(z, newdata = data.frame(x = x_plot)) # build model m <- model(z_pred) draws <- mcmc(m, n_samples = 100) plot(x, y, pch = 19, cex = 0.4, col = "red") apply(draws[[1]], 1, lines, x = x_plot, col = "blue") points(x, y, pch = 19, cex = 0.4, col = "red") ## End(Not run)
## Not run: n <- 30 x <- runif(n, 0, 10) f <- function(x) { sin(x * 2) + 1.6 * (x < 3) - 1.4 * (x > 7) } y <- f(x) + rnorm(n, 0, 0.3) x_plot <- seq(0, 10, length.out = 200) z <- smooths(~ s(x), data = data.frame(x = x)) distribution(y) <- normal(z, 0.3) z_pred <- evaluate_smooths(z, newdata = data.frame(x = x_plot)) # build model m <- model(z_pred) draws <- mcmc(m, n_samples = 100) plot(x, y, pch = 19, cex = 0.4, col = "red") apply(draws[[1]], 1, lines, x = x_plot, col = "blue") points(x, y, pch = 19, cex = 0.4, col = "red") ## End(Not run)