1
$\begingroup$

How do I get the confidence intervals from NonlinearModelFit so that I can use them in subsequent calculations?

In this example I generate some data and then use NonlinearModelFit to fit the data. I get a nice parameter table from the fit. (The PValue is crazy but that is not the problem here). I am after the values in the last column which can be seen if you slide to the right.

     SeedRandom[123];
        dd = Table[{t, (3 t)/(1 + 2 t^2) + RandomReal[{0, 0.05}]}, {t, 0.1, 
            10, 0.1}];
    ClearAll[a, b];
    nlm = NonlinearModelFit[dd, (a t)/(1 + b t^2), {{a, 2.8}, {b, 2.1}}, 
       t];
nlm["ParameterEstimates"]

enter image description here

I want the confidence interval values as a list that I can use in further calculations. As far as I can see it is not one of the properties you can get from the list of properties nlm["Properties"]

$\endgroup$
4
  • 3
    $\begingroup$ Would something like this work: Normal@nlm["ParameterEstimates"][[;; ,"ConfidenceInterval"]] $\endgroup$ Commented Feb 26 at 20:50
  • $\begingroup$ @Roderic Very good. One of those outputs that requires a Normal to make it usable. Howe did you find that or is Normal something you find you need all the time? Many thanks. $\endgroup$ Commented Feb 26 at 21:27
  • 1
    $\begingroup$ Apparently nlm["ParameterEstimates"] is a Dataset structure. Somewhere in the documentation it indicated that Normal can be used to convert it into the underlying data, which is the list of parameters. $\endgroup$ Commented Feb 26 at 22:32
  • $\begingroup$ From the documentation for Dataset, "Normal can be used to convert any Dataset object to its underlying data, which is typically a combination of lists and associations." $\endgroup$ Commented Feb 27 at 0:14

1 Answer 1

4
$\begingroup$

It depends on what confidence intervals you need.

If you want confidence intervals for the individual regression parameters, then use the suggestion by @Roderic.

If you want a joint confidence region for the two parameters in your model, try

SeedRandom[123];
dd = Table[{t, (3 t)/(1 + 2 t^2) + RandomReal[{0, 0.05}]}, {t, 0.1, 10, 0.1}];
ClearAll[a, b];
nlm = NonlinearModelFit[dd, (a t)/(1 + b t^2), {{a, 2.8}, {b, 2.1}}, t];
jointCR = nlm["ParameterConfidenceRegion"]
(* FittedModels`ParameterEllipsoid[{3.01788, 1.90351}, {0.117753, 0.0192635},
  {{-0.775608, -0.631215}, {0.631215, -0.775608}}] *)
Show[Graphics[jointCR], ListPlot[{{a, b}} /. nlm["BestFitParameters"]], Frame -> True,
  FrameLabel -> (Style[#, Bold, 18, Italic] &) /@ {"a", "b"}]

Confidence ellipse for parameters a and b

If you want mean or single prediction bands, then try

Plot[{Evaluate[nlm["MeanPredictionBands"]], Evaluate[nlm["SinglePredictionBands"]]},
 {t, Min[dd[[All, 1]]], Max[dd[[All, 1]]]}, PlotStyle -> {Blue, Blue, Orange, Orange},
 PlotLegends -> LineLegend[{Blue, White, Orange, White},
   {"Mean prediction bands", "", "Single prediction bands", ""}]]

Mean and single prediction bands

Finally, if you want to construct confidence intervals for functions of $a$ and $b$, then you'll need to obtain the parameter covariance matrix:

(covmat=nlm["CovarianceMatrix"])//MatrixForm

Parameter covariance matrix

$\endgroup$
1
  • $\begingroup$ This is very helpful. Most useful guidance. Many thanks. $\endgroup$ Commented Feb 27 at 11:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.