Skip to content

Commit

Permalink
[FIX] p-values going to zero, replace with epsilon (#124)
Browse files Browse the repository at this point in the history
* fix and test small p_values

* fix flake8
  • Loading branch information
jdkent authored Aug 8, 2024
1 parent e94ddf3 commit eb67d0b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pymare/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@ def get_fe_stats(self, alpha=0.05):
=========== ==========================================================================
"""
beta, se = self.fe_params, self.fe_se
epsilon = np.finfo(beta.dtype).eps
z_se = ss.norm.ppf(1 - alpha / 2)
z = beta / se

p = 1 - np.abs(0.5 - ss.norm.cdf(z)) * 2
p[p == 0] += epsilon
stats = {
"est": beta,
"se": se,
"ci_l": beta - z_se * se,
"ci_u": beta + z_se * se,
"z": z,
"p": 1 - np.abs(0.5 - ss.norm.cdf(z)) * 2,
"p": p,
}

return stats
Expand Down
15 changes: 15 additions & 0 deletions pymare/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@ def variables():
return (y, v, X)


@pytest.fixture(scope="package")
def small_variance_variables(variables):
"""Make highly correlated variables."""
y, v, X = variables
y = X.copy()
v /= 10
return (y, v, X)


@pytest.fixture(scope="package")
def dataset(variables):
"""Build a Dataset compiled from the variables fixture."""
return Dataset(*variables, X_names=["my_covariate"])


@pytest.fixture(scope="package")
def small_variance_dataset(small_variance_variables):
"""Build a Dataset compiled from the small variance variables fixture."""
return Dataset(*small_variance_variables, X_names=["my_covariate"])


@pytest.fixture(scope="package")
def small_dataset_2d(variables):
"""Build a small Dataset with 2D data."""
Expand Down
22 changes: 22 additions & 0 deletions pymare/tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ def fitted_estimator(dataset):
return est.fit_dataset(dataset)


@pytest.fixture
def small_variance_estimator(small_variance_dataset):
"""Create a fitted Estimator with small variances as a fixture."""
est = DerSimonianLaird()
return est.fit_dataset(small_variance_dataset)


@pytest.fixture
def results(fitted_estimator):
"""Create a results object as a fixture."""
return fitted_estimator.summary()


@pytest.fixture
def small_variance_results(small_variance_estimator):
"""Create a results object with small variances as a fixture."""
return small_variance_estimator.summary()


@pytest.fixture
def results_2d(fitted_estimator, dataset_2d):
"""Create a 2D results object as a fixture."""
Expand Down Expand Up @@ -169,6 +182,15 @@ def test_mrr_to_df(results):
assert np.allclose(df["p-value"].values, [0.9678, 0.4369], atol=1e-4)


def test_small_variance_mrr_to_df(small_variance_results):
"""Test conversion of MetaRegressionResults to DataFrame."""
df = small_variance_results.to_df()
assert df.shape == (2, 7)
col_names = {"estimate", "p-value", "z-score", "ci_0.025", "ci_0.975", "se", "name"}
assert set(df.columns) == col_names
assert np.allclose(df["p-value"].values, [1, np.finfo(np.float64).eps], atol=1e-4)


def test_estimator_summary(dataset):
"""Test Estimator's summary method."""
est = WeightedLeastSquares()
Expand Down

0 comments on commit eb67d0b

Please sign in to comment.