.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_normodols.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_normodols.py: ================================== Plotting NormodOLS normative model ================================== This example showcases the usage of the `NormodOLS` class from the `sknormod` library. It focuses on fitting a normative model using ordinary least squares (OLS) regression, followed by visualizing the results including centiles and z-scores. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Import necessary libraries -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import numpy as np from sknormod import NormodOLS from sknormod.plotting import plot_scatter_with_lines from sknormod.datasets import make_gaussian .. GENERATED FROM PYTHON SOURCE LINES 21-26 Simulate Data ------------- Generate synthetic data mimicking a population's age-related variable (e.g., cognitive score) and their corresponding age. Here, we create a sample of 500 individuals. .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: Python n_subj = 500 age, y = make_gaussian(n_subj, interpolate_mu=[95, 98, 92]) # We also generate a quadratic feature of age to capture potential nonlinear # relationships. X = np.column_stack((age, age**2)) .. GENERATED FROM PYTHON SOURCE LINES 33-38 Fitting Normative Model ----------------------- We employ the NormodOLS class, which implements Ordinary Least Squares (OLS) regression for normative modeling. .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: Python normod = NormodOLS() # Fit the normative model using the generated data. normod.fit(X, y) .. raw:: html
NormodOLS()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 44-50 Get centiles and z-scores ------------------------- Generating Centiles and Z-Scores Once the model is fitted, we can use it to predict centiles and calculate z-scores for new observations. We predict centiles at specific percentiles (e.g., 10th, 20th, ..., 90th). .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. code-block:: Python # Now the centiles estimated_centiles = normod.predict_distr_p(X, np.linspace(0.1, 0.9, 9)) # We also transform the observed data into z-scores. subjects_z_scores = normod.transform_to_z(X, y) .. GENERATED FROM PYTHON SOURCE LINES 57-61 Plotting Results ---------------- Finally, we visualize the fitted model along with the centiles and the distribution of z-scores. .. GENERATED FROM PYTHON SOURCE LINES 61-62 .. code-block:: Python plot_scatter_with_lines(age, y, lines=estimated_centiles, c=subjects_z_scores) .. image-sg:: /auto_examples/images/sphx_glr_plot_normodols_001.png :alt: plot normodols :srcset: /auto_examples/images/sphx_glr_plot_normodols_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.055 seconds) .. _sphx_glr_download_auto_examples_plot_normodols.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_normodols.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_normodols.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_