Adding vertical trendline for mutiple y variables in one scatterplot

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!

 Accepted Answer

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 Comments

I'm getting an error on this section:
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
  • it says "Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:)"
This is what i set my x and y as:
x = data(:,1)
y = data(:,2:9)
That is certainly true for table arrays.
There are two options, the first is to use the table2array function to get ‘data’ as an array rather than a table. You can then use the addressing in the code you quoted.
The second is to change the addressing to use curly brackets {} instead, for example:
x = data{:,1}
y = data{:,2:9}
since that will also extract the data from the table, creating arrays.
It would likely be easier to use the second (curly brackets) option.
NOTE — In my original code, both ‘x’ and ‘y’ are matrices, so reshaping them into vectors produces vectors of equal lengths. Your ‘x’ is a vector (with I assume the same number of rows as ‘y’), so you will need to do:
x = repmat(x, size(y,2), 1)
This will make their row lengths equal, and the rest of my code should run without error.
.
Hi,
This almost worked but I think the statement "x = repmat(x, size(y,2), 1)" made the x rows increase much more than my y rows.
x in workspace is now 2464x1 double
y in workspace is 308x8 double
Both should be 308 rows long. Do you know how to fix this? I think the statement made my x rows increase, not my x columns.
That should work, since my ‘Line’ variable reshapes ‘y’ to create it as a column vector that will be the same size as ‘x’. It will work with the ‘x’ I calculated earlier using repmat.
Try it!
Hi,
It gives me an error that says :
Error using scatter
X and Y must be vectors of the same length, matrices of the same size, or a combination of a vector and a matrix
where the length of the vector matches either the number of rows or columns of the matrix.
Please help!!
I do not understand the problem. My code should work if your data matches my assumptions of it.
I need your data and your code to see what the problem is.
My data is a excel file with x variable seconds, y variables mmHg values for different trials. All trials go up to 301 seconds, and I have 8 trials.
My code is below. Thanks for your help!
x = data{:,1}
y = data{:,2:9}
x = repmat(x, size(y,2), 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')
My pleasure!
I now see the problem in adapting my code to your data.
Try this —
data = array2table(randn(308,9).*(1:9)+(0:8)*10); % Create 'data'
xv = data{:,1}; % Change This To 'xv'
y = data{:,2:9};
x = repmat(xv, size(y,2), 1); % Change Reference To 'xv' Here
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
figure
hs = scatter(xv, y, 'filled', 'DisplayName','Data');
hold on
hp = plot(x(:), Line, '-k', 'LineWidth',2, 'DisplayName','Linear Regression');
hold off
grid
xlabel('X')
ylabel('Y')
legend([hs hp], 'Location','best')
.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!