偏相关系数计算
偏相关系数计算
参考
简单相关系数旨在反映变量之间两两线性关系,但实际上,每⼀个简单相关系数不可能绝对不包括其他因素的相关成分。为了克服简单相关系数的间接相关信息,有⼈设计了另⼀种检验指标,称为偏相关系数( partial correlation coefficient )。偏相关系数旨在排除其它因素的影响,单纯反映某个⾃变量与因变量之间的密切程度。
当⾃变量较多时,利⽤公式计算偏相关系数相当⿇烦,⽐较便捷的⽅式是借助简单相关系数构成的相关矩阵进⾏运算,计算公式如下:
R x j y =−c jy
c jj c yy
这⾥R x j y 为第 j 个⾃变量与因变量 y 的偏相关系数, c 为相关系数矩阵的逆矩阵中对应的元素。
下⾯是python 实现
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 20 16:53:39 2021
modified: /fabianp/9396204419c7b638d38f
@author: pan
"""
import numpy as np
from numpy.linalg import inv
from osgeo import gdal, gdal_array
import os, time,glob
from sklearn import linear_model
from sklearn import preprocessing沈阳在线
from matplotlib import pyplot as plt
def partial_corr(C):
"""
Returns the sample linear partial correlation coefficients between pairs of variables in C, controlling
for the remaining variables in C.
corner怎么读
Parameters
混响英文
alpha什么意思----------
C : array-like, shape (n, p)
Array with the different variables. Each column of C is taken as a variable
Returns
teachers day-
------
P_corr : array-like, shape (p, p)
P_corr[i, j] contains the partial correlation of C[:, i] and C[:, j] controlling
for the remaining variables in C.
sva
imgur"""
C = np.asarray(C)
p = C.shape[1]济南雅思培训
√
P_corr = np.zeros((p, p)) # sample linear partial correlation coefficients
corr = np.corrcoef(C,rowvar=Fal) # Pearson product-moment correlation coefficients. corr_inv = inv(corr) # the (multiplicative) inver of a matrix.
for i in range(p):
P_corr[i, i] = 1北京王府学校
for j in range(i+1, p):
内容的英文pcorr_ij = -corr_inv[i,j]/(np.sqrt(corr_inv[i,i]*corr_inv[j,j]))
P_corr[i,j]=pcorr_ij
P_corr[j,i]=pcorr_ij
return P_corr
Processing math: 100%