Adding vertical trendline for mutiple y variables in one scatterplot

5 次查看(过去 30 天)
Hi,
I have a scatterplot with mutiple y variables from different trials. I want to make a trendline for all the trials together, not separate trendlines for each trial. I was reccomended to "draw" a vertical line on the x axis, then all of the trial points that touch that line should be averaged and a single point drawn on that x plane for the accumulative trendline. Then, to make the trendline, I would have to connect all these new dots together from each point of the x plane. Does this make sense? I'm not sure how to do it and would appreciate help!
Thanks!

采纳的回答

Star Strider
Star Strider 2023-7-3
One option is to simply reshape all of the different ‘x’ and ‘y’ data vectors (combined into matrices here for convenience) into a single column using either reshape or the ‘(:)’ operator, and then perform the regression —
x = sort(randn(20, 5));
y = 5*(1:size(x,2)) + randn(size(x)) + 0.5*(1:size(x,1)).';
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
figure
hs = scatter(x, y, 'filled', 'DisplayName','Data');
hold on
hp = plot(x(:), Line, 'DisplayName','Linear Regression');
hold off
grid
xlabel('X')
ylabel('Y')
legend([hs hp], 'Location','best')
That is how I would approach it, anyway.
.
  10 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by