Intraclass Correlation in Python Module?

前端 未结 5 1360
抹茶落季
抹茶落季 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 14:51

    The R package psych has an implementation of the Intraclass Correlations (ICC) that calculates many types of variants including ICC(1,1), ICC(1,k), ICC(2,1), ICC(2,k), ICC(3,1) and ICC(3,k) plus other metrics.

    This page has a good comparison between the different variants,

    You can use the R ICC function via rpy2 package.

    Example:

    1. First install psych and lme4 in R:
    install.packages("psych")
    install.packages("lme4")
    
    1. Calculate ICC coefficients in Python using rpy2:
    import rpy2
    from rpy2.robjects import IntVector, pandas2ri
    from rpy2.robjects.packages import importr
    
    psych = importr("psych")
    
    values = rpy2.robjects.r.matrix(
        IntVector(
            [9,    2,   5,    8,
            6,    1,   3,    2,
            8,    4,   6,    8,
            7,    1,   2,    6,
            10,   5,   6,    9,
            6,   2,   4,    7]),
        ncol=4, byrow=True
    )
    
    icc = psych.ICC(values)
    
    # Convert to Pandas DataFrame
    icc_df = pandas2ri.rpy2py(icc[0])
    

    Results:

                                type    ICC        F           df1   df2    p          lower bound   upper bound  
      Single_raters_absolute    ICC1    0.165783   1.794916    5.0   18.0   0.164720   -0.132910     0.722589     
      Single_random_raters      ICC2    0.289790   11.026650   5.0   15.0   0.000135   0.018791      0.761107     
      Single_fixed_raters       ICC3    0.714829   11.026650   5.0   15.0   0.000135   0.342447      0.945855     
      Average_raters_absolute   ICC1k   0.442871   1.794916    5.0   18.0   0.164720   -0.884193     0.912427     
      Average_random_raters     ICC2k   0.620080   11.026650   5.0   15.0   0.000135   0.071153      0.927240     
      Average_fixed_raters      ICC3k   0.909311   11.026650   5.0   15.0   0.000135   0.675657      0.985891  
    

提交回复
热议问题