terms.Call

terms.Call(call, is_response=False)

Call in a model term.

This class and Variable are the atomic components of a model term.

This object supports stateful transformations defined in formulae.transforms. A transformation of this type defines its parameters the first time it is called, and then can be used to recompute the transformation with memorized parameter values. This behavior is useful when implementing a predict method and using transformations such as center(x) or scale(x). center(x) memorizes the value of the mean, and scale(x) memorizes both the mean and the standard deviation.

Parameters

call : formulae.terms.call_resolver.LazyCall

The call expression returned by the parser.

is_response : bool = False

Indicates whether this call represents a response. Defaults to False.

Attributes

Name Description
labels Obtain labels of the columns in the design matrix associated with this Call
var_names Returns the names of the variables involved in the call, not including the callee.

Methods

Name Description
accept Accept method called by a visitor.
eval_categoric Finishes evaluation of categoric call.
eval_new_data Evaluates the function call with new data.
eval_new_data_categoric Evaluates the call with new data when the result of the call is categoric.
eval_numeric Finishes evaluation of a numeric call.
set_data Finishes the evaluation of the call according to its type.
set_type Evaluates function and determines the type of the result of the call.

accept

terms.Call.accept(visitor)

Accept method called by a visitor.

Visitors are those available in call_utils.py, and are used to work with call terms.

eval_categoric

terms.Call.eval_categoric(x, spans_intercept)

Finishes evaluation of categoric call.

First, it checks whether the intermediate evaluation returned is ordered. If not, it creates a category where the levels are the observed in the variable. They are sorted according to sorted() rules.

Then, it determines the reference level as well as all the other levels. If the variable is a response, the value returned is a dummy with 1s for the reference level and 0s elsewhere. If it is not a response variable, it determines the matrix of dummies according to the levels and the encoding passed.

Parameters

x : np.ndarray or pd.Series

The intermediate values of the variable.

spans_intercept : bool

Indicates if the encoding of categorical variables spans the intercept or not. Omitted when the variable is numeric.

eval_new_data

terms.Call.eval_new_data(data_mask)

Evaluates the function call with new data.

This method evaluates the function call within a new data mask. If the transformation applied is a stateful transformation, it uses the proper object that remembers all parameters or settings that may have been set in a first pass.

Parameters

data_mask : pd.DataFrame

The data frame where variables are taken from

Returns

result : np.array

The rules for the shape of this array are the rules for self.eval_numeric() and self.eval_categoric(). The first applies for numeric calls, the second for categoric ones.

eval_new_data_categoric

terms.Call.eval_new_data_categoric(x)

Evaluates the call with new data when the result of the call is categoric.

This method also checks the levels observed in the new data frame are included within the set of the levels of the result of the original call If not, an error is raised.

x : np.ndarray or pd.Series The intermediate values of the variable.

Returns

result : np.array

Numeric numpy array (n, p), where n is the number of observations and p the number of dummy variables used in the numeric representation of the categorical variable.

eval_numeric

terms.Call.eval_numeric(x)

Finishes evaluation of a numeric call.

Converts the intermediate values of the call into a numpy array of shape (n, 1), where n is the number of observations. This method is used both in self.set_data and in self.eval_new_data.

Parameters

x : np.ndarray or pd.Series

The intermediate values resulting from the call.

Returns

result : dict

A dictionary with keys "value" and "kind". The first contains the result of the evaluation, and the latter is equal to "numeric".

set_data

terms.Call.set_data(spans_intercept=False)

Finishes the evaluation of the call according to its type.

It does not support multi-level categoric responses yet. If self.is_response is True and the variable is of a categoric type, this method returns a 1d array of 0-1 instead of a matrix. # XTODO: Fix previous point In practice, it just completes the evaluation that started with self.set_type().

Parameters

spans_intercept : bool = False

Indicates if the encoding of categorical variables spans the intercept or not. Omitted when the variable is numeric.

set_type

terms.Call.set_type(data_mask, env)

Evaluates function and determines the type of the result of the call.

Evaluates the function call and sets the .kind property to "numeric" or "categoric" depending on the type of the result. It also stores the intermediate result of the evaluation in ._intermediate_data to prevent us from computing the same thing more than once.

Parameters

data_mask : pd.DataFrame

The data frame where variables are taken from

env : Environment

The environment where values and functions are taken from.