-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelling.py
executable file
·53 lines (40 loc) · 1.22 KB
/
modelling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import scipy as sp
import scipy.stats
import matplotlib.pyplot as plt
import pymc3 as pm
import theano.tensor as tt
import theano
import bambi
import arviz as az
import pandas as pd
import pickle
import gzip
import itertools as it
import statsmodels as sm
import logging
import pickle
from fastprogress.fastprogress import progress_bar
def build_bambi_model(df, depvar, family, factors):
"""
Construct a Bambi model that has factors as fixed factors, as well as subject-level
random factors, as well as item-level random factors (i.e., this function always
constructs a model with the full random structure afforded by the design.
"""
model = bambi.Model(df)
model.add(factors)
model.add(random=[factors + '|subnum'], categorical=['subnum'])
model.add(random=[factors + '|item'], categorical=['item'])
model.add(depvar + ' ~ 0', family=family)
model.build('pymc')
return model
def sample_and_get_results(model):
results = model.fit(
samples=3000, chains=4, tune=6000, target_accept=0.9,
init='advi+adapt_diag', n_init=35000
)
data = az.from_pymc3(
model=model.backend.model,
trace=model.backend.trace
)
return data