library(greta.gp)
#> Loading required package: greta
#>
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#>
#> binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#>
#> %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#> eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#> tapply
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.
# simulate data
<- runif(20, 0, 10)
x <- sin(x) + rnorm(20, 0, 0.5)
y <- seq(-1, 11, length.out = 200) x_plot
library(greta)
library(greta.gp)
# hyperparameters
<- lognormal(0, 1)
rbf_var <- lognormal(0, 1)
rbf_len <- lognormal(0, 1)
obs_sd
# kernel & GP
<- rbf(rbf_len, rbf_var) + bias(1)
kernel <- gp(x, kernel)
f
# likelihood
distribution(y) <- normal(f, obs_sd)
# prediction
<- project(f, x_plot) f_plot
# fit the model by Hamiltonian Monte Carlo
<- model(f_plot)
m <- mcmc(m) draws
# plot 200 posterior samples
plot(
~ x,
y 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(
1]][i, ] ~ x_plot,
draws[[lwd = 2,
col = rgb(0.7, 0.1, 0.4, 0.1)
) }