예제 #1
0
    def _stats_table(self):
        # TODO: do we want individual statistics or should users just
        # use results if wanted?
        # Handle overall fit statistics

        model = self.model


        part2Lstubs = ('No. of Equations:',
                       'Nobs:',
                       'Log likelihood:',
                       'AIC:')
        part2Rstubs = ('BIC:',
                       'HQIC:',
                       'FPE:',
                       'Det(Omega_mle):')
        part2Ldata = [[model.neqs], [model.nobs], [model.llf], [model.aic]]
        part2Rdata = [[model.bic], [model.hqic], [model.fpe], [model.detomega]]
        part2Lheader = None
        part2L = SimpleTable(part2Ldata, part2Lheader, part2Lstubs,
                             txt_fmt = self.part2_fmt)
        part2R = SimpleTable(part2Rdata, part2Lheader, part2Rstubs,
                             txt_fmt = self.part2_fmt)
        part2L.extend_right(part2R)

        return str(part2L)
예제 #2
0
    def _stats_table(self):
        # TODO: do we want individual statistics or should users just
        # use results if wanted?
        # Handle overall fit statistics

        model = self.model

        part2Lstubs = ('No. of Equations:',
                       'Nobs:',
                       'Log likelihood:',
                       'AIC:')
        part2Rstubs = ('BIC:',
                       'HQIC:',
                       'FPE:',
                       'Det(Omega_mle):')
        part2Ldata = [[model.neqs], [model.nobs], [model.llf], [model.aic]]
        part2Rdata = [[model.bic], [model.hqic], [model.fpe], [model.detomega]]
        part2Lheader = None
        part2L = SimpleTable(part2Ldata, part2Lheader, part2Lstubs,
                             txt_fmt = self.part2_fmt)
        part2R = SimpleTable(part2Rdata, part2Lheader, part2Rstubs,
                             txt_fmt = self.part2_fmt)
        part2L.extend_right(part2R)

        return str(part2L)
