tlo.lm module

class Predictor(property_name: Optional[str] = None, external: bool = False, conditions_are_mutually_exclusive: Optional[bool] = None, conditions_are_exhaustive: Optional[bool] = False)[source]

Bases: object

when(condition: Union[str, float, bool], value: float) tlo.lm.Predictor[source]
otherwise(value: float) tlo.lm.Predictor[source]
apply(callback: Callable[[Any], float]) tlo.lm.Predictor[source]
class LinearModelType(value)[source]

Bases: enum.Enum

The type of model specifies how the results from the predictor are combined: ‘additive’ -> adds the effect_sizes from the predictors ‘logisitic’ -> multiples the effect_sizes from the predictors and applies the transform x/(1+x) [Thus, the intercept can be taken to be an Odds and effect_sizes Odds Ratios, and the prediction is a probability.] ‘multiplicative’ -> multiplies the effect_sizes from the predictors

ADDITIVE = 1
LOGISTIC = 2
MULTIPLICATIVE = 3
CUSTOM = 4
class LinearModel(lm_type: tlo.lm.LinearModelType, intercept: Union[float, int], *predictors: tlo.lm.Predictor)[source]

Bases: object

property lm_type: tlo.lm.LinearModelType

The model type.

property intercept: Union[float, int]

The intercept value for the model.

property predictors: Tuple[tlo.lm.Predictor]

The predictors used in calculating the model output.

static multiplicative(*predictors: tlo.lm.Predictor)[source]

Returns a multplicative LinearModel with intercept=1.0

Parameters

predictors – One or more Predictor objects defining the model

static custom(predict_function, **kwargs)[source]

Define a linear model using the supplied function

The function acts as a drop-in replacement to the predict function and must implement the interface:

(

self: LinearModel, df: Union[pd.DataFrame, pd.Series], rng: Optional[np.random.RandomState] = None, **kwargs

) -> pd.Series

It is the responsibility of the caller of predict to ensure they pass either a dataframe or an individual record as expected by the custom function.

See test_custom() in test_lm.py for a couple of examples.

predict(df: pandas.core.frame.DataFrame, rng: Optional[numpy.random.mtrand.RandomState] = None, squeeze_single_row_output=True, **kwargs) pandas.core.series.Series[source]

Evaluate linear model output for a given set of input data.

Parameters
  • df – The input DataFrame containing the input data to evaluate the model with.

  • rng – If set to a NumPy RandomState instance, returned output will be boolean Series corresponding to Bernoulli random variables sampled according to probabilities specified by model output. Otherwise model output directly returned.

  • squeeze_single_row_output – If rng argument is not None and this argument is set to True, the output for a df input with a single-row will be a scalar boolean value rather than a boolean Series.

  • **kwargs

    Values for any external variables included in model predictors.