- Use the "corr" function to calculate correlation between ( Y ) and each independent variable. Below is the MATLAB code snippet to achieve this:
Pearson correlation graph between variables
조회 수: 37 (최근 30일)
이전 댓글 표시
Hi
There are 6 variables (10,000 of each) which I am importing from excel and then using these, I am calculating a new variable let us say, Y (again, 10,000 iterations). How do I plot Pearson correlelation coeffcient between Y and each of 6 variables as tornado plots?
Thanks in advance
댓글 수: 0
채택된 답변
Shubham
2024년 8월 30일
편집: Shubham
2024년 8월 30일
Hi Sunil,
To create a tornado plot of Pearson Correlation Coefficients between a dependent variable ( Y ) and six independent variables, follow these steps:
% List of variable names
variables = {'Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var6'};
correlations = zeros(1, length(variables));
% Calculate correlations
for i = 1:length(variables)
correlations(i) = corr(data.(variables{i}), Y);
end
2. Use the "barh" function to create a horizontal bar chart for the tornado plot.
barh(correlations, 'FaceColor', [0.5, 0.7, 1]);
Refer to the following MathWorks documentation link for more information on "corr":
Hope this helps.
댓글 수: 4
Shubham
2024년 9월 3일
Hi Sunil,
This issue may be due to Field Names mismatch. Ensure that your "data" structure actually contains a field named "C".
You can check the fields of "data" using:
fieldnames(data)
Hope this helps.
추가 답변 (1개)
KSSV
2024년 8월 30일
clc; clear ;
R = zeros(6,1) ;
for i = 1:6
X = rand(100,1) ;
y = rand(100,1) ;
R(i) = regression(X',y') ;
end
barh(R)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!