Example #1
0
def _multi_power(alpha: float, df1: float, df2: float, omega: float,
                 total_N: float, **kwargs) -> Power:
    """ The common part for these four multirep methods computing power"""
    noncentrality_dist = None
    quantile = None
    confidence_interval = None
    for key, value in kwargs.items():
        if key == 'noncentrality_distribution':
            noncentrality_dist = value
        if key == 'quantile':
            quantile = value
        if key == 'confidence_interval':
            confidence_interval = value

    fcrit = finv(1 - alpha, df1, df2)
    if noncentrality_dist and quantile:
        omega = __calc_quantile_omega(noncentrality_dist, quantile)
        prob, fmethod = probf(fcrit, df1, df2, omega)
    elif noncentrality_dist and not quantile:
        prob, fmethod = noncentrality_dist.unconditional_power_simpson(
            fcrit=fcrit, df1=df1, df2=df2)
    else:
        prob, fmethod = probf(fcrit, df1, df2, omega)

    if fmethod == Constants.FMETHOD_NORMAL_LR and prob == 1:
        powerval = alpha
    else:
        powerval = 1 - prob
    powerval = float(powerval)
    power = Power(powerval, omega, fmethod)
    if confidence_interval:
        cl_type = _get_cl_type(confidence_interval)

        power.glmmpcl(is_multirep=True,
                      alphatest=alpha,
                      dfh=df1,
                      n2=total_N,
                      dfe2=df2,
                      cl_type=cl_type,
                      n_est=confidence_interval.n_est,
                      rank_est=confidence_interval.rank_est,
                      alpha_cl=confidence_interval.lower_tail,
                      alpha_cu=confidence_interval.upper_tail,
                      fcrit=fcrit,
                      tolerance=1e-12,
                      omega=omega)
    return power
Example #2
0
    def test_glmmpcl(self):
        """
        This should correctly calculate the confidence intervals for power base on???
        """
        #todo where is this example from? whay are we rounding?
        expected = Power(0.9, 0.05, Constants.FMETHOD_NOAPPROXIMATION)
        dfe1 = 20 - 1
        alphatest = 0.05
        dfh = 20
        dfe2 = 28
        fcrit = finv(1 - alphatest, dfh, dfe2)
        actual = expected.glmmpcl(
            is_multirep=True,
            alphatest=0.05,
            dfh=20,  # df1
            n2=30,  # total_N ??? what is this
            dfe2=28,  # df2
            cl_type=Constants.CLTYPE_DESIRED_KNOWN,
            n_est=20,
            rank_est=1,
            alpha_cl=0.048,
            alpha_cu=0.052,
            fcrit=fcrit,
            tolerance=0.01,
            omega=200)

        power_l = 0.9999379
        noncen_l = 105.66408
        power_u = 1
        noncen_u = 315.62306
        fmethod = Constants.FMETHOD_NOAPPROXIMATION

        self.assertEqual(round(expected.lower_bound.power, 7), power_l)
        self.assertEqual(
            round(expected.lower_bound.noncentrality_parameter, 5), noncen_l)
        self.assertEqual(expected.lower_bound.fmethod, fmethod)
        self.assertEqual(expected.upper_bound.power, power_u)
        self.assertEqual(
            round(expected.upper_bound.noncentrality_parameter, 5), noncen_u)
        self.assertEqual(expected.upper_bound.fmethod, fmethod)
