Regression error data

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

 採用された回答

Wayne King
Wayne King 2011 年 11 月 3 日

0 投票

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 件のコメント

Suguru
Suguru 2011 年 11 月 3 日
Hi thanks for your help! :)
Managed to plot the line and get the regression coefficient.
One last thing how do I output the equation of the line with errors?
Many thanks
Wayne King
Wayne King 2011 年 11 月 3 日
The errors are the residuals. But just so you understand, the errors here are y-yhat, the actual data values- fitted value.
Suguru
Suguru 2011 年 11 月 3 日
Ah I see what you mean thank you problem solved now :)

サインインしてコメントする。

その他の回答 (1 件)

Wayne King
Wayne King 2011 年 11 月 3 日

0 投票

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 件のコメント

Suguru
Suguru 2011 年 11 月 3 日
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...)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by