Skip to content

Add ICC Calculation #48

@Sharpz7

Description

@Sharpz7
def compute_icc(x, y, agreement="absolute"):
    """
    Compute ICC for paired data using pingouin.

    Parameters
    ----------
    x, y : array-like
        Lists (or arrays) of measurements.
    agreement : str, optional
        Type of agreement to compute. Either 'absolute' (default) or 'consistency'.
        'absolute' corresponds to ICC(2,1) or ICC(3,1) depending on the model.
        'consistency' corresponds to ICC(1,1), ICC(2,1), or ICC(3,1) depending on the model.

    Returns
    -------
    dict
        Dictionary containing:
        - 'icc': The computed ICC value
        - 'ci_lower': Lower bound of 95% confidence interval
        - 'ci_upper': Upper bound of 95% confidence interval
        - 'agreement': Type of agreement used
    """
    # Convert inputs to numpy arrays
    x = np.array(x)
    y = np.array(y)

    # Stack data for pingouin format (long format)
    n = len(x)
    data = np.vstack((x, y)).T  # shape: (n subjects, 2 raters)

    # Prepare data for pingouin (long format)
    subjects = np.repeat(np.arange(n), 2)
    # identifiers for the two different raters
    raters = np.tile(np.array([1, 2]), n)
    ratings = data.flatten()

    # Determine ICC type based on agreement parameter
    if agreement == "absolute":
        icc_type = "ICC2"  # Two-way mixed effects, absolute agreement
    elif agreement == "consistency":
        icc_type = "ICC3"  # Two-way mixed effects, consistency
    else:
        raise ValueError(
            "agreement must be either 'absolute' or 'consistency'"
        )

    # Compute ICC using pingouin
    icc_results = pg.intraclass_corr(
        data=pd.DataFrame(
            {"subjects": subjects, "raters": raters, "ratings": ratings}
        ),
        targets="subjects",
        raters="raters",
        ratings="ratings",
    )

    # Extract the relevant ICC type
    icc_row = icc_results.loc[icc_results["Type"] == icc_type]

    if icc_row.empty:
        raise ValueError(f"ICC type {icc_type} not found in results")

    # Return ICC value and confidence interval
    return {
        "icc": icc_row["ICC"].values[0],
        "ci_lower": icc_row["CI95%"].values[0][0],
        "ci_upper": icc_row["CI95%"].values[0][1],
        "agreement": agreement,
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions