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

问题描述 投票:1回答:1

我试图将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 )

然而,由于某种原因,re_form=ri.NA_Logical未能将NA传递给re.form(我也试过别名REformReForm等)。有任何想法吗?

这个R函数:https://www.rdocumentation.org/packages/lme4/versions/1.1-20/topics/predict.merMod

python r rpy2 lme4
1个回答
2
投票

这可能是泛型签名中函数调度/省略号的问题(如果在泛型rpy2的签名中使用省略号,则无法知道它应该将.转换为_以获取尚未知的命名参数)。

尝试:

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

要么:

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

文档中的相关部分是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(后者主要是指文档是代码)。

编辑:

也可以通过创造性的方式将R代码与Python混合使用。例如:

myfunc = robjects.r('function (x) predict.merMod(x, re.form=NA)')
myfunc(mymodel)
© www.soinside.com 2019 - 2024. All rights reserved.