Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/greta-dev/greta.gam
a greta extension for generalised additive modelling using mgcv
https://github.com/greta-dev/greta.gam
Last synced: 20 days ago
JSON representation
a greta extension for generalised additive modelling using mgcv
- Host: GitHub
- URL: https://github.com/greta-dev/greta.gam
- Owner: greta-dev
- Created: 2017-07-13T02:07:35.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-08-09T11:49:05.000Z (3 months ago)
- Last Synced: 2024-08-09T13:08:26.309Z (3 months ago)
- Language: R
- Homepage:
- Size: 368 KB
- Stars: 7
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# greta.gam: Generalised additive models in greta using mgcv
[![R-CMD-check](https://github.com/greta-dev/greta.gam/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/greta-dev/greta.gam/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/greta-dev/greta.gam/branch/main/graph/badge.svg)](https://app.codecov.io/gh/greta-dev/greta.gam?branch=main)greta.gam lets you use [mgcv](https://CRAN.R-project.org/package=mgcv)'s smoother functions and formula syntax to define smooth terms for use in a [greta](https://greta-dev.github.io/greta) model.
You can then define your own likelihood to complete the model, and fit it by MCMC.This is work in progress!
## Example
Here's a simple example adapted from the `mgcv` `?gam` help file:
In `mgcv`:
```{r mgcv-generate-and-fit}
library(mgcv)
set.seed(2)# simulate some data...
dat <- gamSim(1, n = 400, dist = "normal", scale = 0.3)# fit a model using gam()
b <- gam(y ~ s(x2), data = dat)
```Now fitting the same model in `greta`:
```{r greta-fit}
library(greta.gam)
set.seed(2024 - 02 - 09)
# setup the linear predictor for the smooth
z <- smooths(~ s(x2), data = dat)# set the distribution of the response
distribution(dat$y) <- normal(z, 1)# make some prediction data
pred_dat <- data.frame(x2 = seq(0, 1, length.out = 100))# z_pred stores the predictions
z_pred <- evaluate_smooths(z, newdata = pred_dat)# build model
m <- model(z_pred)# draw from the posterior
draws <- mcmc(m, n_samples = 200)# plot the mgcv fit
plot(b, scheme = 1, shift = coef(b)[1])# add in a line for each posterior sample
apply(draws[[1]], 1, lines, x = pred_dat$x2, col = "blue")# plot the data
points(dat$x2, dat$y, pch = 19, cex = 0.2)
```## Brief technical details
`greta.gam` uses a few tricks from the `jagam` (Wood, 2016) routine in `mgcv` to get things to work. Here are some brief details for those interested in the internal workings...
### Bayesian interpretation of the GAM
GAMs are models with Bayesian interpretations (even when fitted using "frequentist" methods). One can think of the smoother penalty matrix as a prior precision matrix in a Bayesian random effects model. Design matrices are constructed exactly as in the frequentist case. See [Miller (2021)](https://arxiv.org/abs/1902.01330) for more background on this.
### Penalty matrices
There is a slight difficulty in the Bayesian interpretation of the GAM in that, in their naïve form the priors are improper as the nullspace of the penalty (in the 1D case, usually the linear term). To get proper priors we can use one of the "tricks" employed in Marra & Wood (2011) -- that is to somehow penalise the parts of the penalty that lead to the improper prior. We take the option provided by `jagam` and create an additional penalty matrix for these terms (from an eigen-decomposition of the penalty matrix; see Marra & Wood, 2011).
### References
Marra, G and Wood, SN (2011) Practical variable selection for generalized additive models. Computational Statistics and Data Analysis, 55, 2372–2387.
Miller DL (2021). Bayesian views of generalized additive modelling. arXiv.
Wood, SN (2016) Just Another Gibbs Additive Modeler: Interfacing JAGS and mgcv. Journal of Statistical Software 75, no. 7