# Where to standardize data

**URL:** <https://discourse.pymc.io/t/where-to-standardize-data/5945>\
**Category:** Questions\
**Created:** [October 9, 2020, 7:36am UTC](https://discourse.pymc.io/t/where-to-standardize-data/5945 "2020-10-09T07:36:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattiasthalen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiasthalen/32/2362_2.png) [@mattiasthalen](https://discourse.pymc.io/u/mattiasthalen)\
**Post date:** [October 9, 2020, 7:36am UTC](https://discourse.pymc.io/t/where-to-standardize-data/5945/1 "2020-10-09T07:36:27Z")

</div>

If I want to standardize data, i.e. `(values - values.mean())/values.std()`, where should I do that, inside or outside the model?

I would love to be able to predict on new values and plot them using `az.plot_posterior()` and having the non-standardized estimations shown. Is the only way to use `pm.Deterministic` do calculate the non-standardized parameters and the non-standardized estimated value? If so, how do I plot that estimated value in `az.plot_posterior()`?

---

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [October 9, 2020, 5:35pm UTC](https://discourse.pymc.io/t/where-to-standardize-data/5945/2 "2020-10-09T17:35:51Z")

</div>

There are many alternatives, each with different strengths and focuses, personally I am not tied to any and I am not really sure what to recommend either so here goes a quick overview of the alternatives:

- using `pm.Deterministics`
- using only standarized values in the model and after inference/forward sampling destandarize the results of the inferencedata object.  
Here you can create new variables on the dataset or directly modify the present variables, InferenceData objects are completely mutable. Moreover there are some helper functions such as [`idata.map`](https://arviz-devs.github.io/arviz/generated/arviz.InferenceData.map.html#arviz.InferenceData.map) to apply the same function to multiple groups such as observed\_data, posterior\_predictive and predictions at the same time…
- like the above but using the `transform` argument of ArviZ plots.
- use only destandardized data in the model and standardize within the model code (either storing the destandardized versions with deterministic or not, it’s probably not necessary in most cases). I guess that in most cases the standardization cost is negligible compared to the rest of the model

And focused on predictions/posterior\_predictive sampling, there is also the option to create a custom sample posterior predictive with xarray/numpy/dask… This option is extremely flexible but generally requires more work than the other alternatives, so I would only recommend it if there is an extra need for this such as a bug in PyMC sample posterior predictive for a required distribution or having to predict a large amout of data using dask because it does not fit in memory…

---

<div class="post-metadata">

**Author:** ![mattiasthalen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiasthalen/32/2362_2.png) [@mattiasthalen](https://discourse.pymc.io/u/mattiasthalen)\
**Post date:** [October 15, 2020, 2:31pm UTC](https://discourse.pymc.io/t/where-to-standardize-data/5945/3 "2020-10-15T14:31:54Z")

</div>

To spin off on standardization.  
What throws me off is how to scale the prior for the error.  
My unstandardized priors are

```auto
intercept = pm.Normal(100, 10)
slope = pm.Normal(-100, 10)
error = pm.HalfNormal(5)

```

My initial thought was to scale the error like this:

```auto
5 / y_obs.std()

```

But that result in estimations that are a bit too narrow. And 1 is waaaay to wide.
