pythonrrpy2dirichlet

Call R library DirichletReg from Python using rpy2


I'm trying to do Dirichlet Regression using Python. Unfortunately I cannot find a Python package that does the job. So I tried to call R library DirichletReg using rpy2. However, it is not very intuitive to me how to call a regression function such as DirichReg(Y ~ X1 + X2 + X3, data=predictorData) where Y = DR_data(compositionalData). I saw an example of calling linear regression function lm in the documentation of rpy2. But my case is slightly different as Y is not a column name in the table but an R object DR_data.

I'm wondering what the proper way is to do this, or whether there is a Python package for Dirichlet Regression.


Solution

  • You can send objects into the "Formula" environment from python. This example is from the rpy2 docs:

    import array
    from rpy2.robjects import IntVector, Formula
    from rpy2.robjects.packages import importr
    stats = importr('stats')
    
    x = IntVector(range(1, 11))
    y = x.ro + stats.rnorm(10, sd=0.2)
    
    fmla = Formula('y ~ x')
    env = fmla.environment
    env['x'] = x
    env['y'] = y
    
    fit = stats.lm(fmla)
    

    You can also create named variables in the R environment (outside the Formula). See here. Worst case scenario, you move some your python data into R through rpy2, then issue the commands directly in R through the rpy2 bridge as described here.