How to fit polyfit coefficients to a linear model

Hi, I've used polyfit to fit a polynomial to a set of data.
I'm trying to create a linear regression model
I have used fitlm() to create models that are straight lines, and this works very well, but I don't know how to fit a polynomial style regression to data.
Can anyone guide me?
Is there a way to force MATLAB to create a linear model using fitlm() with the polynomial I found using polyfit?
The main is I want the estimates, SE's, t-stats and Rsquared etc from fitlm()
I've attached an image to show what I've done.
Thanks

 채택된 답변

Star Strider
Star Strider 2020년 9월 4일
Try this:
x = 1950:2020; % Create Data
y = 0.07*x.^2 -300*x + 304130 + randn(size(x))*100; % Create Data
mdl = fitlm(x(:), y(:), 'purequadratic'); % Choose Model Specification
[ypred,yci] = predict(mdl, x(:));
figure
plot(x, y, 'p')
hold on
plot(x, ypred, '-r', x, yci, '--r')
hold off
grid
The data I created from your polyfit coefficients do not look like the data in the plot, in spite of fitlm reproducing them reasonably well. Obviously, use your own data (that I do not have access to).

댓글 수: 5

Hi Star Strider
I actually figured out how to get fitlm() to predict my model for me and create the coefficients itself. Didn't need to use polyfit in the end. Thanks for your answer however.
If you could, I have another question which I still need help with if you don't mind having a go at it.
As always, my pleasure!
Jeff Miller appears to have posted an Answer to your other Question that I likely cannot improve upon. (It would help for you to copy your data and paste it to your original post or to a Comment. A picture may be worth a thousand words, however the actual data is worth a thousand keystrokes attempting to reproduce it!)
Thanks man
I put the data in there. Gonna try my hand at the question now.
As always, my pleasure!
Jeff and I both thank you!

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

추가 답변 (0개)

카테고리

제품

질문:

2020년 9월 4일

댓글:

2020년 9월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by