通过rpy2在Python中通过基于R的glmmTMB安装逻辑AR1

通过rpy2在Python中通过基于R的glmmTMB安装逻辑AR1,python,r,rpy2,glmmtmb,Python,R,Rpy2,Glmmtmb,我试图用Python来拟合带有序列自相关(AR1错误)的逻辑回归。不幸的是,statsmodels还没有走到这一步;但是,R-packageglmmTMB已经完成。我很接近(似乎)通过Python中的rpy2实现这一点,但我被卡住了 我的R技能(以及错误:RRuntimeError:na.fail.default中的错误(as.ts(x)):object中缺少值)表明需要修改na.action参数以实现函数glmmTMB(在包glmmTMB中),但这在R中是不必要的——我的示例适用于此,并且很高

我试图用Python来拟合带有序列自相关(AR1错误)的逻辑回归。不幸的是,
statsmodels
还没有走到这一步;但是,R-package
glmmTMB
已经完成。我很接近(似乎)通过Python中的
rpy2
实现这一点,但我被卡住了

我的R技能(以及错误:
RRuntimeError:na.fail.default中的错误(as.ts(x)):object
中缺少值)表明需要修改
na.action
参数以实现函数
glmmTMB
(在包
glmmTMB
中),但这在R中是不必要的——我的示例适用于此,并且很高兴。因此,我怀疑Python-to-R链中的其他东西是错误的

我的钱花在变量
时间
公式
中的
ar
部分所需的因子表征上。有人知道我能做什么吗?我是否没有正确创建因子变量

from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri 
from rpy2 import robjects as ro
import pandas as pd

# Make some data.  
data = {'Success': [10, 20, 30, 11, 14, 16, 18, 29, 17, 19], 
        'Failure': [12, 25, 61, 8, 22, 21, 10, 16, 15, 19]}
df = pd.DataFrame(data)

# Allow rpy2 to talk to pandas.  
pandas2ri.activate() 

# Bring in some R stuff.  
base = importr('base')
utils = importr('utils')
stats = importr('stats')
glmmTMB = importr('glmmTMB')

# Modify the dataframe for use with glmmTMB.  
df['time'] = range(1, len(df) + 1)
df['group'] = 1

# Convert df to an R dataframe.  
r_df = pandas2ri.py2ri(df)

# Add in group as a factor to R dataframe r_df.
col_group = ro.vectors.FactorVector(r_df.rx2('group'))
assessor_col_index = df.columns.get_loc('group')   # python 
r_df[assessor_col_index] = col_group

# Add in time as a factor to R dataframe r_df.  
col_time = ro.vectors.FactorVector(r_df.rx2('time'))
assessor_col_index = df.columns.get_loc('time')   # python 
r_df[assessor_col_index] = col_time

# Take a look at stuff.  Looks correct.  
print(r_df)
print(col_group)
print(col_time) 

# This works.  Normal outcome.  
m0 = ro.r.glmmTMB(formula=ro.r('Success ~ 1'), data=r_df)
# print(m0[1][0])

# This works.  Logistic outcome.  
m1 = ro.r.glmmTMB(formula=ro.r('cbind(Success, Failure) ~ 1'), family=ro.r('binomial'), data=r_df)
# print(m1[1][0])

# This does not work.  AR1 with factors, per documentation.  
# m2 = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df)
# m2[1][0]


# Added for clarity, 2021-03-08:  Other variations using na_action argument.  

# m3  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action=na_omit)            
# name 'na_omit' is not defined.

# m4  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action=ro.r(na_omit))     
# name 'na_omit' is not defined. 

# m5  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action='na_omit')        
# Error in na.fail.default(as.ts(x)): missing values in object 

# m6  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action=ro.r('na_omit'))  
# Error in (function(expr, envir=parent.frame(), enclos=if(is.list(envir) || object 'na_omit' not found)))
 
# m7  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action=na.omit)          
# name 'na' is not defined 

# m8  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action=ro.r(na.omit))      
# name 'na' is not defined

# m9  = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action='na.omit')        
# Error in na.fail.default(as.ts(x)): missing values in object

# m10 = ro.r.glmmTMB(formula=ro.r("cbind(Success, Failure) ~ 1 + ar(time + 0 | group)"), family=ro.r('binomial'), data=r_df, na_action=ro.r('na.omit'))   
# Error in na.fail.default(as.ts(x)): missing values in object 


如果命名参数
na.action
在函数
glmmTMB()
的签名中,您应该能够执行以下操作

m = ro.r.glmmTMB(<blah>, na_action=<your value>)
m=ro.r.glmmTMB(,na_action=)

文档在这里:

我包括了我最初的尝试,包括包含这个变量,以及它们无法在没有错误的情况下完成。你对我的玩具示例尝试过你的建议吗?你看到我遗漏了什么吗?