https://github.com/cran/bayestestR
Raw File
Tip revision: fe07bfa906d7e155439160caee538a3449cd3877 authored by Dominique Makowski on 08 April 2019, 08:42:41 UTC
version 0.1.0
Tip revision: fe07bfa
utils_density_at.R
#' Probability of a Given Point
#'
#' Compute the density of a given point of a distribution.
#'
#' @param posterior Vector representing a posterior distribution.
#' @param x The value of which to get the approximate probability.
#' @param precision Number of points for density estimation. See the \code{n} parameter in \link[=density]{density}.
#'
#' @examples
#' library(bayestestR)
#' posterior <- rnorm_perfect(n = 10)
#' density_at(posterior, 0)
#' density_at(posterior, c(0, 1))
#' @importFrom stats approx density
#' @export
density_at <- function(posterior, x, precision = 2^10) {
  density <- stats::density(posterior, n = precision)
  stats::approx(density$x, density$y, xout = x)$y
}
back to top