RPy2 中的方差分析语法
ANOVA syntax in RPy2
第一次使用 RPy2 实现是在 Python。尝试使用两个因素进行单向方差分析。它在另一台机器上的 R 中工作,但 Python 不喜欢语法。任何想法表示赞赏!
from rpy2.robjects import aov
huanova = aov(formula = df1['human_den'] ~ df1['region']+df1['years'])
错误消息指向 tilda。
huanova = aov(formula=df1['human_den'] ~ df1['region']+df1['years'])
^
SyntaxError: invalid syntax
波浪号 ~
在 python 中是一元的,但您将其用作二进制。你可能想要:
huanova = aov(formula = df1['human_den'] + ~ df1['region']+df1['years'])
请注意,我在波浪号之前添加了一个加号。
根据 documentation about Formulae in Rpy2,您必须将公式作为字符串传递。这是一种方法:
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 will work but you need load the 'stats' package that contains the aov function using importr, as shown in :
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)
第一次使用 RPy2 实现是在 Python。尝试使用两个因素进行单向方差分析。它在另一台机器上的 R 中工作,但 Python 不喜欢语法。任何想法表示赞赏!
from rpy2.robjects import aov
huanova = aov(formula = df1['human_den'] ~ df1['region']+df1['years'])
错误消息指向 tilda。
huanova = aov(formula=df1['human_den'] ~ df1['region']+df1['years'])
^
SyntaxError: invalid syntax
波浪号 ~
在 python 中是一元的,但您将其用作二进制。你可能想要:
huanova = aov(formula = df1['human_den'] + ~ df1['region']+df1['years'])
请注意,我在波浪号之前添加了一个加号。
根据 documentation about Formulae in Rpy2,您必须将公式作为字符串传递。这是一种方法:
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 will work but you need load the 'stats' package that contains the aov function using importr, as shown in
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)