How do I Plot a Regression Line (not simple regression) on gscatter?

3 次查看(过去 30 天)
I have a scatter plot that I made in gscatter. I wish to add a regression line (which I already have paremeters such as slope for calculated). I can't add a simple regression line (as most existing code and examples use) as I need a Model II regression (I have code for this, to calculate slope, y-intercept, etc.), but I am unsure how to add a Model II regression line, given that I have the slope, etc., onto this gscatter plot. Does anyone have any idea as to possible solutions?
Thank you for your time.

回答(1 个)

Jeff Miller
Jeff Miller 2022-6-23
Something like this should work, after your gscatter:
lowX = 0; % set low & hi X to span the x-axis range that you want the line to cover.
hiX = 100;
predYatlowX = intercept * slope*lowX; % using the slope & intercept values from your model
predYathiX = intercept * slope*hiX;
hold on
plot([lowX, hiX],[predYatlowX, predYathiX],'-')
  9 个评论
Austin Davis
Austin Davis 2022-7-6
I tried using a different means of calculating the Model II Regression and got a slope of ~12, to the same results or lack thereof. Is there another way in terms of code that I could proceed where I may have better luck with visualization?
Jeff Miller
Jeff Miller 2022-7-7
It's a little hard to suggest anything without seeing your code or data. Here is a small code snippet illustrating one approach to the problem (as I understand it). Maybe it will help to have a little example...
% Example code illustrating one way to plot line over a
% scattergram produced by gscatter
% Make up some random-ish data for two groups
X = 2 + randn(20,1);
Y = 4*X + 5*randn(20,1);
G = repmat([1;2],10,1);
G = G(randsample(G,20)); % Randomly permute the groups to make it look more realistic
% Plot the data with gscatter
figure;
gscatter(X,Y,G)
% Estimate the slope and intercept.
% You said you did this with the command
% [m,b,r,sm,sb] = lsqfitma(X,Y)
% but I don't know this function so I just
% picked some arbitrary values.
intercept = 0;
slope = 4;
% Compute predictions from the estimated slope & intercept
lowX = 0; % set low & hi X to span the x-axis range that you want the line to cover.
hiX = 21;
predYatlowX = intercept + slope*lowX; % using the slope & intercept values from your model
predYathiX = intercept + slope*hiX;
% Add the predicted line to the graph
hold on
plot([lowX, hiX],[predYatlowX, predYathiX],'-')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by