# First attempt at linear regressions with pymc

**URL:** <https://discourse.pymc.io/t/first-attempt-at-linear-regressions-with-pymc/6772>\
**Category:** Questions\
**Created:** [February 17, 2021, 9:04am UTC](https://discourse.pymc.io/t/first-attempt-at-linear-regressions-with-pymc/6772 "2021-02-17T09:04:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_P](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mark_p/32/3544_2.png) [@Mark\_P](https://discourse.pymc.io/u/Mark_P)\
**Post date:** [February 17, 2021, 9:04am UTC](https://discourse.pymc.io/t/first-attempt-at-linear-regressions-with-pymc/6772/1 "2021-02-17T09:04:48Z")

</div>

This is my very first exposure to pymc and I am trying to create a simple linear regression on my dataset.  
This is my model:

```auto
with pm.Model() as linear_model:
    mu = pm.Exponential('mu', lam=0.1)
    m = pm.Uniform('m', lower=0, upper=100)
    intercept = pm.Normal('intercept', mu=mu, sigma=100)
    
    link = df['size'].values * m + intercept
    y = pm.Normal('y', mu=link, sigma=mu, observed=df['price'].values)
    
with linear_model:
    trace = pm.sample(1000, tune=1000)

```

However, it returns the following error:

```auto
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [intercept, m, mu]
Sampling 4 chains, 0 divergences: 20%|██ | 1624/8000 [00:06<00:26, 236.68draws/s] 
---------------------------------------------------------------------------
RemoteTraceback Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/Users/mark/opt/miniconda3/envs/pymc/lib/python3.6/site-packages/pymc3/parallel_sampling.py", line 110, in run
    self._start_loop()
  File "/Users/mark/opt/miniconda3/envs/pymc/lib/python3.6/site-packages/pymc3/parallel_sampling.py", line 160, in _start_loop
    point, stats = self._compute_point()
  File "/Users/mark/opt/miniconda3/envs/pymc/lib/python3.6/site-packages/pymc3/parallel_sampling.py", line 191, in _compute_point
    point, stats = self._step_method.step(self._point)
  File "/Users/mark/opt/miniconda3/envs/pymc/lib/python3.6/site-packages/pymc3/step_methods/arraystep.py", line 247, in step
    apoint, stats = self.astep(array)
  File "/Users/mark/opt/miniconda3/envs/pymc/lib/python3.6/site-packages/pymc3/step_methods/hmc/base_hmc.py", line 130, in astep
    self.potential.raise_ok(self._logp_dlogp_func._ordering.vmap)
  File "/Users/mark/opt/miniconda3/envs/pymc/lib/python3.6/site-packages/pymc3/step_methods/hmc/quadpotential.py", line 231, in raise_ok
    raise ValueError('\n'.join(errmsg))
ValueError: Mass matrix contains zeros on the diagonal. 
The derivative of RV `intercept`.ravel()[0] is zero.
"""

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)
ValueError: Mass matrix contains zeros on the diagonal. 
The derivative of RV `intercept`.ravel()[0] is zero.

The above exception was the direct cause of the following exception:

RuntimeError Traceback (most recent call last)
<ipython-input-48-56135084a3c4> in <module>
      8 
      9 with linear_model:
---> 10 trace = pm.sample(1000, tune=1000)

```

[After some search](https://discourse.pymc.io/t/model-diagnostics-for-mass-matrix-contains-zeros-on-the-diagonal/957), I found out that it could be caused by overflow however I am not sure how I would change the parameters in order to fit the data. There are also no NaN values and the range does not seem to be too large as you can see here:

 ![Screenshot 2021-02-16 at 23.47.39](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/8/8b0dfd675cfdd0dafe2c33e56155ef1d273f2caf.jpeg)
