所以我现在正在尝试计算Pearson的R和p值,以获得我的一些数据。这是通过以下代码完成的:
import numpy as np
from scipy.stats import pearsonr, betai
from pandas import DataFrame
import seaborn as sns
import matplotlib.pyplot as plt
def corrcoef(matrix): #function that calculates the Pearson's R and p-value
r = np.corrcoef(matrix)
rf = r[np.triu_indices(r.shape[0], 1)]
df = matrix.shape[1] - 2
ts = rf * rf * (df / (1 - rf * rf))
pf = betai(0.5 * df, 0.5, df / (df + ts))
p = np.zeros(shape=r.shape)
p[np.triu_indices(p.shape[0], 1)] = pf
p[np.tril_indices(p.shape[0], -1)] = pf
p[np.diag_indices(p.shape[0])] = np.ones(p.shape[0])
return r, p
data = np.loadtxt('corr-data.txt') #data matrix loaded
sig_lvl = 0.05 #significance level
r_mat, p_mat = corrcoef(data) #use function on data and put the answers in two different matrices
df_rmat = DataFrame(r_mat, columns=Index, index=Index) #make data readable for the seaborn package
df_pmat = DataFrame(p_mat, columns=Index, index=Index)
r_mat[abs(r_mat) <= .90] = np.nan #if the R-value matrix elements are under 0.90, don't show them - make them NaN.
p_mat[abs(p_mat) >= sig_lvl] = np.nan #this is probably the issue.
mask_pmat = np.zeros_like(p_mat)
mask_pmat[np.tril_indices_from(mask_pmat)] = True #only showing the upper triangle of the values since it's symmetrical in the diagonal
sns.plt.subplot(1,2,2)
ax_pmat = sns.heatmap(np.around(df_pmat, decimals=2), annot=True, mask = mask_pmat) #subplot sequence for the p-value matrix only
sns.plt.show()
它可能不是最优化的代码,但是到目前为止它按预期工作。使用seaborn包我得到不同值的热/色图,如果它们足够高(> = 0.95)或具有正确的显着性水平,并且只有上三角形。但是,我真正想要做的只是显示第一个图中表示的那些R值的p值。小于0.95的值仅由NaN替换,并且在热图中没有颜色。因此,如果表示R值矩阵中的值,则只应表示p值矩阵中的值。
可以这样做,还是......?
如果不清楚,请告诉我。然后我会尝试进一步解释。
提前致谢
答案 0 :(得分:2)
我认为你所说的是:
p_mat[r_mat < 0.95] = np.nan
这可行,因为p
和r
的形状相同。它会进入你的代码而不是:
if r_mat[abs(r_mat) <= .90] == np.nan:
p_mat = np.nan
请注意,如果将NaN
与值进行比较,则结果始终为false。