利用PYTHON計算偏相關系數(Partial correlation coefficient)
在統計學中,我們經常使用皮爾遜相關系數來衡量兩個變量之間的線性關系。然而,有時我們感興趣的是理解兩個變量之間的關系,同時控制第三個變量。
例如,假設我們想要測量學生學習的小時數和他們獲得的期末考試成績之間的關聯,同時控制學生在班級中的當前成績。在這種情況下,我們可以使用部分相關來衡量學習時間和期末考試成績之間的關系。
例如:Partial Correlation in Python
假設我們有如下的DataFrame,它顯示了10名學生的當前年級、學習總小時數和期末考試成績:
為了在控制currentGrade的同時計算hours和examScore之間的部分相關性,我們可以使用pingouin包中的partial_corr()函數,它使用以下語法:
partial_corr(data, x, y, covar)
where:
data: name of the dataframe
x, y: names of columns in the dataframe
covar: the name of the covariate column in the dataframe (e.g. the variable you’re controlling for)
我們可以看到,學習時數與期末考試成績的偏相關系數為0.191,是一個很小的正相關。隨着學習時間的增加。如果當前的分數保持不變,考試分數也會增加。
要一次性計算多個變量之間的部分相關性,可以使用.pcorr()函數:
翻譯於https://www.statology.org/partial-correlation-python/
參考鏈接:https://blog.csdn.net/qq_32678749/article/details/108125508?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.control