def summary(self, yname=None, xname=None, title=0, alpha=.05, return_fmt='text'): """ This is for testing the new summary setup """ from statsmodels.iolib.summary import (summary_top, summary_params, summary_return) ## left = [(i, None) for i in ( ## 'Dependent Variable:', ## 'Model type:', ## 'Method:', ## 'Date:', ## 'Time:', ## 'Number of Obs:', ## 'df resid', ## 'df model', ## )] top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['IRLS']), ('Norm:', [self.fit_options['norm']]), ('Scale Est.:', [self.fit_options['scale_est']]), ('Cov Type:', [self.fit_options['cov']]), ('Date:', None), ('Time:', None), ('No. Iterations:', ["%d" % self.fit_history['iteration']]) ] top_right = [('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None) ] if not title is None: title = "Robust linear Model Regression Results" #boiler plate from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, #[], yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=.05, use_t=False) #diagnostic table is not used yet # smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, # yname=yname, xname=xname, # title="") #add warnings/notes, added to text format only etext =[] wstr = \ '''If the model instance has been used for another fit with different fit parameters, then the fit options might not be the correct ones anymore .''' etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self, yname=None, xname=None, title=0, alpha=0.05, return_fmt="text"): """ This is for testing the new summary setup """ from statsmodels.iolib.summary import summary_top, summary_params, summary_return ## left = [(i, None) for i in ( ## 'Dependent Variable:', ## 'Model type:', ## 'Method:', ## 'Date:', ## 'Time:', ## 'Number of Obs:', ## 'df resid', ## 'df model', ## )] top_left = [ ("Dep. Variable:", None), ("Model:", None), ("Method:", ["IRLS"]), ("Norm:", [self.fit_options["norm"]]), ("Scale Est.:", [self.fit_options["scale_est"]]), ("Cov Type:", [self.fit_options["cov"]]), ("Date:", None), ("Time:", None), ("No. Iterations:", ["%d" % self.fit_history["iteration"]]), ] top_right = [("No. Observations:", None), ("Df Residuals:", None), ("Df Model:", None)] if not title is None: title = "Robust linear Model Regression Results" # boiler plate from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) # [], smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) # diagnostic table is not used yet # smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, # yname=yname, xname=xname, # title="") # add warnings/notes, added to text format only etext = [] wstr = """If the model instance has been used for another fit with different fit parameters, then the fit options might not be the correct ones anymore .""" etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self): """Summary table of first-stage estimation results""" stubs_lookup = { 'rsquared': 'R-squared', 'partial.rsquared': 'Partial R-squared', 'shea.rsquared': 'Shea\'s R-squared', 'f.stat': 'F-statistic', 'f.pval': 'P-value (F-stat)' } smry = Summary() diagnostics = self.diagnostics vals = [[_str(v) for v in diagnostics[c]] for c in diagnostics] stubs = [stubs_lookup[s] for s in list(diagnostics.columns)] header = list(diagnostics.index) params = [] for var in header: res = self.individual[var] v = c_[res.params.values, res.tstats.values] params.append(v.ravel()) params = array(params) if params.ndim == 1: params = params[:, None] params_fmt = [[_str(val) for val in row] for row in params.T] for i in range(1, len(params_fmt), 2): for j in range(len(params_fmt[i])): params_fmt[i][j] = '({0})'.format(params_fmt[i][j]) params_stub = [] for var in res.params.index: params_stub.extend([var, '']) title = 'First Stage Estimation Results' vals = table_concat((vals, params_fmt)) stubs = stub_concat((stubs, params_stub)) txt_fmt = default_txt_fmt.copy() txt_fmt['data_aligns'] = 'r' txt_fmt['header_align'] = 'r' table = SimpleTable(vals, headers=header, title=title, stubs=stubs, txt_fmt=txt_fmt) smry.tables.append(table) extra_txt = [ 'T-stats reported in parentheses', 'T-stats use same covariance type as original model' ] smry.add_extra_txt(extra_txt) return smry
def summary(self, yname=None, xname=None, title=0, alpha=.05, return_fmt='text'): """ This is for testing the new summary setup """ top_left = [ ('Dep. Variable:', None), ('Model:', None), ('Method:', ['IRLS']), ('Norm:', [self.fit_options['norm']]), ('Scale Est.:', [self.fit_options['scale_est']]), ('Cov Type:', [self.fit_options['cov']]), ('Date:', None), ('Time:', None), ('No. Iterations:', ["%d" % self.fit_history['iteration']]) ] top_right = [('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None)] if title is not None: title = "Robust linear Model Regression Results" # boiler plate from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) # add warnings/notes, added to text format only etext = [] wstr = ("If the model instance has been used for another fit " "with different fit\n" "parameters, then the fit options might not be the correct " "ones anymore .") etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self) -> Summary: """Summary of test, containing statistic, p-value and critical values""" if self.bandwidth == int(self.bandwidth): bw = str(int(self.bandwidth)) else: bw = f"{self.bandwidth:0.3f}" table_data = [ ("Test Statistic", f"{self.stat:0.3f}"), ("P-value", f"{self.pvalue:0.3f}"), ("Kernel", f"{self.kernel}"), ("Bandwidth", bw), ] title = self.name table = SimpleTable( table_data, stubs=None, title=title, colwidths=18, datatypes=[0, 1], data_aligns=("l", "r"), ) smry = Summary() smry.tables.append(table) cv_string = "Critical Values: " for val in self.critical_values.keys(): p = str(int(val)) + "%" cv_string += f"{self.critical_values[val]:0.2f}" cv_string += " (" + p + ")" cv_string += ", " # Remove trailing ,<space> cv_string = cv_string[:-2] extra_text = [ "Trend: " + TREND_DESCRIPTION[self._trend], cv_string, "Null Hypothesis: " + self.null_hypothesis, "Alternative Hypothesis: " + self.alternative_hypothesis, "Distribution Order: " + str(self.distribution_order), ] smry.add_extra_txt(extra_text) return smry
def summary(self, yname=None, xname=None, title=0, alpha=.05, return_fmt='text'): """ This is for testing the new summary setup """ top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['IRLS']), ('Norm:', [self.fit_options['norm']]), ('Scale Est.:', [self.fit_options['scale_est']]), ('Cov Type:', [self.fit_options['cov']]), ('Date:', None), ('Time:', None), ('No. Iterations:', ["%d" % self.fit_history['iteration']]) ] top_right = [('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None) ] if title is not None: title = "Robust linear Model Regression Results" # boiler plate from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) # add warnings/notes, added to text format only etext = [] wstr = ("If the model instance has been used for another fit " "with different fit\n" "parameters, then the fit options might not be the correct " "ones anymore .") etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self): """Summary of test, containing statistic, p-value and critical values """ table_data = [('Test Statistic', '{0:0.3f}'.format(self.stat)), ('P-value', '{0:0.3f}'.format(self.pvalue)), ('Lags', '{0:d}'.format(self.lags))] title = self._title if not title: title = self._test_name + " Results" table = SimpleTable(table_data, stubs=None, title=title, colwidths=18, datatypes=[0, 1], data_aligns=("l", "r")) smry = Summary() smry.tables.append(table) cv_string = 'Critical Values: ' cv = self._critical_values.keys() g = lambda x: float(x.split('%')[0]) cv_numeric = array(lmap(g, cv)) cv_numeric = sort(cv_numeric) for val in cv_numeric: p = str(int(val)) + '%' cv_string += '{0:0.2f}'.format(self._critical_values[p]) cv_string += ' (' + p + ')' if val != cv_numeric[-1]: cv_string += ', ' extra_text = [ 'Trend: ' + TREND_DESCRIPTION[self._trend], cv_string, 'Null Hypothesis: ' + self.null_hypothesis, 'Alternative Hypothesis: ' + self.alternative_hypothesis ] smry.add_extra_txt(extra_text) if self._summary_text: smry.add_extra_txt(self._summary_text) return smry
def summary(self): """Summary of test, containing statistic, p-value and critical values """ table_data = [('Test Statistic', '{0:0.3f}'.format(self.stat)), ('P-value', '{0:0.3f}'.format(self.pvalue)), ('Lags', '{0:d}'.format(self.lags))] title = self._title if not title: title = self._test_name + " Results" table = SimpleTable(table_data, stubs=None, title=title, colwidths=18, datatypes=[0, 1], data_aligns=("l", "r")) smry = Summary() smry.tables.append(table) cv_string = 'Critical Values: ' cv = self._critical_values.keys() g = lambda x: float(x.split('%')[0]) cv_numeric = array(lmap(g, cv)) cv_numeric = sort(cv_numeric) for val in cv_numeric: p = str(int(val)) + '%' cv_string += '{0:0.2f}'.format(self._critical_values[p]) cv_string += ' (' + p + ')' if val != cv_numeric[-1]: cv_string += ', ' extra_text = ['Trend: ' + TREND_DESCRIPTION[self._trend], cv_string, 'Null Hypothesis: ' + self.null_hypothesis, 'Alternative Hypothesis: ' + self.alternative_hypothesis] smry.add_extra_txt(extra_text) if self._summary_text: smry.add_extra_txt(self._summary_text) return smry
def summary(self): """ Constructs a summary of the results from a fit model. Returns ------- summary : Summary instance Object that contains tables and facilitated export to text, html or latex """ # Summary layout # 1. Overall information # 2. Mean parameters # 3. Volatility parameters # 4. Distribution parameters # 5. Notes model = self.model model_name = model.name + ' - ' + model.volatility.name # Summary Header top_left = [('Dep. Variable:', self._dep_name), ('Mean Model:', model.name), ('Vol Model:', model.volatility.name), ('Distribution:', model.distribution.name), ('Method:', 'Maximum Likelihood'), ('', ''), ('Date:', self._datetime.strftime('%a, %b %d %Y')), ('Time:', self._datetime.strftime('%H:%M:%S'))] top_right = [('R-squared:', '%#8.3f' % self.rsquared), ('Adj. R-squared:', '%#8.3f' % self.rsquared_adj), ('Log-Likelihood:', '%#10.6g' % self.loglikelihood), ('AIC:', '%#10.6g' % self.aic), ('BIC:', '%#10.6g' % self.bic), ('No. Observations:', self._nobs), ('Df Residuals:', self.nobs - self.num_params), ('Df Model:', self.num_params)] title = model_name + ' Model Results' stubs = [] vals = [] for stub, val in top_left: stubs.append(stub) vals.append([val]) table = SimpleTable(vals, txt_fmt=fmt_2cols, title=title, stubs=stubs) # create summary table instance smry = Summary() # Top Table # Parameter table fmt = fmt_2cols fmt['data_fmts'][1] = '%18s' top_right = [('%-21s' % (' ' + k), v) for k, v in top_right] stubs = [] vals = [] for stub, val in top_right: stubs.append(stub) vals.append([val]) table.extend_right(SimpleTable(vals, stubs=stubs)) smry.tables.append(table) conf_int = np.asarray(self.conf_int()) conf_int_str = [] for c in conf_int: conf_int_str.append('[' + format_float_fixed(c[0], 7, 3) + ',' + format_float_fixed(c[1], 7, 3) + ']') stubs = self._names header = ['coef', 'std err', 't', 'P>|t|', '95.0% Conf. Int.'] vals = (self.params, self.std_err, self.tvalues, self.pvalues, conf_int_str) formats = [(10, 4), (9, 3), (9, 3), (9, 3), None] pos = 0 param_table_data = [] for _ in range(len(vals[0])): row = [] for i, val in enumerate(vals): if isinstance(val[pos], np.float64): converted = format_float_fixed(val[pos], *formats[i]) else: converted = val[pos] row.append(converted) pos += 1 param_table_data.append(row) mc = self.model.num_params vc = self.model.volatility.num_params dc = self.model.distribution.num_params counts = (mc, vc, dc) titles = ('Mean Model', 'Volatility Model', 'Distribution') total = 0 for title, count in zip(titles, counts): if count == 0: continue table_data = param_table_data[total:total + count] table_stubs = stubs[total:total + count] total += count table = SimpleTable(table_data, stubs=table_stubs, txt_fmt=fmt_params, headers=header, title=title) smry.tables.append(table) extra_text = ['Covariance estimator: ' + self.cov_type] if self.convergence_flag: extra_text.append(""" WARNING: The optimizer did not indicate sucessful convergence. The message was {string_message}. See convergence_flag.""".format( string_message=self._optim_output[-1])) smry.add_extra_txt(extra_text) return smry
def summary(self): """Summary table of model comparison""" smry = Summary() models = list(self._results.keys()) title = 'Model Comparison' stubs = [ 'Dep. Variable', 'Estimator', 'No. Observations', 'Cov. Est.', 'R-squared', 'Adj. R-squared', 'F-statistic', 'P-value (F-stat)' ] dep_name = {} for key in self._results: dep_name[key] = self._results[key].model.dependent.cols[0] dep_name = Series(dep_name) vals = concat([ dep_name, self.estimator_method, self.nobs, self.cov_estimator, self.rsquared, self.rsquared_adj, self.f_statistic ], 1) vals = [[i for i in v] for v in vals.T.values] vals[2] = [str(v) for v in vals[2]] for i in range(4, len(vals)): vals[i] = [_str(v) for v in vals[i]] params = self.params tstats = self.tstats params_fmt = [] params_stub = [] for i in range(len(params)): params_fmt.append([_str(v) for v in params.values[i]]) tstats_fmt = [] for v in tstats.values[i]: v_str = _str(v) v_str = '({0})'.format(v_str) if v_str.strip() else v_str tstats_fmt.append(v_str) params_fmt.append(tstats_fmt) params_stub.append(params.index[i]) params_stub.append(' ') vals = table_concat((vals, params_fmt)) stubs = stub_concat((stubs, params_stub)) all_instr = [] for key in self._results: res = self._results[key] all_instr.append(res.model.instruments.cols) ninstr = max(map(lambda l: len(l), all_instr)) instruments = [] instrument_stub = ['Instruments'] for i in range(ninstr): if i > 0: instrument_stub.append('') row = [] for j in range(len(self._results)): instr = all_instr[j] if len(instr) > i: row.append(instr[i]) else: row.append('') instruments.append(row) if instruments: vals = table_concat((vals, instruments)) stubs = stub_concat((stubs, instrument_stub)) txt_fmt = default_txt_fmt.copy() txt_fmt['data_aligns'] = 'r' txt_fmt['header_align'] = 'r' table = SimpleTable(vals, headers=models, title=title, stubs=stubs, txt_fmt=txt_fmt) smry.tables.append(table) smry.add_extra_txt(['T-stats reported in parentheses']) return smry
def summary(self, yname=None, xname=None, title=None, alpha=.05): """Summarize the Regression Results Parameters ---------- yname : string, optional Default is `y` xname : list of strings, optional Default is `var_##` for ## in p the number of regressors title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ eigvals = self.eigenvals condno = self.condition_number top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['Least Squares']), ('Date:', None), ('Time:', None) ] top_right = [('Pseudo R-squared:', ["%#8.4g" % self.prsquared]), ('Bandwidth:', ["%#8.4g" % self.bandwidth]), ('Sparsity:', ["%#8.4g" % self.sparsity]), ('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None) ] if title is None: title = self.model.__class__.__name__ + ' ' + "Regression Results" # create summary table instance from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) # add warnings/notes, added to text format only etext = [] if eigvals[-1] < 1e-10: wstr = "The smallest eigenvalue is %6.3g. This might indicate " wstr += "that there are\n" wstr += "strong multicollinearity problems or that the design " wstr += "matrix is singular." wstr = wstr % eigvals[-1] etext.append(wstr) elif condno > 1000: # TODO: what is recommended wstr = "The condition number is large, %6.3g. This might " wstr += "indicate that there are\n" wstr += "strong multicollinearity or other numerical " wstr += "problems." wstr = wstr % condno etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self, yname=None, xname=None, title=None, alpha=.05): """Summarize the Regression Results Parameters ----------- yname : string, optional Default is `y` xname : list of strings, optional Default is `var_##` for ## in p the number of regressors title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ # #TODO: import where we need it (for now), add as cached attributes # from statsmodels.stats.stattools import (jarque_bera, # omni_normtest, durbin_watson) # jb, jbpv, skew, kurtosis = jarque_bera(self.wresid) # omni, omnipv = omni_normtest(self.wresid) # eigvals = self.eigenvals condno = self.condition_number # # self.diagn = dict(jb=jb, jbpv=jbpv, skew=skew, kurtosis=kurtosis, # omni=omni, omnipv=omnipv, condno=condno, # mineigval=eigvals[0]) top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['Least Squares']), ('Date:', None), ('Time:', None)] top_right = [ ('Pseudo R-squared:', ["%#8.4g" % self.prsquared]), ('Bandwidth:', ["%#8.4g" % self.bandwidth]), ('Sparsity:', ["%#8.4g" % self.sparsity]), ('No. Observations:', None), ('Df Residuals:', None), #[self.df_resid]), #TODO: spelling ('Df Model:', None) #[self.df_model]) ] # diagn_left = [('Omnibus:', ["%#6.3f" % omni]), # ('Prob(Omnibus):', ["%#6.3f" % omnipv]), # ('Skew:', ["%#6.3f" % skew]), # ('Kurtosis:', ["%#6.3f" % kurtosis]) # ] # # diagn_right = [('Durbin-Watson:', ["%#8.3f" % durbin_watson(self.wresid)]), # ('Jarque-Bera (JB):', ["%#8.3f" % jb]), # ('Prob(JB):', ["%#8.3g" % jbpv]), # ('Cond. No.', ["%#8.3g" % condno]) # ] if title is None: title = self.model.__class__.__name__ + ' ' + "Regression Results" #create summary table instance from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=.05, use_t=True) # smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, #yname=yname, xname=xname, #title="") #add warnings/notes, added to text format only etext = [] if eigvals[-1] < 1e-10: wstr = "The smallest eigenvalue is %6.3g. This might indicate " wstr += "that there are\n" wstr += "strong multicollinearity problems or that the design " wstr += "matrix is singular." wstr = wstr % eigvals[-1] etext.append(wstr) elif condno > 1000: #TODO: what is recommended wstr = "The condition number is large, %6.3g. This might " wstr += "indicate that there are\n" wstr += "strong multicollinearity or other numerical " wstr += "problems." wstr = wstr % condno etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self, yname=None, xname=None, title=None, alpha=.05): """ Summarize the Regression Results Parameters ----------- yname : string, optional Default is `y` xname : list of strings, optional Default is `var_##` for ## in p the number of regressors title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ top_left = [('Dep. Variable:', None), ('Model:', None), ('Model Family:', [self.family.__class__.__name__]), ('Link Function:', [self.family.link.__class__.__name__]), ('Method:', ['IRLS']), ('Date:', None), ('Time:', None), ('No. Iterations:', ["%d" % self.fit_history['iteration']]), ] top_right = [('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None), ('Scale:', [self.scale]), ('Log-Likelihood:', None), ('Deviance:', ["%#8.5g" % self.deviance]), ('Pearson chi2:', ["%#6.3g" % self.pearson_chi2]) ] if title is None: title = "Generalized Linear Model Regression Results" #create summary tables from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, # [], yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) if hasattr(self, 'constraints'): smry.add_extra_txt(['Model has been estimated subject to linear ' 'equality constraints.']) #diagnostic table is not used yet: #smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, # yname=yname, xname=xname, # title="") return smry
def summary( self, alpha=0.05, start=None, title=None, model_name=None, display_params=True, ): """ Summarize the Model Parameters ---------- alpha : float, optional Significance level for the confidence intervals. Default is 0.05. start : int, optional Integer of the start observation. Default is 0. model_name : str The name of the model used. Default is to use model class name. Returns ------- summary : Summary instance This holds the summary table and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary """ from statsmodels.iolib.summary import Summary # Model specification results model = self.model if title is None: title = "Statespace Model Results" if start is None: start = 0 if self.model._index_dates: ix = self.model._index d = ix[start] sample = ["%02d-%02d-%02d" % (d.month, d.day, d.year)] d = ix[-1] sample += ["- " + "%02d-%02d-%02d" % (d.month, d.day, d.year)] else: sample = [str(start), " - " + str(self.nobs)] # Standardize the model name as a list of str if model_name is None: model_name = model.__class__.__name__ # Diagnostic tests results try: het = self.test_heteroskedasticity(method="breakvar") except Exception: # FIXME: catch something specific het = np.array([[np.nan] * 2]) try: lb = self.test_serial_correlation(method="ljungbox") except Exception: # FIXME: catch something specific lb = np.array([[np.nan] * 2]).reshape(1, 2, 1) try: jb = self.test_normality(method="jarquebera") except Exception: # FIXME: catch something specific jb = np.array([[np.nan] * 4]) # Create the tables if not isinstance(model_name, list): model_name = [model_name] top_left = [("Dep. Variable:", None)] top_left.append(("Model:", [model_name[0]])) for i in range(1, len(model_name)): top_left.append(("", ["+ " + model_name[i]])) top_left += [ ("Date:", None), ("Time:", None), ("Sample:", [sample[0]]), ("", [sample[1]]), ] top_right = [ ("No. Observations:", [self.nobs]), ("Log Likelihood", ["%#5.3f" % self.llf]), ] if hasattr(self, "rsquared"): top_right.append(("R-squared:", ["%#8.3f" % self.rsquared])) top_right += [ ("AIC", ["%#5.3f" % self.aic]), ("BIC", ["%#5.3f" % self.bic]), ("HQIC", ["%#5.3f" % self.hqic]), ] if hasattr(self, "filter_results"): if (self.filter_results is not None and self.filter_results.filter_concentrated): top_right.append(("Scale", ["%#5.3f" % self.scale])) else: top_right.append(("Scale", ["%#5.3f" % self.scale])) if hasattr(self, "cov_type"): top_left.append(("Covariance Type:", [self.cov_type])) format_str = lambda array: [ # noqa:E731 ", ".join(["{0:.2f}".format(i) for i in array]) ] diagn_left = [ ("Ljung-Box (Q):", format_str(lb[:, 0, -1])), ("Prob(Q):", format_str(lb[:, 1, -1])), ("Heteroskedasticity (H):", format_str(het[:, 0])), ("Prob(H) (two-sided):", format_str(het[:, 1])), ] diagn_right = [ ("Jarque-Bera (JB):", format_str(jb[:, 0])), ("Prob(JB):", format_str(jb[:, 1])), ("Skew:", format_str(jb[:, 2])), ("Kurtosis:", format_str(jb[:, 3])), ] summary = Summary() summary.add_table_2cols(self, gleft=top_left, gright=top_right, title=title) if len(self.params) > 0 and display_params: summary.add_table_params(self, alpha=alpha, xname=self.param_names, use_t=False) summary.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, title="") # Add warnings/notes, added to text format only etext = [] if hasattr(self, "cov_type") and "description" in self.cov_kwds: etext.append(self.cov_kwds["description"]) if self._rank < (len(self.params) - len(self.fixed_params)): cov_params = self.cov_params() if len(self.fixed_params) > 0: mask = np.ix_(self._free_params_index, self._free_params_index) cov_params = cov_params[mask] etext.append("Covariance matrix is singular or near-singular," " with condition number %6.3g. Standard errors may be" " unstable." % _safe_cond(cov_params)) if etext: etext = [ "[{0}] {1}".format(i + 1, text) for i, text in enumerate(etext) ] etext.insert(0, "Warnings:") summary.add_extra_txt(etext) return summary
def summary(self, yname=None, xname=None, title=None, alpha=.05): """ Summarize the Regression Results Parameters ----------- yname : string, optional Default is `y` xname : list of strings, optional Default is `var_##` for ## in p the number of regressors title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ top_left = [ ('Dep. Variable:', None), ('Model:', None), ('Model Family:', [self.family.__class__.__name__]), ('Link Function:', [self.family.link.__class__.__name__]), ('Method:', ['IRLS']), ('Date:', None), ('Time:', None), ('No. Iterations:', ["%d" % self.fit_history['iteration']]), ] top_right = [('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None), ('Scale:', [self.scale]), ('Log-Likelihood:', None), ('Deviance:', ["%#8.5g" % self.deviance]), ('Pearson chi2:', ["%#6.3g" % self.pearson_chi2])] if title is None: title = "Generalized Linear Model Regression Results" #create summary tables from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols( self, gleft=top_left, gright=top_right, # [], yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) if hasattr(self, 'constraints'): smry.add_extra_txt([ 'Model has been estimated subject to linear ' 'equality constraints.' ]) #diagnostic table is not used yet: #smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, # yname=yname, xname=xname, # title="") return smry
def summary(self): """ Constructs a summary of the results from a fit model. Returns ------- summary : Summary instance Object that contains tables and facilitated export to text, html or latex """ # Summary layout # 1. Overall information # 2. Mean parameters # 3. Volatility parameters # 4. Distribution parameters # 5. Notes model = self.model model_name = model.name + " - " + model.volatility.name # Summary Header top_left = [ ("Dep. Variable:", self._dep_name), ("Mean Model:", model.name), ("Vol Model:", model.volatility.name), ("Distribution:", model.distribution.name), ("Method:", "User-specified Parameters"), ("", ""), ("Date:", self._datetime.strftime("%a, %b %d %Y")), ("Time:", self._datetime.strftime("%H:%M:%S")), ] top_right = [ ("R-squared:", "--"), ("Adj. R-squared:", "--"), ("Log-Likelihood:", "%#10.6g" % self.loglikelihood), ("AIC:", "%#10.6g" % self.aic), ("BIC:", "%#10.6g" % self.bic), ("No. Observations:", self._nobs), ("", ""), ("", ""), ] title = model_name + " Model Results" stubs = [] vals = [] for stub, val in top_left: stubs.append(stub) vals.append([val]) table = SimpleTable(vals, txt_fmt=fmt_2cols, title=title, stubs=stubs) # create summary table instance smry = Summary() # Top Table # Parameter table fmt = fmt_2cols fmt["data_fmts"][1] = "%18s" top_right = [("%-21s" % (" " + k), v) for k, v in top_right] stubs = [] vals = [] for stub, val in top_right: stubs.append(stub) vals.append([val]) table.extend_right(SimpleTable(vals, stubs=stubs)) smry.tables.append(table) stubs = self._names header = ["coef"] vals = (self.params,) formats = [(10, 4)] pos = 0 param_table_data = [] for _ in range(len(vals[0])): row = [] for i, val in enumerate(vals): if isinstance(val[pos], np.float64): converted = format_float_fixed(val[pos], *formats[i]) else: converted = val[pos] row.append(converted) pos += 1 param_table_data.append(row) mc = self.model.num_params vc = self.model.volatility.num_params dc = self.model.distribution.num_params counts = (mc, vc, dc) titles = ("Mean Model", "Volatility Model", "Distribution") total = 0 for title, count in zip(titles, counts): if count == 0: continue table_data = param_table_data[total : total + count] table_stubs = stubs[total : total + count] total += count table = SimpleTable(table_data, stubs=table_stubs, txt_fmt=fmt_params, headers=header, title=title) smry.tables.append(table) extra_text = ( "Results generated with user-specified parameters.", "Since the model was not estimated, there are no std. " "errors.", ) smry.add_extra_txt(extra_text) return smry
def summary(self): """Summary table of model estimation results""" title = self._method + ' Estimation Summary' mod = self.model top_left = [('Dep. Variable:', mod.dependent.cols[0]), ('Estimator:', self._method), ('No. Observations:', self.nobs), ('Date:', self._datetime.strftime('%a, %b %d %Y')), ('Time:', self._datetime.strftime('%H:%M:%S')), ('Cov. Estimator:', self._cov_type), ('', '')] top_right = [('R-squared:', _str(self.rsquared)), ('Adj. R-squared:', _str(self.rsquared_adj)), ('F-statistic:', _str(self.f_statistic.stat)), ('P-value (F-stat)', pval_format(self.f_statistic.pval)), ('Distribution:', str(self.f_statistic.dist_name)), ('', ''), ('', '')] stubs = [] vals = [] for stub, val in top_left: stubs.append(stub) vals.append([val]) table = SimpleTable(vals, txt_fmt=fmt_2cols, title=title, stubs=stubs) # create summary table instance smry = Summary() # Top Table # Parameter table fmt = fmt_2cols fmt['data_fmts'][1] = '%18s' top_right = [('%-21s' % (' ' + k), v) for k, v in top_right] stubs = [] vals = [] for stub, val in top_right: stubs.append(stub) vals.append([val]) table.extend_right(SimpleTable(vals, stubs=stubs)) smry.tables.append(table) param_data = c_[self.params.values[:, None], self.std_errors.values[:, None], self.tstats.values[:, None], self.pvalues.values[:, None], self.conf_int()] data = [] for row in param_data: txt_row = [] for i, v in enumerate(row): f = _str if i == 3: f = pval_format txt_row.append(f(v)) data.append(txt_row) title = 'Parameter Estimates' table_stubs = list(self.params.index) header = [ 'Parameters', 'Std. Err.', 'T-stat', 'P-value', 'Lower CI', 'Upper CI' ] table = SimpleTable(data, stubs=table_stubs, txt_fmt=fmt_params, headers=header, title=title) smry.tables.append(table) instruments = self.model.instruments if instruments.shape[1] > 0: extra_text = [] endog = self.model.endog extra_text.append('Endogenous: ' + ', '.join(endog.cols)) extra_text.append('Instruments: ' + ', '.join(instruments.cols)) cov_descr = str(self._cov_estimator) for line in cov_descr.split('\n'): extra_text.append(line) smry.add_extra_txt(extra_text) return smry
def summary(self, yname=None, xname=None, title=None, alpha=.05): """Summarize the Regression Results Parameters ---------- yname : str, optional Default is `y` xname : list[str], optional Names for the exogenous variables. Default is `var_##` for ## in the number of regressors. Must match the number of parameters in the model title : str, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ eigvals = self.eigenvals condno = self.condition_number top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['Least Squares']), ('Date:', None), ('Time:', None) ] top_right = [('Pseudo R-squared:', ["%#8.4g" % self.prsquared]), ('Bandwidth:', ["%#8.4g" % self.bandwidth]), ('Sparsity:', ["%#8.4g" % self.sparsity]), ('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None) ] if title is None: title = self.model.__class__.__name__ + ' ' + "Regression Results" # create summary table instance from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) # add warnings/notes, added to text format only etext = [] if eigvals[-1] < 1e-10: wstr = "The smallest eigenvalue is %6.3g. This might indicate " wstr += "that there are\n" wstr += "strong multicollinearity problems or that the design " wstr += "matrix is singular." wstr = wstr % eigvals[-1] etext.append(wstr) elif condno > 1000: # TODO: what is recommended wstr = "The condition number is large, %6.3g. This might " wstr += "indicate that there are\n" wstr += "strong multicollinearity or other numerical " wstr += "problems." wstr = wstr % condno etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self): """ Constructs a summary of the results from a fit model. Returns ------- summary : Summary instance Object that contains tables and facilitated export to text, html or latex """ # Summary layout # 1. Overall information # 2. Mean parameters # 3. Volatility parameters # 4. Distribution parameters # 5. Notes model = self.model model_name = model.name + ' - ' + model.volatility.name # Summary Header top_left = [('Dep. Variable:', self._dep_name), ('Mean Model:', model.name), ('Vol Model:', model.volatility.name), ('Distribution:', model.distribution.name), ('Method:', 'User-specified Parameters'), ('', ''), ('Date:', self._datetime.strftime('%a, %b %d %Y')), ('Time:', self._datetime.strftime('%H:%M:%S'))] top_right = [ ('R-squared:', '--'), ('Adj. R-squared:', '--'), ('Log-Likelihood:', '%#10.6g' % self.loglikelihood), ('AIC:', '%#10.6g' % self.aic), ('BIC:', '%#10.6g' % self.bic), ('No. Observations:', self._nobs), ('', ''), ('', ''), ] title = model_name + ' Model Results' stubs = [] vals = [] for stub, val in top_left: stubs.append(stub) vals.append([val]) table = SimpleTable(vals, txt_fmt=fmt_2cols, title=title, stubs=stubs) # create summary table instance smry = Summary() # Top Table # Parameter table fmt = fmt_2cols fmt['data_fmts'][1] = '%18s' top_right = [('%-21s' % (' ' + k), v) for k, v in top_right] stubs = [] vals = [] for stub, val in top_right: stubs.append(stub) vals.append([val]) table.extend_right(SimpleTable(vals, stubs=stubs)) smry.tables.append(table) stubs = self._names header = ['coef'] vals = (self.params, ) formats = [(10, 4)] pos = 0 param_table_data = [] for _ in range(len(vals[0])): row = [] for i, val in enumerate(vals): if isinstance(val[pos], np.float64): converted = format_float_fixed(val[pos], *formats[i]) else: converted = val[pos] row.append(converted) pos += 1 param_table_data.append(row) mc = self.model.num_params vc = self.model.volatility.num_params dc = self.model.distribution.num_params counts = (mc, vc, dc) titles = ('Mean Model', 'Volatility Model', 'Distribution') total = 0 for title, count in zip(titles, counts): if count == 0: continue table_data = param_table_data[total:total + count] table_stubs = stubs[total:total + count] total += count table = SimpleTable(table_data, stubs=table_stubs, txt_fmt=fmt_params, headers=header, title=title) smry.tables.append(table) extra_text = ('Results generated with user-specified parameters.', 'Since the model was not estimated, there are no std. ' 'errors.') smry.add_extra_txt(extra_text) return smry
def summary(self, alpha=.05, start=None, model_name=None): """ Summarize the Model Parameters ---------- alpha : float, optional Significance level for the confidence intervals. Default is 0.05. start : int, optional Integer of the start observation. Default is 0. model_name : string The name of the model used. Default is to use model class name. Returns ------- summary : Summary instance This holds the summary table and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary """ from statsmodels.iolib.summary import Summary model = self.model title = 'Statespace Model Results' if start is None: start = 0 if self.data.dates is not None: dates = self.data.dates d = dates[start] sample = ['%02d-%02d-%02d' % (d.month, d.day, d.year)] d = dates[-1] sample += ['- ' + '%02d-%02d-%02d' % (d.month, d.day, d.year)] else: sample = [str(start), ' - ' + str(self.model.nobs)] if model_name is None: model_name = model.__class__.__name__ top_left = [('Dep. Variable:', None), ('Model:', [model_name]), ('Date:', None), ('Time:', None), ('Sample:', [sample[0]]), ('', [sample[1]])] top_right = [('No. Observations:', [self.model.nobs]), ('Log Likelihood', ["%#5.3f" % self.llf]), ('AIC', ["%#5.3f" % self.aic]), ('BIC', ["%#5.3f" % self.bic]), ('HQIC', ["%#5.3f" % self.hqic])] if hasattr(self, 'cov_type'): top_left.append(('Covariance Type:', [self.cov_type])) summary = Summary() summary.add_table_2cols(self, gleft=top_left, gright=top_right, title=title) summary.add_table_params(self, alpha=alpha, xname=self._param_names, use_t=False) # Add warnings/notes, added to text format only etext = [] if hasattr(self, 'cov_type'): etext.append(self.cov_kwds['description']) if etext: etext = [ "[{0}] {1}".format(i + 1, text) for i, text in enumerate(etext) ] etext.insert(0, "Warnings:") summary.add_extra_txt(etext) return summary
def summary(self): """ Constructs a summary of the results from a fit model. Returns ------- summary : Summary instance Object that contains tables and facilitated export to text, html or latex """ # Summary layout # 1. Overall information # 2. Mean parameters # 3. Volatility parameters # 4. Distribution parameters # 5. Notes model = self.model model_name = model.name + ' - ' + model.volatility.name # Summary Header top_left = [('Dep. Variable:', self._dep_name), ('Mean Model:', model.name), ('Vol Model:', model.volatility.name), ('Distribution:', model.distribution.name), ('Method:', 'User-specified Parameters'), ('', ''), ('Date:', self._datetime.strftime('%a, %b %d %Y')), ('Time:', self._datetime.strftime('%H:%M:%S'))] top_right = [('R-squared:', '--'), ('Adj. R-squared:', '--'), ('Log-Likelihood:', '%#10.6g' % self.loglikelihood), ('AIC:', '%#10.6g' % self.aic), ('BIC:', '%#10.6g' % self.bic), ('No. Observations:', self._nobs), ('', ''), ('', ''),] title = model_name + ' Model Results' stubs = [] vals = [] for stub, val in top_left: stubs.append(stub) vals.append([val]) table = SimpleTable(vals, txt_fmt=fmt_2cols, title=title, stubs=stubs) # create summary table instance smry = Summary() # Top Table # Parameter table fmt = fmt_2cols fmt['data_fmts'][1] = '%18s' top_right = [('%-21s' % (' ' + k), v) for k, v in top_right] stubs = [] vals = [] for stub, val in top_right: stubs.append(stub) vals.append([val]) table.extend_right(SimpleTable(vals, stubs=stubs)) smry.tables.append(table) stubs = self._names header = ['coef'] vals = (self.params,) formats = [(10, 4)] pos = 0 param_table_data = [] for _ in range(len(vals[0])): row = [] for i, val in enumerate(vals): if isinstance(val[pos], np.float64): converted = format_float_fixed(val[pos], *formats[i]) else: converted = val[pos] row.append(converted) pos += 1 param_table_data.append(row) mc = self.model.num_params vc = self.model.volatility.num_params dc = self.model.distribution.num_params counts = (mc, vc, dc) titles = ('Mean Model', 'Volatility Model', 'Distribution') total = 0 for title, count in zip(titles, counts): if count == 0: continue table_data = param_table_data[total:total + count] table_stubs = stubs[total:total + count] total += count table = SimpleTable(table_data, stubs=table_stubs, txt_fmt=fmt_params, headers=header, title=title) smry.tables.append(table) extra_text = ('Results generated with user-specified parameters.', 'Since the model was not estimated, there are no std. ' 'errors.') smry.add_extra_txt(extra_text) return smry
def summary(self, alpha=.05, start=None, model_name=None): """ Summarize the Model Parameters ---------- alpha : float, optional Significance level for the confidence intervals. Default is 0.05. start : int, optional Integer of the start observation. Default is 0. model_name : string The name of the model used. Default is to use model class name. Returns ------- summary : Summary instance This holds the summary table and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary """ from statsmodels.iolib.summary import Summary model = self.model title = 'Statespace Model Results' if start is None: start = 0 if self.data.dates is not None: dates = self.data.dates d = dates[start] sample = ['%02d-%02d-%02d' % (d.month, d.day, d.year)] d = dates[-1] sample += ['- ' + '%02d-%02d-%02d' % (d.month, d.day, d.year)] else: sample = [str(start), ' - ' + str(self.model.nobs)] if model_name is None: model_name = model.__class__.__name__ top_left = [ ('Dep. Variable:', None), ('Model:', [model_name]), ('Date:', None), ('Time:', None), ('Sample:', [sample[0]]), ('', [sample[1]]) ] top_right = [ ('No. Observations:', [self.model.nobs]), ('Log Likelihood', ["%#5.3f" % self.llf]), ('AIC', ["%#5.3f" % self.aic]), ('BIC', ["%#5.3f" % self.bic]), ('HQIC', ["%#5.3f" % self.hqic]) ] if hasattr(self, 'cov_type'): top_left.append(('Covariance Type:', [self.cov_type])) summary = Summary() summary.add_table_2cols(self, gleft=top_left, gright=top_right, title=title) summary.add_table_params(self, alpha=alpha, xname=self.data.param_names, use_t=False) # Add warnings/notes, added to text format only etext = [] if hasattr(self, 'cov_type'): etext.append(self.cov_kwds['description']) if etext: etext = ["[{0}] {1}".format(i + 1, text) for i, text in enumerate(etext)] etext.insert(0, "Warnings:") summary.add_extra_txt(etext) return summary
def summary(self, regpyhdfe, yname=None, xname=None, title=None, alpha=.05): """ Summarize the Regression Results. Parameters ---------- yname : str, optional Name of endogenous (response) variable. The Default is `y`. xname : list[str], optional Names for the exogenous variables. Default is `var_##` for ## in the number of regressors. Must match the number of parameters in the model. title : str, optional Title for the top table. If not None, then this replaces the default title. alpha : float The significance level for the confidence intervals. Returns ------- Summary Instance holding the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : A class that holds summary results. """ ########################################################################################################## ########################################################################################################## # https://apithymaxim.wordpress.com/2020/03/16/clustering-standard-errors-by-hand-using-python/ # http://cameron.econ.ucdavis.edu/research/Cameron_Miller_JHR_2015_February.pdf #N,k,Nclusts = len(df.index),3,50 # Number of observations, right hand side columns counting constant, number of clusters #X = np.hstack( (np.random.random((N,k-1)), np.ones((N,1)) ) ) #X = get_np_columns(df, ['wks_ue', 'tenure'], intercept=True) X = regpyhdfe.data[:, 1:] #y = get_np_columns(df, ['ttl_exp']) y = np.expand_dims(regpyhdfe.data[:, 0], 1) # Calculate (X'X)^-1 and the vector of coefficients, beta XX_inv = np.linalg.inv(X.T.dot(X)) beta = (XX_inv).dot(X.T.dot(y)) resid = y - X.dot(beta) #ID = np.random.choice([x for x in range(Nclusts)],N) # Vector of cluster IDs #ID = np.squeeze(get_np_columns(df, ['delete_me'])) ID = np.squeeze(regpyhdfe.groups_np) c_list = np.unique(ID) # Get unique list of clusters N, k, Nclusts = X.shape[0], X.shape[1], int(c_list.shape[0]) sum_XuuTX = 0 for c in range(0, Nclusts): in_cluster = (ID == c_list[c]) # Indicator for given cluster value resid_c = resid[in_cluster] uuT = resid_c.dot(resid_c.T) Xc = X[in_cluster] XuuTX = Xc.T.dot(uuT).dot(Xc) sum_XuuTX += XuuTX adj = (Nclusts / (Nclusts - 1)) * ( (N - 1) / (N - k) ) # Degrees of freedom correction from https://www.stata.com/manuals13/u20.pdf p. 54 # TODO: actually check if the fixed effects are nested df_a_nested = 1 adj = ((N - 1) / (N - df_a_nested - k)) * (Nclusts / (Nclusts - 1)) V_beta = adj * (XX_inv.dot(sum_XuuTX).dot(XX_inv)) se_beta = np.sqrt(np.diag(V_beta)) # Output data for Stata for_stata = pd.DataFrame(X) for_stata.columns = ["X" + str(i) for i in range(k)] for_stata['ID'] = ID for_stata['y'] = y ##for_stata.to_stata("resid_test.dta") print('B', beta, '\n SE: \n', se_beta) beta = np.squeeze(beta) t_values = beta / se_beta print('T values', t_values) from scipy.stats import t p_values = 2 * t.cdf(-np.abs(t_values), regpyhdfe.model.df_resid) # confidence interval size t_interval = np.asarray( t.interval(alpha=(1 - alpha), df=regpyhdfe.model.df_resid)) print("t_interval", t_interval) intervals = np.empty(shape=(beta.shape[0], 2)) # for each variables for i in range(0, intervals.shape[0]): intervals[i] = t_interval * se_beta[i] + beta[i] print('intervals', intervals) tmp1 = np.linalg.solve(V_beta, np.mat(beta).T) tmp2 = np.dot(np.mat(beta), tmp1) fvalue = tmp2[0, 0] / k import pdb pdb.set_trace() print('fvalue', fvalue) # from statsmodels.stats.stattools import ( # jarque_bera, omni_normtest, durbin_watson) # jb, jbpv, skew, kurtosis = jarque_bera(self.wresid) # omni, omnipv = omni_normtest(self.wresid) # eigvals = self.eigenvals # condno = self.condition_number # TODO: Avoid adding attributes in non-__init__ # self.diagn = dict(jb=jb, jbpv=jbpv, skew=skew, kurtosis=kurtosis, # omni=omni, omnipv=omnipv, condno=condno, # mineigval=eigvals[-1]) # TODO not used yet # diagn_left_header = ['Models stats'] # diagn_right_header = ['Residual stats'] # TODO: requiring list/iterable is a bit annoying # need more control over formatting # TODO: default do not work if it's not identically spelled top_left = [ ('Dep. Variable:', None), ('Model:', None), ('Method:', ['Least Squares']), ('Date:', None), ('Time:', None), ('No. Observations:', None), ('Df Residuals:', None), ('Df Model:', None), ] if hasattr(self, 'cov_type'): top_left.append(('Covariance Type:', [self.cov_type])) rsquared_type = '' if self.k_constant else ' (uncentered)' top_right = [ ('R-squared' + rsquared_type + ':', ["%#8.3f" % self.rsquared]), ('Adj. R-squared' + rsquared_type + ':', ["%#8.3f" % self.rsquared_adj]), ('F-statistic:', ["%#8.4g" % self.fvalue]), ('Prob (F-statistic):', ["%#6.3g" % self.f_pvalue]), ] # diagn_left = [('Omnibus:', ["%#6.3f" % omni]), # ('Prob(Omnibus):', ["%#6.3f" % omnipv]), # ('Skew:', ["%#6.3f" % skew]), # ('Kurtosis:', ["%#6.3f" % kurtosis]) # ] # # diagn_right = [('Durbin-Watson:', # ["%#8.3f" % durbin_watson(self.wresid)] # ), # ('Jarque-Bera (JB):', ["%#8.3f" % jb]), # ('Prob(JB):', ["%#8.3g" % jbpv]), # ] if title is None: title = self.model.__class__.__name__ + ' ' + "Regression Results" # create summary table instance from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=alpha, use_t=self.use_t) # smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, # yname=yname, xname=xname, # title="") # add warnings/notes, added to text format only etext = [] if not self.k_constant: etext.append("R² is computed without centering (uncentered) since the " "model does not contain a constant.") if hasattr(self, 'cov_type'): etext.append(self.cov_kwds['description']) if self.model.exog.shape[0] < self.model.exog.shape[1]: wstr = "The input rank is higher than the number of observations." etext.append(wstr) # if eigvals[-1] < 1e-10: # wstr = "The smallest eigenvalue is %6.3g. This might indicate " # wstr += "that there are\n" # wstr += "strong multicollinearity problems or that the design " # wstr += "matrix is singular." # wstr = wstr % eigvals[-1] # etext.append(wstr) # elif condno > 1000: # TODO: what is recommended? # wstr = "The condition number is large, %6.3g. This might " # wstr += "indicate that there are\n" # wstr += "strong multicollinearity or other numerical " # wstr += "problems." # wstr = wstr % condno # etext.append(wstr) if etext: etext = [ "[{0}] {1}".format(i + 1, text) for i, text in enumerate(etext) ] etext.insert(0, "Notes:") smry.add_extra_txt(etext) return smry
def summary(self, yname=None, xname=None, title=None, alpha=.05): """Summarize the Regression Results Parameters ----------- yname : string, optional Default is `y` xname : list of strings, optional Default is `var_##` for ## in p the number of regressors title : string, optional Title for the top table. If not None, then this replaces the default title alpha : float significance level for the confidence intervals Returns ------- smry : Summary instance this holds the summary tables and text, which can be printed or converted to various output formats. See Also -------- statsmodels.iolib.summary.Summary : class to hold summary results """ # #TODO: import where we need it (for now), add as cached attributes # from statsmodels.stats.stattools import (jarque_bera, # omni_normtest, durbin_watson) # jb, jbpv, skew, kurtosis = jarque_bera(self.wresid) # omni, omnipv = omni_normtest(self.wresid) # eigvals = self.eigenvals condno = self.condition_number # # self.diagn = dict(jb=jb, jbpv=jbpv, skew=skew, kurtosis=kurtosis, # omni=omni, omnipv=omnipv, condno=condno, # mineigval=eigvals[0]) top_left = [('Dep. Variable:', None), ('Model:', None), ('Method:', ['Least Squares']), ('Date:', None), ('Time:', None) ] top_right = [('Pseudo R-squared:', ["%#8.4g" % self.prsquared]), ('Bandwidth:', ["%#8.4g" % self.bandwidth]), ('Sparsity:', ["%#8.4g" % self.sparsity]), ('No. Observations:', None), ('Df Residuals:', None), #[self.df_resid]), #TODO: spelling ('Df Model:', None) #[self.df_model]) ] # diagn_left = [('Omnibus:', ["%#6.3f" % omni]), # ('Prob(Omnibus):', ["%#6.3f" % omnipv]), # ('Skew:', ["%#6.3f" % skew]), # ('Kurtosis:', ["%#6.3f" % kurtosis]) # ] # # diagn_right = [('Durbin-Watson:', ["%#8.3f" % durbin_watson(self.wresid)]), # ('Jarque-Bera (JB):', ["%#8.3f" % jb]), # ('Prob(JB):', ["%#8.3g" % jbpv]), # ('Cond. No.', ["%#8.3g" % condno]) # ] if title is None: title = self.model.__class__.__name__ + ' ' + "Regression Results" #create summary table instance from statsmodels.iolib.summary import Summary smry = Summary() smry.add_table_2cols(self, gleft=top_left, gright=top_right, yname=yname, xname=xname, title=title) smry.add_table_params(self, yname=yname, xname=xname, alpha=.05, use_t=True) # smry.add_table_2cols(self, gleft=diagn_left, gright=diagn_right, #yname=yname, xname=xname, #title="") #add warnings/notes, added to text format only etext = [] if eigvals[-1] < 1e-10: wstr = "The smallest eigenvalue is %6.3g. This might indicate " wstr += "that there are\n" wstr += "strong multicollinearity problems or that the design " wstr += "matrix is singular." wstr = wstr % eigvals[-1] etext.append(wstr) elif condno > 1000: #TODO: what is recommended wstr = "The condition number is large, %6.3g. This might " wstr += "indicate that there are\n" wstr += "strong multicollinearity or other numerical " wstr += "problems." wstr = wstr % condno etext.append(wstr) if etext: smry.add_extra_txt(etext) return smry
def summary(self): """ Constructs a summary of the results from a fit model. Returns ------- summary : Summary instance Object that contains tables and facilitated export to text, html or latex """ # Summary layout # 1. Overall information # 2. Mean parameters # 3. Volatility parameters # 4. Distribution parameters # 5. Notes model = self.model model_name = model.name + " - " + model.volatility.name # Summary Header top_left = [ ("Dep. Variable:", self._dep_name), ("Mean Model:", model.name), ("Vol Model:", model.volatility.name), ("Distribution:", model.distribution.name), ("Method:", "Maximum Likelihood"), ("", ""), ("Date:", self._datetime.strftime("%a, %b %d %Y")), ("Time:", self._datetime.strftime("%H:%M:%S")), ] top_right = [ ("R-squared:", "%#8.3f" % self.rsquared), ("Adj. R-squared:", "%#8.3f" % self.rsquared_adj), ("Log-Likelihood:", "%#10.6g" % self.loglikelihood), ("AIC:", "%#10.6g" % self.aic), ("BIC:", "%#10.6g" % self.bic), ("No. Observations:", self._nobs), ("Df Residuals:", self.nobs - self.num_params), ("Df Model:", self.num_params), ] title = model_name + " Model Results" stubs = [] vals = [] for stub, val in top_left: stubs.append(stub) vals.append([val]) table = SimpleTable(vals, txt_fmt=fmt_2cols, title=title, stubs=stubs) # create summary table instance smry = Summary() # Top Table # Parameter table fmt = fmt_2cols fmt["data_fmts"][1] = "%18s" top_right = [("%-21s" % (" " + k), v) for k, v in top_right] stubs = [] vals = [] for stub, val in top_right: stubs.append(stub) vals.append([val]) table.extend_right(SimpleTable(vals, stubs=stubs)) smry.tables.append(table) conf_int = np.asarray(self.conf_int()) conf_int_str = [] for c in conf_int: conf_int_str.append("[" + format_float_fixed(c[0], 7, 3) + "," + format_float_fixed(c[1], 7, 3) + "]") stubs = self._names header = ["coef", "std err", "t", "P>|t|", "95.0% Conf. Int."] vals = (self.params, self.std_err, self.tvalues, self.pvalues, conf_int_str) formats = [(10, 4), (9, 3), (9, 3), (9, 3), None] pos = 0 param_table_data = [] for _ in range(len(vals[0])): row = [] for i, val in enumerate(vals): if isinstance(val[pos], np.float64): converted = format_float_fixed(val[pos], *formats[i]) else: converted = val[pos] row.append(converted) pos += 1 param_table_data.append(row) mc = self.model.num_params vc = self.model.volatility.num_params dc = self.model.distribution.num_params counts = (mc, vc, dc) titles = ("Mean Model", "Volatility Model", "Distribution") total = 0 for title, count in zip(titles, counts): if count == 0: continue table_data = param_table_data[total : total + count] table_stubs = stubs[total : total + count] total += count table = SimpleTable(table_data, stubs=table_stubs, txt_fmt=fmt_params, headers=header, title=title) smry.tables.append(table) extra_text = ("Covariance estimator: " + self.cov_type,) smry.add_extra_txt(extra_text) return smry