Beispiel #1
0
def test_2x2_errorRaises():
    df = pd.DataFrame({'Exposed':['Y', 'Y', 'N', 'Y'], \
                          'Sick':['Y', 'N', 'N', 'Y']})

    with pytest.raises(TypeError):
        table = analyses.create_2x2(df, 'Exposed', 'Sick', ['Y', 'N'], \
            'Y')

    with pytest.raises(AssertionError):
        table = analyses.create_2x2(df, 'Exposed', 'Sick', ['Y', 'N'], \
            ['Y'])
Beispiel #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
Beispiel #3
0
def test_create2x2():
    df = pd.DataFrame({'Exposed':['Y', 'Y', 'N', 'Y'], \
                          'Sick':['Y', 'N', 'N', 'Y']})
    table = analyses.create_2x2(df, 'Exposed', 'Sick', ['Y', 'N'], \
            ['Y', 'N'])

    assert table.ix[0][0] == 2
    assert table.ix[0][1] == 1
    assert table.ix[1][0] == 0
    assert table.ix[1][1] == 1
Beispiel #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