rpy2 - 'R' object has no attribute 'nls'

后端 未结 1 1435
萌比男神i
萌比男神i 2021-01-26 19:36

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         


        
相关标签:
1条回答
  • 2021-01-26 20:23

    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)
    
    0 讨论(0)
提交回复
热议问题