Regression error data

3 Ansichten (letzte 30 Tage)
Suguru
Suguru am 3 Nov. 2011
Hi after plotting the regression line and inserting the error I have noticed that it doesn't seem to have an error on it i.e. I want the equation to be in:
y = ax±b + c±d
format rather than
y = ax±c
I would also like to display the rsquare value of the fit. How would I be able to do this?
Thank you in advance

Akzeptierte Antwort

Wayne King
Wayne King am 3 Nov. 2011
Let
y = a(:,2);
x1 = a(:,1);
I'm assuming the 2nd column is the dependent variable and the first column is your predictor, independent variable.
Then,
X = [ones(size(x1)) x1];
[b,bint,r,rint,stats] = regress(y,X);
rsquared = stats(1) % this is the R-squared
% plot the data and the fit
yhat = b(1)+b(2)*x1;
plot(x1,y,'b*');
plot(x1,yhat,'r-.');
  3 Kommentare
Wayne King
Wayne King am 3 Nov. 2011
The errors are the residuals. But just so you understand, the errors here are y-yhat, the actual data values- fitted value.
Suguru
Suguru am 3 Nov. 2011
Ah I see what you mean thank you problem solved now :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Wayne King
Wayne King am 3 Nov. 2011
Hi, the simple linear regression model is
y = beta0 + beta1*x+epsilon
where epsilon is the error.
But when you fit the model, you only fit the beta0 (intercept) and beta1 (slope) terms, so you fit a model of the form
yhat = \hat{beta0}+\hat{beta1}*x
I've used \hat{} to designate estimates.
You can output the rsquared value as below.
load carsmall
x1 = Weight;
y = MPG;
X = [ones(size(x1)) x1];
[b,bint,r,rint,stats] = regress(y,X);
rsquared = stats(1)
The fitted model is:
yhat = b(1)+b(2)*x1;
plot(x1,y,'b*');
plot(x1,yhat,'r-.');
The errors, residuals, are in the r variable.
  1 Kommentar
Suguru
Suguru am 3 Nov. 2011
Sorry still a bit confused on the fit so let's say I have a 10rowx2column variable a and plotted a scatter by:
scatter(a(:,1),a(:,2))
What commands would I use to plot a fit in the format that you have told me? (I've used the GUI to the the initial fit so I don't quite know the command for it... I did google but its not very clear...)

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by