Responses#

Responses#

The response of a model, built from the columns of a dataframe as a ModelSpec builds its design from them. A response is one of

  • Surv: a survival response, as R’s Surv(time, status) or Surv(start, stop, status);

  • Binomial: a binary response, or successes out of a number of trials;

  • Multinomial: the class of each observation, or counts of each class.

A response is fit, e.g. to learn the levels of a categorical, so that it is coded the same way on new data: y = Surv(‘time’, ‘status’).fit_transform(D). To leave its columns out of a design, ModelSpec([wildcard(exclude=response)]) is the design of R’s y ~ ..

class modelmatrix.Surv(*args, start=None, stop=None, status=None)#

Bases: Response

A survival response, as R’s Surv: Surv(time, status) for right-censored data, Surv(start, stop, status) for (start, stop] intervals (counting process data), or with keywords Surv(stop=…, status=…, start=…).

Parameters:
  • stop (column identifier) – Times of the events, or of censoring.

  • status (column identifier) – 1 (or True) for an event, 0 (or False) for censoring.

  • start (column identifier (optional)) – Start of the interval at risk.

Notes

The response is a dataframe with columns named by their role, whatever their names in X: start (if given), stop and status, the names of R’s Surv and of lifelines’ CoxTimeVaryingFitter. (scikit-survival’s Surv is another object, a structured array.)

property columns#

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

class modelmatrix.Binomial(successes, trials=None, level=None, output='counts')#

Bases: Response

A binomial response: a binary column, or the number of successes out of a number of trials.

Parameters:
  • successes (column identifier) – A binary column (0/1, boolean, or with two levels), or with trials, the number of successes.

  • trials (column identifier (optional)) – The number of trials.

  • level (level identifier (optional)) – For a binary column: the level counted as a success, any other level being a failure. Defaults to the second level, as in R: of the categories of a categorical, else of the sorted values.

  • output ({'counts', 'proportion'}) – With trials: ‘counts’ gives the successes and the failures, two columns as R’s cbind(successes, failures) (e.g. for statsmodels’ GLM with a Binomial family); ‘proportion’ the proportion of successes and the number of trials, as a response and its weights (e.g. for glmstar’s response_id and weight_id).

Notes

A binary column gives a pd.Series of 0.0 and 1.0 (missing values are NaN); with trials, a pd.DataFrame.

property columns#

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

class modelmatrix.Multinomial(classes, output='labels')#

Bases: Response

A multinomial response: the class of each observation, or the counts of each class.

Parameters:
  • classes (column identifier, or list of column identifiers) – A column holding the class of each observation, or a column of counts for each class.

  • output ({'labels', 'onehot'}) – For a column of classes: ‘labels’ gives the classes as a categorical, its categories the levels seen at fit (e.g. for a scikit-learn classifier); ‘onehot’ a column of indicators for each level.

Notes

The levels are the categories of a categorical, else the sorted values. A level not seen at fit raises an error.

property columns#

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

class modelmatrix.Outcome(response, **columns)#

Bases: Response

A response, with other columns named by their role.

Parameters:
  • response (Response or column identifier) – The response: a Surv, Binomial or Multinomial, or a column of X as it is.

  • **columns (column identifier or Feature) – Other columns, each named by its keyword, e.g. offset=’O’ or weight=’w’: a column of X, as it is (a categorical, such as the strata of a Cox model, keeps its labels), or a feature of a single column, e.g. the log of a column, fit as a term of a ModelSpec.

Notes

The columns are those of the response, then the others in the order of their keywords. With a Binomial of output=’proportion’, its number of trials is the column weight (times the weights given as weight=, if any), as R’s prior weights of a proportion.

wildcard(exclude=outcome) leaves out every column the outcome uses, so that none of them is a term of its own design. For an offset of new data, without its response, use transform(X, response=False).

property columns#

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

transform(X, response=True)#

Build the outcome from X, after fitting.

Parameters:
  • X (dataframe or np.ndarray) – Data holding the columns of the outcome.

  • response (bool, default=True) – Build the response? If False, only the other columns, e.g. the offset of new data, whose response is not known.

Returns:

y – Of the backend of X, an np.ndarray for an array.

Return type:

dataframe or np.ndarray

class modelmatrix.Response#

Bases: object

Base class of the responses: the columns of X used, fit and transform.

X is a pd.DataFrame, a dataframe of another backend supported by narwhals (e.g. polars), whose response is then in that backend, or an np.ndarray, whose columns are integers.

fit(X, y=None)#

Fit the response on X, e.g. its levels.

Parameters:
  • X (dataframe or np.ndarray) – Data holding the columns of the response.

  • y (None) – Ignored.

fit_transform(X, y=None)#

Fit the response on X and build it.

transform(X)#

Build the response from X, after fitting.

Parameters:

X (dataframe or np.ndarray) – Data holding the columns of the response.

Returns:

y – The response, e.g. for fit(X, y) of an estimator: of the backend of X (e.g. a pd.Series for a pd.DataFrame), an np.ndarray for an array.

Return type:

series, dataframe or np.ndarray