#' Bayesian simple linear regression
#'
#' @param x variable (SNP), n*1 vector.
#' @param y trait, n*1 vector.
#' @param sig2 hyperparameter, the variance of the trait y.
#' @param sig02 hyperparameter, the variance of the coefficient.
#'
#' @return Bayes factor (log scale), the estimate of the coefficient, the posterior mean of the coefficient, the posterior variance of the coefficient.
#' @export
BSLR <- function(x, y, sig2 = 1, sig02 = 0.1) {
b_hat <- sum(x * y) / sum(x^2)
s2 <- sig2 / sum(x^2)
z <- b_hat / sqrt(s2)
sig12 <- 1 / (1 / s2 + 1 / sig02)
mu1 <- sig12 * b_hat / s2
log_bf <- 0.5 * log(s2 / (sig02 + s2)) + 0.5 * z^2 * sig02 / (sig02 + s2)
return(bslr = c(log_bf = log_bf, b = b_hat, mu1 = mu1, sig12 = sig12))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.