How to use the bambi.utils.listify function in bambi

To help you get started, we’ve selected a few bambi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bambinos / bambi / bambi / models.py View on Github external
# Custom patsy.missing.NAAction class. Similar to patsy drop/raise
        # defaults, but changes the raised message and logs any dropped rows
        NA_handler = Custom_NA(dropna=self.dropna)

        # screen fixed terms
        if fixed is not None:
            if "~" in fixed:
                clean_fix = re.sub(r"\[.+\]", "", fixed)
                dmatrices(clean_fix, data=data, NA_action=NA_handler)
            else:
                dmatrix(fixed, data=data, NA_action=NA_handler)

        # screen random terms
        if random is not None:
            for term in listify(random):
                for side in term.split("|"):
                    dmatrix(side, data=data, NA_action=NA_handler)

        # update the running list of complete cases
        if NA_handler.completes:
            self.completes.append(NA_handler.completes)

        # save arguments to pass to _add()
        args = dict(
            zip(
                ["fixed", "random", "priors", "family", "link", "categorical"],
                [fixed, random, priors, family, link, categorical],
            )
        )
        self.added_terms.append(args)
github bambinos / bambi / bambi / models.py View on Github external
# Primitive values (floats, strs) can be overwritten with Prior objects
        # so we need to make sure to copy first to avoid bad things happening
        # if user is re-using same prior dict in multiple models.
        if priors is None:
            priors = {}
        else:
            priors = deepcopy(priors)

        if not append:
            self.reset()

        # Explicitly convert columns to category if desired--though this
        # can also be done within the formula using C().
        if categorical is not None:
            data = data.copy()
            cats = listify(categorical)
            data[cats] = data[cats].apply(lambda x: x.astype("category"))

        # Custom patsy.missing.NAAction class. Similar to patsy drop/raise
        # defaults, but changes the raised message and logs any dropped rows
        NA_handler = Custom_NA(dropna=self.dropna)

        # screen fixed terms
        if fixed is not None:
            if "~" in fixed:
                clean_fix = re.sub(r"\[.+\]", "", fixed)
                dmatrices(clean_fix, data=data, NA_action=NA_handler)
            else:
                dmatrix(fixed, data=data, NA_action=NA_handler)

        # screen random terms
        if random is not None:
github bambinos / bambi / bambi / results.py View on Github external
def _filter_names(self, var_names=None, ranefs=False, transformed=False):
        names = self.untransformed_vars if not transformed else self.names
        if var_names is not None:
            names = [n for n in listify(var_names) if n in names]
        if not ranefs:
            names = [x for x in names if re.sub(r"_offset$", "", x) not in self.model.random_terms]
        intercept = [n for n in names if "intercept" in n]
        std = [n for n in names if "_" in n]
        rand_eff = [n for n in names if "|" in n and n not in std]
        interac = [n for n in names if ":" in n and n not in rand_eff + std]
        main_eff = [n for n in names if n not in interac + std + rand_eff + intercept]
        names = (
            intercept
            + sorted(main_eff, key=len)
            + sorted(interac, key=len)
            + sorted(rand_eff, key=len)
            + sorted(std, key=len)
        )
        return names
github bambinos / bambi / bambi / models.py View on Github external
):
        """Internal version of add(), with the same arguments.

        Runs during Model.build()
        """

        # use cleaned data with NAs removed (if user requested)
        data = self.clean_data
        # alter this pandas flag to avoid false positive SettingWithCopyWarnings
        data._is_copy = False  # pylint: disable=protected-access

        # Explicitly convert columns to category if desired--though this
        # can also be done within the formula using C().
        if categorical is not None:
            data = data.copy()
            cats = listify(categorical)
            data[cats] = data[cats].apply(lambda x: x.astype("category"))

        if fixed is not None:
            if "~" in fixed:
                # check to see if formula is using the 'y[event] ~ x' syntax
                # (for bernoulli models). If so, chop it into groups:
                # 1 = 'y[event]', 2 = 'y', 3 = 'event', 4 = 'x'
                # If this syntax is not being used, event = None
                event = re.match(r"^((\S+)\[(\S+)\])\s*~(.*)$", fixed)
                if event is not None:
                    fixed = "{}~{}".format(event.group(2), event.group(4))
                y_vector, x_matrix = dmatrices(fixed, data=data, NA_action="raise")
                y_label = y_vector.design_info.term_names[0]
                if event is not None:
                    # pass in new Y data that has 1 if y=event and 0 otherwise
                    y_data = y_vector[:, y_vector.design_info.column_names.index(event.group(1))]
github bambinos / bambi / bambi / results.py View on Github external
transformed : bool
            Whether or not to include internally transformed variables in the result. Default is
            False.
        chains: int, list
            Index, or list of indexes, of chains to concatenate. E.g., [1, 3] would concatenate
            the first and third chains, and ignore any others. If None (default), concatenates all
            available chains.
        """

        # filter out unwanted variables
        names = self._filter_names(var_names, ranefs, transformed)

        # concatenate the (pre-sliced) chains
        if chains is None:
            chains = list(range(self.n_chains))
        chains = listify(chains)
        data = self.data.T
        return {name: data[idx] for idx, name in enumerate(names)}
github bambinos / bambi / bambi / models.py View on Github external
"""Internal version of set_priors(), with same arguments.

        Runs during Model.build().
        """

        targets = {}

        if fixed is not None:
            targets.update({name: fixed for name in self.fixed_terms.keys()})

        if random is not None:
            targets.update({name: random for name in self.random_terms.keys()})

        if priors is not None:
            for k, prior in priors.items():
                for name in listify(k):
                    term_names = list(self.terms.keys())
                    msg = "No terms in model match '%s'." % name
                    if name not in term_names:
                        terms = self._match_derived_terms(name)
                        if not match_derived_names or terms is None:
                            raise ValueError(msg)
                        for term in terms:
                            targets[term.name] = prior
                    else:
                        targets[name] = prior

        for name, prior in targets.items():
            self.terms[name].prior = prior