.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_massunivnormodols.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_massunivnormodols.py: ================================= Fitting Multiple Normative Models ================================= This script demonstrates the process of fitting multiple normative models to data and visualizing the results. We first generate synthetic data for multiple variables across a range of ages. Then, we fit a MassUnivNormodOLS model to the data, which is capable of handling multiple variables simultaneously. Finally, we visualize the results, including centiles and z-scores, for each variable. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from sknormod import MassUnivNormodOLS from sknormod.datasets import make_gaussian from sknormod.plotting import plot_scatter_with_lines .. GENERATED FROM PYTHON SOURCE LINES 22-25 Generate synthetic data ----------------------- We generate synthetic data for multiple variables across a range of ages. .. GENERATED FROM PYTHON SOURCE LINES 25-35 .. code-block:: Python n_subj = 500 # Number of subjects n_cols = 9 # Number of variables age = np.linspace(0, 100, n_subj) X = np.column_stack((age, age**2)) # Age and age squared as features # Generate synthetic Y data for each variable Y = np.column_stack([make_gaussian(n_subj, interpolate_mu = np.random.normal(loc=100, scale=2, size=3))[1] for _ in range(n_cols)]) .. GENERATED FROM PYTHON SOURCE LINES 36-38 Fit and get z-scores and centiles --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-49 .. code-block:: Python # Fit MassUnivNormodOLS model mass_normod = MassUnivNormodOLS() mass_normod.fit(X, Y) # Calculate z-scores and centiles Z = mass_normod.transform_to_z(X, Y) # Calculate z-scores for each variable # Calculate centiles for each variable mass_estimated_centiles = mass_normod.predict_distr_p(X, np.linspace(0.1, 0.9, 9), range(n_cols)) .. GENERATED FROM PYTHON SOURCE LINES 50-53 Plotting -------- Visualize the results for each variable .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. code-block:: Python fig, axes = plt.subplots(3, 3, figsize=(15, 15)) for i_col, ax in enumerate(axes.flat): plt.sca(ax) plot_scatter_with_lines(age, Y[:, i_col], lines=mass_estimated_centiles[i_col], c=Z[:, i_col]) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_massunivnormodols_001.png :alt: plot massunivnormodols :srcset: /auto_examples/images/sphx_glr_plot_massunivnormodols_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.461 seconds) .. _sphx_glr_download_auto_examples_plot_massunivnormodols.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_massunivnormodols.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_massunivnormodols.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_