Skip to main content
Update more accurately explains the cause of the loss of precision.
Source Link
JimB
  • 42.9k
  • 3
  • 51
  • 108

UPDATE

A more accurate explanation than the culprit being "low variability" is that because all of the dependent variable values begin with "-0.907" or "-0.908" essentially "eats up"/"wastes" the first 3 significant digits. Simply subtracting the minimimum value of the dependent variable works even better than standardizing by the mean and standard deviation:

`(* Subtract minimum value from the dependent variable *)
tableofvalues[[All, 5]] = tableofvalues[[All, 5]] - Min[tableofvalues[[All, 5]]];`

The resulting values of the two coefficients in question become

0.0619247172727312
0.0619247172719015

This is a very common issue that can produce odd results for nearly all statistical packages. Looking intensely at the data and using some form of standardization is always recommended.

End of UPDATE

The main culprit is the low variability of the dependent variable: tableofvalues[[All,5]]. If you standardize that variable (subtracting the mean and then dividing by the standard deviation), then all your troubles go away. In fact standardizing variables is almost always recommended even for the independent variables whenever there might be round-off error involved especially for the fitting of polynomials. However, the suggestion by @MarcoB (setting a higher precision) achieves essentially the same result.

stdvalues = tableofvalues;
stdvalues[[All, 5]] = Standardize[tableofvalues[[All, 5]]];
stdev5 = StandardDeviation[tableofvalues[[All, 5]]];

polynomtabelle = {1, x[1], x[1]^2, x[1]^3, x[1]^4, x[1]^5, x[2], 
  x[2]^2, x[2]^3, x[2]^4, x[2]^5, x[3], x[3]^2, x[3]^3, x[3]^4, 
  x[3]^5, x[4], x[4]^2, x[4]^3, x[4]^4, x[4]^5}

polynom = LinearModelFit[stdvalues, polynomtabelle, Array[x, {4}]];

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {5, 0, 0, 0} + 
    1], Floor@$MachinePrecision]

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {0, 0, 0, 5} + 
    1], Floor@$MachinePrecision]

with output

(* 0.0619247172791471 *)
(* 0.0619247172736994 *)

Note that one must multiply by the standard deviation of tableofvalues[[All,5]] to get the appropriate coefficient.

The main culprit is the low variability of the dependent variable: tableofvalues[[All,5]]. If you standardize that variable (subtracting the mean and then dividing by the standard deviation), then all your troubles go away. In fact standardizing variables is almost always recommended even for the independent variables whenever there might be round-off error involved especially for the fitting of polynomials. However, the suggestion by @MarcoB (setting a higher precision) achieves essentially the same result.

stdvalues = tableofvalues;
stdvalues[[All, 5]] = Standardize[tableofvalues[[All, 5]]];
stdev5 = StandardDeviation[tableofvalues[[All, 5]]];

polynomtabelle = {1, x[1], x[1]^2, x[1]^3, x[1]^4, x[1]^5, x[2], 
  x[2]^2, x[2]^3, x[2]^4, x[2]^5, x[3], x[3]^2, x[3]^3, x[3]^4, 
  x[3]^5, x[4], x[4]^2, x[4]^3, x[4]^4, x[4]^5}

polynom = LinearModelFit[stdvalues, polynomtabelle, Array[x, {4}]];

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {5, 0, 0, 0} + 
    1], Floor@$MachinePrecision]

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {0, 0, 0, 5} + 
    1], Floor@$MachinePrecision]

with output

(* 0.0619247172791471 *)
(* 0.0619247172736994 *)

Note that one must multiply by the standard deviation of tableofvalues[[All,5]] to get the appropriate coefficient.

UPDATE

A more accurate explanation than the culprit being "low variability" is that because all of the dependent variable values begin with "-0.907" or "-0.908" essentially "eats up"/"wastes" the first 3 significant digits. Simply subtracting the minimimum value of the dependent variable works even better than standardizing by the mean and standard deviation:

`(* Subtract minimum value from the dependent variable *)
tableofvalues[[All, 5]] = tableofvalues[[All, 5]] - Min[tableofvalues[[All, 5]]];`

The resulting values of the two coefficients in question become

0.0619247172727312
0.0619247172719015

This is a very common issue that can produce odd results for nearly all statistical packages. Looking intensely at the data and using some form of standardization is always recommended.

End of UPDATE

The main culprit is the low variability of the dependent variable: tableofvalues[[All,5]]. If you standardize that variable (subtracting the mean and then dividing by the standard deviation), then all your troubles go away. In fact standardizing variables is almost always recommended even for the independent variables whenever there might be round-off error involved especially for the fitting of polynomials. However, the suggestion by @MarcoB (setting a higher precision) achieves essentially the same result.

stdvalues = tableofvalues;
stdvalues[[All, 5]] = Standardize[tableofvalues[[All, 5]]];
stdev5 = StandardDeviation[tableofvalues[[All, 5]]];

polynomtabelle = {1, x[1], x[1]^2, x[1]^3, x[1]^4, x[1]^5, x[2], 
  x[2]^2, x[2]^3, x[2]^4, x[2]^5, x[3], x[3]^2, x[3]^3, x[3]^4, 
  x[3]^5, x[4], x[4]^2, x[4]^3, x[4]^4, x[4]^5}

polynom = LinearModelFit[stdvalues, polynomtabelle, Array[x, {4}]];

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {5, 0, 0, 0} + 
    1], Floor@$MachinePrecision]

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {0, 0, 0, 5} + 
    1], Floor@$MachinePrecision]

with output

(* 0.0619247172791471 *)
(* 0.0619247172736994 *)

Note that one must multiply by the standard deviation of tableofvalues[[All,5]] to get the appropriate coefficient.

Source Link
JimB
  • 42.9k
  • 3
  • 51
  • 108

The main culprit is the low variability of the dependent variable: tableofvalues[[All,5]]. If you standardize that variable (subtracting the mean and then dividing by the standard deviation), then all your troubles go away. In fact standardizing variables is almost always recommended even for the independent variables whenever there might be round-off error involved especially for the fitting of polynomials. However, the suggestion by @MarcoB (setting a higher precision) achieves essentially the same result.

stdvalues = tableofvalues;
stdvalues[[All, 5]] = Standardize[tableofvalues[[All, 5]]];
stdev5 = StandardDeviation[tableofvalues[[All, 5]]];

polynomtabelle = {1, x[1], x[1]^2, x[1]^3, x[1]^4, x[1]^5, x[2], 
  x[2]^2, x[2]^3, x[2]^4, x[2]^5, x[3], x[3]^2, x[3]^3, x[3]^4, 
  x[3]^5, x[4], x[4]^2, x[4]^3, x[4]^4, x[4]^5}

polynom = LinearModelFit[stdvalues, polynomtabelle, Array[x, {4}]];

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {5, 0, 0, 0} + 
    1], Floor@$MachinePrecision]

NumberForm[
 stdev5 Extract[
   CoefficientList[polynom["BestFit"], Array[x, {4}]], {0, 0, 0, 5} + 
    1], Floor@$MachinePrecision]

with output

(* 0.0619247172791471 *)
(* 0.0619247172736994 *)

Note that one must multiply by the standard deviation of tableofvalues[[All,5]] to get the appropriate coefficient.