Using polyfit for two y values
Mostrar comentarios más antiguos
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 comentarios
dpb
el 3 de Jul. de 2019
Not clear what your purpose is...fitting the two observations together will produce one line that is the best fit of all together thus fitting neither class particularly well. There's pretty obvious difference in both slope and magnitude of the upper two; the lower two are more nearly same slope but definitely offset from each other.
What's the purpose of the fitting is the first thing to define as to what make sense (if anything--sometimes just because one can do something doesn't mean really should).
TANMAYEE PATHRE
el 3 de Jul. de 2019
dpb
el 3 de Jul. de 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().
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Errorbars en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!