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.

Installation

To install the developmental version from GitHub

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

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
  • 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

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

478

Version

0.7.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

August 27th, 2024

Functions in extras (0.7.3)

dev_norm

Normal Deviances
exp2

Exponential Transformation of Base 2
dev_pois_zi

Zero-Inflated Poisson Deviances
dev_skewnorm

Skew Normal Deviances
fill_na

Fill Missing Values
dev_pois

Poisson Deviances
inv_logit

Inverse Logistic Transformation
log_lik_beta_binom

Beta-Binomial Log-Likelihood
dev_student

Student's t Deviances
fabs

Absolute
ilogit

Inverse Logistic Transformation
log_lik_bern

Bernoulli Log-Likelihood
ilog

Inverse Log Transformation
log_lik_gamma

Gamma Log-Likelihood
inv_odds

Inverse Odds
fill_all

Fill All Values
ilog2

Inverse Log Base 2 Transformation
log_lik_gamma_pois

Gamma-Poisson Log-Likelihood
log_lik_binom

Binomial Log-Likelihood
log_lik_lnorm

Log-Normal Log-Likelihood
log_lik_gamma_pois_zi

Zero-Inflated Gamma-Poisson Log-Likelihood
log_lik_norm

Normal Log-Likelihood
invlogit

Inverse Logistic Transformation
ilog10

Inverse Log Base 10 Transformation
log_lik_pois

Poisson Log-Likelihood
lower

Lower Credible Limit
logit

Logistic Transformation
log_lik_skewnorm

Skew Normal Log-Likelihood
log_lik_pois_zi

Zero-Inflated Poisson Log-Likelihood
log_odds_ratio2

Log Odds Ratio2
log10<-

Log Base 10 Transformation
extras-package

extras: Helper Functions for Bayesian Analyses
log2<-

Log Base 2 Transformation
odds_ratio

Odds Ratio
proportional_change

Proportional Change
ran_gamma_pois

Gamma-Poisson Random Samples
logit<-

Logistic Transformation
exp10

Exponential Transformation of Base 10
odds

Odds
proportional_change2

Proportional Change2
proportional_difference

Proportional Difference
ran_gamma_pois_zi

Zero-Inflated Gamma-Poisson Random Samples
kurtosis

Kurtosis
log_lik_student

Student's t Log-Likelihood
log_odds<-

Inverse Log Odds Transformation
log_odds

Log Odds
log_odds_ratio

Log-Odds Ratio
log<-

Log Transformation
numericise

Numericise (or Numericize)
log_lik_neg_binom

Negative Binomial Log-Likelihood
pextreme

Extreme Probability
params

Parameter Descriptions
par_pattern

Parameter Pattern
odds_ratio2

Odds Ratio2
proportional_difference2

Proportional Difference2
ran_bern

Bernoulli Random Samples
ran_lnorm

Log-Normal Random Samples
ran_gamma

Gamma Random Samples
res_gamma

Gamma Residuals
ran_binom

Binomial Random Samples
ran_norm

Normal Random Samples
pvalue

Bayesian P-Value
pzeros

Proportion of Zeros
odds<-

Inverse Odds Transformation
phi

Phi
ran_pois

Poisson Random Samples
pow

Power
res_pois

Poisson Residuals
ran_beta_binom

Beta-Binomial Random Samples
res_pois_zi

Zero-Inflated Poisson Residuals
ran_neg_binom

Negative Binomial Random Samples
sens_gamma_pois_zi

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

Gamma-Poisson Residuals
res_neg_binom

Negative Binomial Residuals
sens_lnorm

Adjust Log-Normal Distribution Parameters for Sensitivity Analysis
xtr_sd

Standard Deviation
zeros

Zeros
sextreme

Extreme Surprisal
sens_student

Adjust Student's t Distribution Parameters for Sensitivity Analyses
ran_pois_zi

Zero-Inflated Poisson Random Samples
sens_exp

Adjust Exponential Distribution Parameters for Sensitivity Analyses
ran_skewnorm

Skew Normal Random Samples
step

Step
sens_beta

Adjust Beta Distribution Parameters for Sensitivity Analyses
svalue

Surprisal Value
res_bern

Bernoulli Residuals
sens_norm

Adjust Normal Distribution Parameters for Sensitivity Analyses
res_student

Student's t Residuals
ran_student

Student's t Random Samples
sens_neg_binom

Adjust Negative Binomial Distribution Parameters for Sensitivity Analyses
res_norm

Normal Residuals
res_skewnorm

Skew Normal Residuals
sens_pois

Adjust Poisson Distribution Parameters for Sensitivity Analyses
xtr_mean

Mean
sens_skewnorm

Adjust Skew Normal Distribution Parameters for Sensitivity Analyses
upper

Upper Credible Limit
res_beta_binom

Beta-Binomial Residuals
xtr_median

Median
zscore

Z-Score
variance

Variance
res_lnorm

Log-Normal Residuals
sens_gamma

Adjust Gamma Distribution Parameters for Sensitivity Analyses
res_gamma_pois_zi

Zero-Inflated Gamma-Poisson Residuals
sens_gamma_pois

Adjust Gamma-Poisson Distribution Parameters for Sensitivity Analyses
res_binom

Binomial Residuals
skewness

Skewness
dskewnorm

Skew-Normal Distribution
dbern

Bernoulli Distribution
chk_pars

Check Parameter Names
chk_indices

Check Indices
dev_bern

Bernoulli Deviances
dev_beta_binom

Beta-Binomial Deviances
chk_index

Check Index
dev_binom

Binomial Deviances
as_list

As List
dev_gamma

Gamma Deviances
as_list_unnamed

As List
dev_gamma_pois

Gamma-Poisson Deviances
dev_gamma_pois_zi

Zero-Inflated Gamma-Poisson Deviances
dev_lnorm

Log-Normal Deviances
dev_neg_binom

Negative Binomial Deviances