How to calculate the determination coefficient R^2 with two matrix ?

조회 수: 31 (최근 30일)
albert Kinda
albert Kinda 2024년 7월 28일
편집: Torsten 2024년 7월 28일
hello I have two matrices A(i,j) and B (i,j). How to calculate for each i the covariance (A,B)? also calculate the determination coefficientR ^2?

채택된 답변

Ayush Modi
Ayush Modi 2024년 7월 28일
Hi Albert,
You can calculate the covariance between two matrices A and B using "cov" function. To calculate covariance for each row, iterate over each row using a loop. Here is the sample code:
A = [1 9 3; 4 5 6; 7 8 6]; % Replace with your matrix A
B = [6 8 7; 6 5 4; 3 2 1]; % Replace with your matrix B
[numRows, numCols] = size(A);
covarianceValues = zeros(numRows, 1);
R2Values = zeros(numRows, 1);
% Loop through each row
for i = 1:numRows
% Extract the i-th row from A and B
Ai = A(i, :);
Bi = B(i, :);
covMatrix = cov(Ai, Bi);
covarianceValues(i) = covMatrix(1, 2);
To calculate the determination coefficient R^2, you can calculate the standard deviation of each row using "std" function and apply the formula for 'r'.
sigmaA = std(Ai);
sigmaB = std(Bi);
% Calculate the determination coefficient R^2
R2Values(i) = (covarianceValues(i) / (sigmaA * sigmaB))^2;
end
disp(covarianceValues);
4.0000 -1.0000 0.5000
disp('Determination coefficient R^2 for each row:');
Determination coefficient R^2 for each row:
disp(R2Values);
0.9231 1.0000 0.2500
Note - covariance matrix covMatrix returned by the cov function contains the variances of Ai and Bi on the diagonal and the covariance between Ai and Bi off the diagonal.
Refer to the following documentation for more information on the function:
  댓글 수: 4
albert Kinda
albert Kinda 2024년 7월 28일
The correlation coefficient is the covariance divided by the two standard deviations marginal
The determination coefficient is the square of the correlation coefficient
Torsten
Torsten 2024년 7월 28일
편집: Torsten 2024년 7월 28일
You asked for RMSE in your comment, not for the correlation coefficient or the coefficient of determination.
Better you just write down the mathematical formula of what you want if a, b are vectors of the same size. This will avoid confusion about terminology .

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by