Example #1
0
 def summary(self, xname=None, alpha=0.05, title=None):
     if self.effect is not None:
         # TODO: should also add some extra information, e.g. robust cov ?
         # TODO: can we infer names for constraints, xname in __init__ ?
         if title is None:
             title = 'Test for Constraints'
         elif title == '':
             # don't add any title,
             # I think SimpleTable skips on None - check
             title = None
         # we have everything for a params table
         use_t = (self.distribution == 't')
         yname='constraints' # Not used in params_frame
         if xname is None:
             xname = ['c%d'%ii for ii in range(len(self.effect))]
         from statsmodels.iolib.summary import summary_params
         summ = summary_params((self, self.effect, self.sd, self.statistic,
                                self.pvalue, self.conf_int(alpha)),
                               yname=yname, xname=xname, use_t=use_t,
                               title=title)
         return summ
     elif hasattr(self, 'fvalue'):
         # TODO: create something nicer for these casee
         return '<F test: F=%s, p=%s, df_denom=%d, df_num=%d>' % \
                (repr(self.fvalue), self.pvalue, self.df_denom, self.df_num)
     else:
         # generic
         return '<Wald test: statistic=%s, p-value=%s>' % \
                (self.statistic, self.pvalue)
Example #2
0
    def summary(self, xname=None, alpha=0.05, title=None):
        """Summarize the Results of the hypothesis test

        Parameters
        -----------

        xname : list of strings, optional
            Default is `c_##` for ## in p the number of regressors
        alpha : float
            significance level for the confidence intervals. Default is
            alpha = 0.05 which implies a confidence level of 95%.
        title : string, optional
            Title for the params table. If not None, then this replaces the
            default title

        Returns
        -------
        smry : string or Summary instance
            This contains a parameter results table in the case of t or z test
            in the same form as the parameter results table in the model
            results summary.
            For F or Wald test, the return is a string.

        """
        if self.effect is not None:
            # TODO: should also add some extra information, e.g. robust cov ?
            # TODO: can we infer names for constraints, xname in __init__ ?
            if title is None:
                title = 'Test for Constraints'
            elif title == '':
                # don't add any title,
                # I think SimpleTable skips on None - check
                title = None
            # we have everything for a params table
            use_t = (self.distribution == 't')
            yname = 'constraints'  # Not used in params_frame
            if xname is None:
                xname = ['c%d' % ii for ii in range(len(self.effect))]
            from statsmodels.iolib.summary import summary_params
            pvalues = np.atleast_1d(self.pvalue)
            summ = summary_params(
                (self, self.effect, self.sd, self.statistic, pvalues,
                 self.conf_int(alpha)),
                yname=yname,
                xname=xname,
                use_t=use_t,
                title=title,
                alpha=alpha)
            return summ
        elif hasattr(self, 'fvalue'):
            # TODO: create something nicer for these casee
            return '<F test: F=%s, p=%s, df_denom=%d, df_num=%d>' % \
                   (repr(self.fvalue), self.pvalue, self.df_denom, self.df_num)
        elif self.distribution == 'chi2':
            return '<Wald test (%s): statistic=%s, p-value=%s, df_denom=%d>' % \
                   (self.distribution, self.statistic, self.pvalue, self.df_denom)
        else:
            # generic
            return '<Wald test: statistic=%s, p-value=%s>' % \
                   (self.statistic, self.pvalue)
Example #3
0
    def summary(self, use_t=True, alpha=0.05, usevar='pooled', value=0):
        '''summarize the results of the hypothesis test

        Parameters
        ----------
        use_t : bool, optional
            if use_t is True, then t test results are returned
            if use_t is False, then z test results are returned
        alpha : float
            significance level for the confidence interval, coverage is
            ``1-alpha``
        usevar : string, 'pooled' or 'unequal'
            If ``pooled``, then the standard deviation of the samples is
            assumed to be the same. If ``unequal``, then the variance of
            Welsh ttest will be used, and the degrees of freedom are those
            of Satterthwaite if ``use_t`` is True.
        value : float
            difference between the means under the Null hypothesis.

        Returns
        -------
        smry : SimpleTable

        '''

        d1 = self.d1
        d2 = self.d2
        
        confint_percents = 100 - alpha * 100
        
        if use_t:
            tstat, pvalue, _ = self.ttest_ind(usevar=usevar, value=value)
            lower, upper = self.tconfint_diff(alpha=alpha, usevar=usevar)
        else:
            tstat, pvalue = self.ztest_ind(usevar=usevar, value=value)
            lower, upper = self.zconfint_diff(alpha=alpha, usevar=usevar)
        
        if usevar == 'pooled':
            std_err = self.std_meandiff_pooledvar
        else:
            std_err = self.std_meandiff_separatevar
        
        std_err = np.atleast_1d(std_err)
        tstat = np.atleast_1d(tstat)
        pvalue = np.atleast_1d(pvalue)
        lower = np.atleast_1d(lower)
        upper = np.atleast_1d(upper)
        conf_int = np.column_stack((lower, upper))
        params = d1.mean - d2.mean - value
        
        title = 'Test for equality of means'
        yname = 'y' # not used in params_frame
        xname = ['subset #%d'%(ii + 1) for ii in range(tstat.shape[0])]

        from statsmodels.iolib.summary import summary_params
        return summary_params((None, params, std_err, tstat, pvalue, conf_int),
                alpha=alpha, use_t=use_t, yname=yname, xname=xname,
                title=title)
Example #4
0
    def summary(self, use_t=True, alpha=0.05, usevar='pooled', value=0):
        '''summarize the results of the hypothesis test

        Parameters
        ----------
        use_t : bool, optional
            if use_t is True, then t test results are returned
            if use_t is False, then z test results are returned
        alpha : float
            significance level for the confidence interval, coverage is
            ``1-alpha``
        usevar : string, 'pooled' or 'unequal'
            If ``pooled``, then the standard deviation of the samples is
            assumed to be the same. If ``unequal``, then the variance of
            Welsh ttest will be used, and the degrees of freedom are those
            of Satterthwaite if ``use_t`` is True.
        value : float
            difference between the means under the Null hypothesis.

        Returns
        -------
        smry : SimpleTable

        '''

        d1 = self.d1
        d2 = self.d2
        
        confint_percents = 100 - alpha * 100
        
        if use_t:
            tstat, pvalue, _ = self.ttest_ind(usevar=usevar, value=value)
            lower, upper = self.tconfint_diff(alpha=alpha, usevar=usevar)
        else:
            tstat, pvalue = self.ztest_ind(usevar=usevar, value=value)
            lower, upper = self.zconfint_diff(alpha=alpha, usevar=usevar)
        
        if usevar == 'pooled':
            std_err = self.std_meandiff_pooledvar
        else:
            std_err = self.std_meandiff_separatevar
        
        std_err = np.atleast_1d(std_err)
        tstat = np.atleast_1d(tstat)
        pvalue = np.atleast_1d(pvalue)
        lower = np.atleast_1d(lower)
        upper = np.atleast_1d(upper)
        conf_int = np.column_stack((lower, upper))
        params = d1.mean - d2.mean - value
        
        title = 'Test for equality of means'
        yname = 'y' # not used in params_frame
        xname = ['subset #%d'%(ii + 1) for ii in range(tstat.shape[0])]

        from statsmodels.iolib.summary import summary_params
        return summary_params((None, params, std_err, tstat, pvalue, conf_int),
                alpha=alpha, use_t=use_t, yname=yname, xname=xname,
                title=title)
Example #5
0
    def summary(self, xname=None, alpha=0.05, title=None):
        """Summarize the Results of the hypothesis test

        Parameters
        -----------

        xname : list of strings, optional
            Default is `c_##` for ## in p the number of regressors
        alpha : float
            significance level for the confidence intervals. Default is
            alpha = 0.05 which implies a confidence level of 95%.
        title : string, optional
            Title for the params table. If not None, then this replaces the
            default title

        Returns
        -------
        smry : string or Summary instance
            This contains a parameter results table in the case of t or z test
            in the same form as the parameter results table in the model
            results summary.
            For F or Wald test, the return is a string.

        """
        if self.effect is not None:
            # TODO: should also add some extra information, e.g. robust cov ?
            # TODO: can we infer names for constraints, xname in __init__ ?
            if title is None:
                title = 'Test for Constraints'
            elif title == '':
                # don't add any title,
                # I think SimpleTable skips on None - check
                title = None
            # we have everything for a params table
            use_t = (self.distribution == 't')
            yname='constraints' # Not used in params_frame
            if xname is None:
                xname = ['c%d' % ii for ii in range(len(self.effect))]
            from statsmodels.iolib.summary import summary_params
            pvalues = np.atleast_1d(self.pvalue)
            summ = summary_params((self, self.effect, self.sd, self.statistic,
                                   pvalues, self.conf_int(alpha)),
                                  yname=yname, xname=xname, use_t=use_t,
                                  title=title, alpha=alpha)
            return summ
        elif hasattr(self, 'fvalue'):
            # TODO: create something nicer for these casee
            return ('<F test: F=%s, p=%s, df_denom=%.3g, df_num=%.3g>' %
                   (repr(self.fvalue), self.pvalue, self.df_denom,
                    self.df_num))
        elif self.distribution == 'chi2':
            return ('<Wald test (%s): statistic=%s, p-value=%s, df_denom=%.3g>' %
                   (self.distribution, self.statistic, self.pvalue,
                    self.df_denom))
        else:
            # generic
            return ('<Wald test: statistic=%s, p-value=%s>' %
                   (self.statistic, self.pvalue))
