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

8 visualizaciones (últimos 30 días)
Austin Davis
Austin Davis el 22 de Jun. de 2022
Comentada: Jeff Miller el 7 de Jul. de 2022
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.

Respuestas (1)

Jeff Miller
Jeff Miller el 23 de Jun. de 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 comentarios
Austin Davis
Austin Davis el 6 de Jul. de 2022
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 el 7 de Jul. de 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],'-')

Iniciar sesión para comentar.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by