-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
147 lines (103 loc) · 5.12 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{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
<!-- badges: start -->
[![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)
<!-- badges: end -->
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-stats.org/) model. You can then define your own likelihood to complete the model, and fit it by MCMC.
The design and architecture of the package was done by [Nick Golding](https://github.com/goldingn), and [David L Miller](https://github.com/dill).
# Installation
You can install the development version of `greta.gam` from the [R-universe](https://greta-dev.r-universe.dev/greta.gam) with:
```{r install, eval = FALSE}
install.packages("greta.gam", repos = c("https://greta-dev.r-universe.dev", "https://cloud.r-project.org"))
```
Or you can install from CRAN with:
```{r install-cran, eval = FALSE}
install.packages("greta.gam")
```
## Example
Here's a simple example adapted from the `mgcv` `?gam` help file. In `mgcv`:
```{r mgcv-generate-and-fit}
library(mgcv)
set.seed(2024 - 12 - 12)
# simulate some data...
dat <- gamSim(1, n = 400, dist = "normal", scale = 0.3)
head(dat)
# fit a model using gam()
mgcv_fit <- gam(y ~ s(x2), data = dat)
mgcv_fit
summary(mgcv_fit)
## show partial residuals
plot(mgcv_fit, scheme = 1, shift = coef(mgcv_fit)[1])
```
Now fitting the same model in `greta`. We first start by setting up the linear predictor for the smooth. That is, the right hand side of the formula:
```{r greta-fit}
library(greta.gam)
set.seed(2024 - 02 - 09)
# setup the linear predictor for the smooth
linear_predictor <- smooths(~ s(x2), data = dat)
linear_predictor
```
Now we specify the distribution of the response:
```{r greta-fit-add-distribution}
dist_sd <- cauchy(0, 1, truncation = c(0, Inf))
distribution(dat$y) <- normal(mean = linear_predictor, sd = dist_sd)
```
Now let's make some prediction data
```{r greta-fit-make-preds}
pred_dat <- data.frame(
x2 = seq(0, 1, length.out = 100)
)
head(pred_dat)
```
We run `evaluate_smooths` on the linear predicting with the new prediction data
```{r greta-fit-eval-preds}
linear_preds <- evaluate_smooths(linear_predictor, newdata = pred_dat)
linear_preds
```
Now we specify that as a model object and then fit with MCMC as we do with greta normally:
```{r greta-fit-mcmc}
# build model
m <- model(linear_preds)
m
# draw from the posterior
draws <- mcmc(m, n_samples = 200, verbose = FALSE)
class(draws)
# 4 chains
length(draws)
# 200 draws, 100 predictors
dim(draws[[1]])
# look at the top corner
draws[[1]][1:5, 1:5]
```
Now let's compare the `mgcv` model fit to the `greta.gam` fit:
```{r greta-fit-show-preds}
plot(mgcv_fit, scheme = 1, shift = coef(mgcv_fit)[1])
# add in a line for each posterior sample
apply(draws[[1]], 1, lines, x = pred_dat$x2,
col = adjustcolor("firebrick", alpha.f = 0.1))
# plot the data
points(dat$x2, dat$y, pch = 19, cex = 0.2)
```
The `mgcv` predictions are in the grey ribbon, and the `greta.gam` ones are in red - we can see that the greta predictions are within the range of the mgcv, which is good news!
## 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