Skip to content

ENH: add meta-analysis (basic methods) - #6632

Merged
josef-pkt merged 11 commits into
statsmodels:masterfrom
josef-pkt:meta_analysis
Apr 30, 2020
Merged

josef-pkt merged 11 commits into
statsmodels:masterfrom
josef-pkt:meta_analysis

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

add basic methods for univariate meta-analysis
will include the corresponding effect size measures

#1575 request for including meta-analysis methods in statsmodels
#6585 comments on technical details and about current implementation

WIP
looks good so far, i.e. I can replicate basic cases

  • missing options not yet included in main function
  • missing effect size measures, e.g. for proportions
  • unit tests currently only for tau, other results not converted to unit test yet
  • missing auxiliary results, e.g. test for heterogeneity (q_stat, pearson_chi2 test)

plan is to give up and merge once I have the basic options and API

@pep8speaks

pep8speaks commented Apr 7, 2020 •

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-04-30 00:59:51 UTC

@josef-pkt

josef-pkt commented Apr 7, 2020 •

Copy link
Copy Markdown
Member Author

There is overlap with oneway anova PR #6526 where Welch anova uses similar weighted average.
But I don't want to mix more strands of literature for now.

One difference in signature based on summary statistic is that oneway includes nobs
anova_generic(means, vars_, nobs, ...
vars_ is within variance, not variance of mean, var_mean = vars_ / nobs is internally computed
We need this to handle pooling of variance_within across samples.

In meta we don't use nobs directly
combine_effects(effect, variance, ...)
variance is variance of effect in a sample, e.g. var of mean or of standardized mean
we don't pool within var.

@lgtm-com

lgtm-com Bot commented Apr 7, 2020

Copy link
Copy Markdown

This pull request introduces 3 alerts when merging b42df4c into 73a2ed7 - view on LGTM.com

new alerts:

  • 3 for Unused local variable

@coveralls

coveralls commented Apr 7, 2020 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.03%) to 87.659% when pulling 1a14405 on josef-pkt:meta_analysis into 5dea830 on statsmodels:master.

@lgtm-com

lgtm-com Bot commented Apr 7, 2020

Copy link
Copy Markdown

This pull request introduces 3 alerts when merging 0abd0bb into 73a2ed7 - view on LGTM.com

new alerts:

  • 3 for Unused local variable

@josef-pkt

Copy link
Copy Markdown
Member Author

"hk" standard errors, now verified and unit tested against metafor

case is DL + HK
# > res = rma(y, v, data=dat, method="DL", test="knha")
and FE + HK

metafor docs reference for this, AFAIR, I didn't look at this article,
Knapp, G., & Hartung, J. (2003). Improved tests for a random effects meta-regression with a single
covariate. Statistics in Medicine, 22, 2693–2710.

When I use test="knha" with method="FE", then I get a warning that knha is not appropriate with FE. But it is just WLS in that case.

wls/hksj scale depends on weights, so need FE and RE versions.

@josef-pkt

Copy link
Copy Markdown
Member Author

q_stat versus pearson_chi2, or are these statistics on the final model.

general issue: My initial implementation didn't use metafor as reference, and returns both FE and RE results to match the tables in books and articles.
q_stat is computed for the FE model, at FE model weights 1 / var_i.
pearson_chi2 if we use GLM with RE var_weights will be computed at the final estimate.

I thought of including both wls/hksj scale and scale=1 results by default (instead of the scale argument).
But then we have 4 different models/options in the results, 2 params and 4 bse and confints.
and mabye q_stat as pearson_chi2_fe, and separate (final model) pearson_chi2_re (they are defined independent of the scale used.)

Advantage of always adding HKSJ/WLS results is that users see it, even if they wouldn't think about requesting it.

Related: Hartung Knapp have an article that truncates scale at 1 from below, i.e. scale=max(1, scale_hksj) to avoid shrinking standard errors for "under dispersion".

The main problem with scale, and RE estimate is if the number of studies/samples is small, since that is base only on between information. On the other side, have reliable estimates of variance of means in individual samples/studies requires large nobs in each sample.
So, if both are small, then variances of means and RE can be very noisy, and fixing scale or weights at "some prior" could be better.
(maybe at a later stage we add robust estimators)

Comment thread statsmodels/stats/meta_analysis.py Resolved
Comment thread statsmodels/stats/meta_analysis.py Resolved
Comment thread statsmodels/stats/meta_analysis.py Resolved
Comment thread statsmodels/stats/meta_analysis.py Resolved
Comment thread statsmodels/stats/meta_analysis.py Resolved
Comment thread statsmodels/stats/meta_analysis.py Outdated


def effectsize_smd(mean2, sd2, nobs2, mean1, sd1, nobs1, row_names=None,
alpha=0.05):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alpha is currently unused, we don't return confint for now

Comment thread statsmodels/stats/meta_analysis.py Outdated
sd_smdbc = np.sqrt(var_smdbc)

ci_low = smd_bc - 1.96 * sd_smdbc
ci_upp = smd_bc + 1.96 * sd_smdbc

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those don't really belong here
either move to effect size, but then we need more information from effect size computation
or move to results class

we should have separate options for samples conf_ints method, instead of hardcoding normal/z confints

@josef-pkt

Copy link
Copy Markdown
Member Author

another addition:
If I add four cases by default, then we could create a params_table similar to models and t_test summary.

I haven't tried meta-analysis plot functions yet.

also, rename
tau2 -> var_re
and option method_re
maybe:
"DL" is "chi2" in analogy to GLM
"PM" is "iterated"

Should we allow user defined weights, or tell the user to use GLM/WLS in that case?
the latter,
e.g. using arithmetic mean as in Cochran or <some name> anova.
what's the Peto method?

@lgtm-com

lgtm-com Bot commented Apr 8, 2020

Copy link
Copy Markdown

This pull request introduces 1 alert when merging 883a391 into 73a2ed7 - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@josef-pkt

josef-pkt commented Apr 10, 2020 •

Copy link
Copy Markdown
Member Author

still problem with confint for individual samples.
Currently we don't have enough information in CombineResults to compute them except when using normal distribution.
I added a messy workaround, where users need to call conf_int_samples before calling summary_frame.

Because we have an alpha option for confidence intervals, it wouldn't help if we get alpha-specific confint for the samples from the effectsize function, i.e. we need access to a conf_int method or function for individual effect sizes.

@lgtm-com

lgtm-com Bot commented Apr 10, 2020

Copy link
Copy Markdown

This pull request introduces 1 alert when merging f56d268 into 1ccd5cd - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@josef-pkt

Copy link
Copy Markdown
Member Author

detail: metafor hksj changes distribution for mean and omnibus test (all slope coefficients=0), but not for Q test for homogeneity
I thought initially this also changes the distribution in test_homogeneity

unit tests for test_homogeneity (using currently only chi2) passes also for hksj, numbers don't change across method and test options in metafor rma.uni

@josef-pkt

Copy link
Copy Markdown
Member Author

wrong convention for sample 1 versus sample 2, see #811
in weightstats and 2-sample tests (e.g. poison PR #2723, proportion PR #4829), I used diff = m1 - m2 - value
here in meta, I use diff = m2 - m1 but it only shows up in effectsize functions

need to rename and switch indices in names

effectsize_2proportions (not yet pushed) overlaps with contingency table and PR #4829

@josef-pkt

Copy link
Copy Markdown
Member Author

I'm giving up here, and plan to merge soon.
Basics are done. I don't want to think about API anymore.

Needs a follow-up before next release to check API again, and review for code duplication with other PRs, especially for effect sizes

@josef-pkt

Copy link
Copy Markdown
Member Author

only one lint failure on travis, and azure for previous commit
statsmodels/stats/tests/test_meta.py:103:9: F841 local variable 'vi_asy' is assigned to but never used

However, maybe meta_analysis.py is not checked for this. (there should be unused variables that are used only with Locals() )

no azur testing for the last doc commit ?

@josef-pkt

Copy link
Copy Markdown
Member Author

AFAICS, no warnings in notebook, except spelling, misspelled measurements
notebook not yet added to toctree

examples/notebooks/generated/metaanalysis1.ipynb:9: (statmodels) 
examples/notebooks/generated/metaanalysis1.ipynb:13: (metafor) 
examples/notebooks/generated/metaanalysis1.ipynb:13: (metafor) 
examples/notebooks/generated/metaanalysis1.ipynb:17: (arcsine) 
examples/notebooks/generated/metaanalysis1.ipynb:24: (Hanszel) 
examples/notebooks/generated/metaanalysis1.ipynb:366: (Paule) 
examples/notebooks/generated/metaanalysis1.ipynb:366: (Mandel) 
examples/notebooks/generated/metaanalysis1.ipynb:368: (Paule) 
examples/notebooks/generated/metaanalysis1.ipynb:368: (Mandel) 
examples/notebooks/generated/metaanalysis1.ipynb:444: (Kacker) 
examples/notebooks/generated/metaanalysis1.ipynb:444: (interlaboratory) 
examples/notebooks/generated/metaanalysis1.ipynb:446: (measurments) 
examples/notebooks/generated/metaanalysis1.ipynb:1300: (Hanszel) 
examples/notebooks/generated/metaanalysis1.ipynb:1302: (Hanszel) 

@josef-pkt

Copy link
Copy Markdown
Member Author

unit test coverage is pretty low, 65% of lines on coveralls:
conf_int_samples, summary_... and plot_forest are not in unit tests,
Not all options for treatment of zeros are tested.

@josef-pkt

Copy link
Copy Markdown
Member Author

merging, unit tests green, style not
I leave more code and api cleanup for after the next PRs

@josef-pkt
josef-pkt merged commit 828ce7a into statsmodels:master Apr 30, 2020
@bashtage bashtage added this to the 0.12 milestone Jul 27, 2020
@josef-pkt
josef-pkt deleted the meta_analysis branch February 21, 2021 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants