@classmethod
    def from_formula(cls, formula, data, re_formula=None, vc_formula=None,
                     subset=None, use_sparse=False, *args, **kwargs):

        if "groups" not in kwargs.keys():
            raise AttributeError("'groups' is a required keyword argument in MixedLM.from_formula")

        # If `groups` is a variable name, retrieve the data for the
        # groups variable.
        group_name = "Group"
        if type(kwargs["groups"]) == str:
            group_name = kwargs["groups"]
            kwargs["groups"] = np.asarray(data[kwargs["groups"]])

        if re_formula is not None:
            if re_formula.strip() == "1":
                # Work around Patsy bug, fixed by 0.3.
                exog_re = np.ones((data.shape[0], 1))
                exog_re_names = ["Group"]
            else:
                eval_env = kwargs.get('eval_env', None)
                if eval_env is None:
                    eval_env = 1
                elif eval_env == -1:
                    from patsy import EvalEnvironment
                    eval_env = EvalEnvironment({})
                exog_re = patsy.dmatrix(re_formula, data, eval_env=eval_env)
                exog_re_names = exog_re.design_info.column_names
                exog_re = np.asarray(exog_re)
            if exog_re.ndim == 1:
                exog_re = exog_re[:, None]
        else:
            exog_re = None
            if vc_formula is None:
                exog_re_names = ["groups"]
            else:
                exog_re_names = []

        if vc_formula is not None:
            eval_env = kwargs.get('eval_env', None)
            if eval_env is None:
                eval_env = 1
            elif eval_env == -1:
                from patsy import EvalEnvironment
                eval_env = EvalEnvironment({})

            exog_vc = {}
            data["_group"] = kwargs["groups"]
            gb = data.groupby("_group")
            kylist = list(gb.groups.keys())
            kylist.sort()
            for vc_name in vc_formula.keys():
                exog_vc[vc_name] = {}
                for group_ix, group in enumerate(kylist):
                    ii = gb.groups[group]
                    vcg = vc_formula[vc_name]
                    mat = patsy.dmatrix(vcg, data.loc[ii, :], eval_env=eval_env,
                                        return_type='dataframe')
                    if use_sparse:
                        exog_vc[vc_name][group] = sparse.csr_matrix(mat)
                    else:
                        exog_vc[vc_name][group] = np.asarray(mat)
            exog_vc = exog_vc
        else:
            exog_vc = None

        mod = super(MixedLM, cls).from_formula(formula, data,
                                               subset=None,
                                               exog_re=exog_re,
                                               exog_vc=exog_vc,
                                               *args, **kwargs)

        # expand re names to account for pairs of RE
        (param_names,
         exog_re_names,
         exog_re_names_full) = mod._make_param_names(exog_re_names)

        mod.data.param_names = param_names
        mod.data.exog_re_names = exog_re_names
        mod.data.exog_re_names_full = exog_re_names_full
        mod.data.vcomp_names = mod._vc_names

        return mod
Create a Model from a formula and dataframe.
dataset/ETHPy150Open statsmodels/statsmodels/statsmodels/regression/mixed_linear_model.py/MixedLM.from_formula