# "Out of sample" predictions with the GLM sub-module

**URL:** <https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773>\
**Category:** Questions\
**Created:** [January 23, 2018, 12:27pm UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773 "2018-01-23T12:27:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gsmafra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gsmafra/32/504_2.png) [@gsmafra](https://discourse.pymc.io/u/gsmafra)\
**Post date:** [January 23, 2018, 12:27pm UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/1 "2018-01-23T12:27:03Z")

</div>

I don’t know if I’m using the right vocabulary here but I want to use a model I’m fitting with GLM to give me the posterior predictive distribution of a variable that was not observed - equivalent to a train/test split. Is there a way to do this and continue using the compact syntax of the GLM sub-module?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [January 23, 2018, 2:53pm UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/2 "2018-01-23T14:53:24Z")

</div>

Yes, you can feed a theano.shared X and y for fitting/sampling, and then replace the test value for prediction. For more information see:  
[http://docs.pymc.io/notebooks/posterior\_predictive.html#Prediction](http://docs.pymc.io/notebooks/posterior_predictive.html#Prediction)  
[http://docs.pymc.io/notebooks/api\_quickstart.html#4.1-Predicting-on-hold-out-data](http://docs.pymc.io/notebooks/api_quickstart.html#4.1-Predicting-on-hold-out-data)

---

<div class="post-metadata">

**Author:** ![benyi-mikara](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@benyi-mikara](https://discourse.pymc.io/u/benyi-mikara)\
**Post date:** [July 13, 2018, 6:47am UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/3 "2018-07-13T06:47:56Z")

</div>

Hi @junpenglao is there an example of this using the GLM module?

I tried creating a shared variable for a model using GLM, received this error:

> PatsyError: Error evaluating factor: TypeError: The generic ‘SharedVariable’ object is not subscriptable. This shared variable contains an object of type: \<class ‘pandas.core.frame.DataFrame’\>. Did you forget to cast it into a Numpy array before calling theano.shared()?

The variable is a dataframe (and the model runs fine without the shared variable).

thanks

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 13, 2018, 10:02am UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/4 "2018-07-13T10:02:53Z")

</div>

I dont think that is possible for pandas data frame, you can try passing the shared array to x and y specifically, but I am not sure it would work as:

> <https://github.com/pymc-devs/pymc3/blob/master/pymc3/glm/linear.py#L102-L103>

---

<div class="post-metadata">

**Author:** ![jean-phi66](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jean-phi66/32/1992_2.png) [@jean-phi66](https://discourse.pymc.io/u/jean-phi66)\
**Post date:** [October 6, 2018, 4:59pm UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/5 "2018-10-06T16:59:52Z")

</div>

Hello,

Just to be sure about the outcome of this discussion as I am currently facing the same issue : can we use any shared theano variables to make OOS prediction with GLM?  
I have tried several configurations without success…  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![DoctorRad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/doctorrad/32/1849_2.png) [@DoctorRad](https://discourse.pymc.io/u/DoctorRad)\
**Post date:** [May 9, 2019, 3:08pm UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/6 "2019-05-09T15:08:43Z")

</div>

This issue does not appear to have been resolved as yet. The following snippet…

```
pca_embed = pca.fit_transform(X_train)
pca_data = dict(x=pca_embed.astype(np.float32), y=shared(y_train.reshape(-1)))

with Model() as model:
    glm.GLM.from_formula('y ~ x', pca_data)

```

… gives me the following error:

`ValueError: length not known: <TensorType(float64, vector)> [id A]`

Since `y` is not a numpy array, I assume this is causing the error. It would be useful if this could be fixed to allow the simplicity of using GLM with the ability to easily perform out-of-sample prediction.

Is there a workaround? Should I raise an issue on Github?

---

<div class="post-metadata">

**Author:** ![Nicky](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nicky](https://discourse.pymc.io/u/Nicky)\
**Post date:** [December 2, 2020, 4:32am UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/7 "2020-12-02T04:32:19Z")

</div>

This was so frustrating for me too! I tried the theano shared and had issues and was almost going to give up when I think I found a workaround just by using the pm.Data but you have to give it the labels when you do pm.Glm…

with pm.Model() as logistic\_model:  
pred = pm.Data(“pred”, X\_train)  
pm.glm.GLM(pred, y\_train, family = pm.glm.families.Binomial(), labels=X\_train.columns.tolist())  
trace = pm.sample(1000, tune = 2000, init = ‘adapt\_diag’,cores=1)

If you just do pred = pm.Data(“pred”, X\_train) you’ll get an error I think you have to add labels=X\_train.columns.tolist()) to pm.glm.GLM.

This should allow you you to do sample from the posterior predictive distribution:  
pm.set\_data({“pred”: X\_test}, model=logistic\_model)  
ppc = pm.sample\_ppc(trace, model=logistic\_model, samples=500)  
predValues = np.rint(ppc[‘y’].mean(axis=0)).astype(‘int’)

---

<div class="post-metadata">

**Author:** ![juanitorduz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juanitorduz/32/8155_2.png) [@juanitorduz](https://discourse.pymc.io/u/juanitorduz)\
**Post date:** [January 3, 2021, 10:52am UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/8 "2021-01-03T10:52:03Z")

</div>

Hey all! Thank you for your input! I was having the same question, in particular to use the pasty formulas to generate out-of-sample predictions. I was not able to do it directly and I had to generate the features outside the model…I wrote a little post explaining the procedure (I took your suggestions, and referenced them accordingly): [https://juanitorduz.github.io/glm\_pymc3/](https://juanitorduz.github.io/glm_pymc3/)

---

<div class="post-metadata">

**Author:** ![cstoafer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cstoafer/32/3628_2.png) [@cstoafer](https://discourse.pymc.io/u/cstoafer)\
**Post date:** [February 3, 2021, 9:26pm UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/9 "2021-02-03T21:26:37Z")

</div>

@juanitorduz Your post was very helpful! You should consider adding the notebook procedure to the pymc3 docs.

---

<div class="post-metadata">

**Author:** ![juanitorduz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juanitorduz/32/8155_2.png) [@juanitorduz](https://discourse.pymc.io/u/juanitorduz)\
**Post date:** [February 4, 2021, 8:48am UTC](https://discourse.pymc.io/t/out-of-sample-predictions-with-the-glm-sub-module/773/10 "2021-02-04T08:48:31Z")

</div>

Thanks! I am happy you found it useful! I created an [issue](https://github.com/pymc-devs/pymc-examples/issues/33) about it to scope the content of the example.
