MATLAB Answers

linear regression

83 views (last 30 days)
i have a table of data
X = 1,2,3,4,5,6,7,8,9
Y= 4 5 6 9 8 7 4 1 2
how can i plot these points without having a line then using linear regression to find the uncertainty with a 95% confidence interval and plot that?
  1 Comment
emmanuel zambrano
emmanuel zambrano on 21 Jul 2021
Can you help me with Linear regression approximation of errors. Please I need a code now

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 May 2012
% Define sample data.
X = [1,2,3,4,5,6,7,8,9];
Y = [4 5 6 9 8 7 4 1 2];
% Do the plotting:
plot(X, Y, 'bo', 'LineWidth', 3, 'MarkerSize', 15);
grid on;
% Maximize the figure window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Label the various parts of the plot.
fontSize = 20;
title('lowcalorie Plot', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
coefficients = polyfit(X, Y, 1);
fittedY = polyval(coefficients, X);
hold on;
% Do the plotting:
plot(X, fittedY, 'rs-', 'LineWidth', 3, 'MarkerSize', 15);
legend('Y', 'Fitted Y');
  1 Comment
emmanuel zambrano
emmanuel zambrano on 21 Jul 2021
Can you help me with Linear regression approximation of errors. Please I need a code now

Sign in to comment.

More Answers (5)

Richard Willey
Richard Willey on 14 May 2012
Alternatively, if you have the 12a version of Statistics Tbx
X = [1,2,3,4,5,6,7,8,9];
Y = [4 5 6 9 8 7 4 1 2];
myFit = LinearModel.fit(X,Y);
plot(myFit)
  1 Comment
emmanuel zambrano
emmanuel zambrano on 21 Jul 2021
Can you help me with Linear regression approximation of errors. Please I need a code now

Sign in to comment.


Nadeem Ali
Nadeem Ali on 4 Apr 2021
%% datasets
x=[3,4,6,4,2,5]';
y=[6,8,9,5,4.5,9.5]';

Nadeem Ali
Nadeem Ali on 4 Apr 2021
% Define sample data.
X = [1,2,3,4,5,6,7,8,9];
Y = [4 5 6 9 8 7 4 1 2];
% Do the plotting:
plot(X, Y, 'bo', 'LineWidth', 3, 'MarkerSize', 15);
grid on;
% Maximize the figure window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Label the various parts of the plot.
fontSize = 20;
title('lowcalorie Plot', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
coefficients = polyfit(X, Y, 1);
fittedY = polyval(coefficients, X);
hold on;
% Do the plotting:
plot(X, fittedY, 'rs-', 'LineWidth', 3, 'MarkerSize', 15);
legend('Y', 'Fitted Y');
  3 Comments
Image Analyst
Image Analyst on 21 Jul 2021
@emmanuel zambranoThe errors on a linear regression will be normally distributed because that's how the regression was determined - that's the assumption that was made. So you cannot approximate the errors with any independent variable, other than perhaps y=0. Just plot the errors for all the data points and you'll see a random pattern along the x-axis.

Sign in to comment.


emmanuel zambrano
emmanuel zambrano on 21 Jul 2021
How do I do then?

emmanuel zambrano
emmanuel zambrano on 21 Jul 2021
  2 Comments
Image Analyst
Image Analyst on 22 Jul 2021
@emmanuel zambrano, this is not an answer to @lowcalorie's original question. I suggest you start your own question, this time make it explained much much better, rather than continue the discussion here in @lowcalorie's thread. But read this first:

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!