I have two collections of noisy observations from a 2d quadratic function:

• collection $$A$$ is from $$[0,1]^2$$
• collection $$B$$ is from $$[1,2]^2$$

When I try to fit the function to the data it works very well on collection $$A$$ but not on collection $$B$$ and I have no idea why this is.

MWE:

dataA = {{0.663419, 0.703687, 4.28865}, {0.263037,
0.24184, -0.564946}, {0.112817, 0.111583, 0.684741}, {0.0540994,
0.26985, -0.0114828}, {0.321676, 0.895135, -1.27625}, {0.534555,
0.440423, 1.16551}, {0.709857, 0.95463, 5.58092}, {0.0658968,
0.870521, -1.60022}, {0.851446, 0.670389, 5.81095}, {0.693849,
0.0632906, 2.35338}, {0.829073, 0.648996, 5.62004}, {0.725963,
0.498424, 4.94386}, {0.502263, 0.580468, 2.92439}, {0.696144,
0.664244, 4.57215}, {0.0291447, 0.663636, -2.62879}, {0.402932,
0.872725, 1.67216}, {0.405552, 0.465293, 1.34099}, {0.124139,
0.425692, 0.881277}, {0.51801, 0.684696, 2.13586}, {0.772306,
0.254602, 5.14878}}
dataB = {{1.93689, 1.44923, 3.66043}, {1.85137, 1.4563, 0.905775}, {1.33793,
1.29292, 1.53175}, {1.33573, 1.85645, -0.747964}, {1.85614,
1.61939, -0.00843461}, {1.73793, 1.18994, 3.20157}, {1.27527,
1.98197, 6.38376}, {1.27735, 1.68567, -2.46345}, {1.47272, 1.66237,
6.81012}, {1.98127, 1.01408, 4.48609}, {1.85979, 1.11748,
6.04641}, {1.76973, 1.32209, 4.71054}, {1.78997, 1.46888,
2.81993}, {1.62344, 1.67492, 5.31341}, {1.1076,
1.75961, -1.45853}, {1.13078, 1.38204, 0.659455}, {1.45618, 1.04366,
0.770407}, {1.31093, 1.44335, -0.391301}, {1.88764, 1.35217,
0.651056}, {1.53726, 1.35367, 4.33438}}

lmA = LinearModelFit[dataA, {1,x1,x2,x1^2,x1 x2,x2^2}, {x1,x2}];
lmB = LinearModelFit[dataB, {1,x1,x2,x1^2,x1 x2,x2^2}, {x1,x2}];

lmA["RSquared"]
(*  0.959048  *)

lmB["RSquared"]
(*  0.136582  *)


This same phenomenon keeps happening for any different quadratic function that I sample the data from and I have no idea what is going wrong here. Any advice would be most appreciated.

• Your x1 x2 term has a syntax problem. There has to be a space between the two variables, otherwise it counts as a single symbol. – Sjoerd Smit Oct 1 '19 at 9:41
• @SjoerdSmit, thanks for pointing that out, sadly that is just in my MWE and not in my actual code. Have fixed it now. – wilsnunn Oct 1 '19 at 9:42
• When I plot the data, dataA looks linear rather than quadratic. I'm not really surprised that it fits a lot better than dataB, to be honest. Also, RSquared is a rather limited measure for the quality of a fit. – Sjoerd Smit Oct 1 '19 at 9:51
• I'm voting to close this question as off-topic because the issued raised is not really a problem; it arises from the OP not understanding of the result returned by Mathematica. – m_goldberg Oct 1 '19 at 22:17

It's your expectations that are wrong. The estimation process is working fine. Take a look at the data and the predicted surface for both sets of data.

Show[ListPointPlot3D[dataA, PlotRange -> {All, All, {-3, 7}},
AxesLabel -> (Style[#, Bold, 18] &) /@ {"x", "y", "z"}],
Plot3D[lmA[x, y], {x, 0, 1}, {y, 0, 1}], ImageSize -> Large]


Show[ListPointPlot3D[dataB, PlotRange -> {All, All, {-3, 7}},
AxesLabel -> (Style[#, Bold, 18] &) /@ {"x", "y", "z"}],
Plot3D[lmB[x, y], {x, 1, 2}, {y, 1, 2}], ImageSize -> Large]


Both figures have the same scaling for the z-axis. dataB shows a lot more noise.

• Thank you, I have now found the source of the error and have fixed the issue. – wilsnunn Oct 1 '19 at 18:30
• Sounds good. Glad you were able to find the source of the error. But please note that I've voted to close the question as with that response I don't see that the question and answer to the question would help anyone else. – JimB Oct 1 '19 at 18:50
• Agreed, I voted to close too once the issue had been fixed. Thanks for your help. – wilsnunn Oct 1 '19 at 21:41
• @SjoerdSmit gave you the best advice (which I then copied): plot your data. – JimB Oct 1 '19 at 22:04

I agrees with JimB. Here is a visualization of dataB and the surface you fitted to it which shows how far from a plane both the data and the model are. The low $$r^2$$ value seems entirely appropriate. Remember that $$r^2$$ is a measure of linearity (flatness in 3D) and not a measure of goodness-of-fit.

Block[{blue, lollies},
blue = RGBColor[0., 0.59, 1.];
lollies[pt : {x_, y_, z_}] := {Point[pt], Line[{pt, {x, y, lmB[x, y]}}]};
dataPlot =
Graphics3D[
{blue, AbsolutePointSize[10], AbsoluteThickness[3.5], lollies /@ dataB},
BoxRatios -> {1, 1, 1}]];

modelPlot =
Plot3D[lmB[x1, x2], {x1, 1, 2}, {x2, 1, 2}, PlotStyle -> Opacity[.4]]

Show[modelPlot, dataPlot,
BoxRatios -> {1, 1, 1},
PlotRange -> All,
ImageSize -> Large]