Intraclass Correlation in Python Module?

前端 未结 5 1359
抹茶落季
抹茶落季 2021-02-07 14:19

I\'m looking to calculate intraclass correlation (ICC) in Python. I haven\'t been able to find an existing module that has this feature. Is there an alternate name, or should I

5条回答
  •  终归单人心
    2021-02-07 15:09

    There are several implementations of the ICC in R. These can be used from Python via the rpy2 package. Example:

    from rpy2.robjects import DataFrame, FloatVector, IntVector
    from rpy2.robjects.packages import importr
    from math import isclose
    
    groups = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
              4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8]
    values = [1, 2, 0, 1, 1, 3, 3, 2, 3, 8, 1, 4, 6, 4, 3,
              3, 6, 5, 5, 6, 7, 5, 6, 2, 8, 7, 7, 9, 9, 9, 9, 8]
    
    r_icc = importr("ICC")
    df = DataFrame({"groups": IntVector(groups),
                    "values": FloatVector(values)})
    icc_res = r_icc.ICCbare("groups", "values", data=df)
    icc_val = icc_res[0] # icc_val now holds the icc value
    
    # check whether icc value equals reference value
    print(isclose(icc_val, 0.728, abs_tol=0.001))
    

提交回复
热议问题