Using polyfit for two y values

3 views (last 30 days)
TANMAYEE PATHRE
TANMAYEE PATHRE on 3 Jul 2019
Edited: dpb on 4 Jul 2019
Hi!
I have one x-variable which is tempo values of 28 drum stimuli (14 happy and 14 sad). I have y-values of emotional repsonses. One group is normal hearing (emot_mean_drums) and the other group is hearing impaired (NV_CI_Emotion_mean). I want to plot a regression line using polyfit. But I do not know how should I specify my y-values in the polyfit function since I have two of them. If I use y-values as emot_mean_drums, then a line is obtained along emotional repsonses of normal hearing and if y-values are of hearing impaired the line is plotted along them.
What should I do that I get best fitting line (blue green errorbars) and another best fitting line (red green errorbars)? I have attached the plot.
Also, if I want to plot the least square line (lsline), it doesnt work? Why so?
So far my code is this,
%%%%% For error bars
errorbar(tempoModedrums(1:14), emot_mean_drums(1:14), emot_std_drums(1:14)/sqrt(12),'ro','MarkerSize',10, 'LineWidth', 0.9,'MarkerFaceColor','r');
hold on
set(e41, 'Color',[0.8500 0.3250 0.0980],'MarkerFaceColor',[0.8500 0.3250 0.0980]); %Color - orange
e42 = errorbar(tempoModedrums(1:14), NV_CI_Emotion_mean(1:14), NV_CI_Emotion_SE(1:14),'o','MarkerSize',10, 'LineWidth', 0.9);
set(e42, 'Color',[0.4660 0.6740 0.1880],'MarkerFaceColor',[0.4660 0.6740 0.1880]);
errorbar(tempoModedrums(15:28), emot_mean_drums(15:28), emot_std_drums(15:28)/sqrt(12), 'b^','MarkerSize',10, 'LineWidth', 0.9,'MarkerFaceColor','b');
set(e43, 'Color',[0 0.4470 0.6510],'MarkerFaceColor',[0 0.4470 0.6510]);
e44 = errorbar(tempoModedrums(15:28), NV_CI_Emotion_mean(15:28), NV_CI_Emotion_SE(15:28), '^','MarkerSize',10, 'LineWidth', 0.9);
set(e44,'Color',[0.4660 0.6740 0.1880], 'MarkerFaceColor',[0.4660 0.6740 0.1880]);
The confusion lies in the y-values
%p51 = polyfit(tempoModedrums(1:14), emot_mean_drums(1:14),1);
%tempodrum1 = linspace(100, 250,100);
% y1 = polyval(p51,tempodrum1);
% plot(tempodrum1,y1, 'k--', 'LineWidth', 1.5)
% p52 = polyfit(tempoModedrums(15:28),emot_mean_drums(15:28),1);
% tempodrum2 = linspace(35, 150,100);
% y2 = polyval(p52,tempodrum2);
% plot(tempodrum2,y2, 'k--', 'LineWidth', 1.5)
  3 Comments
TANMAYEE PATHRE
TANMAYEE PATHRE on 3 Jul 2019
May be I should try to rephrase. Also, I wanted to ask why least square line or lsline function does not work on error bars?
dpb
dpb on 3 Jul 2019
"...why least square line or lsline function does not work on error bars?"
Because TMW didn't implement it to do so...per the documentation it works only on axes with scatter or plot and no connected lines. The code used looks specifically for a 'Type' property of 'scatter' while the type for an errorbar is 'errorbar' so it isn't found.
Seems like reasonable extension to other plot types such as errorbar, indeed.
It's simple enough to plot the fitted response as well, just evaluate the fitted equation(s) at the two end points and plot().

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 3 Jul 2019
I agree with dpb. You have two very distinct data sets so fit two lines, one to each data set.
If you're trying to fit the line(s) to perform some statistical analysis on the two data sets, that's a bit outside my area of expertise. Perhaps this page on hypothesis testing from the Statistics and Machine Learning Toolbox documentation may be of use, as may the "Related Topics" linked at the end of the page.
  1 Comment
dpb
dpb on 3 Jul 2019
Edited: dpb on 4 Jul 2019
Alternatively, one might try a model with auxiliary explanatory variable given the description of the dataset for each of the two basic datasets as there are what appear to be four separate individual response curves by classification.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!