Created
April 13, 2017 10:46
-
-
Save smathot/68ecc023973b6a08e92163a80bc228f2 to your computer and use it in GitHub Desktop.
A script to calculate the Baws Factor from the JASP model comparison tabel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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