【问题标题】:use model.matrix through rpy2?通过rpy2使用model.matrix?
【发布时间】:2015-04-02 03:17:37
【问题描述】:

在我的工作中,我更喜欢 python 而不是 R。有时,我需要使用 R 函数,为此我开始尝试使用 Rpy2。

我尝试过,但未能找到如何使用 Rpy2 复制以下内容

design <- model.matrix(~Subject+Treat)

我已经做到了:

import rpy2.robjects as robjects
fmla = robjects.Formula('~subject+treatment')
env = fmla.environment
env['subject'] = sbj_group
env['treatment'] = trt_group

根据我看到的here。 但我找不到如何执行model.matrix。我尝试了几种不同的方法:

robjects.r.model_matrix(fmla)
robjects.r('model.matrix(%s)' %fmla.r_repr())

如你所见,没有一个是正确的。

我是 Rpy2 的新手,并且在 R 方面相当缺乏经验。任何帮助将不胜感激!

【问题讨论】:

    标签: python r rpy2 model.matrix


    【解决方案1】:

    你可以evaluate strings as R code:

    import numpy as np
    import rpy2.robjects as ro
    import rpy2.robjects.numpy2ri
    ro.numpy2ri.activate() 
    R = ro.r
    
    subject = np.repeat([1,2,3], 4)
    treatment = np.tile([1,2,3,4], 3)
    R.assign('subject', subject)
    R.assign('treatment', treatment)
    R('subject <- as.factor(subject)')
    R('treatment <- as.factor(treatment)')
    R('design <- model.matrix(~subject+treatment)')
    R('print(design)')
    

    产量

       (Intercept) subject2 subject3 treatment2 treatment3 treatment4
    1            1        0        0          0          0          0
    2            1        0        0          1          0          0
    3            1        0        0          0          1          0
    4            1        0        0          0          0          1
    5            1        1        0          0          0          0
    6            1        1        0          1          0          0
    7            1        1        0          0          1          0
    8            1        1        0          0          0          1
    9            1        0        1          0          0          0
    10           1        0        1          1          0          0
    11           1        0        1          0          1          0
    12           1        0        1          0          0          1
    attr(,"assign")
    [1] 0 1 1 2 2 2
    attr(,"contrasts")
    attr(,"contrasts")$subject
    [1] "contr.treatment"
    
    attr(,"contrasts")$treatment
    [1] "contr.treatment"
    

    R(...) 返回可以在 Python 端操作的对象。 例如,

    design = R('model.matrix(~subject+treatment)')
    

    rpy2.robjects.vectors.Matrix 分配给design

    arr = np.array(design)
    

    使 arr 成为 NumPy 数组

    [[ 1.  0.  0.  0.  0.  0.]
     [ 1.  0.  0.  1.  0.  0.]
     [ 1.  0.  0.  0.  1.  0.]
     [ 1.  0.  0.  0.  0.  1.]
     [ 1.  1.  0.  0.  0.  0.]
     [ 1.  1.  0.  1.  0.  0.]
     [ 1.  1.  0.  0.  1.  0.]
     [ 1.  1.  0.  0.  0.  1.]
     [ 1.  0.  1.  0.  0.  0.]
     [ 1.  0.  1.  1.  0.  0.]
     [ 1.  0.  1.  0.  1.  0.]
     [ 1.  0.  1.  0.  0.  1.]]
    

    列名可以通过

    来访问
    np.array(design.colnames)
    # array(['(Intercept)', 'subject2', 'subject3', 'treatment2', 'treatment3',
    #        'treatment4'], 
    #       dtype='|S11')
    

    【讨论】:

    • 谢谢!它按描述工作,尽管不是我最初想要的。一个后续问题:如果我想在其他语句中使用它,我如何让“设计”回到 python 端?我看到您使用 R.assign 传递给 R。是否有等效的函数可以从 R 中提取对象?对不起我的无知。谢谢!
    • R(...) 返回可以在 Python 端操作的对象。我已经编辑了上面的帖子以说明我的意思。
    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多