Title: | Bayesian Adaptive Spline Surfaces |
---|---|
Description: | Bayesian fitting and sensitivity analysis methods for adaptive spline surfaces described in <doi:10.18637/jss.v094.i08>. Built to handle continuous and categorical inputs as well as functional or scalar output. An extension of the methodology in Denison, Mallick and Smith (1998) <doi:10.1023/A:1008824606259>. |
Authors: | Devin Francom [aut, cre], Bruno Sanso [ths], Kellin Rumsey [aut] |
Maintainer: | Devin Francom <[email protected]> |
License: | GPL-3 |
Version: | 1.3.1 |
Built: | 2025-02-23 03:31:12 UTC |
Source: | https://github.com/cran/BASS |
Fits a BASS model using RJMCMC. Optionally uses parallel tempering to improve mixing. Can be used with scalar or functional response. Also can use categorical inputs.
bass( xx, y, maxInt = 3, maxInt.func = 3, maxInt.cat = 3, xx.func = NULL, degree = 1, maxBasis = 1000, npart = NULL, npart.func = NULL, nmcmc = 10000, nburn = 9000, thin = 1, g1 = 0, g2 = 0, s2.lower = 0, h1 = 10, h2 = 10, a.tau = 0.5, b.tau = NULL, w1 = 5, w2 = 5, beta.prior = "g", temp.ladder = NULL, start.temper = NULL, curr.list = NULL, save.yhat = TRUE, small = FALSE, verbose = TRUE, ret.str = F )
bass( xx, y, maxInt = 3, maxInt.func = 3, maxInt.cat = 3, xx.func = NULL, degree = 1, maxBasis = 1000, npart = NULL, npart.func = NULL, nmcmc = 10000, nburn = 9000, thin = 1, g1 = 0, g2 = 0, s2.lower = 0, h1 = 10, h2 = 10, a.tau = 0.5, b.tau = NULL, w1 = 5, w2 = 5, beta.prior = "g", temp.ladder = NULL, start.temper = NULL, curr.list = NULL, save.yhat = TRUE, small = FALSE, verbose = TRUE, ret.str = F )
xx |
a data frame or matrix of predictors. Categorical predictors should be included as factors. |
y |
a response vector (scalar response) or matrix (functional response). Note: If |
maxInt |
integer for maximum degree of interaction in spline basis functions. Defaults to the number of predictors, which could result in overfitting. |
maxInt.func |
(functional response only) integer for maximum degree of interaction in spline basis functions describing the functional response. |
maxInt.cat |
(categorical input only) integer for maximum degree of interaction of categorical inputs. |
xx.func |
a vector, matrix or data frame of functional variables. |
degree |
degree of splines. Stability should be examined for anything other than 1. |
maxBasis |
maximum number of basis functions. This should probably only be altered if you run out of memory. |
npart |
minimum number of non-zero points in a basis function. If the response is functional, this refers only to the portion of the basis function coming from the non-functional predictors. Defaults to 20 or 0.1 times the number of observations, whichever is smaller. |
npart.func |
same as npart, but for functional portion of basis function. |
nmcmc |
number of RJMCMC iterations. |
nburn |
number of the |
thin |
keep every |
g1 |
shape for IG prior on |
g2 |
scale for IG prior on |
s2.lower |
lower bound for s2. Turns IG prior for s2 into a truncated IG. |
h1 |
shape for gamma prior on |
h2 |
rate for gamma prior on |
a.tau |
shape for gamma prior on |
b.tau |
rate for gamma prior on |
w1 |
nominal weight for degree of interaction, used in generating candidate basis functions. Should be greater than 0. |
w2 |
nominal weight for variables, used in generating candidate basis functions. Should be greater than 0. |
beta.prior |
what type of prior to use for basis coefficients, "g" or "jeffreys" |
temp.ladder |
temperature ladder used for parallel tempering. The first value should be 1 and the values should increase. |
start.temper |
when to start tempering (after how many MCMC iterations). Defaults to 1000 or half of burn-in, whichever is smaller. |
curr.list |
list of starting models (one element for each temperature), could be output from a previous run under the same model setup. |
save.yhat |
logical; should predictions of training data be saved? |
small |
logical; if true, returns a smaller object by leaving out |
verbose |
logical; should progress be displayed? |
ret.str |
logical; return data and prior structures |
Explores BASS model space by RJMCMC. The BASS model has
and is a BASS basis function (tensor product of spline basis functions). We use priors
as well as the priors mentioned in the arguments above.
An object of class 'bass'. The other output will only be useful to the advanced user. Rather, users may be interested in prediction and sensitivity analysis, which are obtained by passing the entire object to the predict.bass or sobol functions.
predict.bass for prediction and sobol for sensitivity analysis.
## Not run: #################################################################################################### ### univariate example #################################################################################################### ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } sigma<-1 # noise sd n<-500 # number of observations x<-matrix(runif(n*10),n,10) #10 variables, only first 5 matter y<-rnorm(n,f(x),sigma) ## fit BASS, no tempering mod<-bass(x,y) plot(mod) ## fit BASS, tempering mod<-bass(x,y,temp.ladder=1.3^(0:8),start.temper=1000) plot(mod) ## prediction npred<-1000 xpred<-matrix(runif(npred*10),npred,10) pred<-predict(mod,xpred,verbose=TRUE) # posterior predictive samples true.y<-f(xpred) plot(true.y,colMeans(pred),xlab='true values',ylab='posterior predictive means') abline(a=0,b=1,col=2) ## sensitivity sens<-sobol(mod) plot(sens,cex.axis=.5) #################################################################################################### ### functional example #################################################################################################### ## simulate data (Friedman function with first variable as functional) sigma<-1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS mod<-bass(x,y,xx.func=xfunc) plot(mod) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobol(mod,mcmc.use=1:10) # for speed, only use a few samples plot(sens) # functional variable labelled "a" sens.func<-sobol(mod,mcmc.use=1:10,func.var=1) plot(sens.func) ## End(Not run) ## minimal example for CRAN testing mod<-bass(1:2,1:2,nmcmc=2,nburn=1)
## Not run: #################################################################################################### ### univariate example #################################################################################################### ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } sigma<-1 # noise sd n<-500 # number of observations x<-matrix(runif(n*10),n,10) #10 variables, only first 5 matter y<-rnorm(n,f(x),sigma) ## fit BASS, no tempering mod<-bass(x,y) plot(mod) ## fit BASS, tempering mod<-bass(x,y,temp.ladder=1.3^(0:8),start.temper=1000) plot(mod) ## prediction npred<-1000 xpred<-matrix(runif(npred*10),npred,10) pred<-predict(mod,xpred,verbose=TRUE) # posterior predictive samples true.y<-f(xpred) plot(true.y,colMeans(pred),xlab='true values',ylab='posterior predictive means') abline(a=0,b=1,col=2) ## sensitivity sens<-sobol(mod) plot(sens,cex.axis=.5) #################################################################################################### ### functional example #################################################################################################### ## simulate data (Friedman function with first variable as functional) sigma<-1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS mod<-bass(x,y,xx.func=xfunc) plot(mod) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobol(mod,mcmc.use=1:10) # for speed, only use a few samples plot(sens) # functional variable labelled "a" sens.func<-sobol(mod,mcmc.use=1:10,func.var=1) plot(sens.func) ## End(Not run) ## minimal example for CRAN testing mod<-bass(1:2,1:2,nmcmc=2,nburn=1)
Fits a BASS model to basis coefficients under the specified basis.
bassBasis(dat, n.cores = 1, parType = "fork", ...)
bassBasis(dat, n.cores = 1, parType = "fork", ...)
dat |
list that includes elements |
n.cores |
integer number of cores (threads) to use |
parType |
either "fork" or "socket". Forking is typically faster, but not compatible with Windows. If |
... |
arguements to be passed to |
Under a user defined basis decomposition, fits a bass model to each PCA basis coefficient independently, bass(dat$xx,dat$newy[i,],...)
for i in 1 to n.pc
, possibly in parallel. The basis does not need to be orthogonal, but independent modeling of basis coefficients should be sensible.
An object of class 'bassBasis' with two elements:
mod.list |
list (of length |
dat |
same as dat above |
predict.bassBasis for prediction and sobolBasis for sensitivity analysis.
## Not run: ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } ## simulate data (Friedman function with first variable as functional) sigma<-.1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS library(parallel) mod<-bassPCA(x,y,n.pc=5,n.cores=min(5,parallel::detectCores())) plot(mod$mod.list[[1]]) plot(mod$mod.list[[2]]) plot(mod$mod.list[[3]]) plot(mod$mod.list[[4]]) plot(mod$mod.list[[5]]) hist(mod$dat$trunc.error) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred,mcmc.use=1:1000) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobolBasis(mod,int.order = 2,ncores = max(parallel::detectCores()-2,1), mcmc.use=1000) # for speed, only use a few samples plot(sens) ## calibration x.true<-runif(9,0,1) # what we are trying to learn yobs<-f(cbind(xfunc,kronecker(rep(1,nfunc),t(x.true)))) + rnorm(nfunc,0,.1) # calibration data (with measurement error) plot(yobs) cal<-calibrate.bassBasis(y=yobs,mod=mod, discrep.mean=rep(0,nfunc), discrep.mat=diag(nfunc)[,1:2]*.0000001, sd.est=.1, s2.df=50, s2.ind=rep(1,nfunc), meas.error.cor=diag(nfunc), bounds=rbind(rep(0,9),rep(1,9)), nmcmc=10000, temperature.ladder=1.05^(0:30),type=1) nburn<-5000 uu<-seq(nburn,10000,5) pairs(rbind(cal$theta[uu,1,],x.true),col=c(rep(1,length(uu)),2),ylim=c(0,1),xlim=c(0,1)) pred<-apply(predict(mod,cal$theta[uu,1,],nugget = T,trunc.error = T, mcmc.use = cal$ii[uu]),3,function(x) diag(x)+rnorm(length(uu),0,sqrt(cal$s2[uu,1,1]))) qq<-apply(pred,2,quantile,probs=c(.025,.975)) matplot(t(qq),col='lightgrey',type='l') lines(yobs,lwd=3) ## End(Not run) ## minimal example for CRAN testing mod<-bassPCA(1:10,matrix(1:20,10),n.pc=2,nmcmc=2,nburn=1)
## Not run: ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } ## simulate data (Friedman function with first variable as functional) sigma<-.1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS library(parallel) mod<-bassPCA(x,y,n.pc=5,n.cores=min(5,parallel::detectCores())) plot(mod$mod.list[[1]]) plot(mod$mod.list[[2]]) plot(mod$mod.list[[3]]) plot(mod$mod.list[[4]]) plot(mod$mod.list[[5]]) hist(mod$dat$trunc.error) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred,mcmc.use=1:1000) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobolBasis(mod,int.order = 2,ncores = max(parallel::detectCores()-2,1), mcmc.use=1000) # for speed, only use a few samples plot(sens) ## calibration x.true<-runif(9,0,1) # what we are trying to learn yobs<-f(cbind(xfunc,kronecker(rep(1,nfunc),t(x.true)))) + rnorm(nfunc,0,.1) # calibration data (with measurement error) plot(yobs) cal<-calibrate.bassBasis(y=yobs,mod=mod, discrep.mean=rep(0,nfunc), discrep.mat=diag(nfunc)[,1:2]*.0000001, sd.est=.1, s2.df=50, s2.ind=rep(1,nfunc), meas.error.cor=diag(nfunc), bounds=rbind(rep(0,9),rep(1,9)), nmcmc=10000, temperature.ladder=1.05^(0:30),type=1) nburn<-5000 uu<-seq(nburn,10000,5) pairs(rbind(cal$theta[uu,1,],x.true),col=c(rep(1,length(uu)),2),ylim=c(0,1),xlim=c(0,1)) pred<-apply(predict(mod,cal$theta[uu,1,],nugget = T,trunc.error = T, mcmc.use = cal$ii[uu]),3,function(x) diag(x)+rnorm(length(uu),0,sqrt(cal$s2[uu,1,1]))) qq<-apply(pred,2,quantile,probs=c(.025,.975)) matplot(t(qq),col='lightgrey',type='l') lines(yobs,lwd=3) ## End(Not run) ## minimal example for CRAN testing mod<-bassPCA(1:10,matrix(1:20,10),n.pc=2,nmcmc=2,nburn=1)
Decomposes a multivariate or functional response onto a principal component basis and fits a BASS model to each basis coefficient.
bassPCA( xx = NULL, y = NULL, dat = NULL, n.pc = NULL, perc.var = 99, n.cores = 1, parType = "fork", center = T, scale = F, ... )
bassPCA( xx = NULL, y = NULL, dat = NULL, n.pc = NULL, perc.var = 99, n.cores = 1, parType = "fork", center = T, scale = F, ... )
xx |
a data frame or matrix of predictors with dimension n x p. Categorical predictors should be included as factors. |
y |
a response matrix (functional response) with dimension n x m. |
dat |
optional (for more control) list with elements |
n.pc |
number of principal components to use |
perc.var |
optionally specify percent of variance to explain instead of n.pc |
n.cores |
integer number of cores (threads) to use |
parType |
either "fork" or "socket". Forking is typically faster, but not compatible with Windows. If |
center |
logical whether to subtract the mean before getting the principal components, or else a numeric vector of dimension m for the center to be used |
scale |
logical whether to divide by the standard deviation before getting the principal components, or else a numeric vector of dimension m for the scale to be used |
... |
arguements to be passed to |
Gets the PCA decomposition of the response y
, and fits a bass model to each PCA basis coefficient, bass(dat$xx,dat$newy[i,],...)
for i in 1 to n.pc
, possibly in parallel.
An object of class 'bassBasis' with two elements:
mod.list |
list (of length |
dat |
same as dat above |
predict.bassBasis for prediction and sobolBasis for sensitivity analysis.
## Not run: ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } ## simulate data (Friedman function with first variable as functional) sigma<-.1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS library(parallel) mod<-bassPCA(x,y,n.pc=5,n.cores=min(5,parallel::detectCores())) plot(mod$mod.list[[1]]) plot(mod$mod.list[[2]]) plot(mod$mod.list[[3]]) plot(mod$mod.list[[4]]) plot(mod$mod.list[[5]]) hist(mod$dat$trunc.error) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred,mcmc.use=1:1000) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobolBasis(mod,int.order = 2,ncores = max(parallel::detectCores()-2,1), mcmc.use=1000) # for speed, only use a few samples plot(sens) ## calibration x.true<-runif(9,0,1) # what we are trying to learn yobs<-f(cbind(xfunc,kronecker(rep(1,nfunc),t(x.true)))) + rnorm(nfunc,0,.1) # calibration data (with measurement error) plot(yobs) cal<-calibrate.bassBasis(y=yobs,mod=mod, discrep.mean=rep(0,nfunc), discrep.mat=diag(nfunc)[,1:2]*.0000001, sd.est=.1, s2.df=50, s2.ind=rep(1,nfunc), meas.error.cor=diag(nfunc), bounds=rbind(rep(0,9),rep(1,9)), nmcmc=10000, temperature.ladder=1.05^(0:30),type=1) nburn<-5000 uu<-seq(nburn,10000,5) pairs(rbind(cal$theta[uu,1,],x.true),col=c(rep(1,length(uu)),2),ylim=c(0,1),xlim=c(0,1)) pred<-apply(predict(mod,cal$theta[uu,1,],nugget = T,trunc.error = T, mcmc.use = cal$ii[uu]),3,function(x) diag(x)+rnorm(length(uu),0,sqrt(cal$s2[uu,1,1]))) qq<-apply(pred,2,quantile,probs=c(.025,.975)) matplot(t(qq),col='lightgrey',type='l') lines(yobs,lwd=3) ## End(Not run) ## minimal example for CRAN testing mod<-bassPCA(1:10,matrix(1:20,10),n.pc=2,nmcmc=2,nburn=1)
## Not run: ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } ## simulate data (Friedman function with first variable as functional) sigma<-.1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS library(parallel) mod<-bassPCA(x,y,n.pc=5,n.cores=min(5,parallel::detectCores())) plot(mod$mod.list[[1]]) plot(mod$mod.list[[2]]) plot(mod$mod.list[[3]]) plot(mod$mod.list[[4]]) plot(mod$mod.list[[5]]) hist(mod$dat$trunc.error) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred,mcmc.use=1:1000) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobolBasis(mod,int.order = 2,ncores = max(parallel::detectCores()-2,1), mcmc.use=1000) # for speed, only use a few samples plot(sens) ## calibration x.true<-runif(9,0,1) # what we are trying to learn yobs<-f(cbind(xfunc,kronecker(rep(1,nfunc),t(x.true)))) + rnorm(nfunc,0,.1) # calibration data (with measurement error) plot(yobs) cal<-calibrate.bassBasis(y=yobs,mod=mod, discrep.mean=rep(0,nfunc), discrep.mat=diag(nfunc)[,1:2]*.0000001, sd.est=.1, s2.df=50, s2.ind=rep(1,nfunc), meas.error.cor=diag(nfunc), bounds=rbind(rep(0,9),rep(1,9)), nmcmc=10000, temperature.ladder=1.05^(0:30),type=1) nburn<-5000 uu<-seq(nburn,10000,5) pairs(rbind(cal$theta[uu,1,],x.true),col=c(rep(1,length(uu)),2),ylim=c(0,1),xlim=c(0,1)) pred<-apply(predict(mod,cal$theta[uu,1,],nugget = T,trunc.error = T, mcmc.use = cal$ii[uu]),3,function(x) diag(x)+rnorm(length(uu),0,sqrt(cal$s2[uu,1,1]))) qq<-apply(pred,2,quantile,probs=c(.025,.975)) matplot(t(qq),col='lightgrey',type='l') lines(yobs,lwd=3) ## End(Not run) ## minimal example for CRAN testing mod<-bassPCA(1:10,matrix(1:20,10),n.pc=2,nmcmc=2,nburn=1)
Robust modular calibration of a bassPCA or bassBasis emulator using adaptive Metropolis, tempering, and decorrelation steps in an effort to be free of any user-required tuning.
calibrate.bassBasis( y, mod, type, sd.est, s2.df, s2.ind, meas.error.cor, bounds, discrep.mean, discrep.mat, nmcmc = 10000, temperature.ladder = 1.05^(0:30), decor.step.every = 100, verbose = T )
calibrate.bassBasis( y, mod, type, sd.est, s2.df, s2.ind, meas.error.cor, bounds, discrep.mean, discrep.mat, nmcmc = 10000, temperature.ladder = 1.05^(0:30), decor.step.every = 100, verbose = T )
y |
vector of calibration data |
mod |
a emulator of class bassBasis, whose predictions should match y (i.e., predictions from mod should be the same length as y) |
type |
one of c(1,2). 1 indicates a model that uses independent truncation error variance, no measurement error correlation, and discrepancy on a basis while type 2 indicates a model that uses a full truncation error covariance matrix, a full measurement error correlation matrix, a fixed full discrepancy covariance matrix, and a fixed discrepancy mean. 1 is for situations where computational efficiency is important (because y is dense), while 2 is only for cases where y is a short vector. |
sd.est |
vector of prior estimates of measurement error standard deviation |
s2.df |
vector of degrees of freedom for measurement error sd prior estimates |
s2.ind |
index vector, same length as y, indicating which sd.est goes with which y |
meas.error.cor |
a fixed correlation matrix for the measurement errors |
bounds |
a 2xp matrix of bounds for each input parameter, where p is the number of input parameters. |
discrep.mean |
discrepancy mean (fixed), only used if type=2 |
discrep.mat |
discrepancy covariance (fixed, for type 2) or basis (if not square, for type 1) |
nmcmc |
number of MCMC iterations. |
temperature.ladder |
an increasing vector, all greater than 1, for tempering. Geometric spacing is recommended, so that you have (1+delta)^(0:ntemps), where delta is small (typically between 0.05 and 0.2) and ntemps is the number of elements in the vector. |
decor.step.every |
integer number of MCMC iterations between decorrelation steps. |
verbose |
logical, whether to print progress. |
Fits a modular Bayesian calibration model, with
and is a BASS basis function (tensor product of spline basis functions). We use priors
as well as the priors mentioned in the arguments above.
An object
predict.bassBasis for prediction and sobolBasis for sensitivity analysis.
## Not run: ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } ## simulate data (Friedman function with first variable as functional) sigma<-.1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS library(parallel) mod<-bassPCA(x,y,n.pc=5,n.cores=min(5,parallel::detectCores())) plot(mod$mod.list[[1]]) plot(mod$mod.list[[2]]) plot(mod$mod.list[[3]]) plot(mod$mod.list[[4]]) plot(mod$mod.list[[5]]) hist(mod$dat$trunc.error) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred,mcmc.use=1:1000) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobolBasis(mod,int.order = 2,ncores = max(parallel::detectCores()-2,1), mcmc.use=1000) # for speed, only use a few samples plot(sens) ## calibration x.true<-runif(9,0,1) # what we are trying to learn yobs<-f(cbind(xfunc,kronecker(rep(1,nfunc),t(x.true)))) + rnorm(nfunc,0,.1) # calibration data (with measurement error) plot(yobs) cal<-calibrate.bassBasis(y=yobs,mod=mod, discrep.mean=rep(0,nfunc), discrep.mat=diag(nfunc)[,1:2]*.0000001, sd.est=.1, s2.df=50, s2.ind=rep(1,nfunc), meas.error.cor=diag(nfunc), bounds=rbind(rep(0,9),rep(1,9)), nmcmc=10000, temperature.ladder=1.05^(0:30),type=1) nburn<-5000 uu<-seq(nburn,10000,5) pairs(rbind(cal$theta[uu,1,],x.true),col=c(rep(1,length(uu)),2),ylim=c(0,1),xlim=c(0,1)) pred<-apply(predict(mod,cal$theta[uu,1,],nugget = T,trunc.error = T, mcmc.use = cal$ii[uu]),3,function(x) diag(x)+rnorm(length(uu),0,sqrt(cal$s2[uu,1,1]))) qq<-apply(pred,2,quantile,probs=c(.025,.975)) matplot(t(qq),col='lightgrey',type='l') lines(yobs,lwd=3) ## End(Not run) ## minimal example for CRAN testing mod<-bassPCA(1:10,matrix(1:20,10),n.pc=2,nmcmc=2,nburn=1)
## Not run: ## simulate data (Friedman function) f<-function(x){ 10*sin(pi*x[,1]*x[,2])+20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } ## simulate data (Friedman function with first variable as functional) sigma<-.1 # noise sd n<-500 # number of observations nfunc<-50 # size of functional variable grid xfunc<-seq(0,1,length.out=nfunc) # functional grid x<-matrix(runif(n*9),n,9) # 9 non-functional variables, only first 4 matter X<-cbind(rep(xfunc,each=n),kronecker(rep(1,nfunc),x)) # to get y y<-matrix(f(X),nrow=n)+rnorm(n*nfunc,0,sigma) ## fit BASS library(parallel) mod<-bassPCA(x,y,n.pc=5,n.cores=min(5,parallel::detectCores())) plot(mod$mod.list[[1]]) plot(mod$mod.list[[2]]) plot(mod$mod.list[[3]]) plot(mod$mod.list[[4]]) plot(mod$mod.list[[5]]) hist(mod$dat$trunc.error) ## prediction npred<-100 xpred<-matrix(runif(npred*9),npred,9) Xpred<-cbind(rep(xfunc,each=npred),kronecker(rep(1,nfunc),xpred)) ypred<-matrix(f(Xpred),nrow=npred) pred<-predict(mod,xpred,mcmc.use=1:1000) # posterior predictive samples (each is a curve) matplot(ypred,apply(pred,2:3,mean),type='l',xlab='observed',ylab='mean prediction') abline(a=0,b=1,col=2) matplot(t(ypred),type='l') # actual matplot(t(apply(pred,2:3,mean)),type='l') # mean prediction ## sensitivity sens<-sobolBasis(mod,int.order = 2,ncores = max(parallel::detectCores()-2,1), mcmc.use=1000) # for speed, only use a few samples plot(sens) ## calibration x.true<-runif(9,0,1) # what we are trying to learn yobs<-f(cbind(xfunc,kronecker(rep(1,nfunc),t(x.true)))) + rnorm(nfunc,0,.1) # calibration data (with measurement error) plot(yobs) cal<-calibrate.bassBasis(y=yobs,mod=mod, discrep.mean=rep(0,nfunc), discrep.mat=diag(nfunc)[,1:2]*.0000001, sd.est=.1, s2.df=50, s2.ind=rep(1,nfunc), meas.error.cor=diag(nfunc), bounds=rbind(rep(0,9),rep(1,9)), nmcmc=10000, temperature.ladder=1.05^(0:30),type=1) nburn<-5000 uu<-seq(nburn,10000,5) pairs(rbind(cal$theta[uu,1,],x.true),col=c(rep(1,length(uu)),2),ylim=c(0,1),xlim=c(0,1)) pred<-apply(predict(mod,cal$theta[uu,1,],nugget = T,trunc.error = T, mcmc.use = cal$ii[uu]),3,function(x) diag(x)+rnorm(length(uu),0,sqrt(cal$s2[uu,1,1]))) qq<-apply(pred,2,quantile,probs=c(.025,.975)) matplot(t(qq),col='lightgrey',type='l') lines(yobs,lwd=3) ## End(Not run) ## minimal example for CRAN testing mod<-bassPCA(1:10,matrix(1:20,10),n.pc=2,nmcmc=2,nburn=1)
Generate diagnostic plots for BASS model fit.
## S3 method for class 'bass' plot(x, quants = c(0.025, 0.975), ...)
## S3 method for class 'bass' plot(x, quants = c(0.025, 0.975), ...)
x |
a |
quants |
quantiles for intervals, if desired. NULL if not desired. |
... |
graphical parameters. |
The first two plots are trace plots for diagnosing convergence. The third plot is posterior predicted vs observed, with intervals for predictions. The fourth plot is a histogram of the residuals (of the posterior mean model), with a red curve showing the assumed Normal density (using posterior mean variance). If bass
was run with save.yhat = FALSE
, the third and fourth plots are omitted.
# See examples in bass documentation.
# See examples in bass documentation.
Generate diagnostic plots for BASS model fit.
## S3 method for class 'bassBasis' plot(x, quants = c(0.025, 0.975), pred = T, ...)
## S3 method for class 'bassBasis' plot(x, quants = c(0.025, 0.975), pred = T, ...)
x |
a |
quants |
quantiles for intervals, if desired. NULL if not desired. |
pred |
logical, should predictive performance be plotted? |
... |
graphical parameters. |
The first two plots are trace plots for diagnosing convergence. The third plot is posterior predicted vs observed, with intervals for predictions. The fourth plot is a histogram of the residuals (of the posterior mean model). If pred = FALSE
, the third and fourth plots are omitted.
bassBasis, bassPCA, predict.bassBasis, sobolBasis
# See examples in bassBasis documentation.
# See examples in bassBasis documentation.
Generate plots for sensitivity analysis of BASS.
## S3 method for class 'bassSob' plot(x, ...)
## S3 method for class 'bassSob' plot(x, ...)
x |
a |
... |
graphical parameters. |
If func.var
in the call to sobol
was NULL
, this returns boxplots of sensitivity indices and total sensitivity indices. If there were functional variables, they are labeled with letters alphabetically. Thus, if I fit a model with 4 categorical/continuous inputs and 2 functional inputs, the functional inputs are labeled a and b. If func.var
was not NULL
, then posterior mean functional sensitivity indices are plotted, along with the functional partitioned variance. Variables and interactions that are excluded did not explain any variance.
# See examples in bass documentation.
# See examples in bass documentation.
Predict function for BASS. Outputs the posterior predictive samples based on the specified MCMC iterations.
## S3 method for class 'bass' predict( object, newdata, newdata.func = NULL, mcmc.use = NULL, verbose = FALSE, nugget = FALSE, ... )
## S3 method for class 'bass' predict( object, newdata, newdata.func = NULL, mcmc.use = NULL, verbose = FALSE, nugget = FALSE, ... )
object |
a fitted model, output from the |
newdata |
a matrix of new input values at which to predict. The columns should correspond to the same variables used in the |
newdata.func |
a matrix of new values of the functional variable. If none, the same values will be used as in the training data. |
mcmc.use |
a vector indexing which MCMC iterations to use for prediction. |
verbose |
logical; should progress be displayed? |
nugget |
logical; should predictions include error? If FALSE, predictions will be for mean. |
... |
further arguments passed to or from other methods. |
Efficiently predicts when two MCMC iterations have the same basis functions (but different weights).
If model output is a scalar, this returns a matrix with the same number of rows as newdata
and columns corresponding to the the MCMC iterations mcmc.use
. These are samples from the posterior predictive distribution. If model output is functional, this returns an array with first dimension corresponding to MCMC iteration, second dimension corresponding to the rows of newdata
, and third dimension corresponding to the rows of newdata.func
.
bass for model fitting and sobol for sensitivity analysis.
# See examples in bass documentation.
# See examples in bass documentation.
Predict function for BASS. Outputs the posterior predictive samples based on the specified MCMC iterations.
## S3 method for class 'bassBasis' predict( object, newdata, mcmc.use = NULL, trunc.error = FALSE, nugget = T, n.cores = 1, parType = "fork", ... )
## S3 method for class 'bassBasis' predict( object, newdata, mcmc.use = NULL, trunc.error = FALSE, nugget = T, n.cores = 1, parType = "fork", ... )
object |
a fitted model, output from the |
newdata |
a matrix of new input values at which to predict. The columns should correspond to the same variables used in the |
mcmc.use |
a vector indexing which MCMC iterations to use for prediction. |
trunc.error |
logical, use basis truncation error when predicting? |
nugget |
logical, use individual |
n.cores |
number of cores, though 1 is often the fastest. |
parType |
either "fork" or "socket". Forking is typically faster, but not compatible with Windows. If |
... |
further arguments passed to or from other methods. |
Prediction combined across bass
models.
An array with first dimension corresponding to MCMC iteration, second dimension corresponding to the rows of newdata
, and third dimension corresponding to the multivariate/functional response.
bassPCA and bassBasis for model fitting and sobolBasis for sensitivity analysis.
# See examples in bass documentation.
# See examples in bass documentation.
Print some of the details of a BASS model.
## S3 method for class 'bass' print(x, ...)
## S3 method for class 'bass' print(x, ...)
x |
a |
... |
further arguments passed to or from other methods. |
Print some of the details of a BASS model.
## S3 method for class 'bassBasis' print(x, ...)
## S3 method for class 'bassBasis' print(x, ...)
x |
a |
... |
further arguments passed to or from other methods. |
Decomposes the variance of the BASS model into variance due to main effects, two way interactions, and so on, similar to the ANOVA decomposition for linear models. Uses the Sobol' decomposition, which can be done analytically for MARS models.
sobol( bassMod, prior = NULL, prior.func = NULL, mcmc.use = NULL, func.var = NULL, xx.func.var = NULL, verbose = TRUE, getEffects = FALSE )
sobol( bassMod, prior = NULL, prior.func = NULL, mcmc.use = NULL, func.var = NULL, xx.func.var = NULL, verbose = TRUE, getEffects = FALSE )
bassMod |
a fitted model output from the |
prior |
a list of priors; uniform, truncated mixture of Normals or Ts for continuous; vector of category weights for categorical. Default is uniform over range of data. |
prior.func |
prior for functional variable. In almost all cases, keep this as the uniform default. |
mcmc.use |
an integer vector indexing which MCMC iterations to use for sensitivity analysis. |
func.var |
an integer indicating which functional variable to make sensitivity indices a function of. Disregard if |
xx.func.var |
grid for functional variable specified by |
verbose |
logical; should progress be displayed? |
getEffects |
logical; should Sobols ANOVA decomposition be computed? |
Performs analytical Sobol' decomposition for each MCMC iteration in mcmc.use (each corresponds to a MARS model), yeilding a posterior distribution of sensitivity indices. Can obtain Sobol' indices as a function of one functional variable.
If non-functional (func.var = NULL
), a list with two elements:
S |
a data frame of sensitivity indices with number of rows matching the length of |
T |
a data frame of total sensitivity indices with number of rows matching the length of |
Otherwise, a list with four elements:
S |
an array with first dimension corresponding to MCMC samples (same length as |
S.var |
same as |
names.ind |
a vector of names of the main effects and interactions used. |
xx |
the grid used for the functional variable. |
bass for model fitting and predict.bass for prediction.
# See examples in bass documentation.
# See examples in bass documentation.
Decomposes the variance of the BASS model into variance due to main effects, two way interactions, and so on, similar to the ANOVA decomposition for linear models. Uses the Sobol' decomposition, which can be done analytically for MARS models.
sobolBasis( mod, int.order, prior = NULL, mcmc.use = NULL, nind = NULL, n.cores = 1, parType = "fork", plot = F, verbose = T )
sobolBasis( mod, int.order, prior = NULL, mcmc.use = NULL, nind = NULL, n.cores = 1, parType = "fork", plot = F, verbose = T )
mod |
output from the |
int.order |
an integer indicating the highest order of interactions to include in the Sobol decomposition. |
prior |
a list with the same number of elements as there are inputs to mod. Each element specifies the prior for the particular input. Each prior is specified as a list with elements |
mcmc.use |
an integer indicating which MCMC iteration to use for sensitivity analysis. Defaults to the last iteration. |
nind |
number of Sobol indices to keep (will keep the largest nind). |
n.cores |
number of cores to use (nearly linear speedup for adding cores). |
parType |
either "fork" or "socket". Forking is typically faster, but not compatible with Windows. If |
plot |
logical; whether to plot results. |
verbose |
logical; print progress. |
Performs analytical Sobol' decomposition for each MCMC iteration in mcmc.use (each corresponds to a MARS model), yeilding a posterior distribution of sensitivity indices. Can obtain Sobol' indices as a function of one functional variable.
If non-functional (func.var = NULL
), a list with two elements:
S |
a data frame of sensitivity indices with number of rows matching the length of |
T |
a data frame of total sensitivity indices with number of rows matching the length of |
Otherwise, a list with four elements:
S |
an array with first dimension corresponding to MCMC samples (same length as |
S.var |
same as |
names.ind |
a vector of names of the main effects and interactions used. |
bassPCA and bassBasis for model fitting and predict.bassBasis for prediction.
# See examples in bass documentation.
# See examples in bass documentation.
Summarize some of the details of a BASS model.
## S3 method for class 'bass' summary(object, ...)
## S3 method for class 'bass' summary(object, ...)
object |
a |
... |
further arguments passed to or from other methods. |
Summarize some of the details of a BASS model.
## S3 method for class 'bassBasis' summary(object, ...)
## S3 method for class 'bassBasis' summary(object, ...)
object |
a |
... |
further arguments passed to or from other methods. |