# NOT RUN {
# Toy example (runs in less than 5 seconds) -----------------------------------
# This example uses a small number of MCMC iterations in order to run quickly
# More iterations are required to get appropriate fits
# Function defaults are recommended (see additional example below)
f <- function(x) {
if (x <= 0.4) return(-1)
if (x >= 0.6) return(1)
if (x > 0.4 & x < 0.6) return(10*(x-0.5))
}
x <- seq(0.05, 0.95, length = 7)
y <- sapply(x, f)
x_new <- seq(0, 1, length = 100)
# Fit model and calculate EI
fit <- fit_one_layer(x, y, nmcmc = 500)
fit <- trim(fit, 400)
fit <- predict(fit, x_new, lite = TRUE, store_all = TRUE)
ei <- EI(fit)
# }
# NOT RUN {
# One Layer and EI ------------------------------------------------------------
f <- function(x) {
sin(5 * pi * x) / (2 * x) + (x - 1) ^ 4
}
# Training data
x <- seq(0.5, 2, length = 30)
y <- f(x) + rnorm(30, 0, 0.01)
# Testing data
xx <- seq(0.5, 2, length = 100)
yy <- f(xx)
# Standardize inputs and outputs
xx <- (xx - min(x)) / (max(x) - min(x))
x <- (x - min(x)) / (max(x) - min(x))
yy <- (yy - mean(y)) / sd(y)
y <- (y - mean(y)) / sd(y)
# Conduct MCMC
fit <- fit_one_layer(x, y, nmcmc = 10000)
plot(fit) # investigate trace plots
fit <- trim(fit, 8000, 2)
# Predict and calculate EI
fit <- predict(fit, xx, lite = TRUE, store_all = TRUE)
ei <- EI(fit)
# Visualize Fit
plot(fit)
par(new = TRUE) # overlay EI
plot(xx, ei$value, type = 'l', lty = 2, axes = FALSE, xlab = '', ylab = '')
# Select next design point
x_new <- xx[which.max(ei$value)]
# Evaluate fit
rmse(yy, fit$mean) # lower is better
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab