Learn R Programming

extras

extras provides helper functions for Bayesian analyses.

In particular it provides functions to summarise vectors of MCMC (Monte Carlo Markov Chain) samples, draw random samples from various distributions and calculate deviance residuals as well as R translations of some BUGS (Bayesian Using Gibbs Sampling), JAGS (Just Another Gibbs Sampler), STAN and TMB (Template Model Builder) functions.

Demonstration

Summarise MCMC Samples

The extras package provides functions to summarise MCMC samples like svalue() which gives the surprisal value (Greenland, 2019)

library(extras)
#> 
#> Attaching package: 'extras'
#> The following object is masked from 'package:stats':
#> 
#>     step

set.seed(1)
x <- rnorm(100)
svalue(rnorm(100))
#> [1] 0.3183615
svalue(rnorm(100, mean = 1))
#> [1] 1.704015
svalue(rnorm(100, mean = 2))
#> [1] 3.850857
svalue(rnorm(100, mean = 3))
#> [1] 5.073249

Distributions

Implemented distributions with functions to draw random samples, calculate log-likelihoods, and calculate deviance residuals for include:

  • Bernoulli
  • Binomial
  • Beta-binomial
  • Gamma
  • Gamma-Poisson
  • Zero-inflated gamma-Poisson
  • Log-Normal
  • Negative Binomial
  • Normal
  • Poisson
  • Zero-inflated Poisson
  • Skew Normal
  • Student’s t

R translations

The package also provides R translations of BUGS (and JAGS) functions such as pow() and log<-.

pow(10, 2)
#> [1] 100

mu <- NULL
log(mu) <- 1
mu
#> [1] 2.718282

Numericise R Objects

Atomic vectors, matrices, arrays and data.frames of appropriate classes can be converted to numeric objects suitable for Bayesian analysis using the numericise() (and numericize()) function.

numericise(
  data.frame(
    logical = c(TRUE, FALSE),
    factor = factor(c("blue", "green")),
    Date = as.Date(c("2000-01-01", "2000-01-02")),
    hms = hms::as_hms(c("00:00:02", "00:01:01"))
  )
)
#>      logical factor  Date hms
#> [1,]       1      1 10957   2
#> [2,]       0      2 10958  61

Installation

Information

For more information see the Get Started vignette.

Installation

Release

To install the release version from CRAN.

install.packages("extras")

The website for the release version is at https://poissonconsulting.github.io/extras/.

Development

To install the development version from GitHub

# install.packages("remotes")
remotes::install_github("poissonconsulting/extras")

or from r-universe.

install.packages("extras", repos = c("https://poissonconsulting.r-universe.dev", "https://cloud.r-project.org"))

References

Greenland, S. 2019. Valid P-Values Behave Exactly as They Should: Some Misleading Criticisms of P-Values and Their Resolution With S-Values. The American Statistician 73(sup1): 106–114.

Contribution

Please report any issues.

Pull requests are always welcome.

Code of Conduct

Please note that the extras project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('extras')

Monthly Downloads

432

Version

0.8.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Nicole Hill

Last Published

January 13th, 2025

Functions in extras (0.8.0)

dev_pois_zi

Zero-Inflated Poisson Deviances
as_list

As List
dev_skewnorm

Skew Normal Deviances
as_list_unnamed

As List
dev_bern

Bernoulli Deviances
dbern

Bernoulli Distribution
dev_binom

Binomial Deviances
dev_beta_binom

Beta-Binomial Deviances
dev_gamma_pois

Gamma-Poisson Deviances
chk_index

Check Index
dev_gamma_pois_zi

Zero-Inflated Gamma-Poisson Deviances
dev_gamma

Gamma Deviances
dev_lnorm

Log-Normal Deviances
dev_norm

Normal Deviances
dev_student

Student's t Deviances
dev_neg_binom

Negative Binomial Deviances
chk_indices

Check Indices
exp10

Exponential Transformation of Base 10
ilog10

Inverse Log Base 10 Transformation
exp2

Exponential Transformation of Base 2
dev_pois

Poisson Deviances
chk_pars

Check Parameter Names
ilogit

Inverse Logistic Transformation
fabs

Absolute
inv_logit

Inverse Logistic Transformation
ilog2

Inverse Log Base 2 Transformation
extras-package

extras: Helper Functions for Bayesian Analyses
kurtosis

Kurtosis
log<-

Log Transformation
log10<-

Log Base 10 Transformation
log_lik_binom

Binomial Log-Likelihood
log_lik_gamma_pois

