Example #1
0
def test_chisquare_power():
    from .results.results_power import pwr_chisquare
    for case in itervalues(pwr_chisquare):
        power = chisquare_power(
            case.w, case.N, case.df + 1, alpha=case.sig_level)
        assert_almost_equal(
            power, case.power, decimal=6, err_msg=repr(vars(case)))
Example #2
0
    def power(self, effect_size, nobs, n_bins, alpha, ddof=0):
              #alternative='two-sided'):
        '''Calculate the power of a chisquare test for one sample

        Only two-sided alternative is implemented

        Parameters
        ----------
        effect_size : float
            standardized effect size, according to Cohen's definition.
            see :func:`statsmodels.stats.gof.chisquare_effectsize`
        nobs : int or float
            sample size, number of observations.
        alpha : float in interval (0,1)
            significance level, e.g. 0.05, is the probability of a type I
            error, that is wrong rejections if the Null Hypothesis is true.
        n_bins : int
            number of bins or cells in the distribution.

        Returns
        -------
        power : float
            Power of the test, e.g. 0.8, is one minus the probability of a
            type II error. Power is the probability that the test correctly
            rejects the Null Hypothesis if the Alternative Hypothesis is true.

       '''
        from statsmodels.stats.gof import chisquare_power
        return chisquare_power(effect_size, nobs, n_bins, alpha, ddof=0)
Example #3
0
    def power(self,
              effect_size,
              nobs,
              alpha,
              n_bins,
              ddof=0):  #alternative='two-sided'):
        '''Calculate the power of a chisquare test for one sample

        Only two-sided alternative is implemented

        Parameters
        ----------
        effect_size : float
            standardized effect size, according to Cohen's definition.
            see :func:`statsmodels.stats.gof.chisquare_effectsize`
        nobs : int or float
            sample size, number of observations.
        alpha : float in interval (0,1)
            significance level, e.g. 0.05, is the probability of a type I
            error, that is wrong rejections if the Null Hypothesis is true.
        n_bins : int
            number of bins or cells in the distribution.

        Returns
        -------
        power : float
            Power of the test, e.g. 0.8, is one minus the probability of a
            type II error. Power is the probability that the test correctly
            rejects the Null Hypothesis if the Alternative Hypothesis is true.

       '''
        from statsmodels.stats.gof import chisquare_power
        return chisquare_power(effect_size, nobs, n_bins, alpha, ddof=0)
Example #4
0
    def power(self,
              effect_size,
              nobs1,
              alpha,
              ratio=1,
              alternative='two-sided'):
        '''Calculate the power of a chisquare for two independent sample

        Parameters
        ----------
        effect_size : float
            standardize effect size, difference between the two means divided
            by the standard deviation. effect size has to be positive.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
        alpha : float in interval (0,1)
            significance level, e.g. 0.05, is the probability of a type I
            error, that is wrong rejections if the Null Hypothesis is true.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ration given the other
            arguments it has to be explicitely set to None.
        alternative : str, 'two-sided' (default) or 'one-sided'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test.
            'one-sided' assumes we are in the relevant tail.

        Returns
        -------
        power : float
            Power of the test, e.g. 0.8, is one minus the probability of a
            type II error. Power is the probability that the test correctly
            rejects the Null Hypothesis if the Alternative Hypothesis is true.

        '''

        from statsmodels.stats.gof import chisquare_power
        nobs2 = nobs1 * ratio
        #equivalent to nobs = n1*n2/(n1+n2)=n1*ratio/(1+ratio)
        nobs = 1. / (1. / nobs1 + 1. / nobs2)
        return chisquare_power(effect_size, nobs, alpha)
Example #5
0
    def power(self, effect_size, nobs1, alpha, ratio=1,
              alternative='two-sided'):
        '''Calculate the power of a chisquare for two independent sample

        Parameters
        ----------
        effect_size : float
            standardize effect size, difference between the two means divided
            by the standard deviation. effect size has to be positive.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
        alpha : float in interval (0,1)
            significance level, e.g. 0.05, is the probability of a type I
            error, that is wrong rejections if the Null Hypothesis is true.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ration given the other
            arguments it has to be explicitely set to None.
        alternative : string, 'two-sided' (default) or 'one-sided'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test.
            'one-sided' assumes we are in the relevant tail.

        Returns
        -------
        power : float
            Power of the test, e.g. 0.8, is one minus the probability of a
            type II error. Power is the probability that the test correctly
            rejects the Null Hypothesis if the Alternative Hypothesis is true.

        '''

        from statsmodels.stats.gof import chisquare_power
        nobs2 = nobs1*ratio
        #equivalent to nobs = n1*n2/(n1+n2)=n1*ratio/(1+ratio)
        nobs = 1./ (1. / nobs1 + 1. / nobs2)
        return chisquare_power(effect_size, nobs, alpha)
