R/MCMCglmm.utilities_fun.R

Defines functions get.sample.covar make.matrix make.sol make.covar

## Get one covar matrix
make.covar <- function(level, VCV, levels, n_traits) {
    matrix(VCV[(1:n_traits^2) + (level-1) * n_traits^2], ncol = n_traits)
}

## Get the estimated solutions
make.sol <- function(level, Sol, levels, n_traits) {
    if(names(levels[level]) == "random") {
        return(rep(0, n_traits))
    } else {
        return(unname(Sol[1:n_traits + (level - (sum(names(levels) == "random") + 1)) * n_traits]))
    }
}

## Sapply wrapper
make.matrix <- function(level, sample_estimates, levels, traits) {
    list(VCV = make.covar(level, sample_estimates$VCV, levels, length(traits)),
         loc =   make.sol(level, sample_estimates$Sol, levels, length(traits)))
}

## Internal function for MCMCglmm.covars
get.sample.covar <- function(one_sample, data, levels, traits) {
    ## Select a specific sample
    sample_estimates <- list(VCV = data$VCV[one_sample, ],
                             Sol = data$Sol[one_sample, ])

    ## Make the matrix
    levels_covar <- sapply(1:length(levels), make.matrix, sample_estimates, levels, traits, simplify = FALSE)
    names(levels_covar) <- levels

    ## Return the covariance matrices and origins
    return(levels_covar)
}

Try the dispRity package in your browser

Any scripts or data that you put into this service are public.

dispRity documentation built on Aug. 9, 2022, 5:11 p.m.