Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created April 5, 2017 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarddmorey/7c1bd06a14384412f2145daee315c036 to your computer and use it in GitHub Desktop.
Save richarddmorey/7c1bd06a14384412f2145daee315c036 to your computer and use it in GitHub Desktop.
One-tailed testing with lmBF
## For the singer dataset
library(lattice)
data(singer)
## Get data ready (recode to two factors)
singer$female = factor(with(singer, grepl("S",voice.part) | grepl("A",voice.part)))
singer$high = factor(with(singer, grepl("S",voice.part) | grepl("T",voice.part)))
## We will test the hypothesis that the main effect
## of "high voice" is such that high voiced singers are shorter
## Compute BF10
library(BayesFactor)
bf = lmBF(height ~ female*high, data = singer)
## Convert to scalar
BF10 = as.vector(bf)
## Sample posterior
chains = posterior(bf, iterations = 10000)
## Compute posterior probability of the desired tail
## which is to test the that the main effect
## of "high voice" goes in one direction
## (low voice people are taller)
post.prob = mean(chains[,"high-FALSE"]>0)
## Compute the prior probability of this restriction
prior.prob = .5
## Compute BFp1 (the Bayes factor of the restriction)
## against the full, unrestricted model
BFp1 = post.prob / prior.prob
## Use the transitivity of the Bayes factor to
## compute the one-tailed BFp0
BFp0 = BFp1 * BF10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment