# Handling missing data in linear regression on timeseries data

**URL:** https://discourse.pymc.io/t/handling-missing-data-in-linear-regression-on-timeseries-data/5364
**Category:** Questions
**Created:** [July 1, 2020, 8:53pm UTC](https://discourse.pymc.io/t/handling-missing-data-in-linear-regression-on-timeseries-data/5364 "2020-07-01T20:53:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ally-lee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ally-lee/32/3046_2.png) [@ally-lee](https://discourse.pymc.io/u/ally-lee)
#### Post date: [July 1, 2020, 8:53pm UTC](https://discourse.pymc.io/t/handling-missing-data-in-linear-regression-on-timeseries-data/5364/1 "2020-07-01T20:53:49Z")

</div>

We would like to perform linear regression on timeseries data, where each data point is conditioned on the previous one, i.e. we want to learn coefficients b\_0 and b\_1 such that

y\_t \sim N(\mu = b\_0 + b\_1 \* y\_{t-1}, \sigma = 1)

for all t. We have been able to create a model that does this when we have complete data, but we are stuck on how to get our model to handle missing data. Using the following code to define our model, PyMC3 is able to easily impute any missing y values. However, for this particular problem, the x values are the same as the y values (just offset by 1 index), so missing y’s are the same as missing x’s.

```
data = np.arange(10)
x = data[:-1]
y = data[1:]

with pm.Model() as model:
    beta = pm.Normal('bias', mu=0, sd=10, shape=2)
    mu = beta[0] + beta[1] * x
    Y_obs = pm.Normal('Y_obs', mu=mu, sigma=1, observed=y)

```

We then tried defining a custom multivariate distribution over our entire dataset so that it would be treated as one series of values, rather than having to separate it into x’s and y’s prior to entering the model. This solution still wasn’t able to handle missing data since there is no way to compute the likelihood of the distribution when there are NaN’s in the data.

```
def likelihood(beta):

    def logp_(data):
        data = data.eval()
        x = counts[:-1]
        y = counts[1:]
        mu = beta[0] + beta[1] * x
        lik = pm.Normal.dist(mu=mu, sigma=1)
        return lik.logp(y)

    return logp_

with pm.Model() as model:
    beta = pm.Normal('beta', mu=0, sd=10, shape=2)
    Y_obs = pm.DensityDist('Y_obs', likelihood(beta), observed=data)

```

Any ideas on how to handle missing data for this problem?

---

<div class="post-metadata">

### Author: ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)
#### Post date: [July 2, 2020, 12:18pm UTC](https://discourse.pymc.io/t/handling-missing-data-in-linear-regression-on-timeseries-data/5364/2 "2020-07-02T12:18:02Z")

</div>

Hi,  
Maybe casting your data to a numpy masked array could help? That way, PyMC should be able to handle and impute the missing data points. Here is [an example](https://github.com/pymc-devs/resources/blob/master/Rethinking/Chp_14.ipynb) of how to do that.  
Hope this helps 🖖