Example #3
0
    def _calc_bound(self, is_multirep, alphatest, alpha, lower_tail_prob,
                    upper_tail_prob, prob, noncentrality, cl_type, dfe1, dfe2,
                    dfh, fcrit, omega, tolerance, **kwargs):
        """Calculate power bounds """
        df1_unirep = None
        if 'df1_unirep' in kwargs.keys():
            df1_unirep = kwargs['df1_unirep']
        fmethod = Constants.FMETHOD_MISSING
        if alpha > tolerance:
            if cl_type == Constants.CLTYPE_DESIRED_KNOWN:
                if is_multirep:
                    chi = chi2.ppf(lower_tail_prob, dfe1)
                    noncentrality = (chi / dfe1) * omega
                else:
                    chi = chi2.ppf(lower_tail_prob, dfh)
                    noncentrality = (chi / dfh) * omega
            elif cl_type == Constants.CLTYPE_DESIRED_ESTIMATE:
                f_a = omega / dfh
                bound = finv(upper_tail_prob, dfh, dfe1)
                if f_a <= bound:
                    noncentrality = 0
                else:
                    noncentrality = special.ncfdtrinc(dfh, dfe1,
                                                      upper_tail_prob, f_a)
            if is_multirep:
                prob, fmethod = probf(fcrit, dfh, dfe2, noncentrality)
            else:
                prob, fmethod = probf(fcrit, df1_unirep, dfe2, noncentrality)
        if fmethod == Constants.FMETHOD_NORMAL_LR and prob == 1:
            power_bound = alphatest
        else:
            power_bound = 1 - prob

        power = Power()
        power.power = power_bound
        power.fmethod = fmethod
        power.noncentrality_parameter = noncentrality
        return power
Example #4
0
 def test_largedf1(self):
     actual = finv(0.05, 10**8, 1)
     self.assertTrue(np.isnan(actual))
Example #5
0
 def test_finv_large_df2(self):
     expected = 6.6348966
     actual = finv(0.99, 1, 10000000000)
     result = round(actual, 7)
     self.assertEqual(expected, result)
Example #6
0
 def test_finv(self):
     expected = 0.7185356
     actual = finv(0.05, 100, 100)
     result = round(actual, 7)
     self.assertEqual(expected, result)
Example #7
0
 def test_negativedf2(self):
     actual = finv(0.05, 1, -1)
     self.assertTrue(np.isnan(actual))
Example #8
0
def _unirep_power_known_sigma_internal_pilot(rank_C, rank_U, total_N, rank_X,
                                             sigma_star, hypo_sum_square,
                                             expected_epsilon, epsilon, alpha,
                                             sigmastareval, unirep_method,
                                             **kwargs):
    """
    This function calculates power for univariate repeated measures power calculations with known Sigma.

    Parameters
    ----------
    rank_C: float
        rank of the C matrix
    rank_U: float
        rank of the U matrix
    total_N: float
        total number of observations
    rank_X:
        rank of the X matrix
    error_sum_square: float
        error sum of squares
    hypo_sum_square: float
        hypothesis sum of squares
    expected_epsilon: float
        expected value epsilon estimator
    epsilon:
        epsilon calculated from U`*SIGMA*U
    alpha:
        Significance level for target GLUM test
    unirep_method:
        Which method was used to find the expected value of epsilon.

        One of:

        * Uncorrected

        * geisser_greenhouse

        * chi_muller

        * hyuhn_feldt

        * box
    approximation_method:
        approximation used for cdf:

        * Muller and Barton (1989) approximation

        * Muller, Edwards and Taylor (2004) approximation
    or the univariate test.
    sigmastareval:
        eigenvalues  of SIGMASTAR=U`*SIGMA*U
    sigmastarevec:
        eigenvectors of SIGMASTAR=U`*SIGMA*U
    n_ip
        total N from internal pilot study
    rank_ip
        rank of WHAT??? in internal pilot

    Returns
    -------
    power: Power
        power for the univariate test.
    """
    approximation_method = Constants.UCDF_MULLER2004_APPROXIMATION
    internal_pilot = None
    noncentrality_dist = None
    quantile = None
    tolerance = 1e-12
    for key, value in kwargs.items():
        if key == 'approximation_method':
            approximation_method = value
        if key == 'internal_pilot':
            internal_pilot = value
        if key == 'noncentrality_distribution':
            noncentrality_dist = value
        if key == 'quantile':
            quantile = value
        if key == 'tolerance':
            tolerance = value
    # optional_args = __process_optional_args(**kwargs)
    # E = SIGMASTAR # (N - rX)
    nue = total_N - rank_X
    undf1, undf2 = _calc_undf1_undf2(unirep_method, expected_epsilon, nue,
                                     rank_C, rank_U)
    # Create defaults - same for either SIGMA known or estimated
    hypothesis_error = HypothesisError(hypo_sum_square, sigma_star, rank_U)
    e_1_2, e_3_5, e_4 = _calc_multipliers_internal_pilot(
        unirep_method, expected_epsilon, epsilon, hypothesis_error,
        sigmastareval, rank_C, rank_U, internal_pilot.n_ip,
        internal_pilot.rank_ip)

    # Error checking
    e_1_2 = _err_checking(e_1_2, rank_U)
    omega = e_3_5 * hypothesis_error.q2 / hypothesis_error.lambar
    fcrit = finv(1 - alpha, undf1 * e_1_2, undf2 * e_1_2)

    df1, df2, power = _calc_power_muller_approx(undf1, undf2, omega, alpha,
                                                e_3_5, e_4, fcrit)

    power = Power(power, omega, Constants.BOX)

    return power
