R/BSLR.R

Defines functions BSLR

Documented in BSLR

#' 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))
}
statwangz/SuSiE documentation built on Nov. 22, 2022, 5:21 p.m.