terms.Variable

terms.Variable(name, level=None, is_response=False)

Variable in a model term.

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

Parameters

name : str

The identifier of the variable.

level : str = None

The level to use as reference. Allows to use the notation variable["level"] to indicate which event should be model as success in binary response models. Can only be used with response terms. Defaults to None.

is_response : bool = False

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

Attributes

Name Description
labels Obtain labels of the columns in the design matrix associated with this Variable
var_names Returns the name of the variable as a set.

Methods

Name Description
eval_categoric Finishes evaluation of a categoric variable.
eval_new_data Evaluates the variable with new data.
eval_new_data_categoric Evaluates the variable with new data when variable is categoric.
eval_numeric Finishes evaluation of a numeric variable.
set_data Obtains and stores the final data object related to this variable.
set_type Determines the type of the variable.

eval_categoric

terms.Variable.eval_categoric(x, spans_intercept)

Finishes evaluation of a categoric variable.

Converts the intermediate values in x into a numpy array of shape (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.

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.Variable.eval_new_data(data_mask)

Evaluates the variable with new data.

This method evaluates the variable within a new data mask. If this object is categorical, original encoding is remembered (and checked) when carrying out the new evaluation.

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 variables, the second for categoric ones.

eval_new_data_categoric

terms.Variable.eval_new_data_categoric(x)

Evaluates the variable with new data when variable is categoric.

This method also checks the levels observed in the new data frame are included within the set of the levels of the original data set. 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.Variable.eval_numeric(x)

Finishes evaluation of a numeric variable.

Converts the intermediate values in x into a 1d numpy array.

Parameters

x : np.ndarray or pd.Series

The intermediate values of the variable.

set_data

terms.Variable.set_data(spans_intercept=None)

Obtains and stores the final data object related to this variable.

Parameters

spans_intercept : bool = None

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

set_type

terms.Variable.set_type(data_mask)

Determines the type of the variable.

Looks for the name of the variable in data_mask and sets the .kind property to "numeric" or "categoric" depending on the type of the variable. It also stores the result of the intermediate evaluation in self._intermediate_data.

Parameters

data_mask : pd.DataFrame

The data frame where variables are taken from