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!
채택된 답변
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
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')

.
It worked! Thank you so much 😊
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
