python-rpy2负二项式glm

我正在尝试从rpy2调用R glm.nb:

from rpy2 import robjects 
from rpy2.robjects.packages import importr

MASS = importr('MASS')
stats = importr('stats')

def glm_nb(x,y):
    formula = robjects.Formula('y~x')
    env = formula.environment
    env["x"] = x
    env["y"] = y
    fitted = MASS.glm_nb(formula)
#     fitted = stats.glm(formula)
    return fitted

测试:

N = 100
x = np.random.rand(N)
y = x + np.random.poisson( 10, N)
fitted = glm_nb(x, np.round(y))

返回错误:

    104         for k, v in kwargs.items():
    105             new_kwargs[k] = conversion.py2ri(v)
--> 106         res = super(Function, self).__call__(*new_args, **new_kwargs)
    107         res = conversion.ri2ro(res)
    108         return res

RRuntimeError: Error in x[good, , drop = FALSE] * w : non-conformable arrays

但是,当我运行简单的glm时,它运行正常.问题可能是什么,如何解决?

解决方法:

基本问题涉及R中矩阵和数组的数据结构.下面重现了R中带有fix的错误,在rpy2中复制fix的挑战以及有效的解决方案:

R错误和修复

library(MASS)

# ARRAY
x <- array(rnorm(100))
y <- as.integer(x) + array(rpois(100, 10))

model2 <- glm.nb(y~x)

Error in x[good, , drop = FALSE] * w : non-conformable arrays

但是,可以使用三个解决方法:1)使用矩阵(二维特殊类型的数组); 2)等效定义的数组(指定dim参数); 3)矩阵转换.请注意:根据随机值可能会出现迭代限制警告,但仍会继续运行.

# MATRIX
x <- matrix(rnorm(100))
y <- as.integer(x) + matrix(rpois(100, 10))

model1 <- glm.nb(y~x)

# EQUIVALENT ARRAY
x <- array(rnorm(100),c(100,1))
y <- as.integer(x) + matrix(rpois(100, 10),c(100,1))

model2 <- glm.nb(y~x)

# EXPLICIT MATRIX CONVERSION (USED IN WORKING SOLUTION)
x <- as.matrix(array(rnorm(100)))
y <- as.integer(x) + as.matrix(array(rpois(100, 10)))

model3 <- glm.nb(y~x)

挑战

Python的rpy2不能从我的脚本工作中有效地将numpy矩阵传递给R矩阵,因为stat的简单glm()和MASS的glm.nb()都会出现不同的错误:

import numpy as np
from rpy2 import robjects 
from rpy2.robjects.packages import importr
from rpy2.robjects.numpy2ri import numpy2ri
MASS = importr('MASS')

#rpy2 + negative binomial glm
stats = importr('stats')

def glm_nb(x,y):
    formula = robjects.Formula('y~x')
    env = formula.environment
    env["x"] = x
    env["y"] = y    
    fitted = MASS.glm_nb(formula)
#   fitted = stats.glm(formula)
    return fitted

N = 100
x = np.random.rand(N)
x = np.asmatrix(x)                            # PYTHON CONVERSION TO MATRIX
r_x = numpy2ri(x)

# REPLACED NP.ROUND FOR AS.TYPE() TO COMPARE WITH R
y = x.astype(int) + np.random.poisson(10, N)  
y = np.asmatrix(y)                            # PYTHON CONVERSION TO MATRIX
r_y = numpy2ri(y)

fitted = glm_nb(r_x, r_y)

rpy2.rinterface.RRuntimeError: Error in glm.fitter(x = X, y = Y, w =
w, start = start, etastart = etastart, : object ‘fit’ not found

甚至numpy2ri.activate()都无法转换numpy矩阵:

from rpy2.robjects import numpy2ri
robjects.numpy2ri.activate()
r_x = numpy2ri.ri2py(x)
r_y = numpy2ri.ri2py(y)

NotImplementedError: Conversion ‘ri2py’ not defined for objects of
type '<class 'numpy.matrixlib.defmatrix.matrix'>'

工作方案

只需与robjects.r()接口,并让R将数组对象转换为矩阵即可.回忆上面的第三个修复:

N = 100
x = np.random.rand(N)
r_x = numpy2ri(x)

y = x.astype(int) + np.random.poisson(10, N)
r_y = numpy2ri(y)

from rpy2.robjects import r
r.assign("y", r_y)
r.assign("x", r_x)
r("x <- as.matrix(x)")
r("y <- as.matrix(y)")
r("res <- glm.nb(y~x)")

r_result = r("res[1:5]")

# CONVERSION INTO PY DICTIONARY    
from rpy2.robjects import pandas2ri
pandas2ri.activate()
pyresult = pandas2ri.ri2py(r_result)
print(pyresult)                       # OUTPUTS COEFF, RESID, FITTED VALS, EFFECTS, R

# OR OLDER DEPRECATED CONVERSION
import pandas.rpy.common as com
pyresult = com.convert_robj(r_result)
print(pyresult)                       # OUTPUTS COEFF, RESID, FITTED VALS, EFFECTS, R

命令行解决方案

如果在您的应用程序中允许,只需从Python作为命令行子进程调用R建模脚本,绕过对rpy2的任何需要,甚至根据需要传递参数:

from subprocess import Popen, PIPE

command = 'Rscript.exe'
path2Script = 'path/to/Script.R'    
args = ['arg1', 'arg2', 'arg3']

cmd = [command, path2Script] + args

p = Popen(cmd,stdin= PIPE, stdout= PIPE, stderr= PIPE)            
output,error = p.communicate()

if p.returncode == 0:            
    print('R OUTPUT:\n {0}'.format(output))            
else:                
    print('R ERROR:\n {0}'.format(error)) 
上一篇:OpenGL基础17:颜色


下一篇:颜色,基础光照,材质。