2

First time using the RPy2 implementation in Python. Attempting to do an one-way ANOVA with two factors. It works in R on another machine, but Python does not like the syntax. Any thoughts are appreciated!

from rpy2.robjects import aov

huanova = aov(formula = df1['human_den'] ~ df1['region']+df1['years'])

Error message points at the tilda.

huanova = aov(formula=df1['human_den'] ~ df1['region']+df1['years'])
                                       ^
SyntaxError: invalid syntax
DJV
  • 863
  • 3
  • 15
  • 30

3 Answers3

1

As per the documentation about Formulae in Rpy2, you have to pass the formula as a string. This is one way of doing it:

from rpy2.robjects import aov
from rpy2.robjects import Formula

formula = Formula('human_den ~ region + years')
env = formula.environment
env['human_den'] = df1['human_den']
env['region'] = df1['region']
env['years'] = df1['years']

huanova = aov(formula = formula)
musically_ut
  • 34,028
  • 8
  • 94
  • 106
0

The above answer from musically_ut will work but you need load the 'stats' package that contains the aov function using importr, as shown in this question:

from rpy2.robjects import Formula 
from rpy2.robjects.packages import importr
stats = importr('stats')

formula = Formula('human_den ~ region + years')
env = formula.environment
env['human_den'] = df1['human_den']
env['region'] = df1['region']
env['years'] = df1['years']

huanova = stats.aov(formula = formula)
-1

Tilde ~ is Unary in python, but you are using it as Binary. You may want:

 huanova = aov(formula = df1['human_den'] + ~ df1['region']+df1['years'])

Notice that I've added a PLUS before Tilde.

Assem
  • 11,574
  • 5
  • 59
  • 97
  • Not at all (! @ all ). The tilde in R is part of a formula's definition. It does not translate to Python's unary tilde. I downvoted because of that. – lgautier Sep 22 '15 at 16:37
  • I dont know about tilde in R but here OP got a syntax error cause the tilde in Python is unary. There should be a way to use the R's tilde but not that way – Assem Sep 22 '15 at 16:59