2
$\begingroup$

I have the following data:

data = {{15, 1}, {18, 1470.7`/1476.9333333333334`}, {21, 1326.2333333333333`/1476.9333333333334`}, {24, 316.8`/1476.9333333333334`}};

Based on the data and knowledge of the underlying dynamics, Hill function, i.e., $f (x) = a^n / (a^n + x^n)$, can be a good candidate for fitting; and, $a$ and $n$ to be determined from the data. So, we have:

nlm = NonlinearModelFit[data, a^n/(a^n + x^n), {a, n}, x]

which gives error:

The function value {-1475.9327805109833`+0.0002583292146366989`I,-1469.699783452097`+0.0001011495715564914`I,-1325.2332352782923`+0.00004579492074835434` I,-44.140176596973895`+0.00002305423442489074` I} is not a list of \real numbers withdimensions {4} at {a,n} = {-3.5525189040549865`,-5.13906385562555`}.

How can this be fixed?

EDIT

Now, by adding the constraint $a > 0$ and $n > 0$, the error is fixed and Mathematica returns an answer:

nlm = NonlinearModelFit[data, {a^n/(a^n + x^n), a > 0, n > 0}, {a, n}, x]

***2.17245*10^17/(2.17245*10^17 + x^6.18796)***

But, it doesn't seem it fits the data:

Show[ListPlot[data], Plot[nlm[x], {x, 15, 24}], Frame -> True]

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ You can add constraints on the parameters, and/or provide an initial value for them. There are examples in the documentation. $\endgroup$ Nov 20 at 14:45
  • 1
    $\begingroup$ You can add constraints wo your model as in {a^n/(a^n + x^n), a > 0, n > 0}; please have a look at the documentation. $\endgroup$ Nov 20 at 15:39
  • $\begingroup$ @BobHanlon 's answer (+1) is a good example of the advantage of having good starting values. But I would argue that by dividing by the largest response variable the model actually being fit is c a^n/(a^n + x^n) so there are 3 parameters with just 4 data points. That suggests overfitting and fudging to me. Some tempering of the feeling of a good fit can be achieved by using the SinglePredictionBands option. $\endgroup$
    – JimB
    Nov 20 at 21:26

2 Answers 2

6
$\begingroup$

Include estimates of parameters in NonlinearModelFit

$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

data = {{15, 1}, {18, 1470.7`/1476.9333333333334`}, {21, 
     1326.2333333333333`/1476.9333333333334`}, {24, 
     316.8`/1476.9333333333334`}} // Rationalize;

f[a_, n_, x_] := a^n/(a^n + x^n)

Develop estimates for parameters

eqns = (f[a, n, #[[1]]] - #[[2]] == 0 & /@ data[[{1, -1}]])

(* {-1 + a^n/(15^n + a^n) == 0, -(216/1007) + a^n/(24^n + a^n) == 0} *)

est = NArgMin[{Total[eqns[[All, 1]]^2], a > 0, n > 0}, {a, n}]

(* {22.8901, 27.4141} *)

(nlm = NonlinearModelFit[data, f[a, n, x],
    Transpose[{{a, n}, est}], x])["BestFitParameters"]

(* {a -> 22.8313, n -> 25.9977} *)

{xmin, xmax} = MinMax[data[[All, 1]]]

(* {15, 24} *)

Plot[nlm[x], {x, xmin, xmax},
 Epilog -> {Red, AbsolutePointSize[4],
   Point[data]}]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ I don't know why the fit is as good as it is. $\endgroup$
    – Bob Hanlon
    Nov 20 at 20:19
3
$\begingroup$

This is just an extended comment. In short, that model can't provide a reasonable fit to your data.

The model can be rewritten as the following:

$$\frac{1}{\left(\frac{x}{a}\right)^n+1}$$

Given that all of the responses are greater than 1 (and real), that means that

$\left(\frac{x}{a}\right)^n+1<1$ or $\left(\frac{x}{a}\right)^n<0$. That can only potentially happen if $a<0$ and $n$ is an odd integer.

There does not appear to be a combination of $a$ and $n$ satisfying those restrictions that can match more than a single data point.

$\endgroup$
2
  • 1
    $\begingroup$ Your adjustment essentially is adding an additional parameter and with 3 parameters and just 4 data points, that doesn't lend to having much confidence in the fit. (And I do really understand that maybe even those 4 data points were very expensive to obtain.) $\endgroup$
    – JimB
    Nov 20 at 21:33
  • 1
    $\begingroup$ That is a very interesting problem but my advice is to get more data. You have 4 parameters (actually at least 5 counting the error structure which has currently been ignored) and only 8 data points. (Biological systems as opposed to Physics systems require many more data points than one would like. And I do understand that getting more data is many times impossible.) $\endgroup$
    – JimB
    Nov 25 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.