Diff of /R/calc.lBF.R [000000] .. [53575d]

Switch to unified view

a b/R/calc.lBF.R
1
#' calculate BF
2
#'
3
#' @param pars A list object as returned by the function set.parameters
4
#' @param data.sub A 3-dimension array (N. SNPs x N. phenotypes x 3) with state likelihoods.
5
#' @param w0 An integer with column index for the null state.
6
#' @param log10 Return Bayes Factor in log 10 base.
7
#'
8
#' @return lBF Tree Bayes Factor
9
#'
10
calc.lBF <- function(
11
    pars = NULL,
12
    data.sub = NULL,
13
    w0 = 2,
14
    log10 = TRUE
15
) {
16
17
    if( is.null(pars) | is.null(data.sub) ) {
18
        stop("Missing input data.\n")
19
    }
20
    
21
    if (ncol(data.sub)==2) w0 <- 1;
22
23
    llk.full <- calc.llk.tree(pars, data.sub);
24
25
    q00 <- pars$p.stay + pars$p.switch * (1-pars$pi1);
26
    lq00 <- log(q00);
27
    n.trans <- nrow(data.sub)-1;
28
    l.p.null <- log(1-pars$pi1)+n.trans*lq00;
29
    l.lk.null <- sum(data.sub[,w0]);
30
    tmp <- c(llk.full, l.p.null+l.lk.null);
31
    mx <- max(tmp);
32
    tmp <- exp(tmp-mx);
33
    lBF <- mx+log(tmp[1]-tmp[2])-l.lk.null-log(1-exp(l.p.null));
34
    if (log10) lBF <- lBF/log(10);
35
    
36
    return(lBF);
37
}
38