rpy2线性模型子集参数

时间:2013-07-02 14:59:21

标签: model subset linear rpy2

我想通过rpy使用R的lm()函数的“subset”参数来过滤类似于这个R语法的行:

model = lm(Trt ~ Ctl, subset=(Name=="A"))

我无法将上述内容翻译成python:

from rpy2.robjects.vectors import DataFrame
from rpy2.robjects.packages import importr
stats = importr('stats')
base = importr('base')

data = DataFrame.from_csvfile("test_table.csv")

model1 = stats.lm("Trt ~ Ctl", data = data) # WORKS FINE
model2 = stats.lm("Trt ~ Ctl", data = data, subset = '(Name == "A")') # FAILS

test_table.csv:

Ctl,Trt,Name
4.17,4.81,A
5.58,4.17,B
5.18,4.41,C
6.11,3.59,A
4.5,5.87,B
4.61,3.83,C
5.17,6.03,A
4.53,4.89,B
5.33,4.32,C
5.14,4.69,A

输出:

data:
    Ctl  Trt Name
1  4.17 4.81    A
2  5.58 4.17    B
3  5.18 4.41    C
4  6.11 3.59    A
5  4.50 5.87    B
6  4.61 3.83    C
7  5.17 6.03    A
8  4.53 4.89    B
9  5.33 4.32    C
10 5.14 4.69    A

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases
Traceback (most recent call last):
  File "test.py", line 754, in <module>
    lm_D9 = stats.lm("Trt ~ Ctl", data = data, subset = 'Name=="A"')
  File "/usr/local/lib64/python2.7/site-packages/rpy2/robjects/functions.py", line 82, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/local/lib64/python2.7/site-packages/rpy2/robjects/functions.py", line 34, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

看起来没有找到匹配项。知道如何正确传递过滤器表达式吗?或者是否有一个简单的解决方法可以从rpy2获得相同的效果?

0 个答案:

没有答案