Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Responses and the wildcard

As in scikit-learn, the design X and the response y are built separately: a ModelSpec builds the design, and a response, such as Surv, Binomial or Multinomial, builds y from the same data. wildcard stands for every column, as . in R’s formulae, and wildcard(exclude=...) leaves out the response: ModelSpec([wildcard(exclude='Sales')]) is the design of R’s Sales ~ ..

y ~ .

intercept 5.660623 CompPrice 0.092815 Income 0.015803 Advertising 0.123095 Population 0.000208 Price -0.095358 ShelveLoc[Good] 4.850183 ShelveLoc[Medium] 1.956715 Age -0.046045 Education -0.021102 Urban[Yes] 0.122886 US[Yes] -0.184093 dtype: float64

The wildcard is expanded when the ModelSpec is fit: terms is unchanged, and expanded_terms_ holds what it stood for.

([. - Sales], ['CompPrice', 'Income', 'Advertising', 'Population', 'Price', 'ShelveLoc', 'Age', 'Education', 'Urban', 'US'])

Columns that are terms themselves are not repeated, as in R’s Sales ~ Price + ., and more columns can be excluded, as R’s . - x:

[Feature(variables=('Price',), name='poly(Price, degree=2)', encoder=Poly(degree=2), use_transform=True, pure_columns=False, override_encoder_colnames=True, spec=('poly', ('Price',), (('degree', 2), ('intercept', False), ('raw', False)))), 'Price', 'CompPrice', 'Income', 'Advertising', 'ShelveLoc', 'Age', 'Urban', 'US']

y ~ .^2 and .:x

wildcard(pow=2) adds every interaction of two columns, as R’s .^2, and the wildcard can be a part of an interaction, as R’s .:US:

['Price', 'Income', 'US', ('Price', 'Income'), ('Price', 'US'), ('Income', 'US')]
['US', ('Price', 'US'), ('Income', 'US')]

Features of the wildcard

A wildcard can be a variable of a feature: here, the first principal components of the numeric columns, but the response.

Loading...

Its parameters are tuned as those of any term:

{'modelspec__terms__0__n_components': 7}

When X and y are both pandas objects, ModelSpec.fit checks that their indices agree.

Responses

A response is fit, e.g. to learn the levels of a categorical, so that it is coded the same way on new data, and transform builds it: response.fit_transform(D). For a dataframe of another backend, such as polars, the response is in that backend. wildcard(exclude=response) leaves out the response’s columns, so that it is not a term of its own design.

Survival responses: Surv

Surv(time, status) is R’s survival response, and Surv(start, stop, status) that of (start, stop] intervals. The response is a dataframe whose columns are named by their role, whatever their names in the data: stop (the times), status and, if given, start, as R’s Surv and lifelines’ CoxTimeVaryingFitter name them.

Loading...
array([ 0.18374761, -1.23954213, -2.15456551, -1.26887043, 0.44119464, -0.05495526, 0.0342925 , 0.17777779])

Binomial responses

Binomial('US') codes a binary column as 0 and 1, the second level, as in R, being a success (or the one given by level=):

Loading...

With the number of trials, Binomial(successes, trials=...) gives the successes and the failures, as R’s cbind(successes, failures) (which statsmodels’ GLM takes too), or with output='proportion' the proportion and the number of trials, as a response and weights:

(intercept 0.065649 x 0.752467 dtype: float64, intercept 0.065649 x 0.752467 dtype: float64)

Multinomial responses

Multinomial('ShelveLoc') gives the class of each row as a categorical whose categories are the levels seen at fit, and with output='onehot' an indicator column for each level; Multinomial([...]) takes a column of counts for each class.

Loading...

Offsets, weights and strata: Outcome

Outcome puts the other columns a model takes with its response next to it, each named by its keyword: an offset, weights, the strata of a Cox model, or any other. A column is taken as it is (a categorical keeps its labels), and a feature of a single column is fit as a term of a ModelSpec, e.g. R’s offset(log(exposure)):

intercept -0.017519 x 0.243477 dtype: float64

As the columns are named by their role, an estimator can find them by name, e.g. a stratified Cox model with an offset:

Loading...

The offset of new data, whose response is not known, is outcome.transform(new, response=False). With a Binomial of output='proportion', the number of trials is the column weight (times the weights given as weight=), as R’s weights of a proportion.

Missing values

As scikit-learn’s transformers, a ModelSpec keeps every row: a row of the design is missing where a variable it uses is missing (a poly or spline is fit on the observed values), and so is the response. A transformer cannot drop rows of X without those of y, so the rows to keep, those of R’s na.omit, are chosen outside the ModelSpec (or the missing values are imputed, e.g. with scikit-learn’s SimpleImputer, for an estimator that does not take them).

(263, 19)