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

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

 Respuesta aceptada

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 comentarios

Thank you for you reply
I can get you script to work in the sence that when i copy your code into my script i get a nice exponential fit the population increase in the US ("help census" at least suggests this is what you fit to)
but when i try incorporate your solution to my data i cannot get it to work - obviously because i'm doing something wrong. we both use the same method for fitting as i see it
[fitresult, gof] = fit( xData, yData, ft, opts ); % my "fitresult", ft = 'poly1'
[fitresult,gof] = fit(cdate,pop,'poly2') % your "fitresult"
So in both cases there should be a gof-variable. so using text(x,y,'string') the way you suggest should work for me when it worked for your example?. i have shown the modified text line below
text(0.0202,5760,sprintf('Rsq = %g\nAdj = %g',gof.rsquare,gof.adjrsquare)) % x and y cordinates changed to fit my axises
gof.rsquare and gof.adjrsquare both exist and give the right values when print them. converting the the number into a string should also not cause a problem - although i am unsure why you divide the coefficients byt something called nAdj - can you elaborate on that?
for good measure i will post the modefied code for supblot 1 below - nothing has changed in the other parts from the original code shown above
% Plot fit with data.
subplot( 2, 1, 1 );
h = plot( fitresult, xData, yData );
legend( h, 'Data', 'Linear Regression', 'Location', 'NorthEast' );
text(0.0202,5760,sprintf('Rsq = %g\nAdj = %g',gof.rsquare,gof.adjrsquare))
% Label axes
xlabel( 'x' );
ylabel( 'y' );
grid on
any tips on how to get to work would be greatly appreciated :)
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
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by