Skip to content

相关系数

计算 Pearson 相关系数矩阵及其显著性。

核心代码

py
    def _fit_correlation(self, df: pd.DataFrame, decimals: int, title: str) -> None:
        """
        执行相关性分析(带显著性星号)
        
        参数:
            df: 清洗后的数据框
            decimals: 小数位数
            title: 表格标题
        """
        cols = df.columns
        n = len(cols)
        corr_matrix = df.corr()
        p_matrix = np.zeros((n, n))
        
        # 计算 p-value
        for i in range(n):
            for j in range(n):
                if i == j:
                    p_matrix[i, j] = 1.0
                else:
                    _, p = pearsonr(df[cols[i]], df[cols[j]])
                    p_matrix[i, j] = p
        
        # 格式化带星号的字符串矩阵
        formatted_df = pd.DataFrame(index=cols, columns=cols)
        for i in range(n):
            for j in range(n):
                val = corr_matrix.iloc[i, j]
                p_val = p_matrix[i, j]
                
                stars = ""
                if i != j:  # 对角线不加星
                    if p_val < 0.01:
                        stars = "***"
                    elif p_val < 0.05:
                        stars = "**"
                    elif p_val < 0.1:
                        stars = "*"
                
                formatted_df.iloc[i, j] = f"{val:.{decimals}f}{stars}"
        
        title = title if title else "Correlation Matrix"
        note = "*** p<0.01, ** p<0.05, * p<0.1"
        self.custom_html = self._generate_html_table(
            formatted_df, title, index_name="Variables", bottom_note=note
        )

Released under the AGPL-3.0 License.