# How to fit multiple independent slopes and intercepts with the GLM module?

Hi pymc3ers,

I have the following issue: In the past I explicitly fitted logistic functions to some psychometric data. However, I now want to give the GLM module a try, mainly because it so nice an compact to use. In general it works as expected. However, my experimental data has two (or more) conditions for which I would like to fit individual slopes and intercepts. With the explicit logistic model I used an condition index vector and parameters of the desired shape. But I can’t figure out how to do something like this with the GLM module. I can split the data and add two GLM formulae (adding prefixes to the autogerated parameter names), but this gives me trouble when I later want to produce a single WAIC or loo score for the fit.

Any suggestions are highly welcome!

Many thanks and best regards
Jan

Any ideas on how to approach this? Or examples? Secret documentation of the GLM module? If you coded the linear equation with interaction between group and slope, it should gives you what you want: `y = x1 + x2 + x1:x2`

Thanks, that works. But how can I transform the estimated parameters into intercepts and slopes for the two individual curves (of the two conditions)?

So I fitted:

group + slope + group : slope

And now I would like to turn these components into:

group1_slope, group1_intercept,
group2_slope, group2_intercept,

It’s probably very obvious but I guess I’m missing something …

Thanks a lot!

I would get the design matrix using patsy: https://patsy.readthedocs.io/en/latest/API-reference.html#patsy.dmatrix (which GLM does internally), and look at the the rows for group1 and group2.
If I remember correctly, the design matrix from patsy is additive, which means you have:

``````group1_intercept = intercept
group2_intercept = intercept + group_beta
group1_slope = slope_beta
group2_slope = slope_beta + group_slope_interaction_beta
``````

Thanks for the suggestion. It seems to be as you remember. I didn’t know how/where to look at the design matrix. But what you say matches exactly to the values I get by setting up two separate GLMs for the conditions.

1 Like

There is also a bit more explanation here: https://patsy.readthedocs.io/en/latest/categorical-coding.html

1 Like

In case someone needs this …

Adding these deterministic vars got me the parameters I wanted:

``````    a = pm.Deterministic('a', tt.stack((model['Intercept'],
model['Intercept']+model['Condition'])))
b = pm.Deterministic('b', tt.stack((model['X'],
model['X']+model['X:Condition'])))
``````

With Condition being 0 or 1 and X the different levels …
a, a are then the intercepts and b, b the slopes for each condition.

Since the GLM module sets up everything, we don’t have direct references to the variables as we would if we manually set up the model. But it turns out they can be easily obtained by keying the model object (model in my example above) with the (auto-generated) parameter names. Didn’t know that … very convenient …