Constructing the Corrplot function

55 visualizaciones (últimos 30 días)
Tarek Hajj Shehadi
Tarek Hajj Shehadi el 13 de Jul. de 2021
Editada: Sivani Pentapati el 6 de Ag. de 2021
I am currently learning the functions used in the code from the user Adam Danz's answer in this link https://www.mathworks.com/matlabcentral/answers/42026-undefined-function-corrplot
My ultimate goal is to understand how the code he wrote works on any set of data chosen. The code he wrote is as follow :
% Load data
load('carsmall')
% Unlike corrplot, plotmatrix cannot receive a table.
% Form a matrix of variables in each column.
data = [Horsepower, Weight, Acceleration, MPG];
nVars = size(data,2);
% Remove any rows that contain NaN values. Otherwise corr() will
% return NaN.
data(any(isnan(data),2), :) = [];
% Create plotmatrix
figure('Name', 'carsmall_data')
[sh, ax, ~, hh] = plotmatrix(data);
% Add axis labels
arrayfun(@(h,lab)ylabel(h,lab),ax(:,1), {'Horsepower','Weight','Accel.','MPG'}')
arrayfun(@(h,lab)xlabel(h,lab),ax(end,:), {'Horsepower','Weight','Accel.','MPG'})
% Compute correlation for each scatter plot axis
[r,p] = arrayfun(@(h)corr(h.Children.XData(:),h.Children.YData(:)),ax(~eye(nVars)));
% Label the correlation and p values
arrayfun(@(h,r,p)text(h,min(xlim(h))+range(xlim(h))*.05,max(ylim(h)),...
sprintf('r=%.2f, p=%.3f',r,p),'Horiz','Left','Vert','top','FontSize',8,'Color','r'),...
ax(~eye(nVars)),r,p)
% Change marker appearance
set(sh, 'Marker', 'o','MarkerSize', 2, 'MarkerEdgeColor', ax(1).ColorOrder(1,:))
lsh = arrayfun(@(h)lsline(h),ax(~eye(nVars)));
% Add least square regression line.
set(lsh,'color', 'm')
From my part, lets say for the sake of simplicity I have a set of data x=[0.25,0.5,0.75,1,2,3,6,8] and y=[1,1,4,6,1,2,3,5] and I wish to construct the correlation matrix as seen in the code above with these data. I would mimic the code written above for which I have written the following :
data = [x',y'];
nVars = size(data,2);
% Remove any rows that contain NaN values. Otherwise corr() will
% return NaN.
data(any(isnan(data),2), :) = [];
% Create plotmatrix
figure('Name', 'Some Data')
[S, AX, ~, H] = plotmatrix(data);
% Add axis labels
X=[x' y'];
arrayfun(@(h,lab)ylabel(h,lab),AX(:,1), {'y'})
arrayfun(@(h,lab)xlabel(h,lab),AX(end,:), {'x'})
% Compute correlation for each scatter plot axis
[r,p] = arrayfun(@(h)corr(X));
% Label the correlation and p values
arrayfun(@(h,r,p)text(h,min(xlim(h))+range(xlim(h))*.05,max(ylim(h)),...
sprintf('r=%.2f, p=%.3f',r,p),'Horiz','Left','Vert','top','FontSize',8,'Color','r'),...
AX(~eye(nVars)),r,p)
% Change marker appearance
set(S, 'Marker', 'o','MarkerSize', 2, 'MarkerEdgeColor', AX(1).ColorOrder(1,:))
lsh = arrayfun(@(h)lsline(h),AX(~eye(nVars)));
% Add least square regression line.
set(lsh,'color', 'm')
The output was this graph :
which is very reasonable but I did not obtain the values of r and p and the regression line for reasons which I want to know why and how to fix it.
The error obtained in this process of execution was the following :
Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 2 in dimension 1. Input #3 has size 1
I would like to also understand more how the arrayfun operates in this case.

Respuesta aceptada

Sivani Pentapati
Sivani Pentapati el 6 de Ag. de 2021
Editada: Sivani Pentapati el 6 de Ag. de 2021
Based on my understanding, you want to obtain the r and p values with your custom data. arrayfun function takes in each element of the input data and applies the corresponding function. Modifying the arrayfun function call to the axes, as shown in the below code will fix the issue
arrayfun(@(h) ylabel(h,'y'),AX(:,1))
arrayfun(@(h) xlabel(h,'x'),AX(end,:))
% Compute correlation for each scatter plot axis
[r,p]=arrayfun(@(h)corr(h.Children.XData(:),h.Children.YData(:)),AX(~eye(nVars)));
Attaching the modified code for your reference.

Más respuestas (0)

Categorías

Más información sobre Visual Exploration en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by