比较 rpy2 中的方法
Compare Methods in rpy2
我有一个 rpy2 脚本:
from rpy2.robjects.packages import importr
binom = importr('binom')
from rpy2 import robjects
robjects.r('''library(binom)
p = seq(0,1,.01)
coverage = binom.coverage(p, 10, method="bayes", type = "central")$coverage
''')
我想用它来比较方法列表的结果:
methods = [("bayes", type = "central"),("asymptotic")]
for method in methods:
robjects.globalenv["method"] = robjects.r(method)
robjects.r('''library(binom)
p = seq(0,1,0.01)
coverage = binom.coverage(p, 10, method=method)$coverage
''')
第一行给我:
invalid syntax
我想为贝叶斯方法包含 'type',但是当我删除它以获取列表中的语法时,我仍然收到错误消息:
object 'bayes' not found
robjects.r()
收到一个字符串,因此对于此特定任务,您只需将单词 method 替换为正确的字符串即可。使用两个引号(单引号和双引号)就可以了,因为 .replace()
将放弃外部引号并替换文本,保留单引号。
from rpy2.robjects.packages import importr
binom = importr('binom')
from rpy2 import robjects
methods = ["'bayes', type='central'","'asymptotic'"]
for method in methods:
r_string = """library(binom)
p = seq(0,1,0.01)
coverage = binom.coverage(p, 10, method=TECHNIQUE)$coverage
""".replace('TECHNIQUE',method)
robjects.r(r_string)
我有一个 rpy2 脚本:
from rpy2.robjects.packages import importr
binom = importr('binom')
from rpy2 import robjects
robjects.r('''library(binom)
p = seq(0,1,.01)
coverage = binom.coverage(p, 10, method="bayes", type = "central")$coverage
''')
我想用它来比较方法列表的结果:
methods = [("bayes", type = "central"),("asymptotic")]
for method in methods:
robjects.globalenv["method"] = robjects.r(method)
robjects.r('''library(binom)
p = seq(0,1,0.01)
coverage = binom.coverage(p, 10, method=method)$coverage
''')
第一行给我:
invalid syntax
我想为贝叶斯方法包含 'type',但是当我删除它以获取列表中的语法时,我仍然收到错误消息:
object 'bayes' not found
robjects.r()
收到一个字符串,因此对于此特定任务,您只需将单词 method 替换为正确的字符串即可。使用两个引号(单引号和双引号)就可以了,因为 .replace()
将放弃外部引号并替换文本,保留单引号。
from rpy2.robjects.packages import importr
binom = importr('binom')
from rpy2 import robjects
methods = ["'bayes', type='central'","'asymptotic'"]
for method in methods:
r_string = """library(binom)
p = seq(0,1,0.01)
coverage = binom.coverage(p, 10, method=TECHNIQUE)$coverage
""".replace('TECHNIQUE',method)
robjects.r(r_string)