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

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)

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

Unfortunately I am not able to see the plotted line. I believe it may be out of bounds but I am unsure why.
EDIT: I zoomed out using the low and high X, and I was able to see the trendline. However, it's patently incorrect. I will have to revisit.
EDIT 2: I am now no longer able to do this in order to see the plotted line. "data1" for the line also appears as white, meaning on multiple levels I am unable to see anything. I think I may have better luck with a slightly different Model II regression code but I am unable to ascertain it because "data1" is white.
Here is the full code I am using.
x = Ratio.VariableA;
y = Ratio.VariableB;
g = {Ratio.Category, Ratio.Temperature};
colororder([1 1 1; 0 1 1]);
gscatter(x, y, g, 'grcm', 'o^s+d', 8, 'on', 'VariableA', 'VariableB')
legend('Location','northeastoutside')
lowX = 0; % set low & hi X to span the x-axis range that you want the line to cover.
hiX = 0.5;
predYatlowX = b * m*lowX; % using the slope & intercept values from your model
predYathiX = b * m*hiX;
hold on
plot([lowX, hiX],[predYatlowX, predYathiX],'-')
Sorry for the typos. These should have been
predYatlowX = b + m*lowX; % using the slope & intercept values from your model
predYathiX = b + m*hiX;
Thank you.
I think my problem is multifaceted and I am unsure how to rectify it. Either
  1. the regression line does not appear ("data1" is simply blank ), or
  2. if I fiddle around with it enough, it does appear, but the regression line is negative. This, I don't think is a function of the code you've so graciously provided and is either an issue with my dataset (which doesn't make sense) or the code used to calculate the regression line.
[m,b,r,sm,sb]=lsqfitma(Ratio.VariableA,Ratio.VariableB)
This is the code that I am using to create the Model II regression line, which I then plugged in to the code that you gave me. For whatever reason, the slope of the regression line generated is negative, although as one can see from the actual data that can't possibly be the case.
Strange. Can you attach a file with the values of VariableA and B, and say what values you are getting for m and b?
The exact input data is somewhat sensitive; let me look at the data again and obfusicate it somewhat without radically changing anything.
The slope that I currently have is 15.6944, while the intercept is -.5587.
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?
After many attempts of attempting to obfuscate the data with a similar slope I am simply not able to. It's completely different from the 15.6944 slope for some reason, which makes even less sense. I am unsure if sending you this mock data set would accomplish anything (in this case, I do get a blue line plotted for "data1 (the slope)," which is nearly horizontal, an issue but unrelated to the one I initially had and have with my genuine dataset).
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?
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],'-')

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 22 Jun 2022

Commented:

on 7 Jul 2022

Community Treasure Hunt

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

Start Hunting!