# Saving data from NonlinearModelFit

If you use NonlinearModelFit the results are put into some sort of output package. How do you save these packages so that you can go back to them later? My particular problem is that I generating hundreds of packages. Here is a toy example with just two

ClearAll[a1, a2, res];
data1 = Table[{x, (x + 3)/(x + 2) + RandomReal[{-0.01, 0.01}]}, {x, 0,5, 0.1}];
data2 = Table[{x, (x + 3)/(x + 2) + RandomReal[{-0.01, 0.01}]}, {x, 0,5, 0.1}];
res1 = NonlinearModelFit[data1, (x + a1)/(x + a2), {a1, a2}, x];
res2 = NonlinearModelFit[data2, (x + a1)/(x + a2), {a1, a2}, x];


The results has all sorts of good information stored within it, for example

res2["ParameterConfidenceIntervalTable"]


Now I wish to save all this information for analysis on another day so I write

SetDirectory[NotebookDirectory[]];
Save["test.s", {res1, res2}]


When I try and get the data back this is what I get

oldfits = Get["test.s"]


Which is just the last example. I guess one solution would be to store each package individually, however this would be a flood of files. I have also tried text files but this does not help. Thanks for any help.

Exporting the NonlinearModelFit result objects as mx seems to work fine.

SetDirectory[NotebookDirectory[]];
ClearAll[dat0, nmf0, pcit0];
dat0 = Table[{x, (x + 3)/(x + 2) + RandomReal[{-0.01, 0.01}]}, {iSet,
1, 10, 1}, {x, 0, 5, 0.01}];
Dimensions@dat0

nmf0 = Table[NonlinearModelFit[dat0[[iSet]], (x + a1)/(x + a2), {a1, a2},x], {iSet, Length@dat0}];

pcit0 = Table[nmf0[[iSet]]["ParameterConfidenceIntervalTable"], {iSet,Length@nmf0}]

Export["nmf0.mx", nmf0]
ClearAll[nmf1];

nmf1 = Import["nmf0.mx"]
nmf1 == nmf0


(* True *)

pcit1 = Table[nmf1[[iSet]]["ParameterConfidenceIntervalTable"], {iSet,Length@nmf1}]

pcit0 == pcit1


(* True *)

• One fact to be aware of: "MX files cannot be exchanged between different operating systems or versions of the Wolfram System." [MX] – Karsten 7. Sep 19 '14 at 19:47
• Good point. Changing the extension to .m for the Import/Export also works and can be read with a text editor. – rivercfd Sep 19 '14 at 19:52
• I would consider WDX as the platform-independet equivalent to the MX format. – Karsten 7. Sep 19 '14 at 19:58
• @rivercfd Thank you. It works well and I can also mix in other strings and results. Is there an obvious class of file types that works? I did try others and failed. – Hugh Sep 19 '14 at 20:24
• @Karsten7. Thank you for pointing WDX out. I will investigate. – Hugh Sep 19 '14 at 20:25