Skip to content

ENH: Meta analysis #1575

Open
Open

Description

@ccsv

Has any attempts been made to implement meta-analysis
http://en.wikipedia.org/wiki/Meta-analysis#cite_note-Manual-42

Basically it is a way to combine multiple statistic samples by a similar factor. I am doing brief research on this for someone but I would like to start an implementation. The only problem is I am not good with creating the classes so it will be an hassle to whoever converts it :(.

We may need an section on Effect size measurements

http://en.wikipedia.org/wiki/Effect_size#Hedges.27_g

Which include
*Correlation (pearson) already in statsmodels
*Coefficient of determination (R^2)
*Eta-squared
*Cohen F^2 (I think statsmodel has this)
*Hedges's g

Related:

#771

Activity

  1. josef-pkt commented on Apr 8, 2014

    @josef-pkt
    Member

    In these cases classes are not so important. In contrast to the estimation models, most statsmodels.stats is based on functions. Some classes are just collections of functions to keep things together and optimize on recalculating intermediate results.

    Unit tests are always important.

    A collection of effectsize functions would be very useful. It's on my wishlist, but I didn't have the time in my latest round of working on this.

    I don't know much about what's used in Meta analysis.
    one case I had to read up on, is combining independent test results or p-values for
    scipy/scipy#3092

    A list of function, both an overview and actual implementation would be very useful. I would gladly review it, but I won't be working on new code for stats myself for at least the next few months.

  2. kshedden commented on Apr 8, 2014

    @kshedden
    Contributor

    Random effects meta analysis can be conducted using linear mixed effects models, and the results are often displayed using forest plots. Both are in the works...

    An article for reference:

    http://www.bmj.com/content/342/bmj.d549

  3. ccsv commented on Apr 8, 2014

    @ccsv
    Author
  4. kshedden commented on Apr 8, 2014

    @kshedden
    Contributor

    The PR's I mentioned are for statsmodels. Mixed models is #1398, dot plots (forest plots) is #1293.

    I don't have any immediate plans to implement specialized meta analysis techniques. But some of the standard meta-analysis techniques are direct applications of more general tools.

    When the mixed models PR is more mature, I will try to put a notebook together illustrating meta-analysis using this and other statsmodels functionality.

  5. josef-pkt commented on Feb 6, 2020

    @josef-pkt
    Member

    I finally looked a bit at Meta-analysis but just to get a bit of an overview.

    I think it wouldn't be difficult to add some basic functions and plots for the statistical part of meta-analysis, e.g. weighted averaging and typical plots.
    Do we need more features in the existing models so they can be reused for meta-analysis, e.g. weights in more models and stats functions?

    The other issues is whether or where we need to improve our results to make including them in meta-analysis easier, for example reporting effect sizes #5896

  6. changed the title [-]FEAT: Meta analysis[/-] [+]ENH: Meta analysis[/+] on Feb 6, 2020
  7. josef-pkt commented on Feb 7, 2020

    @josef-pkt
    Member

    one detail (while semi-randomly skimming some parts)

    It seems that when using WLS for variance weighted regression combination needs scale=1
    "lm will then estimate sigma^2 which can be seen from the ANOVA table to be sigma^2 = 1:14. With
    meta-regression, this sigma^2 should set to 1.
    "
    Chen, Ding-Geng, and Karl E. Peace. 2013. Applied Meta-Analysis with R. Chapman & Hall/CRC Biostatistics Series. Boca Raton: CRC Press/Taylor & Francis Group.

    book looks good, with both explicit computation and using R packages, and formulas/equations
    (based on briefly skimming some parts)

  8. josef-pkt commented on Feb 7, 2020

    @josef-pkt
    Member

    parking a first draft for the simple case
    (I just wanted to see what we need for effect sizes)

    Replicates an example using R meta package in "Applied Meta-Analysis with R" book


    class CombineResults(object):
        
        def __init__(self, **kwds):
            self.__dict__.update(kwds)
            self._ini_keys = list(kwds.keys())
            
        def summary_array(self):
            res = np.column_stack([self.smd_bc, self.sd_diff, self.ci_low, self.ci_upp,
                                   self.weights_rel_fe, self.weights_rel_re])
            
            res_fe = [[self.smd_effect_fe, self.sd_smd_w_fe, 
                      self.ci_low_smd_fe, self.ci_upp_smd_fe, 1, np.nan]]
            res_re = [[self.smd_effect_re, self.sd_smd_w_re, 
                      self.ci_low_smd_re, self.ci_upp_smd_re, np.nan, 1]]
            
            res = np.concatenate([res, res_fe, res_re], axis=0)
            column_names = ['smd', "sd_smd", "ci_low", "ci_upp", "w_fe", "w_re"]
            return res, column_names
            
        def summary_frame(self):
            labels = list(self.row_names) + ["fixed effect", "random effect"]
            res, col_names = self.summary_array()
            results = pd.DataFrame(res, index=labels, columns=col_names)
            return results
            
    
    def combine_cont(mean2, sd2, nobs2, mean1, sd1, nobs1, row_names=None, alpha=0.05):
        """combining effect sizes for mean difference using meta-analysis
        
        mean2, sd2, nobs2 are for treatment
        mean1, sd1, nobs1 are for control
        
        Effect sizes are computed for the mean difference ``mean2 - mean1``
        
        This does not have option yet.
        It uses standardized mean difference with bias correction as effect size.
        
        This currently does not use np.asarray, all computations are possible in pandas.
        
        References
        ----------
        Borenstein, Michael. 2009. Introduction to Meta-Analysis. Chichester: Wiley.
        
        Chen, Ding-Geng, and Karl E. Peace. 2013. Applied Meta-Analysis with R. 
            Chapman & Hall/CRC Biostatistics Series. Boca Raton: CRC Press/Taylor & Francis Group.
    
        
        """
        k = len(mean2)
        if row_names is None:
            row_names = list(range(k))
        crit = stats.norm.isf(alpha / 2)
        
        var_diff_uneq = sd2**2 / nobs2 + sd1**2 / nobs1
        var_diff = (sd2**2 * (nobs2 - 1) + sd1**2 * (nobs2 - 1)) /  (nobs2 + nobs1 - 2)
        sd_diff = np.sqrt(var_diff)
        nobs = nobs2 + nobs1
        bias_correction = 1 - 3 / (4 * nobs - 9)
        smd = (mean2 - mean1) / sd_diff
        smd_bc = bias_correction * smd
        var_smdbc = nobs / nobs2 / nobs1  + smd_bc**2 / 2 / (nobs - 3.94)
        sd_smdbc = np.sqrt(var_smdbc)
        
        ci_low = smd_bc - 1.96 * sd_smdbc
        ci_upp = smd_bc + 1.96 * sd_smdbc
        
        
        # fixed effects computation
        
        weights_fe = 1 / var_smdbc # no bias correction ?
        w_total_fe = weights.sum(0)
        weights_rel_fe = weights_fe / w_total_fe
        
        smd_w_fe = weights_rel_fe * smd_bc
        smd_effect_fe = smd_w_fe.sum()
        var_smd_w_fe = 1 / w_total_fe
        sd_smd_w_fe = np.sqrt(var_smd_w_fe)
        
        ci_low_smd_fe = smd_effect_fe - crit * sd_smd_w_fe
        ci_upp_smd_fe = smd_effect_fe + crit * sd_smd_w_fe
        
        # random effects computation
        
        q = (weights_fe * smd_bc**2).sum(0) - (weights_fe * smd_bc).sum()**2 / w_total_fe
        df = k - 1
        c = w_total - (weights_fe**2).sum() / w_total_fe
        tau2 = (q - df) / c
        
        weights_re = 1 / (var_smdbc + tau2) # no  bias_correction ?
        w_total_re = weights_re.sum(0)
        weights_rel_re = weights_re / weights_re.sum(0)
        
        smd_w_re = weights_re_rel * smd_bc
        smd_effect_re = smd_w_re.sum()
        var_smd_w_re = 1 / w_total_re
        sd_smd_w_re = np.sqrt(var_smd_w_re)
        ci_low_smd_re = smd_effect_re - crit * sd_smd_w_re
        ci_upp_smd_re = smd_effect_re + crit * sd_smd_w_re
        
        res = CombineResults(**locals())
        return res
    

    mean2, sd2, nobs2, mean1, sd1, nobs1 = np.asarray(dframe[["mean_t","sd_t","n_t","mean_c","sd_c","n_c"]]).T
    res = combine_cont(mean2, sd2, nobs2, mean1, sd1, nobs1, row_names=dframe["study"])
    print(res.summary_frame())
    
                        smd     sd_smd    ci_low    ci_upp      w_fe      w_re
    Carroll        0.094524  21.023796 -0.263528  0.452576  0.123885  0.157476
    Grant          0.277356  21.505813 -0.068151  0.622864  0.133045  0.162785
    Peck           0.366546  27.018512 -0.075577  0.808670  0.081250  0.126124
    Donat          0.664385  18.027756  0.462998  0.865772  0.391606  0.232903
    Stewart        0.449310  22.076358  0.041316  0.857305  0.095412  0.137971
    Young          0.185165  21.505813 -0.116144  0.486474  0.174939  0.182740
    fixed effect   0.413832   0.064298  0.287810  0.539855  1.000000       NaN
    random effect  0.356762   0.105326  0.150326  0.563198       NaN  1.000000
    
    
  9. josef-pkt commented on Feb 9, 2020

    @josef-pkt
    Member

    Design:
    Combining effect sizes seems to be the same for different effect size measures, except for additional back transformation to normality.

    So we can split into effect size computation and into combining effect estimates

    eff, var_eff = effectsize_smd(mean2, sd2, nobs2, mean1, sd1, nobs1, row_names=None, alpha=0.05)
    res3 = combine_effects(eff, var_eff)
    print(res3.summary_frame())
    

    We might need a wrapper function or class to get all the information into the results class if we want to combine original estimates and results from combination for some results tables, plots, ...
    It's not clear to me yet where backtransform to original measures should go. (analogy to eform PR?)

  10. josef-pkt commented on Feb 27, 2020

    @josef-pkt
    Member
  11. josef-pkt commented on Apr 2, 2020

    @josef-pkt
    Member

    As far as I currently understand, MixedLM cannot be used to replicate meta-analysis random effects estimation because MixedLM doesn't allow for var_weights

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions