コード例 #1
0
ファイル: ancova.py プロジェクト: kyuhwas/formula
    def __init__(self, *expr_factor_tuples, **keywords):
        # set intercept / main_effect behaviour

        add_main_effects = False
        if "add_main_effects" in keywords:
            add_main_effects = keywords['add_main_effects']

        add_intercept = True
        if "add_intercept" in keywords:
            add_intercept = keywords['add_intercept']

        self.default_contrast = 'drop_reference'
        if "default_contrast" in keywords:
            self.default_contrast = keywords['default_contrast']

        self.graded_dict = {}

        # create a copy of graded_dict
        # removing duplicate tuples of factors in the values
        for expr_factors in expr_factor_tuples:
            # each element of the sequence should have the form
            # (sympy, [factors]) or sympy or (sympy, factor)
            if is_factor(expr_factors):
                expr_factors = (1, expr_factors)

            try:
                expr, factors = tuple(expr_factors)
            except TypeError:  # not a sequence
                expr, factors = expr_factors, ()

            if is_factor(factors):
                factors = [factors]
            factors = tuple(factors)
            l = self.graded_dict.setdefault(sympy.sympify(expr),
                                            {}).setdefault(len(factors), [])
            # ensure uniqueness
            if factors not in l:
                l.append(factors)

        # aliases for the intercept

        aliases_for_intercept = [1, 'constant', '(Intercept)']
        for s in aliases_for_intercept:
            if s in self.graded_dict:
                for k in self.graded_dict[s].keys():
                    self.graded_dict.setdefault(sympy.Number(1),
                                                {}).setdefault(k, []).extend(
                                                    self.graded_dict[s][k])
                del (self.graded_dict[s])

        if add_intercept:
            self.graded_dict.setdefault(sympy.Number(1), {})[0] = [()]

        if add_main_effects:
            for expr in self.graded_dict:
                self.graded_dict[expr][0] = [()]
コード例 #2
0
ファイル: ancova.py プロジェクト: statsmodels/formula
    def __init__(self, *expr_factor_tuples, **keywords):
        # set intercept / main_effect behaviour

        add_main_effects = False
        if "add_main_effects" in keywords:
            add_main_effects = keywords['add_main_effects']

        add_intercept=True
        if "add_intercept" in keywords:
            add_intercept = keywords['add_intercept']

        self.default_contrast='drop_reference'
        if "default_contrast" in keywords:
            self.default_contrast = keywords['default_contrast']

        self.graded_dict = {}

        # create a copy of graded_dict
        # removing duplicate tuples of factors in the values
        for expr_factors in expr_factor_tuples:
            # each element of the sequence should have the form
            # (sympy, [factors]) or sympy or (sympy, factor)
            if is_factor(expr_factors):
                expr_factors = (1, expr_factors)

            try:
                expr, factors = tuple(expr_factors)
            except TypeError: # not a sequence
                expr, factors = expr_factors, ()

            if is_factor(factors):
                factors = [factors]
            factors = tuple(factors)
            l = self.graded_dict.setdefault(sympy.sympify(expr), {}).setdefault(len(factors), [])
            # ensure uniqueness
            if factors not in l:
                l.append(factors)

        # aliases for the intercept

        aliases_for_intercept=[1, 'constant', '(Intercept)']
        for s in aliases_for_intercept:
            if s in self.graded_dict:
                for k in self.graded_dict[s].keys():
                    self.graded_dict.setdefault(sympy.Number(1), {}).setdefault(k, []).extend(self.graded_dict[s][k])
                del(self.graded_dict[s])

        if add_intercept:
            self.graded_dict.setdefault(sympy.Number(1), {})[0] = [()]

        if add_main_effects:
            for expr in self.graded_dict:
                self.graded_dict[expr][0] = [()]
コード例 #3
0
ファイル: ancova.py プロジェクト: jarrodmillman/formula
    def __init__(self, *expr_factor_tuples):
        self.graded_dict = {}

        # create a copy of graded_dict
        # removing duplicate tuples of factors in the values
        for expr_factors in expr_factor_tuples:
            # each element of the sequence should have the form
            # (sympy, [factors]) or sympy or (sympy, factor)
            try:
                expr, factors = tuple(expr_factors)
            except TypeError: # not a sequence
                expr, factors = expr_factors, ()

            if is_factor(factors):
                factors = [factors]
            factors = tuple(factors)
            self.graded_dict.setdefault(expr, {}).setdefault(len(factors), []).append(factors)

        # aliases for the intercept

        for s in ANCOVA.aliases_for_intercept:
            if s in self.graded_dict:
                for k in self.graded_dict[s].keys():
                    self.graded_dict.setdefault(sympy.Number(1), {}).setdefault(k, []).extend(self.graded_dict[s][k])
                del(self.graded_dict[s])

        if ANCOVA.add_intercept:
            self.graded_dict.setdefault(sympy.Number(1), {})[0] = [[]]

        if ANCOVA.add_main_effects:
            for expr in self.graded_dict:
                self.graded_dict[expr][0] = [[]] 
