I am working on fitting an equation from a dataset. In my dataset I have two variables where I am interested at and I looking a function that gives me the value the free variables that I use in the equation. My equation looks like
Y = 4 * log(X/n)/log(n/m) + 5,
where Y and X are variables from my dataset. So I am looking a mechanism to get the value of n and m. With some googling I found R function called nls, it looks working but the values which I got for n and m seems to be non realistic. That might be due to the problem with choosing the starting value for these variables.

Can any body suggest me other ways to do this? I am fine if I got R or Python libraries.

Thanks in advance.

  • $\begingroup$ Are n and m the parameters you are trying to estimate? $\endgroup$ Commented Jun 20, 2017 at 14:40
  • 6
    $\begingroup$ This model is equivalent to $$Y = \beta_0 + \beta_1 Z$$ where $Z=\log(X)$, $\beta_1=4/\log(n/m)$, and $\beta_0=5-4\log(n)/\log(n/m)$. In particular, it is linear. The usual way to fit such models, under mild assumptions about the deviations between the observed values and true values, is Ordinary Least Squares regression. Provided $\hat\beta_1 \ne 0$, you can then estimate $\hat n = \exp((5-\hat\beta_0)/\hat\beta_1)$ and $\hat m = \exp((1-\hat\beta_0)/\hat\beta_1)$. $\endgroup$
    – whuber
    Commented Jun 20, 2017 at 19:18
  • $\begingroup$ @Guilherme: Yes I want to estimate n and m. $\endgroup$ Commented Jun 27, 2017 at 14:06

1 Answer 1


You might try my pyeq3 Python fitting library, as it uses the Differential Evolution genetic algorithm to generate initial fitting parameters. You can install with the command "pip3 install pyeq3". Here is a tested, working example using your equation:

import pyeq3, warnings
warnings.filterwarnings("ignore") # we can ignore covariance warning

functionString = '4 * log(X/n)/log(n/m) + 5'

# note that the constructor is passed the function string here
equation = pyeq3.Models_2D.UserDefinedFunction.UserDefinedFunction(inUserFunctionString = functionString)

textData = '''
X     Y
1  -1.2131790227
2   1
3   2.2946267356
4   3.2131790227
5   3.9256635291

pyeq3.dataConvertorService().ConvertAndSortColumnarASCII(textData, equation, False)


for i in range(len(equation.solvedCoefficients)):
    print("    %s = %-.16E" % (equation.GetCoefficientDesignators()[i], equation.solvedCoefficients[i]))

Your Answer

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

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