source('BuildEmulator/BuildEmulator.R') simulator = function(x) { sin(2*x) * exp(-0.5 * x^2) } df_simulator = data.frame(x = seq(-3, 3, 0.1), y = simulator(seq(-3, 3, 0.1))) # Normalize your input x to [-1, 1] df_simulator$x = 2*(df_simulator$x - min(df_simulator$x))/(max(df_simulator$x) - min(df_simulator$x)) - 1 ggplot(df_simulator, aes(x=x, y=y)) + geom_line() # Data frame with design set and noise tData = data.frame(x = seq(-2.5, 2.5, length.out = 15), Noise = rnorm(15, 0, 0.4), y = simulator(seq(-2.5, 2.5, length.out = 15))) # Normalize your input x to [-1, 1] tData$x = 2*(tData$x - (-3))/(3 - (-3)) - 1 print(tData) ggplot(tData, aes(x = x, y=y)) + geom_point() cands <- names(tData)[1] # Construct a mean function for GP emulator. myem.lm <- EMULATE.lm(Response=names(tData)[3], tData=tData, tcands=cands,tcanfacs=NULL,TryFouriers=FALSE, maxOrder=2, maxdf = 2) # Construct GP emulator. myem.gp <- EMULATE.gpstan(meanResponse=myem.lm, nugget=0.0001, tData=tData, additionalVariables=cands, FastVersion=TRUE) # Diagnostics: LOO tLOOs <- LOO.plot(StanEmulator = myem.gp, ParamNames=names(tData)[1]) # Predictions for a new data set. NewData <- data.frame(x=df_simulator$x) head(NewData) valid <- EMULATOR.gpstan(data.frame(x=df_simulator$x), Emulator = myem.gp) fit.valid <- cbind(valid$Expectation, valid$Expectation - 2*sqrt(valid$Variance), valid$Expectation + 2*sqrt(valid$Variance)) ValidPlot(fit.valid, matrix(df_simulator$x, ncol = 1), df_simulator$y, interval = c(), axis = 1, heading = "", xrange = c(-1, 1), ParamNames = c('x'))