Example #9
0
def _unirep_power_estimated_sigma(rank_C, rank_U, total_N, rank_X, sigma_star,
                                  hypo_sum_square, expected_epsilon, epsilon,
                                  alpha, unirep_method, **kwargs):
    """
    This function calculates power for univariate repeated measures power calculations with known Sigma.

    Parameters
    ----------
    rank_C: float
        rank of the C matrix
    rank_U: float
        rank of the U matrix
    total_N: float
        total number of observations
    rank_X:
        rank of the X matrix
    error_sum_square: float
        error sum of squares
    hypo_sum_square: float
        hypothesis sum of squares
    expected_epsilon: float
        expected value epsilon estimator
    epsilon:
        epsilon calculated from U`*SIGMA*U
    alpha:
        Significance level for target GLUM test
    unirep_method:
        Which method was used to find the expected value of epsilon.

        One of:

        * Uncorrected

        * geisser_greenhouse

        * chi_muller

        * hyuhn_feldt

        * box
    approximation_method:
        approximation used for cdf:

        * Muller and Barton (1989) approximation

        * Muller, Edwards and Taylor (2004) approximation
    or the univariate test.
    n_est:
        total N from estimate study
    rank_est:
        rank of WHAT??? from estimate study
    alpha_cl:
        type one error (alpha) for lower confidence bound
    alpha_cu:
        type one error (alpha) for lower confidence bound
    tolerance:
        value below which, numbers are considered zero

    Returns
    -------
    power: Power
        power for the univariate test.
    """
    approximation_method = Constants.UCDF_MULLER2004_APPROXIMATION
    confidence_interval = None
    noncentrality_dist = None
    quantile = None
    tolerance = 1e-12
    for key, value in kwargs.items():
        if key == 'approximation_method':
            approximation_method = value
        if key == 'confidence_interval':
            confidence_interval = value
        if key == 'noncentrality_distribution':
            noncentrality_dist = value
        if key == 'quantile':
            quantile = value
        if key == 'tolerance':
            tolerance = value

    # optional_args = __process_optional_args(**kwargs)
    # E = SIGMASTAR # (N - rX)
    nue = total_N - rank_X
    undf1, undf2 = _calc_undf1_undf2(unirep_method, expected_epsilon, nue,
                                     rank_C, rank_U)
    # Create defaults - same for either SIGMA known or estimated
    hypothesis_error = HypothesisError(hypo_sum_square, sigma_star, rank_U)
    cl1df, e_1_2, e_3_5, e_4, omegaua = _calc_multipliers_est_sigma(
        unirep_method=unirep_method,
        eps=epsilon.eps,
        hypothesis_error=hypothesis_error,
        nue=nue,
        rank_C=rank_C,
        rank_U=rank_U,
        approximation_method=approximation_method,
        n_est=confidence_interval.n_est,
        rank_est=confidence_interval.rank_est)
    # Error checking
    e_1_2 = _err_checking(e_1_2, rank_U)
    omega = e_3_5 * hypothesis_error.q2 / hypothesis_error.lambar
    if unirep_method == Constants.CM:
        omega = omegaua
    fcrit = finv(1 - alpha, undf1 * e_1_2, undf2 * e_1_2)

    df1, df2, power = _calc_power_muller_approx(undf1, undf2, omega, alpha,
                                                e_3_5, e_4, fcrit)
    power = Power(power, omega, Constants.SIGMA_ESTIMATED)
    if confidence_interval:
        cl_type = _get_cl_type(confidence_interval)
        power.glmmpcl(is_multirep=False,
                      alphatest=alpha,
                      dfh=cl1df,
                      n2=total_N,
                      rank_est=confidence_interval.rank_est,
                      dfe2=df2,
                      cl_type=cl_type,
                      n_est=confidence_interval.n_est,
                      alpha_cl=confidence_interval.lower_tail,
                      alpha_cu=confidence_interval.upper_tail,
                      fcrit=fcrit,
                      tolerance=tolerance,
                      omega=omega,
                      df1_unirep=df1)

    return power