Example #6
0
def log_results(log, result, source):
    """
    Log the fitting results.

    Notes
    -----
    The resulting mixture parameters are stored into a 2d array with rows
    [location in degrees (mu), shape (kappa), probability].
    """
    sparams = result.model.get_summary_params(result.full_params)[:, [1, 0, 2]]
    sparams[:, 0] = tr.transform_pi_deg(tr.fix_range(sparams[:, 0]),
                                        neg_shift=source.neg_shift)
    converged = result.mle_retvals['converged']

    fit_criteria = [-result.llf, result.aic, result.bic]
    print 'llf / nobs:', fit_criteria[0] / result.model.endog.shape[0]

    chisquare = result.gof_chisquare()

    # Chisquare test with effect size.
    alpha = 0.05 # Significance level.
    data = source.source_data.data
    n_obs = data[:, 1].sum()
    rad_diff = data[1, 0] - data[0, 0]

    pdf = result.model.pdf_mix(result.full_params, data[:, 0])
    probs = pdf * rad_diff * n_obs
    effect_size = gof.chisquare_effectsize(data[:, 1], probs)
    chi2 = gof.chisquare(data[:, 1], probs, value=effect_size)
    power = gof.chisquare_power(effect_size, n_obs,
                                data.shape[0], alpha=alpha)

    chisquare_all = list(chisquare) + [n_obs, effect_size] \
                    + list(chi2) + [power]

    log.write_row(source.current.dir_base, source.current.base_names,
                  chisquare_all, sparams, converged, fit_criteria)
Example #7
0
    def chisquare_test_scores(self, E1_score_list, E2_score_list):
        obs = np.array([E1_score_list, E2_score_list])
        print(obs)
        #obs = np.array([female_list]).T
        chi2_stat, p_val, dof, ex = chi2_contingency(obs, correction=False)

        #compute effect size
        effect_size = sms.chisquare_effectsize(E1_score_list, E2_score_list)
        number_observations = len(E1_score_list)

        #compute power
        from statsmodels.stats.gof import chisquare_power
        power = chisquare_power(effect_size,
                                nobs=number_observations,
                                n_bins=dof + 1,
                                alpha=0.05)
        #smp.GofChisquarePower.solve_power(effect_size, nobs=number_observations, n_bins=degrees_freedom+1, alpha=0.05)

        print("Chi-square Stat: " + str(chi2_stat))
        print(" degrees of freedom: " + str(dof))
        print(" p-value: " + str(p_val))
        print(" contingency table: " + str(ex))
        print(" effect size:" + str(effect_size))
        print(" power:" + str(power))
Example #8
0
def log_results(log, result, source):
    """
    Log the fitting results.

    Notes
    -----
    The resulting mixture parameters are stored into a 2d array with rows
    [location in degrees (mu), shape (kappa), probability].
    """
    sparams = result.model.get_summary_params(result.full_params)[:, [1, 0, 2]]
    sparams[:, 0] = tr.transform_pi_deg(tr.fix_range(sparams[:, 0]),
                                        neg_shift=source.neg_shift)
    converged = result.mle_retvals['converged']

    fit_criteria = [-result.llf, result.aic, result.bic]
    print 'llf / nobs:', fit_criteria[0] / result.model.endog.shape[0]

    chisquare = result.gof_chisquare()

    # Chisquare test with effect size.
    alpha = 0.05  # Significance level.
    data = source.source_data.data
    n_obs = data[:, 1].sum()
    rad_diff = data[1, 0] - data[0, 0]

    pdf = result.model.pdf_mix(result.full_params, data[:, 0])
    probs = pdf * rad_diff * n_obs
    effect_size = gof.chisquare_effectsize(data[:, 1], probs)
    chi2 = gof.chisquare(data[:, 1], probs, value=effect_size)
    power = gof.chisquare_power(effect_size, n_obs, data.shape[0], alpha=alpha)

    chisquare_all = list(chisquare) + [n_obs, effect_size] \
                    + list(chi2) + [power]

    log.write_row(source.current.dir_base, source.current.base_names,
                  chisquare_all, sparams, converged, fit_criteria)
Example #9
0
print(stats.ncx2.sf(chisq, n_bins, 0.001 * nobs))
print(stats.ncx2.sf(chisq, n_bins, d_delta * nobs))
print(chisquare(freq, nobs*probs_d, value=np.sqrt(d_delta)))
print(chisquare(freq, nobs*probs_d, value=np.sqrt(chisq / nobs)))
print()

assert_almost_equal(stats.chi2.sf(d_delta * nobs, n_bins - 1),
                    chisquare(freq, nobs*probs_d)[1], decimal=13)

crit = stats.chi2.isf(0.05, n_bins - 1)
power = stats.ncx2.sf(crit, n_bins-1, 0.001**2 * nobs)
#> library(pwr)
#> tr = pwr.chisq.test(w =0.001, N =30000 , df = 5-1, sig.level = 0.05, power = NULL)
assert_almost_equal(power, 0.05147563, decimal=7)
effect_size = 0.001
power = chisquare_power(effect_size, nobs, n_bins, alpha=0.05)
assert_almost_equal(power, 0.05147563, decimal=7)
print(chisquare(freq, nobs*probs, value=0, ddof=0))
d_null_alt = ((probs - probs_d)**2 / probs).sum()
print(chisquare(freq, nobs*probs, value=np.sqrt(d_null_alt), ddof=0))


#Monte Carlo to check correct size and power of test

d_delta_r = chisquare_effectsize(probs, probs_d)
n_rep = 10000
nobs = 3000
res_boots = np.zeros((n_rep, 6))
for i in range(n_rep):
    rvs = np.argmax(np.random.rand(nobs,1) < probs_cs, 1)
    freq = np.bincount(rvs)