https://github.com/cran/CARBayes
Tip revision: a8f860faed530b83e77aff5e710dcd60c417f762 authored by Duncan Lee on 03 February 2021, 08:10:02 UTC
version 5.2.3
version 5.2.3
Tip revision: a8f860f
summarise.lincomb.R
summarise.lincomb <- function(model, columns=NULL, quantiles=0.5, distribution=FALSE)
{
#### Summarise a linear combination of the covariates
X <- model$X
n <- nrow(X)
p <- ncol(X)
beta <- model$samples$beta
n.sample <- nrow(beta)
if(!is.numeric(columns)) stop("the columns argument is not numeric.", call.=FALSE)
N.all <- length(columns)
if(sum(floor(columns)==ceiling(columns)) < N.all) stop("the columns argument is not all integers.", call.=FALSE)
if(min(columns)<1) stop("some of the columns chosen are less than 1.", call.=FALSE)
if(max(columns)>p) stop("some of the columns chosen are greater than the size of the samples object.", call.=FALSE)
if(!is.numeric(quantiles)) stop("the quantiles argument is not numeric.", call.=FALSE)
if(min(quantiles) < 0) stop("some of the quantiles are less than zero.", call.=FALSE)
if(max(quantiles) > 1) stop("some of the quantiles are greater than one.", call.=FALSE)
if(!is.logical(distribution)) stop("The distribution argument must be true or false.", call.=FALSE)
#### Compute the posterior distribution
beta.samples <- beta[ ,columns]
samples <- array(NA, c(n.sample, n))
for(i in 1:n)
{
samples[ ,i] <- beta.samples %*% X[i,columns]
}
samples.quantiles <- apply(samples, 2, quantile, quantiles)
#### Create the results list
if(distribution==TRUE)
{
results <- list(quantiles=t(samples.quantiles), posterior=samples)
}else
{
results <- list(quantiles=t(samples.quantiles), posterior=NULL)
}
}