Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var_names = re.findall(r"(\w+)", grpr)
for var_name in var_names:
if var_name in data.columns:
data.loc[:, var_name] = data.loc[:, var_name].astype("category")
self.clean_data.loc[:, var_name] = data.loc[:, var_name]
# Default to including random intercepts
intcpt = 1 if intcpt is None else int(intcpt)
grpr_df = dmatrix("0+%s" % grpr, data, return_type="dataframe", NA_action="raise")
# If there's no predictor, we must be adding random intercepts
if not pred and grpr not in self.terms:
name = "1|" + grpr
pred = np.ones((len(grpr_df), 1))
term = RandomTerm(
name, grpr_df, pred, grpr_df.values, categorical=True, prior=prior
)
self.terms[name] = term
else:
pred_df = dmatrix(
"%s+%s" % (intcpt, pred), data, return_type="dataframe", NA_action="raise"
)
# determine value of the 'constant' attribute
const = np.atleast_2d(pred_df.T).T.sum(1).var() == 0
for col, i in pred_df.design_info.column_name_indexes.items():
pred_data = pred_df.iloc[:, i]
lev_data = grpr_df.multiply(pred_data, axis=0)
# Also rename intercepts and skip if already added.
# This can happen if user specifies something like
def __init__(
self, name, data, predictor, grouper, categorical=False, prior=None, constant=None
):
super(RandomTerm, self).__init__(name, data, categorical, prior, constant)
self.grouper = grouper
self.predictor = predictor
self.group_index = self.invert_dummies(grouper)
label = "1|%s" % grpr
else:
label = col + "|" + grpr
prior = priors.pop(label, priors.get("random", None))
# Categorical or continuous is determined from data
ld_vals = lev_data.values
if ((ld_vals == 0) | (ld_vals == 1)).all():
lev_data = lev_data.astype(int)
cat = True
else:
cat = False
pred_data = pred_data[:, None] # Must be 2D later
term = RandomTerm(
label,
lev_data,
pred_data,
grpr_df.values,
categorical=cat,
constant=const if const else None,
prior=prior,
)
self.terms[label] = term