Statsmodels allows the use of R-style formulas for equation fitting using patsy and statsmodels.formula.api
. I would like to fit a specific function using columns in a pandas DataFrame, however, I can only seem to get close. For example, if I have the following dataframe with columns ['A', 'B', 'C', 'D']
and want to fit an equation of the form:
y = (A + B) / D
I can write the formula string as y ~ (A + B):D-1
which results in two coefficients: A:D and B:D
. I can then do some algebra and get rid of the coefficient in front of one of them, but not both.
Is there a simple way to fit a custom function of this form, without switching to scipy curve_fit or lmfit?
Edits
To clarify, my goal is to obtain a fit value for D, letting A and B be values stored in the dataframe. To get this to work, I generated a dummy column of 1's called D
. So the known values I have are y, A, and B
, with D
being my fit parameter. As of now, I obtain two a result that looks like y=A:D*A + B:D*B
, which I can then extract to get something like y=(A + B:D/A:D*B)/A:D.
This works well, except that I would to not have the coefficient in front of B.