Message Boards Message Boards

GROUPS:

Use LinearModelFit for 2D fitting?

Posted 3 months ago
456 Views
|
3 Replies
|
2 Total Likes
|

Hello, Is there any way to use LinearModelFit command to fit bivariate functions (e.g. bivariate polynomials) to discrete 2D data? If yes, I would appreciate some examples or guidance. Or maybe MATHEMATICA has some alternative method for such fitting? Lesław

3 Replies

A general program in the book : André Dauphiné, Geographical models with Mathematica

country = "France";
ny = ToExpression@
   DialogInput[
    DynamicModule[{name = ""}, 
     Column[{"Combien de villes ?", InputField[Dynamic[name], String],
        ChoiceButtons[{DialogReturn[name], DialogReturn[]}]}]]];
coord = Take[
   Table[Reverse[CityData[c, "Coordinates"]], {c, 
     CityData[{All, country}]}], ny];
coordxy = 
  GeoGridPosition[
      GeoPosition[#, "WGS84"], {"UTMZone31", 
       "CentralScaleFactor" -> 0.9996, 
       "GridOrigin" -> {500000, 0}}][[1]] & /@ coord;
pop = Take[
   Table[QuantityMagnitude[CityData[c, "Population"]], {c, 
     CityData[{All, country}]}], ny];
nom = Take[Table[CityData[c, "Name"], {c, CityData[{All, country}]}], 
   ny];
n1 = Length[coord];
xyz = Partition[Flatten[Riffle[coordxy, pop]], 3];
minx = Min[xyz[[All, 1]]];
miny = Min[xyz[[All, 2]]];
maxx = Max[xyz[[All, 1]]];
maxy = Max[xyz[[All, 2]]];
Print["Paramètres du modèle d'ordre 3"]
ln = GeneralizedLinearModelFit[
   xyz, {x, y, x*y, x^2, y^2, x*y^2, y*x^2, x^3, y^3}, {x, y}];
Normal[ln]
test = ln[{"AIC", "BIC"}];
Grid[{{test_AIC, test[[1]]}, {test_BIC, test[[2]]}}, Frame -> All]
fit = ln["BestFit"];
resid = ln[{"FitResiduals", "StandardizedPearsonResiduals", 
    "CookDistances"}];
xyzr = Partition[Flatten[Riffle[nom, Transpose[resid]]], 4];
nn = {Noms_ville, Résidus_bruts, Résidus_standardisés, 
   Distance_Cook};
xyzr = Insert[xyzr, nn, 1];
Print[]
Print["Surface de tendance et valeurs des données"]
surf = Show[ContourPlot[fit, {x, minx, maxx}, {y, miny, maxy}], 
  Frame -> False, ClippingStyle -> Automatic, 
  ColorFunction -> ColorData[{"GrayLevel", "Reverse"}]]
GeoGraphics[{GeoStyling[{"GeoImage", 
    ImageCrop[Rasterize[surf, RasterSize -> 400]]}], EdgeForm[Thin], 
  Polygon[Entity["Country", country]]}, 
 GeoRange -> {{40.5, 52.5}, {-5, 9.5}}]
Print[]
Grid[xyzr, Alignment -> ".", Frame -> All, 
 Background -> {{Yellow, LightGray, LightBlue, LightRed}, None}]

This is covered by the second usage message on the reference page for LinearModelFit.

Hi, I am trying now to perform the 2D polynomial fitting according (more or less) to the second usage message on the reference page for LinearModelFit. However, I am often getting error messages: "The rank of the design matrix <number1> is less than the number of terms <number2> in the model. The model and results based on it may contain significant numerical error", and it appears that the results of fitting indeed have large errors. I need a good accuracy; the model parameters should be calculated with at least 30 accurate digits, and the data have a comparable accuracy. My question is: what should be done to eliminate these messages and errors? I am trying various modifications of the number of data points, polynomial orders, and precision of the data, but the error messages persist, only the numbers "number1" and "number2" change (note that typically I have a 100 by 100 table of data points, and the bivariate polynomial order is 10 - 20. The number "number1" does not seem to be related to these dimensions in any clear way). I cannot figure out any logical trends. Is there any lucid description of how LinearModelFit actually works, so that one can understand what happens? Is there any systematic method of setting right the algorithm parameters/options? From a general knowledge I would expect that in some place in the algorithm there is probably a numerical solution of a large system of normal equations involved, which are known to be ill-conditioned, so that their exact numerical solution is difficult. But, isn't MATHEMATICA designed to deal automatically with this sort of difficulties, by appropriately modifying the working precision? I previously had a similar problem while performing 1D fits, but I managed to overcome it somehow by increasing the number of data points. In the 2D case this seems not to help, and is also problematic due to large computational costs. Lesław

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract