[英]Fitting Logistic AR1 via R-based glmmTMB in Python via rpy2
我正在嘗試在 Python 中擬合具有串行自相關或 AR1 錯誤的邏輯回歸。 不幸的是, statsmodels
還沒有走到這一步。 但是,R-package glmmTMB
有。 我很接近(似乎)通過rpy2
中的 rpy2 來解決這個問題,但我被卡住了。
My R skills (and the error: RRuntimeError: Error in na.fail.default(as.ts(x)): missing values in object
) suggest a need to modify the na.action
argument to function glmmTMB
(in the package glmmTMB
) ,但在 R 中這不是必需的——我的例子適合那里並且很高興。 所以,我懷疑 Python-to-R 鏈中的其他東西是錯誤的。
我的錢花在了變量time
和group
到formula
中的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
在 function glmmTMB()
的簽名中,您應該可以這樣做
m = ro.r.glmmTMB(<blah>, na_action=<your value>)
文檔在這里: https://rpy2.github.io/doc/v3.4.x/html/robjects_functions.html?highlight=function#
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.