R Under development (unstable) (2024-09-21 r87186 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(testthat) > Sys.setenv('OMP_THREAD_LIMIT'=2) > library(rlibkriging) Attaching package: 'rlibkriging' The following objects are masked from 'package:base': load, save > > ##library(rlibkriging, lib.loc="bindings/R/Rlibs") > ##library(testthat) > > library(RobustGaSP) ######### ## ## Robust Gaussian Stochastic Process, RobustGaSP Package ## Copyright (C) 2016-2024 Mengyang Gu, Jesus Palomo and James O. Berger ######### Attaching package: 'RobustGaSP' The following object is masked from 'package:rlibkriging': simulate The following object is masked from 'package:stats': simulate > > context("RobustGaSP-Nugget / Fit: 1D") > > f = function(x) 10*(1-1/2*(sin(12*x)/(1+x)+2*cos(7*x)*x^5+0.7)) > #plot(f) > n <- 5 > set.seed(123) > X <- as.matrix(runif(n)) > y = f(X) > #points(X,y) > k = RobustGaSP::rgasp(design=X,response=y,nugget.est = TRUE) The upper bounds of the range parameters are 184.9743 Inf The initial values of range parameters are 3.699485 Start of the optimization 1 : The number of iterations is 13 The value of the marginal posterior function is -6.32876 Optimized range parameters are 0.3498584 Optimized nugget parameter is 0.02539959 Convergence: TRUE The initial values of range parameters are 0.05223118 Start of the optimization 2 : The number of iterations is 24 The value of the marginal posterior function is -6.32876 Optimized range parameters are 0.3498584 Optimized nugget parameter is 0.02539959 Convergence: TRUE > #library(rlibkriging) > r <- NuggetKriging(y, X, + kernel="matern5_2", + regmodel = "constant", normalize = FALSE, + optim = "none", + objective = "LMP", + parameters=list( + theta=matrix(1/k@beta_hat),is_theta_estim=F, + nugget=k@nugget*k@sigma2_hat,is_nugget_estim=F, + sigma2=k@sigma2_hat,is_sigma2_estim=F)) > # m = as.list(r) > > # Check predict > > ntest <- 10 > Xtest <- seq(0,1,,ntest) > Ytest_rgasp <- predict(k,matrix(Xtest,ncol=1)) > Ytest_libK <- predict(r,Xtest) > > plot(f) > points(X,y) > lines(Xtest,Ytest_rgasp$mean,col='blue') > polygon(c(Xtest,rev(Xtest)), + c(Ytest_rgasp$mean+2*Ytest_rgasp$sd,rev(Ytest_rgasp$mean-2*Ytest_rgasp$sd)), + col=rgb(0,0,1,0.1), border=NA) > > lines(Xtest,Ytest_libK$mean,col='red') > polygon(c(Xtest,rev(Xtest)), + c(Ytest_libK$mean+2*Ytest_libK$stdev,rev(Ytest_libK$mean-2*Ytest_libK$stdev)), + col=rgb(1,0,0,0.1), border=NA) > > precision <- 1e-3 > test_that(desc=paste0("pred mean is the same that RobustGaSP one"), + expect_equal(predict(r,0.7)$mean[1],predict(k,matrix(0.7))$mean,tol = precision)) Test passed 🥳 > test_that(desc=paste0("pred sd is the same that RobustGaSP one"), + expect_equal(predict(r,0.7)$stdev[1],predict(k,matrix(0.7))$sd,tol = precision)) Test passed 🥳 > > > > context("RobustGaSP-Nugget / Fit: 2D (Branin)") > > f = function(X) apply(X,1,DiceKriging::branin) > n <- 15 > set.seed(1234) > X <- cbind(runif(n),runif(n)) > y = f(X) > model = NULL > r = NULL > library(RobustGaSP) > k = rgasp(design=X,response=y, nugget.est = T) The upper bounds of the range parameters are 58.61045 56.09173 Inf The initial values of range parameters are 1.172209 1.121835 Start of the optimization 1 : The number of iterations is 30 The value of the marginal posterior function is -61.1843 Optimized range parameters are 0.7974221 2.544159 Optimized nugget parameter is 1.716403e-13 Convergence: TRUE The initial values of range parameters are 0.2437167 0.2332433 Start of the optimization 2 : The number of iterations is 30 The value of the marginal posterior function is -61.1843 Optimized range parameters are 0.7974571 2.54443 Optimized nugget parameter is 3.708305e-14 Convergence: TRUE > #library(rlibkriging) > r <- NuggetKriging(y, X, + kernel="matern5_2", + regmodel = "constant", normalize = FALSE, + optim = "none", + objective = "LMP", + parameters=list( + theta=matrix(1/k@beta_hat,nrow=2),is_theta_estim=F, + nugget=k@nugget*k@sigma2_hat,is_nugget_estim=F, + sigma2=k@sigma2_hat,is_sigma2_estim=F)) > > > # Check predict > x2=0.8 > f1 = function(x) f(cbind(x,x2)) > > ntest <- 10 > Xtest <- seq(0,1,,ntest) > Ytest_rgasp <- predict(k,matrix(cbind(Xtest,x2),ncol=2)) > Ytest_libK <- predict(r,cbind(Xtest,x2)) > > plot(f1) > lines(Xtest,Ytest_rgasp$mean,col='blue') > polygon(c(Xtest,rev(Xtest)), + c(Ytest_rgasp$mean+2*Ytest_rgasp$sd,rev(Ytest_rgasp$mean-2*Ytest_rgasp$sd)), + col=rgb(0,0,1,0.1), border=NA) > > lines(Xtest,Ytest_libK$mean,col='red') > polygon(c(Xtest,rev(Xtest)), + c(Ytest_libK$mean+2*Ytest_libK$stdev,rev(Ytest_libK$mean-2*Ytest_libK$stdev)), + col=rgb(1,0,0,0.1), border=NA) > > precision <- 1e-1 > test_that(desc=paste0("pred mean is the same that RobustGaSP one"), + expect_equal(predict(r,c(0.7,x2))$mean[1],predict(k,matrix(c(0.7,x2),ncol=2))$mean,tol = precision)) Test passed 😸 > test_that(desc=paste0("pred sd is the same that RobustGaSP one"), + expect_equal(predict(r,c(0.7,x2))$stdev[1],predict(k,matrix(c(0.7,x2),ncol=2))$sd,tol = precision)) Test passed 🌈 > > proc.time() user system elapsed 1.87 0.29 2.07