Trend line polynomial

stellanhakansson

New Member
Joined
Dec 30, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am trying to reproduce y-values generated by the equation for a polynomial trendline with x-values 1-42. Y-values seem to be correct up to x=14 but as x increases beyond this value, y-values become increasingly incorrect.
I have checked my equation in detail but cannot see that it differs from that given by Excel. Something I missed? Greatful for advice.
BR, Stellan Håkansson
Observed vs estimated frequency.jpg
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I'd play around with it, but to do that I'd have to manually type in the equation from your graphic, and life is too short for that. You do have a minor error in the last term (51.862 vs. 51.682). You could also construct the equation in VBA as a UDF and use it for comparison. That said, your correlation coeff (R2) is only ~0.7 which is not a great fit, suggesting that you try other curve fits.
 
Upvote 0
You may also consider getting the coefficients from Excel rather than typing them based on the trendline equation. The displayed equation will only show a level of precision as set in the options; but the calculated coefficients will be the full precision. Using Office 365, I use the formula shown below to calculate and apply the coefficients for a polynomial of a given order. I have replaced range references with pseudocode names. You should be able to use a range for X_Input and calculate the entire set of resulting Y values (it will spill as needed).

edit:
apparently formatting between code tags is a bad idea

=MMULT(X_Input^TRANSPOSE((order+1)-SEQUENCE(order+1))*LINEST(Y_Data,X_Data^TRANSPOSE(SEQUENCE(order))),SEQUENCE(order+1)/SEQUENCE(order+1))

Hope that helps.

Regards,
Ken
 
Last edited:
Upvote 0
@KenU is spot on...this is a matter of coefficient precision. If you determine the coefficients to greater precision, this issue will disappear. The clue is that at higher x values, the x^5 and x^6 terms become very large, and your coefficients on those terms are very small, so any loss in coefficient precision means that your estimated values will differ substantially. Here is an example with the regression extracting higher precision (reg2...the red points, and coefficients shown above table). Generally, I would critically examine whether polynomial regression is appropriate. In some cases it is, but in other cases, it may not be.
Book1
ABCD
1x65.00E-074.77E-07
2x5-7.00E-05-7.25E-05
3x44.30E-034.31E-03
4x3-1.25E-01-1.25E-01
5x21.80E+001.80E+00
6x1-1.20E+01-1.20E+01
7x05.19E+015.17E+01
8
9
10DayObserved (%)reg1reg2
11144.4441.4965341.31322
12220.0034.0437933.86104
13337.5028.8983528.71596
14437.9325.5419725.35902
15515.1523.5360623.35006
16625.0022.5146122.32028
17714.7122.1773321.96507
18830.7722.2833122.03718
19913.2122.6448922.34029
201017.6523.12222.72283
211122.4523.6168123.07227
221226.0324.0687523.30961
231326.9224.4498923.38435
241424.1424.7606923.26968
251525.4025.0260622.95815
261625.3725.2918922.45751
271722.0825.6217921.78708
281821.3326.0943520.97431
291920.8826.8006120.05181
302022.5027.84219.05458
312111.1129.3285918.01775
322211.1131.3777116.97449
332311.6934.1129315.95445
342416.8437.6634114.98237
352516.9042.1635614.07711
362611.9747.7531713.25109
372718.0054.5777512.50993
382811.1162.7893911.85254
392910.0072.5478311.27154
403013.4684.02210.75397
413113.1697.3918710.28241
42326.30112.85079.836392
433310.53130.60759.394194
44346.20150.89018.93495
45359.70173.94868.441114
46365.26200.05847.901272
47379.35229.52527.31329
48386.14262.68846.687809
49396.47299.92666.052084
50403.54341.6625.454161
51418.46388.36674.967409
52423.15440.56764.695382
Sheet2
Cell Formulas
RangeFormula
C11:D52C11=SUMPRODUCT($A11^{6;5;4;3;2;1;0},C$1:C$7)

1655480339535.png
 
Upvote 0
Solution
VBA Code:
You may also consider getting the coefficients from Excel rather than typing them based on the trendline equation. The displayed equation will only show a level of precision as set in the options; but the calculated coefficients will be the full precision. Using Office 365, I use the formula shown below to calculate and apply the coefficients for a polynomial of a given order. I have replaced range references with pseudocode names. You should be able to use a range for X_Input and calculate the entire set of resulting Y values (it will spill as needed).

edit:
apparently formatting between code tags is a bad idea

=MMULT(X_Input^TRANSPOSE((order+1)-SEQUENCE(order+1))*LINEST(Y_Data,X_Data^TRANSPOSE(SEQUENCE(order))),SEQUENCE(order+1)/SEQUENCE(order+1))

Hope that helps.

Regards,
Ken
Thanks, much appreciated. Best, /Stellan
 
Upvote 0
@KenU is spot on...this is a matter of coefficient precision. If you determine the coefficients to greater precision, this issue will disappear. The clue is that at higher x values, the x^5 and x^6 terms become very large, and your coefficients on those terms are very small, so any loss in coefficient precision means that your estimated values will differ substantially. Here is an example with the regression extracting higher precision (reg2...the red points, and coefficients shown above table). Generally, I would critically examine whether polynomial regression is appropriate. In some cases it is, but in other cases, it may not be.
Book1
ABCD
1x65.00E-074.77E-07
2x5-7.00E-05-7.25E-05
3x44.30E-034.31E-03
4x3-1.25E-01-1.25E-01
5x21.80E+001.80E+00
6x1-1.20E+01-1.20E+01
7x05.19E+015.17E+01
8
9
10DayObserved (%)reg1reg2
11144.4441.4965341.31322
12220.0034.0437933.86104
13337.5028.8983528.71596
14437.9325.5419725.35902
15515.1523.5360623.35006
16625.0022.5146122.32028
17714.7122.1773321.96507
18830.7722.2833122.03718
19913.2122.6448922.34029
201017.6523.12222.72283
211122.4523.6168123.07227
221226.0324.0687523.30961
231326.9224.4498923.38435
241424.1424.7606923.26968
251525.4025.0260622.95815
261625.3725.2918922.45751
271722.0825.6217921.78708
281821.3326.0943520.97431
291920.8826.8006120.05181
302022.5027.84219.05458
312111.1129.3285918.01775
322211.1131.3777116.97449
332311.6934.1129315.95445
342416.8437.6634114.98237
352516.9042.1635614.07711
362611.9747.7531713.25109
372718.0054.5777512.50993
382811.1162.7893911.85254
392910.0072.5478311.27154
403013.4684.02210.75397
413113.1697.3918710.28241
42326.30112.85079.836392
433310.53130.60759.394194
44346.20150.89018.93495
45359.70173.94868.441114
46365.26200.05847.901272
47379.35229.52527.31329
48386.14262.68846.687809
49396.47299.92666.052084
50403.54341.6625.454161
51418.46388.36674.967409
52423.15440.56764.695382
Sheet2
Cell Formulas
RangeFormula
C11:D52C11=SUMPRODUCT($A11^{6;5;4;3;2;1;0},C$1:C$7)

View attachment 67306
Many hanks. Problem solved. Learned a lot! Much grateful.
All the best, Stellan
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top