Regression error data

조회 수: 13 (최근 30일)
Suguru
Suguru 2011년 11월 3일
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일
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
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일
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...)

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by