-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
105 lines (82 loc) · 2.62 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
knitr::opts_knit$set(global.par = TRUE)
set.seed(1)
```
# greta.gp: Gaussian Process in `greta`
<!-- badges: start -->
[![R-CMD-check](https://github.com/greta-dev/greta.gp/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/greta-dev/greta.gp/actions/workflows/R-CMD-check.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/greta.gp)](https://CRAN.R-project.org/package=greta.gp)
[![codecov](https://codecov.io/gh/greta-dev/greta.gp/graph/badge.svg?token=sDr01k2p2r)](https://codecov.io/gh/greta-dev/greta.gp)
<!-- badges: end -->
`greta.gp` extends greta to let you define Gaussian processes as part of your model. It provides a syntax to create and combine GP kernels, and use them to define either full rank or sparse Gaussian processes.
## Installation
You can install the current release version of `greta.gp` from CRAN:
``` r
install.packages("greta.gp")
```
Or install the development version of `greta.dynamics` from [r-universe](http://greta-dev.r-universe.dev/):
```r
install.packages("greta.gp", repos = "https://greta-dev.r-universe.dev")
```
You can also install the development version of `greta.gp` via GitHub:
``` r
remotes::install_github("greta-dev/greta.gp")
```
## Example usage
```{r simulate, message = FALSE}
# simulate data
x <- runif(20, 0, 10)
y <- sin(x) + rnorm(20, 0, 0.5)
x_plot <- seq(-1, 11, length.out = 200)
```
```{r model, message = FALSE}
library(greta)
library(greta.gp)
# hyperparameters
rbf_var <- lognormal(0, 1)
rbf_len <- lognormal(0, 1)
obs_sd <- lognormal(0, 1)
# kernel & GP
kernel <- rbf(rbf_len, rbf_var) + bias(1)
f <- gp(x, kernel)
# likelihood
distribution(y) <- normal(f, obs_sd)
# prediction
f_plot <- project(f, x_plot)
```
```{r fit, message = FALSE}
# fit the model by Hamiltonian Monte Carlo
m <- model(f_plot)
draws <- mcmc(m, n_samples = 250)
```
```{r plotting, fig.width = 10, fig.height = 6, dpi = 200}
# plot 200 posterior samples
# plot 200 posterior samples
plot(
y ~ x,
pch = 16,
col = grey(0.4),
xlim = c(0, 10),
ylim = c(-2.5, 2.5),
las = 1,
fg = grey(0.7),
)
for (i in 1:200) {
lines(draws[[1]][i, ] ~ x_plot,
lwd = 2,
col = rgb(0.7, 0.1, 0.4, 0.1)
)
}
```
## Code of Conduct
Please note that the greta.gp project is released with a [Contributor Code of Conduct](https://greta-dev.github.io/greta.gp/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.