Example #6
0
    def get_confidence_interval(self):
        summary_data = summary_params(self.logit_result_linear).data
        intercept_range = [
            float(summary_data[1][5]),
            float(summary_data[1][6])
        ]
        slope_range = [float(summary_data[2][5]), float(summary_data[2][6])]

        # print(intercept_range)
        # print(slope_range)

        return intercept_range, slope_range
Example #7
0
            def make_table(self, mask, title, strip_end=True):
                res = (self, self.params[mask], self.bse[mask],
                       self.zvalues[mask], self.pvalues[mask],
                       self.conf_int(alpha)[mask])

                param_names = [
                    '.'.join(name.split('.')[:-1]) if strip_end else name
                    for name in
                    np.array(self.data.param_names)[mask].tolist()
                ]

                return summary_params(res, yname=None, xname=param_names,
                                      alpha=alpha, use_t=False, title=title)
Example #8
0
            def make_table(self, mask, title, strip_end=True):
                res = (self, self.params[mask], self.bse[mask],
                       self.zvalues[mask], self.pvalues[mask],
                       self.conf_int(alpha)[mask])

                param_names = [
                    '.'.join(name.split('.')[:-1]) if strip_end else name
                    for name in
                    np.array(self.data.param_names)[mask].tolist()
                ]

                return summary_params(res, yname=None, xname=param_names,
                                      alpha=alpha, use_t=False, title=title)
Example #9
0
    def summary(self, alpha=0.05, xname=None):
        """summary table for probability that random draw x1 is larger than x2

        Parameters
        ----------
        alpha : float
            Significance level for confidence intervals. Coverage is 1 - alpha
        xname : None or list of str
            If None, then each row has a name column with generic names.
            If xname is a list of strings, then it will be included as part
            of those names.

        Returns
        -------
        SimpleTable instance with methods to convert to different output
        formats.
        """

        yname = "None"
        effect = np.atleast_1d(self.prob1)
        if self.pvalue is None:
            statistic, pvalue = self.test_prob_superior()
        else:
            pvalue = self.pvalue
            statistic = self.statistic
        pvalues = np.atleast_1d(pvalue)
        ci = np.atleast_2d(self.conf_int(alpha))
        if ci.shape[0] > 1:
            ci = ci.T
        use_t = self.use_t
        sd = np.atleast_1d(np.sqrt(self.var_prob))
        statistic = np.atleast_1d(statistic)
        if xname is None:
            xname = ['c%d' % ii for ii in range(len(effect))]

        xname2 = ['prob(x1>x2) %s' % ii for ii in xname]

        title = "Probability sample 1 is stochastically larger"
        from statsmodels.iolib.summary import summary_params

        summ = summary_params((self, effect, sd, statistic, pvalues, ci),
                              yname=yname,
                              xname=xname2,
                              use_t=use_t,
                              title=title,
                              alpha=alpha)
        return summ
Example #10
0
            def make_table(self, mask, title, strip_end=True):
                res = (self, self.params[mask], self.bse[mask],
                       self.zvalues[mask], self.pvalues[mask],
                       self.conf_int(alpha)[mask])

                param_names = []
                for name in np.array(self.data.param_names)[mask].tolist():
                    if strip_end:
                        param_name = '.'.join(name.split('.')[:-1])
                    else:
                        param_name = name
                    if name in self.fixed_params:
                        param_name = '%s (fixed)' % param_name
                    param_names.append(param_name)

                return summary_params(res, yname=None, xname=param_names,
                                      alpha=alpha, use_t=False, title=title)
