Formulae

For tasks such as modelling and plotting, an R formula can be a terse, yet readable, way of expressing what is wanted.

In R, it generally looks like:

x <- 1:10
y <- x + rnorm(10, sd=0.2)

fit <- lm(y ~ x)

In the call to lm, the argument is a formula, and it can read like model y using x. A formula is a R language object, and the terms in the formula are evaluated in the environment it was defined in. Without further specification, that environment is the environment in which the the formula is created.

The class robjects.Formula is representing an R formula.

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)

One drawback with that approach is that pretty printing of the fit object is note quite as good as what one would expect when working in R: the call item now displays the code for the function used to perform the fit.

If one still wants to avoid polluting the R global environment, the answer is to evaluate R call within the environment where the function is defined.

from rpy2.robjects import Environment

eval_env = Environment()
eval_env['fmla'] = fmla
base = importr('base')

fit = base.eval.rcall(base.parse(text = 'lm(fmla)'), stats._env)

Other options are:

  • Evaluate R code on the fly so we that model fitting function has a symbol in R

    fit = robjects.r('lm(%s)' %fmla.r_repr())
    
  • Evaluate R code where all symbols are defined

class rpy2.robjects.Formula(formula, environment=rinterface.globalenv)[source]

Bases: rpy2.robjects.robject.RObjectMixin, rpy2.rinterface_lib.sexp.Sexp

property environment

R environment in which the formula will look for its variables.

getenvironment()[source]

Get the environment in which the formula is finding its symbols.

setenvironment(val)[source]

Set the environment in which a formula will find its symbols.