testFunction_SDDR: Test Function: Savage-Dickey Density Ratio

View source: R/TestFunctions.R

testFunction_SDDRR Documentation

Test Function: Savage-Dickey Density Ratio

Description

Takes matrices of prior and posterior samples the effect parameters. Calculates a deviance measure, D, for the prior and posterior effect parameters. A deviance value of 0 happens if there is no effect. Thus, the tested hypotheses are H_0: D = 0 and H_1: D =/= 0. Returns Bayes factors related to both the null and alternative hypotheses. See the "Deviance Measure" and "Bayes Factor Estimation with Savage-Dickey" sections of the manual for more information about this procedure.

Usage

testFunction_SDDR(priorEffects, postEffects, devianceFunction = NULL,
  postMaxMult = 10, min_pKept = 0.9, truncatePosterior = TRUE,
  warnOnLength = TRUE)

Arguments

priorEffects

Numeric matrix of effect parameters sampled from the priors. Each column is one parameter and each row is one iteration. It must have at least two columns.

postEffects

Numeric matrix of effect parameters sampled from the posteriors. Must have the same number of columns as priorEffects. Mathematically, this procedure works even with different numbers of columns, but such a test would be bizarre and meaningless (some parameters didn't have priors?). If the number of columns is not the same, an error will be emitted.

devianceFunction

A function used for calculating the deviation of the effect parameters, D. It takes a vector of effect parameters and calculates some measure of how dispersed they are. One example of such a function is the sample variance (see stats::var).

postMaxMult

The prior is cut off above postMaxMult * max(post_D), where post_D is the posterior deviance measure. See also min_pKept.

min_pKept

The minimum proportion of the prior that will be kept. Overrides postMaxMult if the postMaxMult rule would keep less than min_pKept of the prior. This rule can be disabled if min_pKept is set to NULL or 1.

truncatePosterior

If TRUE, the posterior will be truncated to the same proportion as the prior. This is generally a good thing as it further equates the treatment of the prior and the posterior and keeps the number of prior and posterior samples the same. If FALSE, the posterior will not be altered.

warnOnLength

If TRUE and the prior and posterior are not the same length, a warning will be emitted.

Details

Due to a limitation of the density estimation procedure, the matrices of samples fom the prior and posterior should have the same number of rows. You will get a warning about this if they are not the same length. Given that sampling from the prior is usually relatively easy, once you have samples from the posterior, sample the same amount from the prior.

It is assumed that the prior may be diffuse, such as a Cauchy prior. In that case, it is possible to have prior values of D that are very far from the largest posterior value (like 10^9 times farther). This hurts the ability of the density estimation to estimate the density in the same way for the prior and posterior, which could result in a bias. This function accounts for unusually large prior D values by only using prior D at most 10 times larger than the largest posterior D.

Note that if this function is failing with errors from the density estimation procedure, you should consider changing the defaults for postMaxMult and min_pKept. It is possible to have cases where the prior is about as diffuse as the posterior, except for extreme values of D. In that case, using values from the prior substantialy larger than the median of the prior can result in the left edge of the prior being very compacted and the density estimation can't deal well with that. You can change the default values with a curried function that then gets passed to, e.g., testHypothesis. See the examples.

Value

A list with four elements:

  • success: A boolean indicating whether there was an exception during density estimation. If success is FALSE, all other values will be NULL or NA.

  • bf01: The Bayes factor in favor of H0.

  • bf10: The Bayes factor in favor of H1.

  • pKept: The proportion of the prior distribution that was used to estimate the density;

Examples

## Not run: 
curriedTestFunction = function(priorEffects, postEffects) { 
  testFunction_SDDR(priorEffects, postEffects, 
    devianceFunction = sd, postMaxMult = 1.5, min_pKept = 0.95)
}

testHypothesis(..., testFunction = curriedTestFunction)

## End(Not run)

hardmanko/CMBBHT documentation built on June 9, 2022, 12:44 a.m.