コード例 #4
0
ファイル: ancova.py プロジェクト: kyuhwas/formula
    def __mul__(self, other):
        if is_factor(other):
            return self.multiply_by_factor(other)
        elif is_ancova(other):
            result = []
            oseq = other.sequence
            sseq = self.sequence

            for se, sf in self.sequence():
                for oe, of in other.sequence():
                    result.append((se * oe, sf + of))
            return ANCOVA(*result)
        else:
            return self.multiply_by_expression(other)
コード例 #5
0
ファイル: ancova.py プロジェクト: statsmodels/formula
    def __mul__(self, other):
        if is_factor(other):
            return self.multiply_by_factor(other)
        elif is_ancova(other):
            result = []
            oseq = other.sequence
            sseq = self.sequence

            for se, sf in self.sequence():
                for oe, of in other.sequence():
                    result.append((se*oe, sf+of))
            return ANCOVA(*result)
        else:
            return self.multiply_by_expression(other)
コード例 #6
0
ファイル: ancova.py プロジェクト: kyuhwas/formula
    def sorted_factors(self):
        """
        Take every factor appearing in the formula
        and sort its levels. The sort matters because
        this is the column that is dropped when
        constructing a design in which this factor
        is to be coded as "contrast".

        >>> x = Term('x'); f = Factor('f', [2,1,3]) ; h =Factor('h', range(2))
        >>> a = ANCOVA((x,f), (x,(f,h)))
        >>> a.sorted_factors
        {'h': Factor('h', [0, 1]), 'f': Factor('f', [1, 2, 3])}
        >>>

        The ordering of the levels of the factors
        changes which columns are produced when
        a factor is coded as a contrast.

        >> x = Term('x'); f = Factor('f', [2,1,3]) ; h =Factor('h', range(2))
        >>> a = ANCOVA((x,h), (x,(f,h)))

        In this example, in the "x:f:h" term, "f" is coded
        as a contrast and its "first" level is dropped. This
        is the "first" of the sorted levels of "f".


        """
        if not hasattr(self, "_sorted_factors"):
            self._sorted_factors = {}
            for expr in self.graded_dict:
                for order in self.graded_dict[expr]:
                    for factors in self.graded_dict[expr][order]:
                        for factor in factors:
                            if is_factor(
                                    factor
                            ) and factor.name not in self._sorted_factors:
                                self._sorted_factors[factor.name] = Factor(
                                    factor.name, sorted(factor.levels))
        return self._sorted_factors
コード例 #7
0
ファイル: ancova.py プロジェクト: statsmodels/formula
    def sorted_factors(self):
        """
        Take every factor appearing in the formula
        and sort its levels. The sort matters because
        this is the column that is dropped when
        constructing a design in which this factor
        is to be coded as "contrast".

        >>> x = Term('x'); f = Factor('f', [2,1,3]) ; h =Factor('h', range(2))
        >>> a = ANCOVA((x,f), (x,(f,h)))
        >>> a.sorted_factors
        {'h': Factor('h', [0, 1]), 'f': Factor('f', [1, 2, 3])}
        >>>

        The ordering of the levels of the factors
        changes which columns are produced when
        a factor is coded as a contrast.

        >> x = Term('x'); f = Factor('f', [2,1,3]) ; h =Factor('h', range(2))
        >>> a = ANCOVA((x,h), (x,(f,h)))

        In this example, in the "x:f:h" term, "f" is coded
        as a contrast and its "first" level is dropped. This
        is the "first" of the sorted levels of "f".


        """
        if not hasattr(self, "_sorted_factors"):
            self._sorted_factors = {}
            for expr in self.graded_dict:
                for order in self.graded_dict[expr]:
                    for factors in self.graded_dict[expr][order]:
                        for factor in factors:
                            if is_factor(factor) and factor.name not in self._sorted_factors:
                                self._sorted_factors[factor.name] = Factor(factor.name, sorted(factor.levels))
        return self._sorted_factors
コード例 #8
0
ファイル: ancova.py プロジェクト: jarrodmillman/formula
 def __mul__(self, other):
     if is_factor(other):
         return self.multiply_by_factor(other)
     else:
         return self.multiply_by_expression(other)