Multiple Regression

[1]:
import arviz as az
import bambi as bmb
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pymc3 as pm

import statsmodels.api as sm
[2]:
az.style.use('arviz-darkgrid')
np.random.seed(1111)

Load and examine Eugene-Springfield community sample data

[3]:
data = pd.read_csv('data/ESCS.csv')
np.round(data.describe(), 2)
[3]:
drugs n e o a c hones emoti extra agree consc openn
count 604.00 604.00 604.00 604.00 604.00 604.00 604.00 604.00 604.00 604.00 604.00 604.00
mean 2.21 80.04 106.52 113.87 124.63 124.23 3.89 3.18 3.21 3.13 3.57 3.41
std 0.65 23.21 19.88 21.12 16.67 18.69 0.45 0.46 0.53 0.47 0.44 0.52
min 1.00 23.00 42.00 51.00 63.00 44.00 2.56 1.47 1.62 1.59 2.00 1.28
25% 1.71 65.75 93.00 101.00 115.00 113.00 3.59 2.88 2.84 2.84 3.31 3.06
50% 2.14 76.00 107.00 112.00 126.00 125.00 3.88 3.19 3.22 3.16 3.56 3.44
75% 2.64 93.00 120.00 129.00 136.00 136.00 4.20 3.47 3.56 3.44 3.84 3.75
max 4.29 163.00 158.00 174.00 171.00 180.00 4.94 4.62 4.75 4.44 4.75 4.72

It’s always a good idea to start off with some basic plotting. Here’s what our outcome variable drugs (some index of self-reported illegal drug use) looks like:

[4]:
data['drugs'].hist();
../_images/notebooks_ESCS_multiple_regression_6_0.png

The five numerical predictors that we’ll use are sum-scores measuring participants’ standings on the Big Five personality dimensions. The dimensions are:

  • O = Openness to experience

  • C = Conscientiousness

  • E = Extraversion

  • A = Agreeableness

  • N = Neuroticism

Here’s what our predictors look like:

[5]:
az.plot_pair(data[['o','c','e','a','n']].to_dict("list"), marginals=True, textsize=24);
../_images/notebooks_ESCS_multiple_regression_8_0.png

We can easily see all the predictors are more or less symmetrically distributed without outliers and the pairwise correlations between them are not strong.

Specify model and examine priors

We’re going to fit a pretty straightforward additive multiple regression model predicting drug index from all 5 personality dimension scores. It’s simple to specify the model using a familiar formula interface. Here we also tell Bambi to run two parallel Markov Chain Monte Carlo (MCMC) chains, each one with 2000 draws. The first 1000 draws are tuning steps that we discard and the last 1000 draws are considered to be taken from the joint posterior distribution of all the parameters (to be confirmed when we analyze the convergence of the chains).

[6]:
model = bmb.Model(data)
fitted = model.fit('drugs ~ o + c + e + a + n', draws=1000, tune=1000, init='adapt_diag')
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [drugs_sigma, n, a, e, c, o, Intercept]
100.00% [4000/4000 00:21<00:00 Sampling 2 chains, 0 divergences]
Sampling 2 chains for 1_000 tune and 1_000 draw iterations (2_000 + 2_000 draws total) took 23 seconds.

Great! But this is a Bayesian model, right? What about the priors? If no priors are given explicitly by the user, then Bambi chooses smart default priors for all parameters of the model based on the implied partial correlations between the outcome and the predictors. Here’s what the default priors look like in this case – the plots below show 1000 draws from each prior distribution:

[7]:
model.plot_priors();
../_images/notebooks_ESCS_multiple_regression_14_0.png
[8]:
# Normal priors on the coefficients
{x.name:x.prior.args for x in model.terms.values()}
[8]:
{'Intercept': {'mu': array(2.21014664), 'sigma': array(7.49872455)},
 'o': {'mu': array(0), 'sigma': array(0.02706881)},
 'c': {'mu': array(0), 'sigma': array(0.03237049)},
 'e': {'mu': array(0), 'sigma': array(0.02957413)},
 'a': {'mu': array(0), 'sigma': array(0.03183624)},
 'n': {'mu': array(0), 'sigma': array(0.02641989)}}