Example #11
0
    def summary(self, alpha=.05):
        """
        Returns a summary table for marginal effects

        Parameters
        ----------
        alpha : float
            Number between 0 and 1. The confidence intervals have the
            probability 1-alpha.

        Returns
        -------
        Summary : SummaryTable
            A SummaryTable instance
        """
        _check_at_is_all(self.margeff_options)
        results = self.results
        model = results.model
        title = model.__class__.__name__ + " Marginal Effects"
        method = self.margeff_options['method']
        top_left = [
            ('Dep. Variable:', [model.endog_names]),
            ('Method:', [method]),
            ('At:', [self.margeff_options['at']]),
        ]

        from statsmodels.iolib.summary import (Summary, summary_params,
                                               table_extend)
        exog_names = model.exog_names[:]  # copy
        smry = Summary()

        # sigh, we really need to hold on to this in _data...
        _, const_idx = _get_const_index(model.exog)
        if const_idx is not None:
            exog_names.pop(const_idx[0])

        J = int(getattr(model, "J", 1))
        if J > 1:
            yname, yname_list = results._get_endog_name(model.endog_names,
                                                        None,
                                                        all=True)
        else:
            yname = model.endog_names
            yname_list = [yname]

        smry.add_table_2cols(self,
                             gleft=top_left,
                             gright=[],
                             yname=yname,
                             xname=exog_names,
                             title=title)

        #NOTE: add_table_params is not general enough yet for margeff
        # could use a refactor with getattr instead of hard-coded params
        # tvalues etc.
        table = []
        conf_int = self.conf_int(alpha)
        margeff = self.margeff
        margeff_se = self.margeff_se
        tvalues = self.tvalues
        pvalues = self.pvalues
        if J > 1:
            for eq in range(J):
                restup = (results, margeff[:, eq], margeff_se[:, eq],
                          tvalues[:, eq], pvalues[:, eq], conf_int[:, :, eq])
                tble = summary_params(restup,
                                      yname=yname_list[eq],
                                      xname=exog_names,
                                      alpha=alpha,
                                      use_t=False,
                                      skip_header=True)
                tble.title = yname_list[eq]
                # overwrite coef with method name
                header = [
                    '', _transform_names[method], 'std err', 'z', 'P>|z|',
                    '[' + str(alpha / 2),
                    str(1 - alpha / 2) + ']'
                ]
                tble.insert_header_row(0, header)
                #from IPython.core.debugger import Pdb; Pdb().set_trace()
                table.append(tble)

            table = table_extend(table, keep_headers=True)
        else:
            restup = (results, margeff, margeff_se, tvalues, pvalues, conf_int)
            table = summary_params(restup,
                                   yname=yname,
                                   xname=exog_names,
                                   alpha=alpha,
                                   use_t=False,
                                   skip_header=True)
            header = [
                '', _transform_names[method], 'std err', 'z', 'P>|z|',
                '[' + str(alpha / 2),
                str(1 - alpha / 2) + ']'
            ]
            table.insert_header_row(0, header)

        smry.tables.append(table)
        return smry
Example #12
0
    def summary(self, alpha=.05):
        """
        Returns a summary table for marginal effects

        Parameters
        ----------
        alpha : float
            Number between 0 and 1. The confidence intervals have the
            probability 1-alpha.

        Returns
        -------
        Summary : SummaryTable
            A SummaryTable instance
        """
        _check_at_is_all(self.margeff_options)
        results = self.results
        model = results.model
        title = model.__class__.__name__ + " Marginal Effects"
        method = self.margeff_options['method']
        top_left = [('Dep. Variable:', [model.endog_names]),
                ('Method:', [method]),
                ('At:', [self.margeff_options['at']]),]

        from statsmodels.iolib.summary import (Summary, summary_params,
                                                table_extend)
        exog_names = model.exog_names[:] # copy
        smry = Summary()

        # sigh, we really need to hold on to this in _data...
        _, const_idx = _get_const_index(model.exog)
        if const_idx is not None:
            exog_names.pop(const_idx)

        J = int(getattr(model, "J", 1))
        if J > 1:
            yname, yname_list = results._get_endog_name(model.endog_names,
                                                None, all=True)
        else:
            yname = model.endog_names
            yname_list = [yname]

        smry.add_table_2cols(self, gleft=top_left, gright=[],
                yname=yname, xname=exog_names, title=title)

        #NOTE: add_table_params is not general enough yet for margeff
        # could use a refactor with getattr instead of hard-coded params
        # tvalues etc.
        table = []
        conf_int = self.conf_int(alpha)
        margeff = self.margeff
        margeff_se = self.margeff_se
        tvalues = self.tvalues
        pvalues = self.pvalues
        if J > 1:
            for eq in range(J):
                restup = (results, margeff[:,eq], margeff_se[:,eq],
                          tvalues[:,eq], pvalues[:,eq], conf_int[:,:,eq])
                tble = summary_params(restup, yname=yname_list[eq],
                              xname=exog_names, alpha=alpha, use_t=False,
                              skip_header=True)
                tble.title = yname_list[eq]
                # overwrite coef with method name
                header = ['', _transform_names[method], 'std err', 'z',
                        'P>|z|', '[%3.1f%% Conf. Int.]' % (100-alpha*100)]
                tble.insert_header_row(0, header)
                #from IPython.core.debugger import Pdb; Pdb().set_trace()
                table.append(tble)

            table = table_extend(table, keep_headers=True)
        else:
            restup = (results, margeff, margeff_se, tvalues, pvalues, conf_int)
            table = summary_params(restup, yname=yname, xname=exog_names,
                    alpha=alpha, use_t=False, skip_header=True)
            header = ['', _transform_names[method], 'std err', 'z',
                        'P>|z|', '[%3.1f%% Conf. Int.]' % (100-alpha*100)]
            table.insert_header_row(0, header)

        smry.tables.append(table)
        return smry
Example #13
0
 def get_parameter_summary(self):
     print(summary_params(self.logit_result_linear).data)