Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 add a result summary method #6

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions firthlogist/firthlogist.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sklearn.preprocessing import LabelEncoder
from sklearn.utils.multiclass import check_classification_targets
from sklearn.utils.validation import check_is_fitted
from tabulate import tabulate


class FirthLogisticRegression(BaseEstimator, ClassifierMixin):
Expand Down Expand Up @@ -187,6 +188,50 @@ def fit(self, X, y):

return self

def summary(self, xname=None, tablefmt="simple"):
"""
Prints a summary table.

Parameters
----------
xname
Names for the X variables. Default is x1, x2, ... Must match the number of
parameters in the model.
tablefmt
`tabulate` table format for output. Please see the documentation for
`tabulate` for options.
"""
check_is_fitted(self)
if xname and len(xname) != len(self.coef_):
raise ValueError(
f"Length of xname ({len(xname)}) does not match the number of "
f"parameters in the model ({len(self.coef_)})"
)

if not xname:
xname = [f"x{i}" for i in range(1, len(self.coef_) + 1)]

coef = list(self.coef_)
if self.fit_intercept:
xname.append("Intercept")
coef.append(self.intercept_)

headers = [
"",
"coef",
"std err",
f"[{self.alpha/2}",
f"{1-self.alpha/2}]",
"p-value",
]
table = zip(xname, coef, self.bse_, self.ci_[:, 0], self.ci_[:, 1], self.pvals_)
table = tabulate(table, headers, tablefmt=tablefmt)
table += "\n\n"
table += f"Log-Likelihood: {round(self.loglik_, 4)}\n"
table += f"Newton-Raphson iterations: {self.n_iter_}\n"
print(table)
return

def decision_function(self, X):
check_is_fitted(self)
X = self._validate_data(X, reset=False)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ classifiers = [
python = ">=3.8,<3.11"
numpy = "^1.22.4"
scikit-learn = "^1.1.1"
tabulate = "^0.8.10"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Expand Down