pg.intraclass_corr源码
时间: 2024-02-06 13:12:42 浏览: 275
pg.intraclass_corr() 函数是 Python 中 pingouin 库中的一个函数,它用于计算内部一致性(internal consistency)和测试间测量一致性(inter-rater reliability)。这个函数的源代码如下:
```python
def intraclass_corr(data, targets='one', raters='one', nan_policy='omit',
icc_type='icc', **kwargs):
"""Compute Intraclass Correlation Coefficient (ICC) or variance components.
Parameters
----------
data : pandas DataFrame
A pandas DataFrame in long format. Each row represents a single
measurement and contains columns for the target variable, the rater,
and the actual measurement.
targets : str or list of str
The column name(s) for the target variable(s). If 'one', a single ICC
value will be computed for all variables.
raters : str or list of str
The column name(s) for the raters. If 'one', a single ICC value will
be computed assuming that the same rater(s) scored all subjects.
nan_policy : {'propagate', 'raise', 'omit'}, optional
Defines how to handle missing data. Default is 'omit'.
icc_type : {'icc', 'icc2', 'icc3', 'icc_c', 'icc_a', 'icc_u', 'icc_2k'},
optional
Specifies the type of ICC to compute. Default is 'icc'.
**kwargs
Additional arguments to pass to the ICC functions.
Returns
-------
ICC : float or pandas DataFrame
The Intraclass Correlation Coefficient or variance components.
If targets='one' and raters='one', a single ICC value is returned.
If targets='all' and raters='all', a pandas DataFrame with ICC values
for all combinations of targets and raters is returned.
Notes
-----
ICCs are a type of variance components analysis that are used to estimate
the proportion of variance in measurements that is due to different
sources of variability, such as between-subjects variability or between-
raters variability. ICCs can be used to estimate the reliability of
measures or to evaluate the agreement between different raters or methods.
ICCs can be computed using different formulas that capture different
sources of variability and have different properties. The most commonly
used ICC formulas are ICC(1), ICC(2), and ICC(3). ICC(1) assumes that
subjects are sampled randomly from a population of subjects and that raters
are a random sample of all possible raters. ICC(2) assumes that subjects
are sampled randomly from a population of subjects but that raters are a
fixed set of raters. ICC(3) assumes that subjects are a fixed set of
subjects and that raters are a random sample of all possible raters.
References
----------
.. [1] Shrout, P. E., & Fleiss, J. L. (1979). Intraclass correlations:
Uses in assessing rater reliability. Psychological bulletin,
86(2), 420-428.
.. [2] McGraw, K. O., & Wong, S. P. (1996). Forming inferences about some
intraclass correlation coefficients. Psychological methods,
1(1), 30-46.
.. [3] Hallgren, K. A. (2012). Computing inter-rater reliability for
observational data: An overview and tutorial. Tutorials in
quantitative methods for psychology, 8(1), 23-34.
"""
# Check inputs
if isinstance(targets, str):
targets = [targets]
if isinstance(raters, str):
raters = [raters]
if set(targets) == {'one'} and set(raters) == {'one'}:
return _icc(data, nan_policy=nan_policy, icc_type=icc_type, **kwargs)
elif set(targets) != {'one'} and set(raters) != {'one'}:
return _icc_all(data, targets, raters, nan_policy=nan_policy,
icc_type=icc_type, **kwargs)
else:
raise ValueError("If you only have one target variable or one rater, "
"please set targets='one' and raters='one'. Otherwise, "
"set targets='all' and raters='all'.")
```
这个函数首先会检查输入的参数,然后根据 targets 和 raters 的值,来决定是计算所有的 ICC 还是只计算单个 ICC。如果计算所有的 ICC,会调用 _icc_all() 函数;否则会调用 _icc() 函数。不同的 ICC 公式由 icc_type 参数控制,默认为 'icc',即 ICC(1) 公式。
阅读全文