[9]:
# HalfStudentT prior on the residual standard deviation
model.response.prior.args['sigma'].args
[9]:
{'nu': array(4), 'sigma': array(0.64824148)}

Some more info about the default prior distributions can be found in this technical paper.

Notice the apparently small SDs of the slope priors. This is due to the relative scales of the outcome and the predictors: remember from the plots above that the outcome, drugs, ranges from 1 to about 4, while the predictors all range from about 20 to 180 or so. A one-unit change in any of the predictors – which is a trivial increase on the scale of the predictors – is likely to lead to a very small absolute change in the outcome. Believe it or not, these priors are actually quite wide on the partial correlation scale!

Examine the model results

Let’s start with a pretty picture of the parameter estimates!

[10]:
az.plot_trace(fitted);
../_images/notebooks_ESCS_multiple_regression_21_0.png

The left panels show the marginal posterior distributions for all of the model’s parameters, which summarize the most plausible values of the regression coefficients, given the data we have now observed. These posterior density plots show two overlaid distributions because we ran two MCMC chains. The panels on the right are “trace plots” showing the sampling paths of the two MCMC chains as they wander through the parameter space. If any of these paths exhibited a pattern other than white noise we would be concerned about the convergence of the chains.

A much more succinct (non-graphical) summary of the parameter estimates can be found like so:

[11]:
az.summary(fitted)
[11]:
mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_bulk ess_tail r_hat
Intercept 3.296 0.370 2.560 3.951 0.013 0.009 782.0 802.0 1.0
o 0.006 0.001 0.004 0.009 0.000 0.000 1197.0 1376.0 1.0
c -0.004 0.002 -0.007 -0.001 0.000 0.000 1013.0 1031.0 1.0
e 0.003 0.001 0.001 0.006 0.000 0.000 1111.0 1195.0 1.0
a -0.012 0.001 -0.015 -0.010 0.000 0.000 1523.0 1278.0 1.0
n -0.001 0.001 -0.004 0.001 0.000 0.000 957.0 1129.0 1.0
drugs_sigma 0.592 0.017 0.561 0.624 0.000 0.000 1540.0 1363.0 1.0

When there are multiple MCMC chains, the default summary output includes some basic convergence diagnostic info (the effective MCMC sample sizes and the Gelman-Rubin “R-hat” statistics), although in this case it’s pretty clear from the trace plots above that the chains have converged just fine.

Summarize effects on partial correlation scale

[12]:
samples = fitted.posterior

It turns out that we can convert each regression coefficient into a partial correlation by multiplying it by a constant that depends on (1) the SD of the predictor, (2) the SD of the outcome, and (3) the degree of multicollinearity with the set of other predictors. Two of these statistics are actually already computed and stored in the fitted model object, in a dictionary called dm_statistics (for design matrix statistics), because they are used internally. We will compute the others manually.

Some information about the relationship between linear regression parameters and partial correlation can be found here.

[13]:
# the names of the predictors
varnames = ['o', 'c', 'e', 'a', 'n']

# compute the needed statistics like R-squared when each predictor is response and all the
# other predictors are the predictor

# x_matrix = common effects design matrix (excluding intercept/constant term)
terms = [t for t in model.common_terms.values() if t.name != "Intercept"]
x_matrix = [pd.DataFrame(x.data, columns=x.levels) for x in terms]
x_matrix = pd.concat(x_matrix, axis=1)

dm_statistics = {
    'r2_x': pd.Series(
        {
            x: sm.OLS(
                endog=x_matrix[x],
                exog=sm.add_constant(x_matrix.drop(x, axis=1))
                if "Intercept" in model.term_names
                else x_matrix.drop(x, axis=1),
            )
            .fit()
            .rsquared
            for x in list(x_matrix.columns)
        }
    ),
    'sigma_x': x_matrix.std(),
    'mean_x': x_matrix.mean(axis=0),
}

r2_x = dm_statistics['r2_x']
sd_x = dm_statistics['sigma_x']
r2_y = pd.Series([sm.OLS(endog=data['drugs'],
                         exog=sm.add_constant(data[[p for p in varnames if p != x]])).fit().rsquared
                  for x in varnames], index=varnames)
sd_y = data['drugs'].std()

# compute the products to multiply each slope with to produce the partial correlations
slope_constant = (sd_x[varnames] / sd_y) * ((1 - r2_x[varnames]) / (1 - r2_y)) ** 0.5
slope_constant
[13]:
o    32.392557
c    27.674284
e    30.305117
a    26.113299
n    34.130431
dtype: float64

Now we just multiply each sampled regression coefficient by its corresponding slope_constant to transform it into a sample partial correlation coefficient.

[14]:
pcorr_samples = samples[varnames] * slope_constant

And voilà! We now have a joint posterior distribution for the partial correlation coefficients. Let’s plot the marginal posterior distributions:

[15]:
# Pass the same axes to az.plot_kde to have all the densities in the same plot
_, ax = plt.subplots()
for idx, (k, v) in enumerate(pcorr_samples.items()):
    az.plot_kde(v, label=k, plot_kwargs={'color':f'C{idx}'}, ax=ax)
ax.axvline(x=0, color='k', linestyle='--');
../_images/notebooks_ESCS_multiple_regression_33_0.png

The means of these distributions serve as good point estimates of the partial correlations:

[16]:
pcorr_samples.mean(dim=['chain', 'draw']).squeeze(drop=True)
[16]:
<xarray.Dataset>
Dimensions:  ()
Data variables:
    o        float64 0.1947
    c        float64 -0.1048
    e        float64 0.1041
    a        float64 -0.324
    n        float64 -0.05058

