How to fit polyfit coefficients to a linear model

11 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 4 Sep 2020
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 Comments
Connor Cheema
Connor Cheema on 5 Sep 2020
Thanks man
I put the data in there. Gonna try my hand at the question now.
Star Strider
Star Strider on 5 Sep 2020
As always, my pleasure!
Jeff and I both thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!