[英]How to use the R 'with' operator in rpy2
I am doing an ordinal logistic regression, and following the guide here for the analysis: R Data Analysis Examples: Ordinal Logistic Regression 我正在执行序数逻辑回归,并按照此处的指导进行分析: R数据分析示例:序数逻辑回归
My dataframe (consult) looks like: 我的数据框(咨询)如下所示:
n raingarden es_score consult_case
garden_id
27436 7 0 3 0
27437 1 0 0 1
27439 1 1 1 1
37253 1 0 3 0
37256 3 0 0 0
I am at the part where I need to to create graph to test the proportional odds assumption, with the command in R as follows: 我需要在R中使用以下命令来创建图以测试比例赔率假设:
(s <- with(dat, summary(es_score ~ n + raingarden + consult_case, fun=sf)))
(es_score is an ordinal ranked score with values between 0 - 4; n is an integer; raingarden and consult_case, binary values of 0 or 1) (es_score是序数排序得分,其值介于0到4之间; n是整数; raingarden和consult_case,二进制值为0或1)
I have the sf function: 我有科幻功能:
sf <- function(y) {
c('Y>=1' = qlogis(mean(y >= 1)),
'Y>=2' = qlogis(mean(y >= 2)),
'Y>=3' = qlogis(mean(y >= 3)))
}
in a utils.r file that I access as follows: 在我访问的utils.r文件中,如下所示:
from rpy2.robjects.packages import STAP
with open('/file_path/utils.r', 'r') as f:
string = f.read()
sf = STAP(string, "sf")
And want to do something along the lines of: 并希望采取以下措施:
R = ro.r
R.with(work_case_control, R.summary(formula, fun=sf))
The major problem is that the R with
operator is seen as a python keyword, so that even if I access it with ro.r.with
it is still recognized as a python keyword. 主要问题是
with
操作符的R被视为python关键字,因此即使我使用ro.r.with
访问它, ro.r.with
被识别为python关键字。 (As a side note: I tried using R's apply
method instead, but got an error that TypeError: 'SignatureTranslatedAnonymousPackage' object is not callable
... I assume this is referring to my function sf
?) (作为旁注:我尝试使用R的
apply
方法,但收到TypeError: 'SignatureTranslatedAnonymousPackage' object is not callable
……我假设这是指我的函数sf
?)
I also tried using the R assignment methods in rpy2 as follows: 我还尝试在rpy2中使用R分配方法,如下所示:
R('sf = function(y) { c(\'Y>=1\' = qlogis(mean(y >= 1)), \'Y>=2\' = qlogis(mean(y >= 2)), \'Y>=3\' = qlogis(mean(y >= 3)))}')
R('s <- with({0}, summary(es_score~raingarden + consult_case, fun=sf)'.format(consult))
but ran into issues where the dataframe column names were somehow causing the error: RRuntimeError: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"), : <text>:1:19: unexpected symbol 1: s <- with( n raingarden
但遇到数据
RRuntimeError: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"), : <text>:1:19: unexpected symbol 1: s <- with( n raingarden
列名以某种方式导致错误的问题: RRuntimeError: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"), : <text>:1:19: unexpected symbol 1: s <- with( n raingarden
I could of course do this all in R, but I have a very involved ETL script in python, and would thus prefer to keep everything in python using rpy2 (I did try this using mord for scipy-learn to run my regreession, but it is pretty primitive). 我当然可以在R中完成所有操作,但是我在python中有一个非常参与的ETL脚本,因此宁愿使用rpy2将所有内容保留在python中(我确实使用mord来进行scipy-learn来运行我的答辩,但是它非常原始)。
Any suggestions would be most welcome right now. 任何建议现在都将受到欢迎。
EDIT 编辑
I tried various combinations @Parfait's suggestions, and qualifying the fun
argument is syntactically incorrect, as per PyCharm interpreter (see image with red highlighting at end): 我尝试了@Parfait的建议的各种组合,并且根据PyCharm解释器,限定
fun
参数在语法上是不正确的(请参见带有红色突出显示的图像): ... it doesn't matter what the qualifier is, either, I always get an error that
SyntaxError: keyword can't be an expression.
...不管是什么限定词,我总是会收到一个错误消息,
SyntaxError: keyword can't be an expression.
On the other hand, with no qualifier, there is no syntax error: 另一方面,没有限定符,则没有语法错误:
, but I do get the error
TypeError: 'SignatureTranslatedAnonymousPackage' object is not callable
when using the function sf
as obtained: ,但我确实收到错误
TypeError: 'SignatureTranslatedAnonymousPackage' object is not callable
使用获得的函数sf
时, TypeError: 'SignatureTranslatedAnonymousPackage' object is not callable
:
from rpy2.robjects.packages import STAP with open('/Users/gregsilverman/development/python/rest_api/rest_api/scripts/utils.r', 'r') as f: string = f.read() sf = STAP(string, "sf")
With that in mind, I created a package in R with the function sf
, imported it, and tried various combos with the only one producing no error, being: print(base._with(consult_case_control, R.summary(formula, fun=gms.sf)))
(gms is a reference to the package in RI made). 考虑到这一点,我在R中使用函数
sf
创建了一个程序包,将其导入,然后尝试了多种组合,唯一的组合不会产生错误,即: print(base._with(consult_case_control, R.summary(formula, fun=gms.sf)))
(gms是对RI中的软件包的引用)。
The output though makes no sense: 输出虽然没有意义:
Length Class Mode 3 formula call
I am expecting a table ala the one on the UCLA site. 我希望UCLA网站上有一张桌子。 Interesting.
有趣。 I am going to try recreating my analysis in R, just for the heck of it.
我将尝试在R中重新创建我的分析,仅出于此目的。 I still would like to complete it in python though.
我仍然想在python中完成它。
Consider bracketing the with
call and be sure to qualify all arguments including fun : 考虑将
with
调用括起来,并确保限定所有参数,包括fun :
ro.r['with'](work_case_control, ro.r.summary(formula, ro.r.summary.fun=sf))
Alternatively, import R's base package. 或者,导入R的基本包。 And to avoid conflict with Python's named method
with()
translate the R name: 为了避免与Python的具名方法
with()
冲突,请翻译R名称:
from rpy2.robjects.packages import importr
base = importr('base', robject_translations={'with': '_with'})
base._with(work_case_control, ro.r.summary(formula, ro.r.summary.fun=sf))
And be sure to properly create your formula. 并确保正确创建您的公式。 Consider using R's stats packages'
as.formula
to build from string. 考虑使用R的stats软件包的
as.formula
从字符串构建。 Notice too another translation is made due to naming conflict: 注意,由于命名冲突,还会进行另一种翻译:
stats = importr('stats', robject_translations={'format_perc': '_format_perc'})
formula = stats.as_formula('es_score ~ n + raingarden + consult_case')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.