我试图将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
(我也试过别名REform
,ReForm
等)。有任何想法吗?
这个R函数:https://www.rdocumentation.org/packages/lme4/versions/1.1-20/topics/predict.merMod
这可能是泛型签名中函数调度/省略号的问题(如果在泛型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-packages和https://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)