Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to calculate the Baws Factor from the JASP model comparison tabel.
#!/usr/bin/env python3
# coding=utf-8
import sys
from datamatrix import io, DataMatrix
INPUTCSV = sys.argv[-1]
clean_model = lambda model: \
'null' if model.startswith('Null model') \
else model.replace('  ✻  ', '*').strip()
clean_posterior = lambda posterior: \
posterior if not isinstance(posterior, str) \
else float(posterior.replace('\u2009','').replace(' ',''))
clean_datamatrix = lambda dm: \
[
setattr(dm, 'Model', list(map(clean_model, dm.Model))),
setattr(dm, 'Posterior', list(map(clean_posterior, dm.Posterior))),
dm
][-1]
get_terms = lambda dm: \
dm.Model[-1].split(' + ')
interaction_in_term = lambda term, complexterm: \
term != complexterm \
and set(term.split('*')).issubset(set(complexterm.split('*')))
interaction_in_model = lambda term, model: \
any(interaction_in_term(term, complexterm) \
for complexterm in model.split(' + '))
models_with_term = lambda models, term: \
[model for model in models \
if term in model and not interaction_in_model(term, model)]
strip_term_from_model = lambda model, term: \
'null' if model == term else \
model.replace(' + ' + term, '').replace(term+' + ', '').strip()
models_without_term = lambda models, term: \
[strip_term_from_model(model, term) \
for model in models_with_term(models, term)]
posteriors = lambda dm, models: \
[row.Posterior for row in dm if row.Model in models]
posteriors_with_term = lambda dm, term: \
posteriors(dm, models_with_term(dm.Model, term))
posteriors_without_term = lambda dm, term: \
posteriors(dm, models_without_term(dm.Model, term))
dm = clean_datamatrix(io.readtxt(INPUTCSV))
terms = get_terms(dm)
bm = DataMatrix(length=len(terms),
Term=None, Nmodel=None, SumPwith=None, SumPwithout=None, BawsFactor=None)
for term in terms:
print('Term: %s\n' % term)
print(' Included models:')
for modelwith, modelwithout in sorted(zip(models_with_term(dm.Model, term),
models_without_term(dm.Model, term))):
print(' + %s' % modelwith)
print(' - %s' % modelwithout)
print()
print(' Excluded models:')
e = set(dm.Model) - \
(set(models_with_term(dm.Model, term)) \
| set(models_without_term(dm.Model, term)))
for m in sorted(e):
print(' %s' % m)
print()
for term, row in zip(terms, bm):
posteriorswith = posteriors_with_term(dm, term)
posteriorswithout = posteriors_without_term(dm, term)
row.Term = term
row.Nmodel = len(posteriorswith)
row.SumPwith = sum(posteriorswith)
row.SumPwithout = sum(posteriorswithout)
row.BawsFactor = sum(posteriorswith)/sum(posteriorswithout)
if row.BawsFactor < 1000:
row.BawsFactor = '%E' % row.BawsFactor
print(bm)
io.writetxt(bm, 'bawsfactor.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.