dyn*_*mic 19 statistics matlab
我有两个向量:
A_1 =
10
200
7
150
A_2 =
0.001
0.450
0.0007
0.200
Run Code Online (Sandbox Code Playgroud)
我想知道这两个载体之间是否存在相关性.
我可以减去每个值的向量的平均值,而不是:
A_1' * A_2
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法?
Pur*_*uit 23
鉴于:
A_1 = [10 200 7 150]';
A_2 = [0.001 0.450 0.007 0.200]';
Run Code Online (Sandbox Code Playgroud)
(正如其他人已经指出的那样)有一些工具可以简单地计算相关性,最明显的是corr:
corr(A_1, A_2); %Returns 0.956766573975184 (Requires stats toolbox)
Run Code Online (Sandbox Code Playgroud)
你也可以使用matlab的基本corrcoef功能,如下所示:
M = corrcoef([A_1 A_2]): %Returns [1 0.956766573975185; 0.956766573975185 1];
M(2,1); %Returns 0.956766573975184
Run Code Online (Sandbox Code Playgroud)
这与cov功能密切相关:
cov([condition(A_1) condition(A_2)]);
Run Code Online (Sandbox Code Playgroud)
正如您在原始问题中所做的那样,如果需要,您可以自己缩放和调整矢量,这样可以更好地理解正在发生的事情.首先创建一个减去均值的条件函数,并除以标准差:
condition = @(x) (x-mean(x))./std(x); %Function to subtract mean AND normalize standard deviation
Run Code Online (Sandbox Code Playgroud)
然后相关性似乎是(A_1*A_2)/(A_1 ^ 2),如下所示:
(condition(A_1)' * condition(A_2)) / sum(condition(A_1).^2); %Returns 0.956766573975185
Run Code Online (Sandbox Code Playgroud)
通过对称,这也应该有效
(condition(A_1)' * condition(A_2)) / sum(condition(A_2).^2); %Returns 0.956766573975185
Run Code Online (Sandbox Code Playgroud)
确实如此.
我相信,但是现在没有足够的精力来确定,在处理多维输入时,只要在处理尺寸和方向时,可以使用相同的数学计算相关和互相关项.输入数组.
Eit*_*n T 10
试试吧xcorr,它是MATLAB中用于互相关的内置函数:
c = xcorr(A_1, A_2);
Run Code Online (Sandbox Code Playgroud)
但请注意,它需要安装信号处理工具箱.如果没有,您可以查看corrcoef命令.
对于相关性,您可以使用corr函数(统计工具箱)
corr(A_1(:), A_2(:))
Run Code Online (Sandbox Code Playgroud)
请注意,您也可以使用
corr(A_1, A_2)
Run Code Online (Sandbox Code Playgroud)
但线性索引可确保您的向量不需要转置.
为了执行两个向量之间的线性回归x和y按照下列步骤:
[p,err] = polyfit(x,y,1); % First order polynomial
y_fit = polyval(p,x,err); % Values on a line
y_dif = y - y_fit; % y value difference (residuals)
SSdif = sum(y_dif.^2); % Sum square of difference
SStot = (length(y)-1)*var(y); % Sum square of y taken from variance
rsq = 1-SSdif/SStot; % Correlation 'r' value. If 1.0 the correlelation is perfect
Run Code Online (Sandbox Code Playgroud)
对于x=[10;200;7;150]和y=[0.001;0.45;0.0007;0.2]我得到rsq = 0.9181.
参考网址:http://www.mathworks.com/help/matlab/data_analysis/linear-regression.html