Calculate the correlation coefficient R and coefficient of determination, R2 and P-value or significance

21 visualizzazioni (ultimi 30 giorni)
Learning il 8 Apr 2023
Risposto: Sai Pavan il 17 Apr 2024
Hello,
I have a question: I have a single Y and multiple X columns (say up to X50). The image below is a simple version. How do I calculate both R and R^2 between Y and each X column as well as the p-values to determine whetther the correlation between Y and each X is significant or not using 95 percent confidence interval? I would like all the results, i.e., R & R^2 and p-values for the correlation between Y and each X column to be output as a table called RESULTS.
Thank you.
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposte (1)

Sai Pavan il 17 Apr 2024
Hello,
I understand that you want to calculate the correlation coefficient R, coefficient of determination R^2 and P-value of Y and each column of X.
The "corr" function returns both the correlation coefficient and the p-value, which are used to determine the significance of the correlation. The "corr" function will return the Pearson's Linear Correlation Coefficient, R, which can then be squared to get the coefficient of determination R^2. By default, the significance level is set for a 95% confidence interval to get the p-value, which is commonly used in statistical analysis.
Please refer to the below code snippet that demonstrates how the calculations can be done:
Y = [1; 2; 3; 4; 5; 6];
X1 = [7; 8; 9; 10; 11; 12];
X2 = [13; 14; 15; 16; 17; 18];
X3 = [19; 20; 21; 22; 23; 24];
X4 = [25; 26; 27; 28; 29; 30];
T = table(Y, X1, X2, X3, X4);
% Initialize arrays to hold the results
variables = T.Properties.VariableNames(2:end); % Exclude Y column
R_values = zeros(length(variables), 1);
R_squared = zeros(length(variables), 1);
P_values = zeros(length(variables), 1);
% Loop through each X column to calculate R, R^2, and p-values
for i = 1:length(variables)
[R, P] = corr(T.Y, T.(variables{i}));
R_values(i) = R;
R_squared(i) = R^2;
P_values(i) = P;
end
% Create a results table
RESULTS = table(variables', R_values, R_squared, P_values, ...
'VariableNames', {'Variable', 'R', 'R_Squared', 'P_Value'});
% Display the results
disp(RESULTS);
Hope it helps!
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by