Example #10
0
def _unirep_power_known_sigma(rank_C, rank_U, total_N, rank_X, sigma_star,
                              hypo_sum_square, expected_epsilon, epsilon,
                              alpha, unirep_method, **kwargs):
    """
    This function calculates power for univariate repeated measures power calculations with known Sigma.

    Parameters
    ----------
    rank_C: float
        rank of the C matrix
    rank_U: float
        rank of the U matrix
    total_N: float
        total number of observations
    rank_X:
        rank of the X matrix
    error_sum_square: np.matrix
        error sum of squares
    hypo_sum_square: np.matrix
        hypothesis sum of squares
    expected_epsilon: float
        expected value epsilon estimator
    epsilon:
        epsilon calculated from U`*SIGMA*U
    alpha:
        Significance level for target GLUM test
    unirep_method:
        Which method was used to find the expected value of epsilon.

        One of:

        * Uncorrected

        * geisser_greenhouse

        * chi_muller

        * hyuhn_feldt

        * box
    approximation_method:
        approximation used for cdf:

        * Muller and Barton (1989) approximation

        * Muller, Edwards and Taylor (2004) approximation


    Returns
    -------
    power: Power
        power for the univariate test.
    """
    # optional_args = __process_optional_args(**kwargs)
    approximation_method = Constants.UCDF_MULLER2004_APPROXIMATION
    noncentrality_dist = None
    quantile = None
    tolerance = 1e-12
    for key, value in kwargs.items():
        if key == 'approximation_method':
            approximation_method = value
        if key == 'noncentrality_distribution':
            noncentrality_dist = value
        if key == 'quantile':
            quantile = value
        if key == 'tolerance':
            tolerance = value

    nue = total_N - rank_X
    undf1, undf2 = _calc_undf1_undf2(unirep_method, expected_epsilon, nue,
                                     rank_C, rank_U)
    # Create defaults - same for either SIGMA known or estimated
    hypothesis_error = HypothesisError(hypo_sum_square, sigma_star, rank_U)
    e_1_2, e_3_5, e_4 = _calc_multipliers_known_sigma(epsilon,
                                                      expected_epsilon,
                                                      hypothesis_error, rank_C,
                                                      rank_U,
                                                      Constants.SIGMA_KNOWN)
    omega = e_3_5 * hypothesis_error.q2 / hypothesis_error.lambar
    # Error checking
    e_1_2 = _err_checking(e_1_2, rank_U)
    fcrit = finv(1 - alpha, undf1 * e_1_2, undf2 * e_1_2)

    if noncentrality_dist and quantile:
        omega = __calc_quantile_omega(noncentrality_dist, quantile)
        df1, df2, power = _calc_power_muller_approx(undf1, undf2, omega, alpha,
                                                    e_3_5, e_4, fcrit)
    elif noncentrality_dist and not quantile:
        df1 = undf1 * e_3_5
        df2 = undf2 * e_4
        power = noncentrality_dist.unconditional_power_simpson(fcrit=fcrit,
                                                               df1=df1,
                                                               df2=df2)
    else:
        # 2. Muller, Edwards & Taylor 2002 and Muller Barton 1989 CDF approx
        # UCDFTEMP[]=4 reverts to UCDFTEMP[]=2 if exact CDF fails
        df1, df2, power = _calc_power_muller_approx(undf1, undf2, omega, alpha,
                                                    e_3_5, e_4, fcrit)

    power = Power(power, omega, Constants.SIGMA_KNOWN)

    return power