简体   繁体   English

rpy2:将Python中的NA表示为R函数的参数

[英]rpy2: represent NA in Python as an argument to R function

I'm trying to pass an NA to R function, eg, make predictions with a lme4 mixed model using only fixed effects (ie, without random effects): 我正在尝试将NA传递给R函数,例如,仅使用固定效果(即没有随机效果)使用lme4混合模型进行预测:

import rpy2.rinterface as ri
from rpy2.robjects.packages import importr
rstats = importr('stats')
rstats.predict( mymodel, re_form=ri.NA_Logical )

However, re_form=ri.NA_Logical fails to pass NA to re.form (I've tried also aliases REform , ReForm , etc.), for some reason. 但是,由于某种原因, re_form=ri.NA_Logical无法将NA传递给re.form (我也尝试过别名REformReForm等)。 Any ideas? 有任何想法吗?

This R function: https://www.rdocumentation.org/packages/lme4/versions/1.1-20/topics/predict.merMod 此R函数: https : //www.rdocumentation.org/packages/lme4/versions/1.1-20/topics/predict.merMod

This might be an issue with function dispatch / ellipsis in the signature of the generic (if an ellipsis is used in the signature of the generic rpy2 has no way to know that it should translate . to _ for a yet-unknown named argument). 这可能是泛型签名中的函数dispatch /省略号的问题(如果在通用rpy2的签名中使用了省略号,则无法知道应该将.转换为_来表示未知的命名参数)。

Try: 尝试:

rstats.predict(mymodel, **{'re.form': ri.NA_Logical})

or: 要么:

lme4 = importr('lme4')
lme4.predict_merMod(mymodel, re_form=ri.NA_Logical)

Relevant sections in the doc are https://rpy2.github.io/doc/v3.0.x/html/robjects_rpackages.html#importing-r-packages and https://rpy2.github.io/doc/v3.0.x/html/robjects_functions.html#rpy2.robjects.functions.SignatureTranslatedFunction (the latter mostly means that the doc is the code). 该文档中的相关部分为https://rpy2.github.io/doc/v3.0.x/html/robjects_rpackages.html#importing-r-packageshttps://rpy2.github.io/doc/v3。 0.x / html / robjects_functions.html#rpy2.robjects.functions.SignatureTranslatedFunction (后者主要表示doc是代码)。

edit: 编辑:

It is also possible to mix R code with Python a creative way. 也可以将R代码与Python混合使用。 For example: 例如:

myfunc = robjects.r('function (x) predict.merMod(x, re.form=NA)')
myfunc(mymodel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM