Skip to content

Commit

Permalink
Merge pull request #20 from greta-dev/spring-clean-i18
Browse files Browse the repository at this point in the history
Spring clean i18
  • Loading branch information
njtierney authored Nov 29, 2024
2 parents 8c72b09 + ce2c580 commit 1e4069f
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ figure/
^README\.Rmd$
^\.github$
^codecov\.yml$
^_pkgdown\.yml$
^docs$
^pkgdown$
49 changes: 49 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
release:
types: [published]
workflow_dispatch:

name: pkgdown.yaml

permissions: read-all

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/[email protected]
with:
clean: false
branch: gh-pages
folder: docs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
.Ruserdata
docs
38 changes: 29 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ Type: Package
Title: Generalised Additive Models in greta using mgcv
Version: 0.1.0
Authors@R: c(
person("Nick", "Golding", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-8916-5570")),
person("Nicholas", "Tierney", , "[email protected]", role = "aut",
comment = c(ORCID = "https://orcid.org/0000-0003-1460-8722")),
person("David L", "Miller", role = c("aut", "cre"),
email = "[email protected]")
)
person(
given = "Nick",
family = "Golding",
email = "[email protected]",
role = "aut",
comment = c(ORCID = "0000-0001-8916-5570")
),
person(
given = "Nicholas",
family = "Tierney",
email = "[email protected]",
role = c("aut"),
comment = c(ORCID = "0000-0003-1460-8722")
),
person(
given = "David L",
family = "Miller",
role = c("aut", "cre"),
email = "[email protected]"
)
)
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
Expand All @@ -20,9 +34,15 @@ Imports:
mgcv,
rlang,
stats
Depends: greta
Depends:
greta (>= 0.5.0),
R (>= 4.1.0)
Suggests:
testthat (>= 3.0.0)
Encoding: UTF-8
RoxygenNote: 7.3.1
LazyData: true
RoxygenNote: 7.3.2
Config/testthat/edition: 3
URL: https://github.com/greta-dev/greta.gam, https://greta-dev.github.io/greta.gam/
BugReports: https://github.com/greta-dev/greta.gam/issues
Roxygen: list(markdown = TRUE)
26 changes: 19 additions & 7 deletions R/jagam2greta.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#' Turn a jagam model definition into a greta model definition
#'
#' Takes a GAM defined by \code{formula} and returns the corresponding \code{greta} model via the power of \code{jagam}. Response variable is generated from dummy data and not used.
#' 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.
#'
#' @inheritParams smooths
#' @param newdata new dataset
#'
#' @return a \code{list} with the following elements: \code{betas} a greta array for the coefficients to be estimated (with appropriate priors applied), \code{X} design matrix for this model, \code{X_pred} prediction matrix.
jagam2greta <- function(formula, data, newdata, sp = NULL, knots = NULL, tol = 0) {
#' @return 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.
jagam2greta <- function(formula,
data,
newdata,
sp = NULL,
knots = NULL,
tol = 0) {
# make a dummy response to get jagam to work
formula <- stats::update(formula, dummy ~ .)

Expand All @@ -31,9 +40,8 @@ jagam2greta <- function(formula, data, newdata, sp = NULL, knots = NULL, tol = 0
X <- jags_stuff$jags.data$X

# do something smart with smoothing parameters
if (is.null(sp)) {
sp <- gamma(0.05, 1 / 0.005, dim = 2 * length(jags_stuff$pregam$smooth))
}
n_smooth_params <- length(jags_stuff$pregam$smooth)
sp <- sp %||% gamma(0.05, 1 / 0.005, dim = 2 * n_smooth_params)

# get all the penalties, form them (because of proper priorness), then
# turn them into vcov matrices via solve()
Expand Down Expand Up @@ -74,5 +82,9 @@ jagam2greta <- function(formula, data, newdata, sp = NULL, knots = NULL, tol = 0
# put all the betas together
betas <- c(int, betas)

return(list(betas = betas, X = X, smooth_list = jags_stuff$pregam$smooth))
return(list(
betas = betas,
X = X,
smooth_list = jags_stuff$pregam$smooth
))
}
8 changes: 4 additions & 4 deletions R/package.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' greta.gam: generalised additive models in greta using mgcv
#' @name greta.gam
#'
#' @description 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.
#' @description 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.
#'
#' @import greta
#' @import mgcv
Expand Down
27 changes: 16 additions & 11 deletions R/smooths.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@
#'
#' @name smooths
#'
#' @description \code{smooths} translates the right hand side of a mgcv GAM
#' @description `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.
#'
#' @param formula a GAM formula representing the smooth terms, as in
#' \code{\link[mgcv:gam]{mgcv::gam}}. Only the right hand side fo the formula
#' [mgcv::gam()]. Only the right hand side fo the formula
#' will be used.
#'
#' @param data a data frame or list containing the covariates required by the
#' formula. These covariates cannot be greta arrays.
#'
#' @param knots an optional list containing user specified knot values to be
#' used for basis construction, as in \code{\link[mgcv:gam]{mgcv::gam}} These
#' used for basis construction, as in [mgcv::gam()] These
#' knots cannot be greta arrays.
#'
#' @param sp an optional vector of smoothing parameters, two per smooth term in
#' the model, in the same order as the formula. If \code{sp = NULL}, all
#' the model, in the same order as the formula. If `sp = NULL`, all
#' smoothing parameters will be learned, otherwise all smoothing parameters
#' must be specified by the user. The smoothing parameters may either be a
#' numeric vector or a greta array (which could be a variable).
#'
#' @param tol a non-negative scalar numerical tolerance parameter. You can try
#' increasing this if the model has numerical stability issues
#'
#' @details Only the right hand side of \code{formula} will be used to define
#' @details 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 \code{-1} as a term in your
#' intercept from the smooth term by adding `-1` as a term in your
#' formula.
#'
#' Like \code{\link[mgcv:jagam]{mgcv::jagam}}, \code{smooths} translates a
#' 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 \code{gam}, \code{smooths} does not perform the
#' 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 \code{smooths}.
#' likelihood optimisation on a greta model that uses `smooths`.
#'
#'
#' @examples
Expand Down Expand Up @@ -72,11 +72,16 @@
#' points(x, y, pch = 19, cex = 0.4, col = "red")
#' }
#' @export
smooths <- function(formula, data = list(), knots = NULL, sp = NULL, tol = 0) {
smooths <- function(formula,
data = list(),
knots = NULL,
sp = NULL,
tol = 0) {
warn_if_formula_has_lhs(formula)

# get all the MGCV objects for Bayesian version, converted to greta arrays
jg <- jagam2greta(formula,
jg <- jagam2greta(
formula,
data = data,
tol = tol
)
Expand Down
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
url: https://greta-dev.github.io/greta.gam/
template:
bootstrap: 5

27 changes: 23 additions & 4 deletions man/greta.gam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions man/jagam2greta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions man/smooths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e4069f

Please sign in to comment.