R/param_classes.R

Defines functions describe_correlation make_correlation_matrix make_signal

Documented in describe_correlation make_correlation_matrix make_signal

#' Define a correlation matrix
#'
#' @param type chr, Type of a correlation matrix: "constant" for a matrix
#' where every off-diagonal term is the same (specified by the correlation
#' parameter), "exponential" for a matrix where correlation between variables
#' decays exponentially.
#' @param correlation Value of the correlation coefficient. In case of an
#' exponentially decaying correlation, this is the exponent base.
#'
#' @return object of class 'cor_desc' - a list of type and correlation value.
#'
#' @export
#'

describe_correlation <- function(type, correlation) {
    cor_matrix <- list(type = type,
                       correlation = correlation)
    class(cor_matrix) <- c("cor_matrix", class(cor_matrix))
    cor_matrix

}


#' Create a correlation matrix object
#'
#' @inheritParams describe_correlation
#' @param dimension Number of columns of the matrix
#'
#' @return Object of class "cor_matrix" that consist of the matrix type,
#' correlation coefficient and dimension of the matrix.
#'
#' @export
#'

make_correlation_matrix <- function(type, correlation, dimension) {
    if(type == "constant") {
        cor_m <- diag(1 - correlation, dimension) + matrix(correlation,
                                                           dimension,
                                                           dimension)
    } else {
        cor_m <- diag(1, dimension)
        for(i in 1:dimension) {
            for(j in 1:dimension) {
                cor_m[i, j] <- correlation^(abs(i - j))
            }
        }
    }
    cor_matrix <- list(type = type,
                       correlation = correlation,
                       matrix = cor_m)
    class(cor_matrix) <- c("cor_matrix", class(cor_matrix))
    cor_matrix
}


#' Create signal object
#'
#' @param num_variables Number of variables
#' @param num_nonzero Number of non-zero elements in the coefficient vector
#' @param val_nonzero Value that will be placed on non-zero elements of the
#' coefficient vector
#'
#' @return Object of class "signal" that consists of the dimension of beta
#' (coefficient) vector, beta vector and the value of non-zero elements.
#'
#' @export
#'

make_signal <- function(num_variables, num_nonzero,
                        val_nonzero = sqrt(2*log(num_variables))) {
    signal <- list(dimension = num_variables,
                   beta = c(rep(val_nonzero, num_nonzero),
                            rep(0, num_variables - num_nonzero)),
                   signal = val_nonzero)
    class(signal) <- c("signal", class(signal))
    signal
}
StatsIMUWr/slobeC documentation built on Oct. 31, 2019, 12:03 a.m.