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

8 ビュー (過去 30 日間)
Austin Davis
Austin Davis 2022 年 6 月 22 日
コメント済み: Jeff Miller 2022 年 7 月 7 日
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 Center および File ExchangeLegend についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by