MATLAB Answers

Error using corrplot function

조회 수: 32(최근 30일)
Quincy Brouwer
Quincy Brouwer 11 Apr 2021 17:53
댓글: Adam Danz 13 Apr 2021 21:46
I try to plot the correlation between two variables, but I keep getting a error:
Undefined function 'corrplot' for input arguments of type 'double'.
The code I used was:
How can I fix this?
  댓글 수: 3
dpb 11 Apr 2021 21:52
Well, it's a slightly enhanced version of the base plotmatrix() function that will produce the diagonal histogram and off-diagonal scatter plots. The addition is to add the reference line and annotation.
If you have Statistics or Curve Fitting toolbox, the linear model plot() function will add a regression line to the plot automagically; otherwise use polyfit/polyval to fit and evaluate the line and text to add the annotation.

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


dpb 12 Apr 2021 18:51
편집: dpb 12 Apr 2021 19:16
OK, I played with one of the sample datasets used for illustrating some other regression is possible to start with plotmatrix and get something closely approximating corrplot without too much trouble.
% create a table and pass the numeric columns to plotmatrix for the starting point
load carbig
[hSc,hAx,hAxBig,hH]=plotmatrix(tAuto{:,5:end}); % the base plot -- 5x5 array
% the engine/modifications/additions to
arrayfun(@(h) hold(h,'on'),hAx); hold(hAxBig,'on') % set all axes to be able to add
[N,M]=size(hAx,1); % axes rows, columns
for i=1:M, xlabel(hAx(N,i),tAuto.Properties.VariableNames(i+4)),end % bottom row x labels
for i=1:N, ylabel(hAx(i,1),tAuto.Properties.VariableNames(i+4)),end % ditto left column y
rho=corr(tAuto{:,5:end},'rows','complete'); % compute the correlation coeff
rho_str=arrayfun(@(n)sprintf('%.2f',n),rho,'UniformOutput',false); % format to cellstr() rho values
pos_str={'right','left'}; % placement for rho string based on sign
fnx=@(rho) -0.45*sign(rho)+0.5; % x position for left, right
fna=@(rho) 0.5*sign(rho)+1.5; % horizontal position ditto
[N,M]=size(hAx,1); % for each axes by row, column
for i=1:N
for j=1:M
if i==j,continue,end % skip diagonals
hTxt(i,j)=text(hAx(i,j),fnx(rho(i,j)),0.95,rho_str(i,j), ... % write the rho value
'Units','normalized','FontWeight','bold', ...
'HorizontalAlignment',pos_str{fna(rho(i,j))}, ...
X=hSc(i,j).XData(:); Y=hSc(i,j).YData(:); % get the data in axes
isOK=all(isfinite([X Y]),2); % only use rows no missing data
mdl{i,j}=polyfit(X(isOK),Y(isOK),1); % fit the regression line
XL=[min(X);max(X)]; % plot line over range of X
Above resulted in
The regression lines here are the OLS best fits; the documentation for corrplot includes the following: "The slopes of the least-squares reference lines in the scatter plots are equal to the displayed correlation coefficients."
I am not sure what that means, precisely nor how such a feat was achieved--I played around with ways to try to make a regression line with such a slope but never was able to figure out any way to do so and make it actually fit the data.
This illustrates the appearance is doable without terrible lot of extra work; it does, granted, take having some comfort level with handle graphics to be ready to jump in and do such, but it's really not that complicated and that there are a zillion axes really doesn't change anything from the simple examples for annotation and enhancement given in the documentation.
I haven't taken the time to take the general idea illustrated above and packaged it as a function to be called similarly as corrplot, but wouldn't be terribly complicated to do -- just replace the references to |tAuto{:,5:end}| with the desired matrix and index into it instead.
  댓글 수: 5
Adam Danz
Adam Danz 13 Apr 2021 21:46
Thanks for bringing that up. I hadn't thought of it previously.

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

Community Treasure Hunt

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

Start Hunting!

Translated by