# NonlinearModelFit

I was trying to fit a model to the following experimental data:

data1 = {{0.00000000001, 100}, {1, 0.7}, {4, 0.03}, {24, 0.001}}


The first value was supposed to be 0.

NonlinearModelFit[data1, a x^-b, {a, b}, x]


resulted in the following equation: 0.390336/x^0.21896

The problem is that it does not fit the data. Using the same set of data in Excel the following equation was obtained: 0.62/x^-2.03 and this equation fits the data. What is wrong with the Mathematica model fit?

• The first value was supposed to be 0 I do not see how this can be possible. You show that your data is almost 100 near x=0 ? Since you wrote {0.00000000001, 100} Commented Mar 31, 2020 at 23:01
• Why do you say "The first value was supposed to be 0." ? Does that mean that the value 0.00000000001 is completely arbitrary? Picking 0.112568 provides a much better fit. You also must mean 0.62*x^-2.03 rather than 0.62/x^-2.03. But if you use that Excel prediction, then when you plug in 0.00000000001 for x, then you get 1.32554 * 10^22 which is bit larger than the observed value of 100.
– JimB
Commented Mar 31, 2020 at 23:05
• The data corresponds to the actual outcome of an experiment in which bacteria are been killed by an antibiotic. 100 means 100% survival at time 0. I should have been clearer. I used 0.00000000001 instead of 0 for the first x value because 0 would give an error. Commented Apr 1, 2020 at 1:29
• Yes, you are right on both accounts. The Excel equation was 0.62*x^-2.03 and 0 instead of 0.00000000001 has been used in the Excel spreadsheet Commented Apr 1, 2020 at 1:33
• Thank you guys, picking 0.000000000001 instead of 0 was my mistake. I should have picked another value. But I still don't get it why 0, the actual experimental value, cannot be used in NonlinearModelFit? Commented Apr 1, 2020 at 1:41

Clear["Global*"]

data1 = {{0.00000000001, 100}, {1, 0.7}, {4, 0.03}, {24, 0.001}};

{xmin, xmax} = MinMax@data1[[All, 1]];

model[a_, b_, x_] := a x^-b


For an exponential model it is better to deal with the log of the data.

logData = {#[[1]], Log[#[[2]]]} & /@ data1;


Include a constraint that the parameters must be positive

(nlm = NonlinearModelFit[logData,
{Log[model[a, b, x]], a > 0, b > 0}, {a, b}, x])["BestFitParameters"]

(* {a -> 0.0403419, b -> 0.321499} *)

f[x_] = E^nlm[x]

(* 0.0403419/x^0.321499 *)


The reason that x == 0 should be avoided is that the model cannot handled x == 0

Limit[f[x], x -> 0]

(* Indeterminate *)


Visually comparing the fit to the data

Plot[f[x], {x, xmin, xmax}, PlotRange -> {-0.05, 0.8},
Epilog -> {Red, AbsolutePointSize[4], Point[data1]}]


You have too few data points to get a good fit.

• Thank you for your thorough answer! So the rational of using 0.0000000001 as an approximation to 0 wasn't completely flawed? Commented Apr 1, 2020 at 13:12

Nothing is wrong with NonlinearModelFit (at least for the data and model given). My guess is that you fit a model in Excel using LINEST which fits a model of the form

$$\log y=\log a + b \log t + error$$

But if not, please add the Excel command that performed the regression.

The first element of data is {0,100}`. But that is not a data point. That is simply a known point that better be on the curve to be fitted. (One might want to call it an "anchor point".)

In addition, if your survival rates are based on counts, then you shouldn't be performing a regression which assumes among other things a constant variance at each time especially with a model that can't predict 100% at time zero. (The variance certainly goes to zero as time increases.)

Fitting 3 parameters ($$a$$, $$b$$, and the error variance) with just 3 data points is at best not recommended.

You should consult with a statistician about Survival Analysis or at least ask this question on CrossValidated after including a more detailed description of your data (describing if the response variable is a ratio of two counts, for example).

• Thanks for your answer. At first I've asked for a trend line with Power Regression, which visually fitted the data. But you are right, Excel ignored the 0,100 data points to construct the regression. I am aware that we have too few data points, but just to be clear the 0,100 is actually based on real data, the 100 represents the number of bacteria at time 0, which were actually counted, so how come it is not a data point? Commented Apr 1, 2020 at 13:10
• Maybe think of it this way: How could it be "data" if every single such experiment ever performed or ever will be performed always has the same exact value? It does offer information in the sense that whatever function you fit needs to go through that point. Kinda like assuming normality: assuming normality isn't data but it adds information and allows one to obtain more efficient estimates. Or maybe this is a better analogy: suppose you force a curve to go through the origin. In that case (0,0) is also NOT data but rather a restriction on the parameters.
– JimB
Commented Apr 1, 2020 at 14:40
• What was the number of bacteria at time 0? Is it an actual count or is it from an estimate of density (bacteria per area) multiplied by an area?
– JimB
Commented Apr 1, 2020 at 14:51
• These are the actual data: {0, 7533333}, {1, 54000}, {4, 2184}, {24, 82} x = time (hours); y = bacteria number (actual count) Commented Apr 1, 2020 at 15:35