Skip to content

stats: add more functions and hypothesis tests based on summary statistics #1843

Open

Description

@josef-pkt

There is no convenient way to calculate t-tests based on sample statistics only.

http://stackoverflow.com/questions/24904822/scipy-stats-ttest-ind-without-array-python

The current, generic functions like _tstat_generic don't contain an additional layer that, for example. calculates the standard deviation of the test statistic.

We could add some "usable" functions that can be used just with the summary statistics from the data, (nobs, mean, std) for each sample.

Activity

  1. josef-pkt commented on Jul 23, 2014

    @josef-pkt
    MemberAuthor

    a quick try:

    question: why does std not require ddof=1 to agree with the existing functions?

    # -*- coding: utf-8 -*-
    """
    Created on Wed Jul 23 05:47:34 2014
    
    Author: Josef Perktold
    License: BSD-3
    
    """
    
    import numpy as np
    from scipy import stats
    from statsmodels.stats.weightstats import CompareMeans, ttest_ind
    
    
    class SummaryStats(object):
    
        def __init__(self, nobs, mean, std):
            self.nobs = nobs
            self.mean = mean
            self.std = std
            self._var = std**2
    
    np.random.seed(123)
    nobs = 20
    x1 = 1 + np.random.randn(nobs)
    x2 = 1 + 1.5 * np.random.randn(nobs)
    
    print stats.ttest_ind(x1, x2, equal_var=False)
    print ttest_ind(x1, x2, usevar='unequal')
    
    s1 = SummaryStats(x1.shape[0], x1.mean(0), x1.std(0))
    s2 = SummaryStats(x2.shape[0], x2.mean(0), x2.std(0))
    
    print CompareMeans(s1, s2).ttest_ind(usevar='unequal')
    
    
    
    def ttest_ind_summ(summ1, summ2, usevar='unequal'):
        """t-test for equality of means based on summary statistic
    
        Parameters
        ----------
        summ1, summ2 : tuples of (nobs, mean, std)
            summary statistic for the two samples
    
        """
    
        s1 = SummaryStats(*summ1)
        s2 = SummaryStats(*summ2)
        return CompareMeans(s1, s2).ttest_ind(usevar=usevar)
    
    print ttest_ind_summ((x1.shape[0], x1.mean(0), x1.std(0)),
                         (x2.shape[0], x2.mean(0), x2.std(0)), 
                         usevar='unequal')
    
    ''' result
    (array(1.1590347327654558), 0.25416326823881513)
    (1.1590347327654555, 0.25416326823881513, 35.573591346616553)
    (1.1590347327654558, 0.25416326823881513, 35.57359134661656)
    (1.1590347327654558, 0.25416326823881513, 35.57359134661656)
    '''
    
  2. Gitman-code commented on Jul 23, 2014

    @Gitman-code

    Ideally this would lead to a function similar to scipy.stats.ttest_ind except with μ, σ and N for each array as the input instead of the array itself.

  3. jseabold commented on Nov 25, 2014

    @jseabold
    Member

    I think this should be added as a simple function to scipy.stats. Surprised it doesn't exist.

  4. josef-pkt commented on Nov 25, 2014

    @josef-pkt
    MemberAuthor

    For scipy, one question is how to refactor the tests to avoid code duplication, and it will add many extra functions if this is done for a larger number of tests.
    The specific function here is just a few lines but the pattern would have a much larger range.
    Nobody submitted a PR to scipy.

    For statsmodels the case is similar, but we already have some basic generic functions for internal code reuse. But they are not designed for end users and we don't have wrapper functions for them.

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