예제 #3
0
파일: summarize.py 프로젝트: USC-CSSL/NTAP
    def _twocol_summary(self, lhs_data, rhs_data, title=None):
        if not isinstance(lhs_data, dict) or not isinstance(rhs_data, dict):
            raise ValueError(f"Args must be type `dict`")

        lhs_rownames, lhs_data = zip(*lhs_data.items())
        lhs_rownames = [rowname + ':' for rowname in lhs_rownames]

        rhs_rownames, rhs_data = zip(*rhs_data.items())
        rhs_rownames = [rowname + ':' for rowname in rhs_rownames]

        lhs_table = SimpleTable(lhs_data, None, lhs_rownames, title)
        rhs_table = SimpleTable(rhs_data, None, rhs_rownames, title)

        lhs_table.extend_right(rhs_table)

        return lhs_table
    def summary_old(self, yname=None, xname=None,
                    title='Generalized linear model', returns='text'):
        """
        Print a table of results or returns SimpleTable() instance which
        summarizes the Generalized linear model results.

        Parameters
        -----------
        yname : string
                optional, Default is `Y`
        xname : list of strings
                optional, Default is `X.#` for # in p the number of regressors
        title : string
                optional, Defualt is 'Generalized linear model'
        returns : string
                  'text', 'table', 'csv', 'latex', 'html'

        Returns
        -------
        Defualt :
        returns='print'
                Prints the summarirized results

        Option :
        returns='text'
                Prints the summarirized results

        Option :
        returns='table'
                 SimpleTable instance : summarizing the fit of a linear model.

        Option :
        returns='csv'
                returns a string of csv of the results, to import into a
                spreadsheet

        Option :
        returns='latex'
        Not implimented yet

        Option :
        returns='HTML'
        Not implimented yet


        Examples (needs updating)
        --------
        >>> import statsmodels.api as sm
        >>> data = sm.datasets.longley.load()
        >>> data.exog = sm.add_constant(data.exog)
        >>> ols_results = sm.OLS(data.endog, data.exog).results
        >>> print ols_results.summary()
        ...

        Notes
        -----
        stand_errors are not implimented.
        conf_int calculated from normal dist.
        """
        import time as Time
        from statsmodels.iolib import SimpleTable

        yname = 'Y'
        if xname is None:
            xname = ['x%d' % i for i in range(self.model.exog.shape[1])]

        #List of results used in summary
        #yname = yname
        #xname = xname
        time = Time.localtime()
        dist_family = self.model.family.__class__.__name__
        df_model = self.df_model
        df_resid = self.df_resid
        llf = self.llf
        nobs = self.nobs
        params = self.params
        scale = self.scale
        #TODO   #stand_errors = self.stand_errors
        stand_errors = self.bse  #[' ' for x in range(len(self.params))]
        #Added note about conf_int
        conf_int = self.conf_int()
        #f_test() = self.f_test()
        t = self.tvalues
        #t_test = self.t_test()



        table_1l_fmt = dict(
            data_fmts = ["%s", "%s", "%s", "%s", "%s"],
            empty_cell = '',
            colwidths = 15,
            colsep='   ',
            row_pre = '  ',
            row_post = '  ',
            table_dec_above='=',
            table_dec_below='',
            header_dec_below=None,
            header_fmt = '%s',
            stub_fmt = '%s',
            title_align='c',
            header_align = 'r',
            data_aligns = "r",
            stubs_align = "l",
            fmt = 'txt'
            )
        # Note table_1l_fmt over rides the below formating. in extend_right? JP
        table_1r_fmt = dict(
            data_fmts = ["%s", "%s", "%s", "%s", "%1s"],
            empty_cell = '',
            colwidths = 12,
            colsep='   ',
            row_pre = '',
            row_post = '',
            table_dec_above='=',
            table_dec_below='',
            header_dec_below=None,
            header_fmt = '%s',
            stub_fmt = '%s',
            title_align='c',
            header_align = 'r',
            data_aligns = "r",
            stubs_align = "l",
            fmt = 'txt'
            )

        table_2_fmt = dict(
            data_fmts = ["%s", "%s", "%s", "%s"],
            #data_fmts = ["%#12.6g","%#12.6g","%#10.4g","%#5.4g"],
            #data_fmts = ["%#10.4g","%#6.4f", "%#6.4f"],
            #data_fmts = ["%#15.4F","%#15.4F","%#15.4F","%#14.4G"],
            empty_cell = '',
            colwidths = 13,
            colsep=' ',
            row_pre = '  ',
            row_post = '   ',
            table_dec_above='=',
            table_dec_below='=',
            header_dec_below='-',
            header_fmt = '%s',
            stub_fmt = '%s',
            title_align='c',
            header_align = 'r',
            data_aligns = 'r',
            stubs_align = 'l',
            fmt = 'txt'
        )
        ########  summary table 1   #######
        table_1l_title = title
        table_1l_header = None
        table_1l_stubs = ('Model Family:',
                          'Method:',
                          'Dependent Variable:',
                          'Date:',
                          'Time:',
                          )
        table_1l_data = [
                         [dist_family],
                         ['IRLS'],
                         [yname],
                         [Time.strftime("%a, %d %b %Y",time)],
                         [Time.strftime("%H:%M:%S",time)],
                        ]
        table_1l = SimpleTable(table_1l_data,
                            table_1l_header,
                            table_1l_stubs,
                            title=table_1l_title,
                            txt_fmt = table_1l_fmt)
        table_1r_title = None
        table_1r_header = None
        table_1r_stubs = ('# of obs:',
                          'Df residuals:',
                          'Df model:',
                          'Scale:',
                          'Log likelihood:'
                          )
        table_1r_data = [
                         [nobs],
                         [df_resid],
                         [df_model],
                         ["%#6.4f" % (scale,)],
                         ["%#6.4f" % (llf,)]
                        ]
        table_1r = SimpleTable(table_1r_data,
                            table_1r_header,
                            table_1r_stubs,
                            title=table_1r_title,
                            txt_fmt = table_1r_fmt)

        ########  summary table 2   #######
        #TODO add % range to confidance interval column header
        table_2header = ('coefficient', 'stand errors', 't-statistic',
        'Conf. Interval')
        table_2stubs = xname
        table_2data = zip(
                      ["%#6.4f" % (params[i]) for i in range(len(xname))],
                      ["%#6.4f" % stand_errors[i] for i in range(len(xname))],
                      ["%#6.4f" % (t[i]) for i in range(len(xname))],
                      [""" [%#6.3f, %#6.3f]""" % tuple(conf_int[i]) for i in
                                                          range(len(xname))]
                      )


        #dfmt={'data_fmt':["%#12.6g","%#12.6g","%#10.4g","%#5.4g"]}
        table_2 = SimpleTable(table_2data,
                            table_2header,
                            table_2stubs,
                            title=None,
                            txt_fmt = table_2_fmt)

        ########  Return Summary Tables ########
        # join table table_s then print
        if returns == 'text':
            table_1l.extend_right(table_1r)
            return str(table_1l) + '\n' +  str(table_2)
        elif returns == 'print':
            table_1l.extend_right(table_1r)
            print(str(table_1l) + '\n' +  str(table_2))
        elif returns == 'tables':
            return [table_1l, table_1r, table_2]
            #return [table_1, table_2 ,table_3L, notes]
        elif returns == 'csv':
            return table_1.as_csv() + '\n' + table_2.as_csv() + '\n' + \
                   table_3L.as_csv()
        elif returns == 'latex':
            print('not avalible yet')
        elif returns == html:
            print('not avalible yet')
예제 #5
0
    def summary_old(self,
                    yname=None,
                    xname=None,
                    title='Generalized linear model',
                    returns='text'):
        """
        Print a table of results or returns SimpleTable() instance which
        summarizes the Generalized linear model results.

        Parameters
        -----------
        yname : string
                optional, Default is `Y`
        xname : list of strings
                optional, Default is `X.#` for # in p the number of regressors
        title : string
                optional, Defualt is 'Generalized linear model'
        returns : string
                  'text', 'table', 'csv', 'latex', 'html'

        Returns
        -------
        Defualt :
        returns='print'
                Prints the summarirized results

        Option :
        returns='text'
                Prints the summarirized results

        Option :
        returns='table'
                 SimpleTable instance : summarizing the fit of a linear model.

        Option :
        returns='csv'
                returns a string of csv of the results, to import into a
                spreadsheet

        Option :
        returns='latex'
        Not implimented yet

        Option :
        returns='HTML'
        Not implimented yet


        Examples (needs updating)
        --------
        >>> import statsmodels.api as sm
        >>> data = sm.datasets.longley.load()
        >>> data.exog = sm.add_constant(data.exog)
        >>> ols_results = sm.OLS(data.endog, data.exog).results
        >>> print ols_results.summary()
        ...

        Notes
        -----
        stand_errors are not implimented.
        conf_int calculated from normal dist.
        """
        import time as Time
        from statsmodels.iolib import SimpleTable

        yname = 'Y'
        if xname is None:
            xname = ['x%d' % i for i in range(self.model.exog.shape[1])]

        #List of results used in summary
        #yname = yname
        #xname = xname
        time = Time.localtime()
        dist_family = self.model.family.__class__.__name__
        df_model = self.df_model
        df_resid = self.df_resid
        llf = self.llf
        nobs = self.nobs
        params = self.params
        scale = self.scale
        #TODO   #stand_errors = self.stand_errors
        stand_errors = self.bse  #[' ' for x in range(len(self.params))]
        #Added note about conf_int
        conf_int = self.conf_int()
        #f_test() = self.f_test()
        t = self.tvalues
        #t_test = self.t_test()

        table_1l_fmt = dict(data_fmts=["%s", "%s", "%s", "%s", "%s"],
                            empty_cell='',
                            colwidths=15,
                            colsep='   ',
                            row_pre='  ',
                            row_post='  ',
                            table_dec_above='=',
                            table_dec_below='',
                            header_dec_below=None,
                            header_fmt='%s',
                            stub_fmt='%s',
                            title_align='c',
                            header_align='r',
                            data_aligns="r",
                            stubs_align="l",
                            fmt='txt')
        # Note table_1l_fmt over rides the below formating. in extend_right? JP
        table_1r_fmt = dict(data_fmts=["%s", "%s", "%s", "%s", "%1s"],
                            empty_cell='',
                            colwidths=12,
                            colsep='   ',
                            row_pre='',
                            row_post='',
                            table_dec_above='=',
                            table_dec_below='',
                            header_dec_below=None,
                            header_fmt='%s',
                            stub_fmt='%s',
                            title_align='c',
                            header_align='r',
                            data_aligns="r",
                            stubs_align="l",
                            fmt='txt')

        table_2_fmt = dict(
            data_fmts=["%s", "%s", "%s", "%s"],
            #data_fmts = ["%#12.6g","%#12.6g","%#10.4g","%#5.4g"],
            #data_fmts = ["%#10.4g","%#6.4f", "%#6.4f"],
            #data_fmts = ["%#15.4F","%#15.4F","%#15.4F","%#14.4G"],
            empty_cell='',
            colwidths=13,
            colsep=' ',
            row_pre='  ',
            row_post='   ',
            table_dec_above='=',
            table_dec_below='=',
            header_dec_below='-',
            header_fmt='%s',
            stub_fmt='%s',
            title_align='c',
            header_align='r',
            data_aligns='r',
            stubs_align='l',
            fmt='txt')
        ########  summary table 1   #######
        table_1l_title = title
        table_1l_header = None
        table_1l_stubs = (
            'Model Family:',
            'Method:',
            'Dependent Variable:',
            'Date:',
            'Time:',
        )
        table_1l_data = [
            [dist_family],
            ['IRLS'],
            [yname],
            [Time.strftime("%a, %d %b %Y", time)],
            [Time.strftime("%H:%M:%S", time)],
        ]
        table_1l = SimpleTable(table_1l_data,
                               table_1l_header,
                               table_1l_stubs,
                               title=table_1l_title,
                               txt_fmt=table_1l_fmt)
        table_1r_title = None
        table_1r_header = None
        table_1r_stubs = ('# of obs:', 'Df residuals:', 'Df model:', 'Scale:',
                          'Log likelihood:')
        table_1r_data = [[nobs], [df_resid], [df_model],
                         ["%#6.4f" % (scale, )], ["%#6.4f" % (llf, )]]
        table_1r = SimpleTable(table_1r_data,
                               table_1r_header,
                               table_1r_stubs,
                               title=table_1r_title,
                               txt_fmt=table_1r_fmt)

        ########  summary table 2   #######
        #TODO add % range to confidance interval column header
        table_2header = ('coefficient', 'stand errors', 't-statistic',
                         'Conf. Interval')
        table_2stubs = xname
        table_2data = zip(
            ["%#6.4f" % (params[i]) for i in range(len(xname))],
            ["%#6.4f" % stand_errors[i] for i in range(len(xname))],
            ["%#6.4f" % (t[i]) for i in range(len(xname))], [
                """ [%#6.3f, %#6.3f]""" % tuple(conf_int[i])
                for i in range(len(xname))
            ])

        #dfmt={'data_fmt':["%#12.6g","%#12.6g","%#10.4g","%#5.4g"]}
        table_2 = SimpleTable(table_2data,
                              table_2header,
                              table_2stubs,
                              title=None,
                              txt_fmt=table_2_fmt)

        ########  Return Summary Tables ########
        # join table table_s then print
        if returns == 'text':
            table_1l.extend_right(table_1r)
            return str(table_1l) + '\n' + str(table_2)
        elif returns == 'print':
            table_1l.extend_right(table_1r)
            print(str(table_1l) + '\n' + str(table_2))
        elif returns == 'tables':
            return [table_1l, table_1r, table_2]
            #return [table_1, table_2 ,table_3L, notes]
        elif returns == 'csv':
            return table_1.as_csv() + '\n' + table_2.as_csv() + '\n' + \
                   table_3L.as_csv()
        elif returns == 'latex':
            print('not avalible yet')
        elif returns == html:
            print('not avalible yet')