I am trying to run the following code but it gives me:
Error in qr.default(.swts * attr(rhs, "gradient")) : NA/NaN/Inf in foreign function call (arg 1) In addition: Warning message: In log(.expr4) : NaNs produced
Can you please help me about? Thanks!
model <- deriv( ~ c*(1+b*(q-1)*t)^(1/(1-q)), c("c", "b", "q"), function (t, c, b,q){})
nls(Frequency ~ model(t, c, b, q), data=DF,start=list(c = 1, b = 1.5, q =0.5))
Following you can see a part of data, for which I am trying to fit a q-exponential distribution functin as I explained above. I am uiung nls function in R to obtain estimates (q-exponential) for the given data.
t Frequency 0 195746 1 93938 2 53181 3 31853 4 19856 5 12182 6 7847 7 5459 8 4325 9 3203 10 2750
tl;dr you need to work harder to think about/find reasonable starting values.
Set up data and model:
If you just evaluate
model()
at the initial parameters you can see that some of the derivatives with respect toq
areNaN
(it is always worthwhile to try evaluating the objective function and gradient at the starting values to make sure they make sense). In addition to this, if you look at the values of the objective function you can see that they're nowhere close to the data (they increase from 1 to 42, whereas the data decrease from 200000 to 3000 ...)The problem is that raising a negative value to a fractional power gives
NaN
. Whenq
is <1, the base will go negative whent
is sufficiently large ...I tried setting
q=0
(so that the base value of the exponent is 1), but that still encounters the same problem.What if we start from values where
q-1
is >0 so that the base will be positive?OK, that's getting a little better but still failing. What if we try to start on a more reasonable scale, i.e. set
c
to 100000?Plot results: