簡體   English   中英

使用 rpy2(R 的 Python 接口)在 Python 中進行序數邏輯回歸:共線預測變量問題

[英]Ordinal Logistic Regression in Python with rpy2 (Python interface for R): issue with collinear predictors

我正在嘗試在 Python 中執行序數邏輯回歸,使用 rpy2(R 語言的 Python 接口)調用 R 的 mass.polr 函數。 但是,當我的預測變量中有一些共線或幾乎共線的列時,我遇到了麻煩:mass.polr 在擬合期間自動丟棄其中一些列,這會在我嘗試對訓練數據進行預測時導致錯誤。

這是一個最小的例子:

from rpy2.robjects import r, pandas2ri
from rpy2.robjects.packages import importr

pandas2ri.activate()

mass = importr("MASS")

# dataframe with two collinear predictors (x1 and x2)
df = pd.DataFrame(columns = ['target', 'x1', 'x2', 'x3'],
                  data    = [[   0   ,  0  ,  0  ,  1  ],
                             [   1   ,  1  ,  1  ,  0  ],
                             [   2   ,  1  ,  1  ,  1  ]])

model = mass.polr('as.factor(target) ~ .', df, Hess = True) # gives warning below
'''
Warning message:
In polr(as.factor(target) ~ ., data = df, Hess = TRUE) :
  design appears to be rank-deficient, so dropping some coefs

'''

r.predict(model, df, type = "class").__array__() # gives error below
'''
Error in X %*% object$coefficients : non-conformable arguments
'''

同樣的錯誤實際上也發生在 R 中,但我至少可以通過查看summary(model)來了解哪些列已被丟棄。

相反,在 Python 中, r.summary(model).rx2('coefficients') (應顯示與 R 中的summary(model)相同的輸出)不顯示系數名稱,而僅顯示裸值:

array([[4.57292582e+01, 8.25605929e+02, 5.53887231e-02],
       [2.11604944e+01, 2.85721885e+02, 7.40597606e-02],
       [3.19476895e+01, 3.60605165e+02, 8.85946531e-02],
       [5.66312792e+01, 8.93862000e+02, 6.33557296e-02]])

有誰知道在 Python 中檢索系數名稱的方法? 或者還有其他解決方法嗎?

即使沒有pandas2ri.activate() ,從r.summary(model).rx2('coefficients')返回的 FloatMatrix 也不包含變量名稱。 但是,我們可以使用 R 的dimnames函數提取這些名稱。 完整示例如下:

import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects import r, pandas2ri
from rpy2.robjects.packages import importr
from rpy2.robjects.conversion import localconverter
mass = importr("MASS")

df = pd.DataFrame(columns = ['target', 'x1', 'x2', 'x3'],
                  data    = [[   0   ,  0  ,  0  ,  1  ],
                             [   1   ,  1  ,  1  ,  0  ],
                             [   2   ,  1  ,  1  ,  1  ]])

with localconverter(ro.default_converter + pandas2ri.converter):
    df = ro.conversion.py2rpy(df)

model = mass.polr('as.factor(target) ~ .', df, Hess = True)

coefs = r.summary(model).rx2('coefficients')

[x for x in r('dimnames')(coefs)[0]]

返回['x1', 'x3', '0|1', '1|2'] ,顯示 x2 已被刪除。

或者,您可以使用r.print(r.summary(model))打印完整的模型輸出

我正在嘗試使用 rpy2(ZE1E1D3D40573127E36EE0480CAFZ18 語言的 Python 接口)在 Python 中調用 R 的 mass.polr function 執行序數邏輯回歸。 但是,當我的預測變量中有一些共線或幾乎共線的列時,我會遇到麻煩:mass.polr 在擬合期間會自動丟棄其中的一些列,這在我嘗試對訓練數據進行預測時會導致錯誤。

這是一個最小的例子:

from rpy2.robjects import r, pandas2ri
from rpy2.robjects.packages import importr

pandas2ri.activate()

mass = importr("MASS")

# dataframe with two collinear predictors (x1 and x2)
df = pd.DataFrame(columns = ['target', 'x1', 'x2', 'x3'],
                  data    = [[   0   ,  0  ,  0  ,  1  ],
                             [   1   ,  1  ,  1  ,  0  ],
                             [   2   ,  1  ,  1  ,  1  ]])

model = mass.polr('as.factor(target) ~ .', df, Hess = True) # gives warning below
'''
Warning message:
In polr(as.factor(target) ~ ., data = df, Hess = TRUE) :
  design appears to be rank-deficient, so dropping some coefs

'''

r.predict(model, df, type = "class").__array__() # gives error below
'''
Error in X %*% object$coefficients : non-conformable arguments
'''

同樣的錯誤實際上也發生在 R 中,但我至少可以通過查看summary(model)看到哪些列已被丟棄。

相反,在 Python、 r.summary(model).rx2('coefficients')中(應該顯示相同的 output 不顯示系數名稱,而只是在 R 中顯示系數名稱,而只是顯示summary(model)值)

array([[4.57292582e+01, 8.25605929e+02, 5.53887231e-02],
       [2.11604944e+01, 2.85721885e+02, 7.40597606e-02],
       [3.19476895e+01, 3.60605165e+02, 8.85946531e-02],
       [5.66312792e+01, 8.93862000e+02, 6.33557296e-02]])

有誰知道在 Python 中檢索系數名稱的方法? 或者還有其他解決方法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM