Error using corrplot function

12 views (last 30 days)
Quincy Brouwer
Quincy Brouwer on 11 Apr 2021
Commented: Adam Danz on 13 Apr 2021
Hi,
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:
corrplot(KNMIGlobalestraling2018,DataHourlyTable.DataTableCorrect2)
How can I fix this?
  3 Comments
Quincy Brouwer
Quincy Brouwer on 11 Apr 2021
@dpb Thanks for your answer. Do you know another way to plot the correlation?
dpb
dpb on 11 Apr 2021
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.

Sign in to comment.

Answers (1)

dpb
dpb on 12 Apr 2021
Edited: dpb on 12 Apr 2021
OK, I played with one of the sample datasets used for illustrating some other regression stuff...it 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
tAuto=table(Mfg,Model,Model_Year,Origin,Weight,Horsepower,Displacement,Cylinders,Acceleration);
tAuto.Mfg=categorical(string(tAuto.Mfg));
tAuto.Model=categorical(string(tAuto.Model));
[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))}, ...
'VerticalAlignment','top');
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
hRL(i,j)=plot(hAx(i,j),XL,polyval(mdl{i,j},XL),'r-');
end
end
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 Comments
Adam Danz
Adam Danz on 13 Apr 2021
Edited: Adam Danz on 13 Apr 2021
Actually, the slope of lsline is the zscore'd pairwise pearson correlation.
Replace
rho=corr(tAuto{:,5:end},'rows','complete');
with
rho = corr(tAuto{:,5:end},'type','pearson','rows','pairwise','tail','both');
Demo:
load carbig
tAuto=table(Mfg,Model,Model_Year,Origin,Weight,Horsepower,Displacement,Cylinders,Acceleration);
rho = corr([tAuto.Weight, tAuto.Horsepower],'type','pearson','rows','pairwise','tail','both')
rho = 2×2
1.0000 0.8666 0.8666 1.0000
n = isnan(tAuto.Weight) | isnan(tAuto.Horsepower);
polyfit(zscore(tAuto.Weight(~n)),zscore(tAuto.Horsepower(~n)),1)
ans = 1×2
0.8666 0.0000
Also, this line appears twice in your answer and throws an error since you're specifying a dimention input.
[N,M]=size(hAx,1);
Adam Danz
Adam Danz on 13 Apr 2021
Thanks for bringing that up. I hadn't thought of it previously.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!