Relative importance: Which predictors have the strongest effects (defined in terms of squared partial correlation?

We just take the square of the partial correlation coefficients, so it’s easy to get posteriors on that scale too:

[17]:
_, ax = plt.subplots()
for idx, (k, v) in enumerate(pcorr_samples.items()):
    az.plot_kde(v**2, label=k, plot_kwargs={'color':f'C{idx}'}, ax=ax)
ax.set_ylim(0, 80);
../_images/notebooks_ESCS_multiple_regression_38_0.png

With these posteriors we can ask: What is the probability that the squared partial correlation for Openness (blue) is greater than the squared partial correlation for Conscientiousness (orange)?

[18]:
(pcorr_samples['o'] ** 2 > pcorr_samples['c'] ** 2).mean()
[18]:
<xarray.DataArray ()>
array(0.9215)

If we contrast this result with the plot we’ve just shown, we may think the probability is too high when looking at the overlap between the blue and orange curves. However, the previous plot is only showing marginal posteriors, which don’t account for correlations between the coefficients. In our Bayesian world, our model parameters’ are random variables (and consequently, any combination of them are too). As such, squared partial correlation have a joint distribution. When computing probabilities involving at least two of these parameters, one has to use the joint distribution. Otherwise, if we choose to work only with marginals, we are implicitly assuming independence.

Let’s check the joint distribution of the squared partial correlation for Openness and Conscientiousness. We highlight with a blue color the draws where the coefficient for Openness is greater than the coefficient for Conscientiousness.

[19]:
sq_partial_c = pcorr_samples['c'].stack(draws=("chain", "draw")).values ** 2
sq_partial_o = pcorr_samples['o'].stack(draws=("chain", "draw")).values ** 2
[20]:
# Just grab first to colors from color map
colors = plt.rcParams['axes.prop_cycle'].by_key()['color'][:2]
colors = [colors[1] if x > y else colors[0] for x, y in zip(sq_partial_c, sq_partial_o)]

plt.scatter(sq_partial_o, sq_partial_c, c=colors)
plt.xlabel("Openness to experience")
plt.ylabel("Conscientiousness");
../_images/notebooks_ESCS_multiple_regression_43_0.png

We can see that in the great majority of the draws (92.7%) the squared partial correlation for Openness is greater than the one for Conscientiousness. In fact, we can check the correlation between them is

[21]:
np.corrcoef(sq_partial_c, sq_partial_o)[0, 1]
[21]:
-0.16987410949240517

which explains why ony looking at the marginal posteriors (i.e. assuming independence) is not the best approach here.

For each predictor, what is the probability that it has the largest squared partial correlation?

[22]:
pc_df = pcorr_samples.to_dataframe()
(pc_df**2).idxmax(axis=1).value_counts() / len(pc_df.index)
[22]:
a    0.9935
o    0.0065
dtype: float64

Agreeableness is clearly the strongest predictor of drug use among the Big Five personality traits in terms of partial correlation, but it’s still not a particularly strong predictor in an absolute sense. Walter Mischel famously claimed that it is rare to see correlations between personality measure and relevant behavioral outcomes exceed 0.3. In this case, the probability that the agreeableness partial correlation exceeds 0.3 is:

[23]:
(np.abs(pcorr_samples['a']) > 0.3).mean()
[23]:
<xarray.DataArray 'a' ()>
array(0.7475)

Posterior Predictive

Once we have computed the posterior distribution, we can use it to compute the posterior predictive distribution. As the name implies, these are predictions assuming the model’s parameter are distributed as the posterior. Thus, the posterior predictive includes the uncertainty about the parameters.

With bambi we can use the model’s posterior_predictive method with the fitted az.InferenceData to generate a posterior predictive samples, which are then automatically added to the az.InferenceData object

[24]:
posterior_predictive = model.posterior_predictive(fitted)
fitted
/home/tomas/anaconda3/envs/bmb/lib/python3.8/site-packages/pymc3/sampling.py:1688: UserWarning: samples parameter is smaller than nchains times ndraws, some draws and/or chains may not be represented in the returned posterior predictive sample
  warnings.warn(
100.00% [500/500 00:00<00:00]
[24]:
arviz.InferenceData
    • <xarray.Dataset>
      Dimensions:      (chain: 2, draw: 1000)
      Coordinates:
        * chain        (chain) int64 0 1
        * draw         (draw) int64 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
      Data variables:
          Intercept    (chain, draw) float64 3.532 3.546 3.461 ... 3.239 3.657 2.802
          o            (chain, draw) float64 0.004573 0.004644 ... 0.006458 0.007668
          c            (chain, draw) float64 -0.004227 -0.004481 ... -0.001508
          e            (chain, draw) float64 0.004794 0.005806 ... 0.0007352 0.005286
          a            (chain, draw) float64 -0.01402 -0.01452 ... -0.01306 -0.01369
          n            (chain, draw) float64 -0.001045 -0.001156 ... -0.001619
          drugs_sigma  (chain, draw) float64 0.5811 0.5692 0.5956 ... 0.5748 0.6206
      Attributes:
          created_at:                  2021-03-08T11:58:57.287708
          arviz_version:               0.11.2
          inference_library:           pymc3
          inference_library_version:   3.11.1
          sampling_time:               22.620290756225586
          tuning_steps:                1000
          modeling_interface:          bambi
          modeling_interface_version:  0.4.0

    • <xarray.Dataset>
      Dimensions:      (chain: 2, draw: 1000, drugs_dim_0: 604, drugs_dim_1: 1)
      Coordinates:
        * chain        (chain) int64 0 1
        * draw         (draw) int64 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
        * drugs_dim_0  (drugs_dim_0) int64 0 1 2 3 4 5 6 ... 598 599 600 601 602 603
        * drugs_dim_1  (drugs_dim_1) int64 0
      Data variables:
          drugs        (chain, draw, drugs_dim_0, drugs_dim_1) float64 -0.9286 ... ...
      Attributes:
          created_at:                  2021-03-08T11:58:57.607626
          arviz_version:               0.11.2
          inference_library:           pymc3
          inference_library_version:   3.11.1
          modeling_interface:          bambi
          modeling_interface_version:  0.4.0

    • <xarray.Dataset>
      Dimensions:             (chain: 2, draw: 1000)
      Coordinates:
        * chain               (chain) int64 0 1
        * draw                (draw) int64 0 1 2 3 4 5 6 ... 994 995 996 997 998 999
      Data variables:
          step_size_bar       (chain, draw) float64 0.08693 0.08693 ... 0.08819
          diverging           (chain, draw) bool False False False ... False False
          perf_counter_start  (chain, draw) float64 4.219e+03 4.219e+03 ... 4.229e+03
          step_size           (chain, draw) float64 0.06353 0.06353 ... 0.09556
          energy_error        (chain, draw) float64 -0.7034 0.3246 ... -0.1005 0.03846
          lp                  (chain, draw) float64 -529.6 -532.1 ... -531.7 -533.8
          acceptance_rate     (chain, draw) float64 0.9999 0.7334 ... 0.721 0.946
          energy              (chain, draw) float64 532.3 536.5 540.7 ... 537.1 544.4
          tree_depth          (chain, draw) int64 6 5 5 6 5 5 5 5 ... 5 7 5 5 5 5 5 6
          process_time_diff   (chain, draw) float64 0.01103 0.005471 ... 0.009978
          n_steps             (chain, draw) float64 63.0 31.0 31.0 ... 31.0 31.0 63.0
          perf_counter_diff   (chain, draw) float64 0.01105 0.005489 ... 0.009985
          max_energy_error    (chain, draw) float64 -0.7213 5.542 ... 1.65 0.1409
      Attributes:
          created_at:                  2021-03-08T11:58:57.293567
          arviz_version:               0.11.2
          inference_library:           pymc3
          inference_library_version:   3.11.1
          sampling_time:               22.620290756225586
          tuning_steps:                1000
          modeling_interface:          bambi
          modeling_interface_version:  0.4.0

    • <xarray.Dataset>
      Dimensions:      (drugs_dim_0: 604, drugs_dim_1: 1)
      Coordinates:
        * drugs_dim_0  (drugs_dim_0) int64 0 1 2 3 4 5 6 ... 598 599 600 601 602 603
        * drugs_dim_1  (drugs_dim_1) int64 0
      Data variables:
          drugs        (drugs_dim_0, drugs_dim_1) float64 1.857 3.071 ... 2.5 3.357
      Attributes:
          created_at:                  2021-03-08T11:58:57.608977
          arviz_version:               0.11.2
          inference_library:           pymc3
          inference_library_version:   3.11.1
          modeling_interface:          bambi
          modeling_interface_version:  0.4.0

    • <xarray.Dataset>
      Dimensions:      (chain: 1, draw: 500, drugs_dim_0: 604)
      Coordinates:
        * chain        (chain) int64 0
        * draw         (draw) int64 0 1 2 3 4 5 6 7 ... 493 494 495 496 497 498 499
        * drugs_dim_0  (drugs_dim_0) int64 0 1 2 3 4 5 6 ... 598 599 600 601 602 603
      Data variables:
          drugs        (chain, draw, drugs_dim_0) float64 1.141 1.422 ... 2.182 1.339
      Attributes:
          created_at:                  2021-03-08T11:59:04.081482
          arviz_version:               0.11.2
          modeling_interface:          bambi
          modeling_interface_version:  0.4.0

One use of the posterior predictive is as a diagnostic tool, shown below using az.plot_ppc.The blue lines represent the posterior predictive distribution estimates, and the black line represents the observed data. Our posterior predictions seems perform an adequately good job representing the observed data in all regions except near the value of 1, where the observed data and posterior estimates diverge.

[25]:
%load_ext watermark
%watermark -n -u -v -iv -w
Last updated: Mon Mar 08 2021

Python implementation: CPython
Python version       : 3.8.5
IPython version      : 7.18.1

arviz      : 0.11.2
matplotlib : 3.3.3
bambi      : 0.4.0
statsmodels: 0.12.2
pandas     : 1.2.2
pymc3      : 3.11.1
numpy      : 1.20.1

Watermark: 2.1.0