Gamma-Poisson Log-Likelihood
log2<-

Log Base 2 Transformation
log_lik_pois_zi

Zero-Inflated Poisson Log-Likelihood
log_lik_skewnorm

Skew Normal Log-Likelihood
log_odds

Log Odds
log_lik_gamma

Gamma Log-Likelihood
log_odds_ratio

Log-Odds Ratio
fill_all

Fill All Values
odds_ratio2

Odds Ratio2
log_lik_norm

Normal Log-Likelihood
inv_odds

Inverse Odds
log_lik_gamma_pois_zi

Zero-Inflated Gamma-Poisson Log-Likelihood
ran_binom

Binomial Random Samples
log_lik_lnorm

Log-Normal Log-Likelihood
invlogit

Inverse Logistic Transformation
fill_na

Fill Missing Values
numericise

Numericise (or Numericize)
ran_gamma

Gamma Random Samples
ran_student

Student's t Random Samples
par_pattern

Parameter Pattern
res_neg_binom

Negative Binomial Residuals
res_bern

Bernoulli Residuals
res_norm

Normal Residuals
sens_pois

Adjust Poisson Distribution Parameters for Sensitivity Analyses
log_lik_pois

Poisson Log-Likelihood
log_lik_neg_binom

Negative Binomial Log-Likelihood
sens_student

Adjust Student's t Distribution Parameters for Sensitivity Analyses
sens_skewnorm

Adjust Skew Normal Distribution Parameters for Sensitivity Analyses
lower

Lower Credible Limit
logit

Logistic Transformation
odds<-

Inverse Odds Transformation
upper

Upper Credible Limit
sextreme

Extreme Surprisal
log_odds_ratio2

Log Odds Ratio2
variance

Variance
proportional_change

Proportional Change
ilog

Inverse Log Transformation
log_lik_bern

Bernoulli Log-Likelihood
log_lik_beta_binom

Beta-Binomial Log-Likelihood
logit<-

Logistic Transformation
params

Parameter Descriptions
pextreme

Extreme Probability
proportional_change2

Proportional Change2
ran_neg_binom

Negative Binomial Random Samples
ran_lnorm

Log-Normal Random Samples
ran_gamma_pois

Gamma-Poisson Random Samples
ran_gamma_pois_zi

Zero-Inflated Gamma-Poisson Random Samples
ran_pois_zi

Zero-Inflated Poisson Random Samples
log_lik_student

Student's t Log-Likelihood
log_odds<-

Inverse Log Odds Transformation
odds

Odds
ran_skewnorm

Skew Normal Random Samples
res_gamma_pois_zi

Zero-Inflated Gamma-Poisson Residuals
res_lnorm

Log-Normal Residuals
res_gamma

Gamma Residuals
phi

Phi
pow

Power
sens_neg_binom

Adjust Negative Binomial Distribution Parameters for Sensitivity Analyses
sens_norm

Adjust Normal Distribution Parameters for Sensitivity Analyses
res_gamma_pois

Gamma-Poisson Residuals
res_skewnorm

Skew Normal Residuals
odds_ratio

Odds Ratio
ran_bern

Bernoulli Random Samples
ran_beta_binom

Beta-Binomial Random Samples
res_student

Student's t Residuals
xtr_mean

Mean
proportional_difference

Proportional Difference
sens_gamma

Adjust Gamma Distribution Parameters for Sensitivity Analyses
sens_gamma_pois

Adjust Gamma-Poisson Distribution Parameters for Sensitivity Analyses
skewness

Skewness
ran_norm

Normal Random Samples
ran_pois

Poisson Random Samples
dskewnorm

Skew-Normal Distribution
proportional_difference2

Proportional Difference2
xtr_median

Median
res_pois

Poisson Residuals
res_pois_zi

Zero-Inflated Poisson Residuals
pvalue

Bayesian P-Value
sens_lnorm

Adjust Log-Normal Distribution Parameters for Sensitivity Analysis
sens_gamma_pois_zi

Adjust Zero-Inflated Gamma-Poisson Distribution Parameters for Sensitivity Analyses
step

Step
pzeros

Proportion of Zeros
svalue

Surprisal Value
res_beta_binom

Beta-Binomial Residuals
res_binom

Binomial Residuals
sens_beta

Adjust Beta Distribution Parameters for Sensitivity Analyses
sens_exp

Adjust Exponential Distribution Parameters for Sensitivity Analyses
zeros

Zeros
xtr_sd

Standard Deviation
zscore

Z-Score