Correlation regression lines between two parameters

Hello dear Researchers,
I have a query need your expertise to resolve.
I want to add correlation regression for two paramters for comparison purpose.
I have attached sub-plots which are scatter plots among two orientations.
Now purpose is to add correlation regression line i.e., one plot I added from a reference.

 採用された回答

yanqi liu
yanqi liu 2021 年 11 月 9 日
編集済み: yanqi liu 2021 年 11 月 9 日

0 投票

sir, may be upload some data, use lsqcurvefit、polyfit and so on to compute the coef
please check the follow code to get some information
clc; clear all; close all;
xdata = linspace(0,3);
ydata = 1.3*xdata + 0.05*randn(size(xdata));
lb = [];
ub = [];
fun = @(x,xdata)x(1)*xdata+x(2);
x0 = [0,0];
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 1×2
1.3048 -0.0023
plot(xdata,ydata,'ko',xdata,fun(x,xdata),'r-','LineWidth',2)
legend('Data','Fitted exponential')
title('Data and Fitted Curve')

10 件のコメント

yanqi liu
yanqi liu 2021 年 11 月 9 日
yes,sir,may be use the color list
clc; clear all; close all;
xdata = linspace(0,3,2e2);
ydata = 1.3*xdata + 0.05*randn(size(xdata));
lb = [];
ub = [];
fun = @(x,xdata)x(1)*xdata+x(2);
x0 = [0,0];
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 1×2
1.2999 -0.0017
%scatter(xdata,ydata,10,randn(size(xdata)));
scatter(xdata,ydata,10,ydata);
hold on
plot(xdata,fun(x,xdata),'r-','LineWidth',2)
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
yanqi liu
yanqi liu 2021 年 11 月 9 日
yes,sir,you are welcome,加油!
Adam Danz
Adam Danz 2021 年 11 月 9 日
編集済み: Adam Danz 2021 年 11 月 9 日
Using lsqcurvefit requires setting an initial guess which is not necessary with linear fits. This function is better for nonlinear data.
Amjad Iqbal
Amjad Iqbal 2021 年 11 月 9 日
編集済み: Amjad Iqbal 2021 年 11 月 9 日
Point to be noted it seems non linear curve fitting. I am working with your methods, till I succeed with any outcomes. Yeah, you are right as curve line appears isolatedly
Adam Danz
Adam Danz 2021 年 11 月 9 日
The objective function in yanqi Liu's answer is linear. Your scatter plots show a linear relationship. And the regression line you plotted his linear. I don't see anything that's nonlinear.
Amjad Iqbal
Amjad Iqbal 2021 年 11 月 9 日
Like in the reference plot in my question, regression line is not isolated from scatter. In this case, line is seems isolated. Although relationship is linear.
Adam Danz
Adam Danz 2021 年 11 月 9 日
What does it mean for a line to be isolated? Are you describing the fit of the line?
Adam Danz
Adam Danz 2021 年 11 月 9 日
Are you describing the color of the line? Line properties can be changed in any method you use.
In the first image of your previous comment, the legend was moved and now I see some points in the lower left corner where the legend was that do not follow the linear trend (around (3,1)). But they don't appear to be affecting the linear fit too much if the blue line is the linear fit.
Amjad Iqbal
Amjad Iqbal 2021 年 11 月 9 日
I understand the fact linear fit. It seems in a good agreement by using both methods recommended by you and yanqi liu. Many many thanks once agian all, also @Image Analyst for your expertised suggestions. It really helps me. Final result is now up to the mark.
Adam Danz
Adam Danz 2021 年 11 月 9 日
Yes, lsqcurvefit will provide the same results as polyfit or fitlm but the latter two are designed for linear models and do not require making initial guesses to the parameter values. I'm not trying to convince anyone to change their approach (or their selected answer). I'm arguing that lsqcurvefit is not the best tool for linear regression.
polyfit is much more efficient than lsqcurvefit (95x faster):
x = rand(1,100);
y = 4.8*x+2.1 +rand(1,100);
n = 5000;
tic
for i = 1:n
opts = struct('Display','off');
fun = @(x,xdata)x(1)*xdata+x(2);
p = lsqcurvefit(fun, [0,0],x,y,[],[],opts);
end
T1 = toc % time in seconds
T1 = 13.2634
p
p = 1×2
4.9170 2.4408
tic
for i = 1:n
p2 = polyfit(x,y,1);
end
T2 = toc % time in seconds
T2 = 0.1386
p2 % same results
p2 = 1×2
4.9170 2.4408
% Difference in time
T1/T2
ans = 95.7249

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2021 年 11 月 8 日
編集済み: Adam Danz 2021 年 11 月 8 日

1 投票

Common methods of adding a simple linear regression line
1. Use lsline which will add a regression line for each set of data in the plot.
2. Use polyfit (degree 1) & refline to compute the regression coefficients and plot the line.
3. Use fitlm & refline to compute the regression coefficients and plot the line.
Examples of #1 & #2
Example of #3
If you want to compute correlation, see corr or corrcoeff.

2 件のコメント

Image Analyst
Image Analyst 2021 年 11 月 9 日
Just to build on Adam's #3, this is how you'd do it:
% First get your model for fitted y values.
% Determine and say how well we did with our predictions, numerically, using several metrics like RMSE and MAE.
% Fit a linear model between predicted and true so we can get the R squared.
mdl = fitlm(actualy, fittedy)
rSquared = mdl.Rsquared.Ordinary;
rmse = mdl.RMSE;
mdlMSE = mdl.MSE;
residuals = abs(y - x);
% Find the mean and median absolute deviations of the elements in X.
meandev = mad(residuals,0,'all');
mediandev = mad(residuals,1,'all');
aveResidual = mean(residuals, 'omitnan');
maxResidual = max(residuals);
fprintf('The RMSE value is %.5f.\n', rmse);
fprintf('The R^2 value is %.5f.\n', rSquared);
fprintf('The MSE value is %.5f.\n', mdlMSE);
fprintf('The fitted y is different from the actual y by an average of %.2f units.\n', aveResidual);
fprintf('The fitted y is different from the actual y by an average of %.2f units.\n', meandev);
fprintf('The fitted y is different from the actual y by a median of %.2f units.\n', mediandev);
fprintf('The max residual from the average human grade is %.2f PSU.\n', maxResidual);
Amjad Iqbal
Amjad Iqbal 2021 年 11 月 9 日
Thanks a lot dear Adam Danz and Image Analyst
That's seems a proper way to add several numerical comparisons. It helps a lot.

サインインしてコメントする。

製品

リリース

R2015a

質問済み:

2021 年 11 月 8 日

コメント済み:

2021 年 11 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by