Skip to content

Commit

Permalink
Adjusting to changes to the GAMMLSS api from mssm
Browse files Browse the repository at this point in the history
  • Loading branch information
JoKra1 committed Sep 24, 2024
1 parent 018fad6 commit 7846cf5
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 149 deletions.
198 changes: 99 additions & 99 deletions 1) GAMMs.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description

Plotting functions for the Massive Smooth Models ([mssm](https://github.com/JoKra1/mssm)) toolbox. ``mssm`` is a toolbox to estimate Generalized Additive Mixed Models (GAMMs) and Generalized Additive Mixed Models of Location Scale and Shape (GAMMLSS). In addition, a tutorial for ``mssm`` is provided with this repository.
Plotting functions for the Mixed Sparse Smooth Models ([mssm](https://github.com/JoKra1/mssm)) toolbox. ``mssm`` is a toolbox to estimate Generalized Additive Mixed Models (GAMMs), Generalized Additive Mixed Models of Location Scale and Shape (GAMMLSS), and even more general smooth models in the sense defined by [Wood, Pya, & Säfken (2016)](https://doi.org/10.1080/01621459.2016.1180986). In addition, a tutorial for ``mssm`` is provided with this repository.

## Installation

Expand Down
22 changes: 11 additions & 11 deletions src/mssmViz/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
################################## Contains functions to extract useful information from GAMM & GAMMLSS models ##################################


def eval_coverage(model:GAMM or GAMLSS,pred_dat,dist_par=0,target:float or [float]=0.0,use:[int]=None,alpha=0.05,whole_function=False,n_ps=10000,seed=None):
def eval_coverage(model:GAMM or GAMMLSS,pred_dat,dist_par=0,target:float or [float]=0.0,use:[int]=None,alpha=0.05,whole_function=False,n_ps=10000,seed=None):
"""Evaluate CI coverage of ``target`` function over domain defined by ``pred_dat``.
:param model: ``GAMM`` or ``GAMLSS`` model.
:type model: GAMM or GAMLSS
:param model: ``GAMM`` or ``GAMMLSS`` model.
:type model: GAMM or GAMMLSS
:param pred_dat: ``pandas.DataFrame`` with data used to compute model predictions to be compared against target as well as CI.
:type pred_dat: pd.Dataframe
:param dist_par: The index corresponding to the parameter for which to make the prediction (e.g., 0 = mean) - only necessary if a GAMLSS model is provided, defaults to 0
:param dist_par: The index corresponding to the parameter for which to make the prediction (e.g., 0 = mean) - only necessary if a GAMMLSS model is provided, defaults to 0
:type dist_par: int, optional
:param target: Target function. Can either be set to a float (e.g., 0.0 if the target function is believed to be zero everywhere across the domain) or a ``list``/``np.array ``of floats. In the latter case the shape of ``target`` must be a flattened 1D array. defaults to 0.0
:type target: float or [float], optional
Expand All @@ -34,7 +34,7 @@ def eval_coverage(model:GAMM or GAMLSS,pred_dat,dist_par=0,target:float or [floa
"""

# Compute model prediction and CI boundaries
if isinstance(model,GAMLSS):
if isinstance(model,GAMMLSS):
pred,_,b = model.predict(dist_par,use,pred_dat,ci=True,whole_interval=whole_function,alpha=alpha,n_ps=n_ps,seed=seed)
else:
pred,_,b = model.predict(use,pred_dat,ci=True,whole_interval=whole_function,alpha=alpha,n_ps=n_ps,seed=seed)
Expand All @@ -57,7 +57,7 @@ def eval_coverage(model:GAMM or GAMLSS,pred_dat,dist_par=0,target:float or [floa
return full_coverage,coverage,IN_CI


def get_term_coef(model:GAMM or GAMLSS,which:[int],dist_par=0):
def get_term_coef(model:GAMM or GAMMLSS,which:[int],dist_par=0):
"""Get the coefficients associated with a specific term included in the ``Formula`` of ``model``. Useful to extract for example
the estimated random intercepts from a random effect model.
Expand All @@ -66,25 +66,25 @@ def get_term_coef(model:GAMM or GAMLSS,which:[int],dist_par=0):
the actual factor-level corresponding to the first encoded level can be determined by inspecting the code-book returned from ``formula.get_factor_codings()``.
:param model: ``GAMM`` or ``GAMLSS`` model.
:type model: GAMM or GAMLSS
:param model: ``GAMM`` or ``GAMMLSS`` model.
:type model: GAMM or GAMMLSS
:param which: Index corresponding to the term in the model's formula for which the coefficients should be extracted.
:type which: [int]
:param dist_par: The index corresponding to the parameter for which to make the prediction (e.g., 0 = mean) - only necessary if a GAMLSS model is provided, defaults to 0
:param dist_par: The index corresponding to the parameter for which to make the prediction (e.g., 0 = mean) - only necessary if a GAMMLSS model is provided, defaults to 0
:type dist_par: int, optional
"""

# Get model-matrix that was used for fitting.
model_mat = model.get_mmat(use_terms=which)

if isinstance(model,GAMLSS):
if isinstance(model,GAMMLSS):
model_mat = model_mat[dist_par]

# Find coefficient indices corresponding to indicated terms:
coef_idx = model_mat.sum(axis=0) != 0

# Return corresponding coefficients.
if isinstance(model,GAMLSS):
if isinstance(model,GAMMLSS):
split_coef = np.split(model.coef,model.coef_split_idx)
return split_coef[dist_par][coef_idx]
else:
Expand Down
Loading

0 comments on commit 7846cf5

Please sign in to comment.