R/RcppExports.R

Defines functions logML getTargetSet getTarget

Documented in getTarget getTargetSet

# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Construct a target matrix for single target linear shrinkage
#'
#' Construct a popular target matrix from the linear shrinkage literature.
#' These targets consist of a combination of variance and correlation
#'  structure. Possible variance structures are unit, sample mean, and 
#'  sample. Possible correlation structures are zero, sample mean, and
#'   autocorrelation.
#' 
#'
#' @param X \code{matrix} -- data matrix with variables in rows and 
#' observations in columns.
#' @param varNumber \code{numeric} -- c(1, 2, 3) variance structure for the
#'  target matrix.
#' 1 sets all variances equal to 1. 2 sets all variances equal to their sample
#'  mean (using \code{X}).
#' 3. sets all variances to their sample values (using \code{X}).
#' @param corNumber \code{numeric} -- c(1, 2, 3) correlation structure for
#'  the target matrix. 1 sets the correlations to 0. 2 sets the correlations
#'   equal 
#' to their sample mean (using \code{X}). 3 sets the correlations equals to an
#'  autocorrelation
#' structure with parameter equal to the sample mean (using \code{X}).
#' @return \code{matrix} -- target matrix for linear shrinkage
#'  estimation.
#' @seealso \code{\link{gcShrink}}
#' @examples
#'   set.seed(102)
#'   X <- matrix(rnorm(50), 10, 5) # p=10, n=5, identity covariance
#'   X <- t(scale(t(X), center=TRUE, scale=FALSE)) # mean 0
#'   getTarget(X, varNumber = 1, corNumber = 1) # unit variance, zero correlation
#'   getTarget(X, varNumber = 2, corNumber = 1) # equal variance, zero correlation
#'   getTarget(X, varNumber = 3, corNumber = 1) # sample variances, zero correlation
#'
getTarget <- function(X, varNumber = 2L, corNumber = 1L) {
    .Call('_TAS_getTarget', PACKAGE = 'TAS', X, varNumber, corNumber)
}

#' Construct a set of target matrices for Target-Averaged linear shrinkage
#'
#' Construct a set of popular target matrices from the linear shrinkage
#'  literature.
#' These nine targets consist of the combinations of variance and correlation
#'  structures; variance structures are unit, sample mean, and 
#'  sample; correlation structures are zero, sample mean, and
#' autocorrelation.
#' 
#' @param X \code{matrix} -- data matrix with variables in rows and 
#' observations in columns.
#' @return \code{array} -- a \code{p}x\code{p}x9 array of
#' target matrices, where \code{p} is the number of variables of \code{X}.
#' @seealso \code{\link{taShrink}}
#' @examples
#'   set.seed(102)
#'   X <- matrix(rnorm(50), 10, 5) # p=10, n=5, identity covariance
#'   X <- t(scale(t(X), center=TRUE, scale=FALSE)) # mean 0
#'   ts <- getTargetSet(X) # an array of targets
#'   # inspect the variances of the targets
#'   vars <- apply(ts, 3, diag)
#'   colnames(vars) <- paste("target", c(1:9), sep="")
#'   vars
#'   boxplot(vars, ylab = "variances")
#'   # inspect the correlations of the targets
#'   corrs <- apply(ts, 3, function(x){cov2cor(x)[lower.tri(x)]})
#'   colnames(corrs) <- paste("target", c(1:9), sep="")
#'   corrs
#'   boxplot(corrs, ylab = "correlations")
getTargetSet <- function(X) {
    .Call('_TAS_getTargetSet', PACKAGE = 'TAS', X)
}

#' Log-marginal likelihood of a Gaussian-inverse Wishart
#' conjugate model
#' 
#' Evaluate the log-marginal likelihood of a Gaussian-inverse Wishart
#' distribution parametrised in terms of its prior mean matrix and its
#' prior variance parameter. In the Bayesian linear shrinkage model,
#' these parameters correspond to the target matrix and the shrinkage
#' intensity (Hannart and Naveau, 2014). 
#' 
#' 
#' @param X \code{matrix} --data matrix with variables in rows and 
#' observations in columns.
#' @param target \code{matrix} -- prior mean matrix parameter of the
#' inverse-Wishart distribution. 
#' @return \code{numeric} -- log-marginal likelihood evaluated at
#'  (\code{target}, \code{alpha}). If \code{alpha} is a vector is a vector
#'  then the function returns a vector evaluated at each element of 
#'  \code{alpha}.
#' @seealso \code{\link{gcShrink}}, \code{\link{taShrink}}
#' @examples
#'   set.seed(102)
#'   X <- matrix(rnorm(50), 10, 5) # p=10, n=5, identity covariance
#'   X <- t(scale(t(X), center=TRUE, scale=FALSE)) # mean 0
#'   target <- getTarget(X)
#'   alpha <- seq(0.01, 0.99, length.out=100)
#'   lml <- logML(X, target, alpha)
#'   plot(alpha, lml, col = 'blue', pch = 16,
#'   ylab = "log marginal likelihoods", xlab = expression(alpha))
#'   lines(x = rep(alpha[which(lml==max(lml))], 2), y = c(min(lml), max(lml)), col='red')
#' @references Alexis Hannart and Philippe Naveau (2014). 
#' Estimating high dimensional covariance matrices: 
#' A new look at the Gaussian conjugate framework. 
#' Journal of Multivariate Analysis. \href{http://dx.doi.org/10.1016/j.jmva.2014.06.001}{doi}.
NULL

logML <- function(X, target) {
    .Call('_TAS_logML', PACKAGE = 'TAS', X, target)
}
HGray384/TAS documentation built on Dec. 14, 2020, 8:41 p.m.