Exemple #1
0
def test_odds_ratio():
    table = [(1, 2), (3, 4)]

    ratio, or_ci = analyses.odds_ratio(table)

    assert np.allclose(ratio, .6667, atol=.01)
    assert np.allclose(or_ci, (0.03939, 11.28), atol=.01)
Exemple #2
0
def or_plot(df, risk_cols, outcome_col, risk_order, outcome_order, fig=None, ax=None):
    """
    df = pandas dataframe of line listing
    cols = list of columns to include in analysis
    risk_order: dictionary with risk_cols as keys, and a list of values as values, e.g. {'sex':['male', 'female']}
    outcome_order: list of values, e.g. ['alive', 'dead']
    """

    ratio_df = []

    for risk_col in risk_cols:
      #  if risk_order != False:
        order = risk_order[risk_col]

        #elif risk_order == False:
        #    risks = ["{}".format(val) for val in df[risk_col].dropna().unique()]
        #    outcome_order = ["{}".format(val) for val in df[outcome_col].dropna().unique()]

        _df = df[[outcome_col, risk_col]].dropna(how='any')

        if len(_df[outcome_col].unique()) > 2:
            raise Exception('More than two unique values in the outcome')

        if len(_df[risk_col].unique()) > 2:
            raise Exception('More than two unique values in {}'.format(risk_col))

        table = analyses.create_2x2(_df, risk_col, outcome_col, order, outcome_order)

        ratio, or_ci = analyses.odds_ratio(table)
        ratio_df.append({'names': risk_col, 'ratio':ratio, 'lower':or_ci[0], 'upper':or_ci[1]})

    fig, ax = _plot(ratio_df, fig, ax)

    return fig, ax
Exemple #3
0
def test_odds_ratio():
    table = [(1, 2),
             (3, 4)]

    ratio, or_ci = analyses.odds_ratio(table)

    assert np.allclose(ratio, .6667, atol=.01)
    assert np.allclose(or_ci, (0.03939, 11.28), atol=.01)
Exemple #4
0
def or_plot(df,
            risk_cols,
            outcome_col,
            risk_order,
            outcome_order,
            fig=None,
            ax=None):
    """
    df = pandas dataframe of line listing
    cols = list of columns to include in analysis
    risk_order: dictionary with risk_cols as keys, and a list of values as values, e.g. {'sex':['male', 'female']}
    outcome_order: list of values, e.g. ['alive', 'dead']
    
    RETURNS
    --------
    fig, ax = figure and axis objects
    """

    ratio_df = []

    for risk_col in risk_cols:
        #  if risk_order != False:
        order = risk_order[risk_col]

        #elif risk_order == False:
        #    risks = ["{}".format(val) for val in df[risk_col].dropna().unique()]
        #    outcome_order = ["{}".format(val) for val in df[outcome_col].dropna().unique()]

        _df = df[[outcome_col, risk_col]].dropna(how='any')

        if len(_df[outcome_col].unique()) > 2:
            raise Exception('More than two unique values in the outcome')

        if len(_df[risk_col].unique()) > 2:
            raise Exception(
                'More than two unique values in {}'.format(risk_col))

        table = analyses.create_2x2(_df, risk_col, outcome_col, order,
                                    outcome_order)
        print risk_col + ':'
        ratio, or_ci = analyses.odds_ratio(table)
        print '\n'

        ratio_df.append({
            'names': risk_col,
            'ratio': ratio,
            'lower': or_ci[0],
            'upper': or_ci[1]
        })

    fig, ax = _plot(ratio_df, fig, ax)

    return fig, ax