1
$\begingroup$

I would like to use LogitModelFit to find the logistic function to my data. The error I obtain is

LogitModelFit::binrsp: The response values are expected to be between 0 and 1 for binomial models.

But in the examples, the given values are not in [0,1]. I normalized the values and the error is the same. Please, help!

Here is the data:

data ={{1.887, 17}, {1.9, 44.96}, {1.905, 50.57}, {1.934, 
    79.60}, {1.946, 68.56}}

Thanks!

$\endgroup$
  • $\begingroup$ I don't think this can be answered without understanding what your data means. What did you try to normalize your data? $\endgroup$ – mikado Nov 10 '19 at 18:01
  • $\begingroup$ I want to find the logistic curve which is closest to the given points $\endgroup$ – Ксения Цочева Nov 10 '19 at 18:36
  • $\begingroup$ A logistic curve will achieve a maximum value. Do you know what this value should be? $\endgroup$ – mikado Nov 10 '19 at 20:56
  • 1
    $\begingroup$ The response variable for LogitModelFit can only take on the values 0 or 1 or ratios of integer counts if you give the number of binomial trials. You probably want NonlinearModelFit but you should make explicit the "logit" form that you want to fit. Is that a/(1+b*Exp[-k t]) or something else? If that is the case, then fitting 4 parameters (a, b, k, and error variance) with just 5 data points is likely not to be very useful. $\endgroup$ – JimB Nov 10 '19 at 22:55
6
$\begingroup$

You'll likely want NonlinearModelFit rather than LogitModelFit as the latter assumes that the response variable has a binomial distribution given the prediction model.

Consider the following "logit" model: $y=a/(1+\exp(-k(t-b)) + e$ where $e\sim N(0,\sigma^2)$ and $a$, $b$, $k$, and $\sigma^2$ are parameters to be estimated. That's 4 parameters with only 5 data points. You can't expect very much.

data = Rationalize[{{1.887, 17}, {1.9, 44.96}, {1.905, 50.57}, {1.934, 79.60}, {1.946, 68.56}}, 0];

nlm = NonlinearModelFit[data, a/(1 +  Exp[-100 k ( t - b)]),
   {{a, 70}, {b, Min[data[[All, 1]]]}, {k, 1.2}}, t, WorkingPrecision -> 30];
nlm["BestFitParameters"]
(* {a -> 74.3454576102087647236067343080, 
    b -> 1.89726015500543769996100150019, 
    k -> 1.17012737566143311476612748243} *)

{tmin, tmax} = MinMax[data[[All, 1]]];
mpd = Table[Flatten[{t, FullSimplify[nlm["MeanPredictionBands"]]}],
    {t, tmin, tmax, (tmax - tmin)/100}] // N;

(* Plot of data, fit, and 95% confidence bands for the mean *)
Show[ListPlot[data, PlotRange -> {All, {-10, 100}}],
 ListPlot[{mpd[[All, {1, 2}]], mpd[[All, {1, 3}]]}, 
  PlotStyle -> {{Blue, Dotted}, {Blue, Dotted}}, Joined -> True],
 Plot[nlm[t], {t, 1.887, 1.946}]]

data, fit, and 95% confidence bands for the mean

The use of Rationalize, WorkingPrecision -> 30, and using $a/(1+\exp{(-100k(t-b))}$ rather than $a/(1+\exp{(-100k(t-b))}$ or $a/(1+b\exp{-k t}$ was to keep the calculations from warnings such as "... is too small to represent as a normalized machine number".

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks a lot! I just do not understand the meaning of this part \begin{align*} {tmin, tmax} = MinMax[data[[All, 1]]]; mpd = Table[Flatten[{t, FullSimplify[nlm["MeanPredictionBands"]]}], {t, tmin, tmax, (tmax - tmin)/100}] // N; \end{align*} $\endgroup$ – Ксения Цочева Nov 11 '19 at 19:45
  • $\begingroup$ I used that code to avoid several warning messages. The following produces the same plot (but with lots of warning messages): mpb = FullSimplify[nlm["MeanPredictionBands"]]; Plot[{nlm[t], mpb}, {t, 1.885, 1.945}, PlotStyle -> {{Blue}, {Blue, Dotted}, {Blue, Dotted}}, WorkingPrecision -> 30]. $\endgroup$ – JimB Nov 13 '19 at 21:29
  • $\begingroup$ Thanks a lot! Very useful! $\endgroup$ – Ксения Цочева Nov 14 '19 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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