# How do I specify the uncertainty of a parameter when using NonlinearModelFit?

I have to fit many large datasets into a linear equation. I am using the following code, employing NonlinearModelFit to avoid the Gaussian error propagation since the parameter of interest is c (if I were to use the linear fit i would have to multiply the slope with n and propagate the error since n itself isn't an exact quantity). I know the values of n (one per dataset) and I want Mathematica to automatically include its uncertainty into the fittet model, so that I get the uncertainty of c in the paramtertable, with the uncertainty of n taken into account.

NonlinearModelFit = [data, (c/n) x + d, {c, d}, x]

• use the option Weights, e.g. Weights -> 1 / nvalues?
– kglr
Dec 25, 2017 at 19:25
• I don't understand the setup. If you know the value of n for a given dataset, in what sense is it uncertain in the context of estimating the model given that dataset?
– mef
Dec 28, 2017 at 11:11

This is an extended comment rather than an answer.

LinearModelFit vs NonlinearModelFit

While using NonlinearModelFit is more straightforward (in my opinion), you'll likely have fewer convergence issues with LinearModelFit if you have large datasets as both give (essentially) the same results and LinearModelFit doesn't need starting values:

SeedRandom;
x = Range[1, 20];
n = 10;
y = 200/n + 2 x + RandomVariate[NormalDistribution[0, 4], 20];
data = Transpose[{x, y}];
ListPlot[data]
lm = LinearModelFit[data, {1/n, z}, z];
nlm = NonlinearModelFit[data, c/n + b z, {c, b}, z];
lm["BestFitParameters"]
(* {211.42453245272156, 1.741387448625845} *)
nlm["BestFitParameters"]
(* {c -> 211.4245324527219, b -> 1.741387448625842} *)


How do you specify the uncertainty of a predictor variable?

It sounds like this is your real question is about $n$ being a predictor variable rather than a parameter to be estimated. This also violates the assumption that the predictors are fixed and known. You can't get around that problem with anything that NonlinearModelFit has to offer.

If the variability of the predictor variable is small enough, then you don't have to worry about it. However, you first need to characterize the unstated amount and kind of variability that $n$ has. For example, is the standard deviation of distribution associated with $n$ small compared to the range of $n$ values you expect to use?

You might consider the following links: Bayesian approach and Error-in-variables.

Questions on data structure

• Do $c$ and $d$ (and the error variance) vary among your many datasets? If they do, then it doesn't matter at all if you're only interested in predictions. You'll get the same predictions if you use $c/n$ or $c$ or $c^2$.

• Is $n$ a predictor variable (i.e., each data point in a data set have potentially a different value of $n$) or just some value you set? If $c$ and $d$ and the error variance are common to all datasets, then you'll need to include $n$ as a predictor variable in the combined dataset given to NonlinearModelFit.

• No Mathematica built-in alternatives? Also, NonLinearModelFit[] seems to work out OK with using a VarianceEstimatorFunction; see How to | Fit Models with Measurement Errors Mar 26, 2021 at 2:15
• @Sterling That link about "measurement errors" deals with measurement errors on the response variable and not about errors associated with any predictor variables.
– JimB
Mar 26, 2021 at 2:42
• ahh, right. Thank you for clarifying. Mar 26, 2021 at 17:39