How can i include the fitted model and goodness of fit into my regression analysis plot

43 次查看(过去 30 天)
Hello everyone
i have some x,y data. i have used the "Curve Fitting" app in matlab to carry out a simple linear regression analysis. it worked wonderfully.
i have quite a few data sets i would like to fit so i asked Matlab to generate Matlab code. But the matlab code that was generated only give the plot of the regression and the plot of the residuals. The model (ax+b) aswell as R^2 and adjusted R^2 is not shown anywhere. the code is shown below
%%Regression analysis
[xData, yData] = prepareCurveData(A(1,start:end),M(1,start:end));
% Set up fittype and options.
ft = fittype( 'poly1' ); % Linear polynomial curve, = ax+b
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf];
opts.Upper = [Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Create a figure for the plots.
figure( 'Name', 'Linear Regression' );
title('Linear Regression')
% Plot fit with data.
subplot( 2, 1, 1 );
h = plot( fitresult, xData, yData );
legend( h, 'Data', 'Linear Regression', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x' );
ylabel( 'y' );
grid on
% Plot residuals.
subplot( 2, 1, 2 );
h = plot( fitresult, xData, yData, 'residuals' );
legend( h, 'Linear Regression - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x' );
ylabel( 'Residuals' );
grid on
Both the model and the goodness of fit is saved as variables, "fitresult" and "gof" respectivly. but i do not know how to display them together/inside the figure. i can of course make the figure and then afterwards use e.g. disp(gof) to retrieve the goodness of fit. but then i have to put them together in another program before i can actually use the figure
i have the statistics toolbox installed in case that is relevant

采纳的回答

Tom Lane
Tom Lane 2013-1-24
Try something like this:
load census
[fitresult,gof] = fit(cdate,pop,'poly2')
plot(fitresult,cdate,pop)
text(1800,100,sprintf('Rsq = %g\nAdj = %g',gof.rsquare,gof.adjrsquare))
  3 个评论
Martin Nors Pedersen
DOH! mixed up the y -and x-axis. using:
text(5760, 0.0202,...
solved the problem. and you are not dividing by "nAdj" - i'm not quite sure what the code does byt the printed values are correct :)
thank you very much
Tom Lane
Tom Lane 2013-1-29
The "\n" part of the string just specifies that the following text should be on a new line. If you have other questions, let me know.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by