I am using rpy2 to do some non-linear regression in r from python.
import rpy2.robjects as robjects
from rpy2.robjects import DataFrame, Formula
from rpy2.robjec
Consider importing R's stats and base libraries and then replicate needed calls. And use as_formula
to convert string representation of formula to actual formula object. Since these are default R libraries find out which method belongs to which package like stats::nls()
and base::list()
.
Notice too in order to keep aligned to Python's syntax rules, any periods in R names are converted to underscores. A few other methods are renamed to avoid clash with Python's own methods.
...
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
base = importr('base', robject_translations={'with': '_with'})
stats = importr('stats', robject_translations={'format_perc': '_format_perc'})
my_formula = stats.as_formula('rates ~ 1-(1/(10^(a * count ^ (b-1))))')
d = ro.ListVector({'a': a, 'b': b})
fit = stats.nls(my_formula, weights=count, start=d)