Learn R Programming

BayesianFirstAid (version 0.1)

bayes.t.test: Bayesian First Aid alternative to the t-test

Description

bayes.t.test estimates the mean of one group, or the difference in means between two groups, using Bayesian estimation and is intended as a replacement for t.test. Is based on Bayesian Estimation Supersedes the t-test (BEST) (Kruschke, 2012).

Usage

bayes.t.test(x, ...)
"bayes.t.test"(x, y = NULL, alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, var.equal = FALSE, cred.mass = 0.95, n.iter = 30000, progress.bar = "text", conf.level, ...)
"bayes.t.test"(formula, data, subset, na.action, ...)

Arguments

x
a (non-empty) numeric vector of data values.
y
an optional (non-empty) numeric vector of data values.
alternative
ignored and is only retained in order to mantain compatibility with t.test.
mu
a fixed relative mean value to compare with the estimated mean (or the difference in means when performing a two sample BEST).
paired
a logical indicating whether you want to estimate a paired samples BEST.
var.equal
ignored and is only retained in order to mantain compatibility with t.test.
cred.mass
the amount of probability mass that will be contained in reported credible intervals. This argument fills a similar role as conf.level in t.test.
n.iter
The number of iterations to run the MCMC sampling.
progress.bar
The type of progress bar. Possible values are "text", "gui", and "none".
conf.level
same as cred.mass and is only retained in order to mantain compatibility with binom.test.
formula
a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs a factor with two levels giving the corresponding groups.
data
an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).
subset
an optional vector specifying a subset of observations to be used.
na.action
a function which indicates what should happen when the data contain NAs. Defaults to getOption("na.action").
...
further arguments to be passed to or from methods.

Value

A list of class bayes_paired_t_test, bayes_one_sample_t_test or bayes_two_sample_t_test that contains information about the analysis. It can be further inspected using the functions summary, plot, diagnostics and model.code.

Details

As with the t.test function bayes.t.test estimates one of three models depending on the arguments given. All three models are based on the Bayesian Estimation Supersedes the t test (BEST) model developed by Kruschke (2013).

If one vecor is supplied a one sample BEST is run. BEST assumes the data ($x$) is distributed as a t distribution, a more robust alternative to the normal distribution due to its wider tails. Except for the mean ($\mu$) and the scale ($\sigma$) the t has one additional parameter, the degree-of-freedoms ($\nu$), where the lower $\nu$ is the wider the tails become. When $\nu$ gets larger the t distribution approaches the normal distribution. While it would be possible to fix $\nu$ to a single value BEST instead estimates $\nu$ allowing the t-distribution to become more or less normal depending on the data. Here is the full model for the one sample BEST:

$$x_i \sim \mathrm{t}(\mu, \sigma, \nu)$$ $$\mu \sim \mathrm{Normal}(M_\mu, S_\mu)$$ $$\sigma \sim \mathrm{Uniform}(L_\sigma, H__\sigma)$$ $$\nu \sim \mathrm{Shifted-Exp}(1/29, \mathrm{shift}=1)$$

one_sample_best_diagram.pngA graphical diagram of the one sample the BEST model

The constants $M[\mu], S[\mu], L[\sigma]$ and $H[\sigma]$ are set so that the priors on $\mu$ and $\sigma$ are essentially flat.

If two vectors are supplied a two sample BEST is run. This is essentially the same as estimaiting two separate one sample BEST except for that both groups are assumed to have the same $\nu$. Here is a Kruschke style diagram showing the two sample BEST model:

two_sample_best_diagram.pngA graphical diagram of the two sample the BEST model

If two vectors are supplied and paired=TRUE then the paired difference between x - y is modeled using the one sample BEST.

References

Kruschke, J. K. (2013). Bayesian estimation supersedes the t test. Journal of Experimental Psychology: General, 142(2), 573.

Examples

Run this code
# Using Student's sleep data as in the t.test example
# bayes.t.test can be called in the same way as t.test
# so you can both supply two vectors...

bayes.t.test(sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2])

# ... or use the formula interface.

bayes.t.test(extra ~ group, data = sleep)

# Save the return value in order to inspect the model result further.
fit <- bayes.t.test(extra ~ group, data = sleep)
summary(fit)
plot(fit)

# MCMC diagnostics
diagnostics(fit)

# Print out the R code to run the model. This can be copy n' pasted into
# an R-script and further modified.
model.code(fit)

Run the code above in your browser using DataLab