R/fct_calculation_helpers.R

Defines functions smoothie mode_HDI logit2prob

Documented in logit2prob mode_HDI smoothie

#' Convert logit to probability
#' 
#' This is used for conversion of the results of the
#' logistic regression to the probability scale.
#' 
#' @param logit numeric, the result of s logistic regression
#' 
#' @export
logit2prob <- function(logit) {
  odds <- exp(logit)
  prob <- odds / (1 + odds)
  return(prob)
}

#' Function calculating the highest density interval using sampling
#' 
#' We use \code{\link[HDInterval]{hdi}} function in our
#' analysis. The present helper function is needed for
#' the Bayesian parameter estimation robustness test.
#' 
#' @param scale numeric, scale of the sample
#' @param density numeric, density
#' @param crit_width numeric, the critical width
#' @param n_samples numeric, number of iteration
#' 
#' @export
mode_HDI <- function(scale, density, crit_width = 0.95, n_samples = 1e5){
  samp <- sample(x = scale, size = n_samples, replace = TRUE, prob = density)
  hdi_result = HDInterval::hdi(samp, credMass = crit_width)
  result = c(scale[which(density == max(density))], # mode
             hdi_result[1], # lower bound
             hdi_result[2]) # upper bound
  
  # Only needed for the names of the result
  Crit_lb = (1 - crit_width) / 2
  Crit_ub = crit_width + (1 - crit_width) / 2
  
  names(result) = c("mode", paste0(Crit_lb * 100, "%"), paste0(Crit_ub * 100, "%"))
  return(result)
}

#' Function to fit smoothing spline
#' 
#' For the confirmatory analysis plot the cumulative
#' Bayes factors calculated with three priors needs
#' to be smoothed to ease understanding.
#' 
#' @param x numeric, the x param
#' @param y numeric, the y param
#' 
#' @export
smoothie <- function(x, y) {
  fit <- smooth.spline(x, y, df = 80)
  
  predict(fit)$y
}
marton-balazs-kovacs/tppr documentation built on Oct. 27, 2021, 3:04 p.m.