.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_gamlss.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_gamlss.py: ================= From LM to GAMLSS ================= This script demonstrates the progression from Linear Model (LM) to Generalized Additive Models for Location, Scale, and Shape (GAMLSS) using the `sknormod` package. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from sknormod import gam from sknormod import NormodOLS from sknormod.datasets import make_lss_SHASHo2 from sknormod.plotting import plot_scatter_with_lines .. GENERATED FROM PYTHON SOURCE LINES 23-26 Ploting functions ----------------- Define a custom plotting function to visualize the models .. GENERATED FROM PYTHON SOURCE LINES 26-39 .. code-block:: Python def plot_model(ax, x, y, normod, title): # Generate centiles for prediction centiles = np.linspace(0.1, 0.9, 9) # Predict centiles using the normative model predicted_centiles = normod.predict_distr_p(x, centiles) # Plot scatter plot of data points ax.scatter(x, y) # Plot centile lines for centile_line in predicted_centiles: ax.plot(x, centile_line, color='black') # Set title for the subplot ax.set_title(title) .. GENERATED FROM PYTHON SOURCE LINES 40-43 Simulate data ------------- Generate synthetic data for demonstration .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python x, y = make_lss_SHASHo2(1000, interpolate_mu=[50, 52, 35], interpolate_sigma=[2, 0.5, 2], interpolate_nu=[-1.5, -1.5], interpolate_tau=[1, 1]) # Reshape the feature array to match the model input requirements x = x.reshape(-1, 1) .. GENERATED FROM PYTHON SOURCE LINES 52-55 Fit models ---------- Initialize normative models: OLS, GAM, GAMLS, and GAMLSS .. GENERATED FROM PYTHON SOURCE LINES 55-70 .. code-block:: Python normod_ols = NormodOLS() # Linear Model normod_gam = gam.GAM(mu_formula="y ~ s(x0)") # GAM normod_gam_ls = gam.GAMLS(mu_formula="y ~ s(x0)", sigma_formula=" ~ s(x0)") # GAM Heteroskedastic (LS) normod_gam_lss = gam.GAMLSS(mu_formula="y ~ s(x0)", sigma_formula=" ~ s(x0)", nu_formula=" ~ 1", tau_formula=" ~ 1") # GAMLSS # Fit the models to the data normod_ols.fit(x, y) normod_gam.fit(x, y) normod_gam_ls.fit(x, y) normod_gam_lss.fit(x, y) .. raw:: html
GAMLSS(mu_formula='y ~ s(x0)', nu_formula=' ~ 1', sigma_formula=' ~ s(x0)',
           tau_formula=' ~ 1')
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 71-73 Visualize the results --------------------- .. GENERATED FROM PYTHON SOURCE LINES 73-86 .. code-block:: Python estimated_centiles = list(( normod.predict_distr_p(x, np.linspace(0.1, 0.9, 9)) for normod in [normod_ols, normod_gam, normod_gam_ls, normod_gam_lss] )) fig, axes = plt.subplots(2, 2, figsize=(10, 10)) titles = ["LM", "GAM", "GAMLS", "GAMLSS"] for i_col, ax in enumerate(axes.flat): plt.sca(ax) plot_scatter_with_lines(x, y, lines=estimated_centiles[i_col], cbar=False) ax.set_title(titles[i_col]) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_gamlss_001.png :alt: LM, GAM, GAMLS, GAMLSS :srcset: /auto_examples/images/sphx_glr_plot_gamlss_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/brick/Code/scikit-normod/sknormod/plotting.py:6: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored plt.scatter(x, y, c=c, cmap="coolwarm") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.451 seconds) .. _sphx_glr_download_auto_examples_plot_gamlss.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gamlss.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gamlss.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_