Repository: OpenSourceEconomics/estimagic

Issue:
Unify usage of `p_value` / `pvalue`
### Bug description

`p_value` and `pvalue` are used as column names in different places; may lead to errors.

### To Reproduce

`estimagic.inference.likelihood_inference.do_likelihood_inference` returns table with column `p_value`
`estimagic.visualization.estimation_table` expects table with column `pvalue` and returns KeyError

### System

 - OS: Ubuntu 20.04
 - Version 0.1.3


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics) Report
> Merging [#222](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics) (43aad87) into [master](https://codecov.io/gh/OpenSourceEconomics/estimagic/commit/dae2f78d83d155c77a76fcfa8d230a15af1b6632?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics) (dae2f78) will **decrease** coverage by `0.00%`.
> The diff coverage is `76.92%`.

[![Impacted file tree graph](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222/graphs/tree.svg?width=650&height=150&src=pr&token=INbcX5rQK2&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics)](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics)

```diff
@@            Coverage Diff             @@
##           master     #222      +/-   ##
==========================================
- Coverage   81.78%   81.78%   -0.01%     
==========================================
  Files          59       59              
  Lines        4052     4057       +5     
==========================================
+ Hits         3314     3318       +4     
- Misses        738      739       +1     
```


| [Impacted Files](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics) | Coverage Δ | |
|---|---|---|
| [estimagic/visualization/estimation\_table.py](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics#diff-ZXN0aW1hZ2ljL3Zpc3VhbGl6YXRpb24vZXN0aW1hdGlvbl90YWJsZS5weQ==) | `61.13% <76.92%> (+0.36%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics). Last update [dae2f78...43aad87](https://codecov.io/gh/OpenSourceEconomics/estimagic/pull/222?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenSourceEconomics).


Patch:
diff --git a/estimagic/visualization/estimation_table.py b/estimagic/visualization/estimation_table.py
index b74ffd2..530cd5e 100644
--- a/estimagic/visualization/estimation_table.py
+++ b/estimagic/visualization/estimation_table.py
@@ -331,28 +331,30 @@ def tabular_html(
 
 def _process_model(model):
     """Check model validity, convert to namedtuple."""
+    NamedTup = namedtuple("NamedTup", "params info")
     if hasattr(model, "params") and hasattr(model, "info"):
         assert isinstance(model.info, dict)
         assert isinstance(model.params, pd.DataFrame)
-        processed_model = model
+        info_dict = model.info
+        params_df = model.params.copy(deep=True)
     else:
-        NamedTup = namedtuple("NamedTup", "params info")
         if isinstance(model, dict):
-            processed_model = NamedTup(
-                params=model["params"], info=model.get("info", {})
-            )
+            params_df = model["params"].copy(deep=True)
+            info_dict = model.get("info", {})
         elif isinstance(model, pd.DataFrame):
-            processed_model = NamedTup(params=model, info={})
+            params_df = model.copy(deep=True)
+            info_dict = {}
         else:
             try:
-                processed_model = NamedTup(
-                    params=_extract_params_from_sm(model),
-                    info={**_extract_info_from_sm(model)},
-                )
+                params_df = _extract_params_from_sm(model)
+                info_dict = {**_extract_info_from_sm(model)}
             except (KeyboardInterrupt, SystemExit):
                 raise
             except BaseException:
                 raise TypeError("Model {} does not have valid format".format(model))
+    if "pvalue" in params_df.columns:
+        params_df = params_df.rename(columns={"pvalue": "p_value"})
+    processed_model = NamedTup(params=params_df, info=info_dict)
     return processed_model
 
 
@@ -378,15 +380,14 @@ def _convert_model_to_series(
     Returns:
         sr (pd.Series): string series with values and inferences.
     """
-    if "p_value" in df.columns:
-        df = df.rename(columns={"p_value": "pvalue"})
+
     if show_stars:
         sig_bins = [-1] + sorted(sig_levels) + [2]
         value_sr = round(df["value"], sig_digits).replace(np.nan, "").astype("str")
         value_sr += "$^{"
         value_sr += (
             pd.cut(
-                df["pvalue"],
+                df["p_value"],
                 bins=sig_bins,
                 labels=[
                     "*" * (len(sig_levels) - i) for i in range(len(sig_levels) + 1)
@@ -701,7 +702,7 @@ def _extract_params_from_sm(model):
         to_concat.append(getattr(model, col))
     to_concat.append(model.conf_int())
     params_df = pd.concat(to_concat, axis=1)
-    params_df.columns = ["value", "pvalue", "standard_error", "ci_lower", "ci_upper"]
+    params_df.columns = ["value", "p_value", "standard_error", "ci_lower", "ci_upper"]
     return params_df
 
 


Test patch:
diff --git a/estimagic/tests/visualization/test_estimation_table.py b/estimagic/tests/visualization/test_estimation_table.py
index 4fe9c10..b588b82 100644
--- a/estimagic/tests/visualization/test_estimation_table.py
+++ b/estimagic/tests/visualization/test_estimation_table.py
@@ -70,9 +70,9 @@ def test_estimation_table():
 
 def test_process_model_namedtuple():
     # checks that process_model doesn't alter values
-    df = pd.DataFrame(columns=["value", "pvalue", "ci_lower", "ci_upper"])
+    df = pd.DataFrame(columns=["value", "p_value", "ci_lower", "ci_upper"])
     df["value"] = np.arange(10)
-    df["pvalue"] = np.arange(10)
+    df["p_value"] = np.arange(10)
     df["ci_lower"] = np.arange(10)
     df["ci_upper"] = np.arange(10)
     info = {"stat1": 0, "stat2": 0}
@@ -84,11 +84,11 @@ def test_process_model_namedtuple():
 
 def test_process_model_stats_model():
     par_df = pd.DataFrame(
-        columns=["value", "pvalue", "standard_error", "ci_lower", "ci_upper"],
+        columns=["value", "p_value", "standard_error", "ci_lower", "ci_upper"],
         index=["const", "Age", "Sex", "BMI", "ABP"],
     )
     par_df["value"] = [152.133484, 37.241211, -106.577520, 787.179313, 416.673772]
-    par_df["pvalue"] = [
+    par_df["p_value"] = [
         2.048808e-193,
         5.616557e-01,
         8.695658e-02,
@@ -114,9 +114,9 @@ def test_process_model_stats_model():
 
 
 def test_process_model_dict():
-    df = pd.DataFrame(columns=["value", "pvalue", "standard_error"])
+    df = pd.DataFrame(columns=["value", "p_value", "standard_error"])
     df["value"] = np.arange(10)
-    df["pvalue"] = np.arange(10)
+    df["p_value"] = np.arange(10)
     df["standard_error"] = np.arange(10)
     info = {"stat1": 0, "stat2": 0}
     mod = {}
@@ -133,7 +133,7 @@ def test_convert_model_to_series_conf_int():
         np.array(
             [[0.6, 2.3, 3.3], [0.11, 0.049, 0.009], [0.6, 2.3, 3.3], [1.2, 3.3, 4.3]]
         ).T,
-        columns=["value", "pvalue", "ci_lower", "ci_upper"],
+        columns=["value", "p_value", "ci_lower", "ci_upper"],
         index=["a", "b", "c"],
     )
     si_lev = [0.1, 0.05, 0.01]
@@ -161,7 +161,7 @@ def test_convert_model_to_series_conf_int():
 def test_convert_model_to_series_std_err():
     df = pd.DataFrame(
         np.array([[0.6, 2.3, 3.3], [0.11, 0.049, 0.009], [0.6, 2.3, 3.3]]).T,
-        columns=["value", "pvalue", "standard_error"],
+        columns=["value", "p_value", "standard_error"],
         index=["a", "b", "c"],
     )
     si_lev = [0.1, 0.05, 0.01]
@@ -182,7 +182,7 @@ def test_convert_model_to_series_std_err():
 def test_convert_model_to_series_no_inference():
     df = pd.DataFrame(
         np.array([[0.6, 2.3, 3.3], [0.11, 0.049, 0.009], [0.6, 2.3, 3.3]]).T,
-        columns=["value", "pvalue", "standard_error"],
+        columns=["value", "p_value", "standard_error"],
         index=["a", "b", "c"],
     )
     si_lev = [0.1, 0.05, 0.01]