How do I Plot a Regression Line (not simple regression) on gscatter?
Show older comments
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.
Answers (1)
Jeff Miller
on 23 Jun 2022
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 Comments
Austin Davis
on 27 Jun 2022
Edited: Austin Davis
on 28 Jun 2022
Jeff Miller
on 27 Jun 2022
Sorry for the typos. These should have been
predYatlowX = b + m*lowX; % using the slope & intercept values from your model
predYathiX = b + m*hiX;
Austin Davis
on 28 Jun 2022
Jeff Miller
on 28 Jun 2022
Strange. Can you attach a file with the values of VariableA and B, and say what values you are getting for m and b?
Austin Davis
on 28 Jun 2022
Jeff Miller
on 28 Jun 2022
Any rough approximation or small subset of the data would be adequate to have a look, but it would be useful to have the new slope & intercept for the revised (posted) dataset.
With a slope of 15.6944, it is hard to see how you could be getting a line with negative slope. You aren't perhaps reversing the slope and intercept when computing the predictions, by any chance?
Austin Davis
on 1 Jul 2022
Austin Davis
on 6 Jul 2022
Jeff Miller
on 7 Jul 2022
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],'-')
Categories
Find more on MATLAB 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!